C 如何利用结构数组根据用户输入输出给定的结构
问题:我正试图用C 如何利用结构数组根据用户输入输出给定的结构,c,file,pointers,struct,C,File,Pointers,Struct,问题:我正试图用beer.dat中的数据填充我的beerDatastruct,只是我不明白structs如何在不破坏代码的情况下很好地实现。我相信我需要一个结构数组 beer.dat文件内容: 7 // total number of beers Coors //beer name 1234567 // beer id 72 // beer quantity 7.40 //beer price Miller 7777777 44 9.70 Bud 7654321 345 9.90 Wachu
beer.dat
中的数据填充我的beerData
struct
,只是我不明白structs如何在不破坏代码的情况下很好地实现。我相信我需要一个结构数组
beer.dat文件内容:
7 // total number of beers
Coors //beer name
1234567 // beer id
72 // beer quantity
7.40 //beer price
Miller
7777777
44
9.70
Bud
7654321
345
9.90
Wachusett
7799435
4
14.70
Corona
9999999
112
9.99
Zima
0000000
1
0.01
Mikes
0890398
12
10.99
/*
User interface, alloc, malloc 13 points
Correct structure and array built 7 points
Recursive sort
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct beerData {
char *beer[7]; // number of brands
char *beer_name; //names
int beer_id[7]; //ID number given to beer
int beer_quantity; //stock
float beer_price; // pricing
} beer_data;
void printStr(char *line) {
char *look = "";
printf("What would you like to search for? \n");
scanf("%s", look);
//printf("Line: %s\n", line);
exit(0);
}
void search() {
//look through beer.dat for a specific beer by ID number.
char str[32];
FILE *fp = fopen("beer.dat", "r");
if (fp == NULL) {
printf("Error: can't open file to read\n");
} else {
while (!feof(fp)) {
fscanf(fp, "%s ", str);
//printf("%s\n",str);
printStr(str);
}
}
fclose(fp);
}
int main() {
int user_choice;
printf("Enter 1 to search for a beer, 2 to view the entire catalogue,"
" and 3 to place an order, press 4 to exit.\n");
while (user_choice != 4) {
scanf("%d", &user_choice);
switch (user_choice) {
case 1:
printf("Searching for a beer\n");
user_choice = 0;
search();
break;
case 2:
printf("Viewing Inventory\n");
//viewInv();
break;
case 3:
printf("Placing an order...\n");
//placeOrder();
break;
case 4:
printf("Goodbye!\n");
exit(0);
default:
printf("Incorrect entry, try again.\n");
continue;
}
}
}
代码:
7 // total number of beers
Coors //beer name
1234567 // beer id
72 // beer quantity
7.40 //beer price
Miller
7777777
44
9.70
Bud
7654321
345
9.90
Wachusett
7799435
4
14.70
Corona
9999999
112
9.99
Zima
0000000
1
0.01
Mikes
0890398
12
10.99
/*
User interface, alloc, malloc 13 points
Correct structure and array built 7 points
Recursive sort
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct beerData {
char *beer[7]; // number of brands
char *beer_name; //names
int beer_id[7]; //ID number given to beer
int beer_quantity; //stock
float beer_price; // pricing
} beer_data;
void printStr(char *line) {
char *look = "";
printf("What would you like to search for? \n");
scanf("%s", look);
//printf("Line: %s\n", line);
exit(0);
}
void search() {
//look through beer.dat for a specific beer by ID number.
char str[32];
FILE *fp = fopen("beer.dat", "r");
if (fp == NULL) {
printf("Error: can't open file to read\n");
} else {
while (!feof(fp)) {
fscanf(fp, "%s ", str);
//printf("%s\n",str);
printStr(str);
}
}
fclose(fp);
}
int main() {
int user_choice;
printf("Enter 1 to search for a beer, 2 to view the entire catalogue,"
" and 3 to place an order, press 4 to exit.\n");
while (user_choice != 4) {
scanf("%d", &user_choice);
switch (user_choice) {
case 1:
printf("Searching for a beer\n");
user_choice = 0;
search();
break;
case 2:
printf("Viewing Inventory\n");
//viewInv();
break;
case 3:
printf("Placing an order...\n");
//placeOrder();
break;
case 4:
printf("Goodbye!\n");
exit(0);
default:
printf("Incorrect entry, try again.\n");
continue;
}
}
}
/*
用户界面,alloc,malloc 13分
正确的结构和阵列构建7点
递归排序
*/
#包括
#包括
#包括
结构比尔达{
char*啤酒[7];//品牌数量
char*beer\u name;//名称
int beer_id[7];//给beer的id号
int beer_数量;//库存
浮动啤酒价格;//定价
}啤酒数据;
void printStr(字符*行){
char*look=“”;
printf(“您想搜索什么?\n”);
scanf(“%s”,外观);
//printf(“行:%s\n”,行);
出口(0);
}
无效搜索(){
//通过beer.dat查找ID号指定的啤酒。
char-str[32];
文件*fp=fopen(“beer.dat”,“r”);
如果(fp==NULL){
printf(“错误:无法打开文件读取\n”);
}否则{
而(!feof(fp)){
fscanf(fp,“%s”,str);
//printf(“%s\n”,str);
printStr(str);
}
}
fclose(fp);
}
int main(){
int用户选择;
printf(“输入1搜索啤酒,输入2查看整个目录,”
“和3要下订单,按4退出。\n”);
while(用户选择!=4){
scanf(“%d”,用户选择(&U);
开关(用户选择){
案例1:
printf(“搜索啤酒”);
用户选择=0;
搜索();
打破
案例2:
printf(“查看清单”);
//viewInv();
打破
案例3:
printf(“下订单…\n”);
//placeOrder();
打破
案例4:
printf(“再见!\n”);
出口(0);
违约:
printf(“输入不正确,请重试。\n”);
继续;
}
}
}
我正在尝试创建一个函数来搜索文件,并根据给定的ID查找特定的啤酒,该ID位于应该是其结构的集合中。。。因此,一旦输入了ID,程序就会搜索啤酒,并打印出名称、ID、数量和价格
为了清楚起见,我会张贴作业问题,以防我没有正确表达我的需求。任务是:
首先,您需要声明一个有意义的结构。该结构包含每个项目的相关信息。例如:
typedef struct beer_data
{
char name[20]; //names
int id; //ID number given to beer
int quantity; //stock
float price; // pricing
} beer_data;
beer_data *beers = malloc(total * sizeof(beer_data));
接下来,您需要一个这种结构的数组。使用malloc
为total
项目数分配足够的内存。例如:
typedef struct beer_data
{
char name[20]; //names
int id; //ID number given to beer
int quantity; //stock
float price; // pricing
} beer_data;
beer_data *beers = malloc(total * sizeof(beer_data));
现在您有了啤酒[0]、啤酒[1]、啤酒[2]…
,读取文件中的每个项目并将其放入结构中
要读取文件,可以使用fscanf
或fgets
文件中的第一行是
7 // total number of beers
您可以使用fscanf
读取数字7
:
int maximum = 0;
fscanf(fp, "%d", &maximum);
这应该可以很好地工作,但之后还有一些角色你不感兴趣。使用fgets
读取到行尾并丢弃这些字符
开始一个循环,阅读每一行,添加到结构中
使用此方法,如果要添加新项,则必须增加内存大小。请参阅使用realloc
的add\u项。这可能太高级了。或者,您可以将新项目保存到文件中,调用免费(啤酒)
,然后再次读取该文件
typedef struct beer_data
{
char name[20]; //names
int id; //ID number given to beer
int quantity; //stock
float price; // pricing
} beer_data;
void search_by_name(beer_data *beers, int total)
{
char buf[20];
printf("Enter name to search: ");
scanf("%19s", buf);
//note, we put %19s because beers[count].name is only 20 bytes long
for(int i = 0; i < total; i++)
{
if(strcmp(beers[i].name, buf) == 0)
{
printf("Found: %s, %d, %d, %.2f\n",
beers[i].name, beers[i].id, beers[i].quantity, beers[i].price);
return;
}
}
printf("%s not found\n", buf);
}
void print_list(beer_data *beers, int total)
{
for(int i = 0; i < total; i++)
{
printf("%s %d %d %.2f\n",
beers[i].name, beers[i].id, beers[i].quantity, beers[i].price);
}
}
void add_item(beer_data *beers, int *total)
{
//note, total has to be passed as pointer
//because we are changing it
//allocate more memory:
beers = realloc(beers, sizeof(beer_data) * (*total + 1));
printf("enter name: ");
scanf("%19s", beers[*total].name);
printf("enter id:");
scanf("%d", &beers[*total].id);
printf("enter quantity:");
scanf("%d", &beers[*total].quantity);
printf("enter price:");
scanf("%f", &beers[*total].price);
//increase the total
*total += 1;
}
int main()
{
FILE *fp = fopen("beer.dat", "r");
if(!fp)
{
printf("Error: can't open file to read\n");
return 0;
}
char buf[500];
int maximum = 0;
fscanf(fp, "%d", &maximum);
//read the rest of the line and discard it
fgets(buf, sizeof(buf), fp);
//allocate memory
beer_data *beers = malloc(maximum * sizeof(beer_data));
int total = 0;
while(1)
{
fgets(buf, sizeof(buf), fp);
sscanf(buf, "%19s", beers[total].name);
if(fscanf(fp, "%d", &beers[total].id) != 1) break;
fgets(buf, sizeof(buf), fp);
if(fscanf(fp, "%d", &beers[total].quantity) != 1) break;
fgets(buf, sizeof(buf), fp);
if(fscanf(fp, "%f", &beers[total].price) != 1) break;
fgets(buf, sizeof(buf), fp);
total++;
if(total == maximum)
break;
}
fclose(fp);
int stop = 0;
while (!stop)
{
printf("\
Enter 0 to exit\n\
Enter 1 print list\n\
Enter 2 for search\n\
Enter 3 add new item\n");
int choice;
scanf("%d", &choice);
switch(choice)
{
case 0:
stop = 1;
break;
case 1:
print_list(beers, total);
break;
case 2:
search_by_name(beers, total);
break;
case 3:
add_item(beers, &total);
break;
}
printf("\n");
}
//cleanup:
free(beers);
return 0;
}
typedef结构数据
{
字符名[20];//名称
int id;//给啤酒的id号
int数量;//库存
浮动价格;//定价
}啤酒数据;
按名称无效搜索(啤酒数据*啤酒,整数合计)
{
char-buf[20];
printf(“输入要搜索的名称:”);
scanf(“%19s”,buf);
//注意,我们之所以放置%19s,是因为beers[count]。名称只有20字节长
对于(int i=0;i