C语言小问题,求解
用C语言的结构体如何完成在的小程序,求解 看起来简单,但是写了两个小时复制到单个文件中就可以编译运行了
(输出格式没有调好)
//关闭vs2019不安全函数-4996警告
#pragma warning(disable : 4996)
#include<stdio.h>
//可食用物品
struct item
{
int id;
char* name;
double cost;
};
//构造可食用物品
struct item create_item(int id, char* name, double cost) {
struct item i;
i.id = id;
i.name = name;
i.cost = cost;
return i;
}
//打印菜单
void print_menu(char* title,struct item items[], int n) {
printf("/***************%s***************/\n", title);
printf("ID\t\t\tName\t\t\tCost\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t\t%s\t\t\t%f\n", items.id, items.name, items.cost);
}
printf("/**********************************/\n");
}
//根据选择的id获取食物
struct item get_item(struct item items[], int n, int id) {
while (n--)
if (items.id == id)
return items;
//id不存在的情况下防止出错
struct item tmp;
tmp.id = 0;
tmp.name = "nothing";
tmp.cost = 0;
return tmp;
}
//主流程
int main(char* v[], int v_n) {
struct item foods;
foods = create_item(1, "Curry beef with rice", 40);
foods = create_item(2, "Sushi set meal", 65);
foods = create_item(3, "YangZhou fire rice", 45);
foods = create_item(4, "Sirloin Steak with Spaghetti", 72);
foods = create_item(5, "Chicken vegetable roll", 42);
struct item drinks;
drinks = create_item(21, "Soft drink", 10);
drinks = create_item(22, "Red wine", 15);
drinks = create_item(23, "Beer", 15);
struct item selected_items;
int selected_amount = 0;
//选择食物
while (selected_amount<100) {
print_menu("Food Menu", foods, 5);
printf("\n");
printf("Please Select Your Food ID:");
int id;
scanf("%d",&id);
struct item selected_food = get_item(foods, 5, id);
selected_items = selected_food;
selected_amount++;
printf("You have selected %s\n", selected_food.name);
printf("Do you need more food?0-No1-Yes\n");
int flag;
scanf("%d",&flag);
printf("\n");
if (flag == 0) break;
}
//选择饮料
while (selected_amount < 100) {
print_menu("Drink Menu", drinks, 3);
printf("\n");
printf("Please Select Your drink ID:");
int id;
scanf("%d", &id);
struct item selected_drind = get_item(drinks, 5, id);
selected_items = selected_drind;
selected_amount++;
printf("You have selected %s\n", selected_drind.name);
printf("Do you need more drink?0-No1-Yes\n");
int flag;
scanf("%d", &flag);
printf("\n");
if (flag == 0) break;
}
double total_cost = 0;
for (int i = 0; i < selected_amount; i++) {
total_cost += selected_items.cost;
}
printf("Your total price:%f", total_cost);
return 0;
}
砚堂_ 发表于 2020-12-19 03:53
看起来简单,但是写了两个小时
复制到单个文件中就可以编译运行了
(输出格式没有调好)
运行截图
{:10_266:}{:10_266:}{:10_266:}{:10_266:} 砚堂_ 发表于 2020-12-19 03:53
看起来简单,但是写了两个小时
复制到单个文件中就可以编译运行了
(输出格式没有调好)
为什么我用VC++6.0编译会报错{:10_266:} vc6.0哪里下载 段段努力秃头 发表于 2020-12-19 15:39
为什么我用VC++6.0编译会报错
可能是第二行报错了吧,可以注释试试。我用的vs2019 66666 lihai
强大,为何不用devc呢 砚堂_ 发表于 2020-12-19 03:53
看起来简单,但是写了两个小时
复制到单个文件中就可以编译运行了
(输出格式没有调好)
https://i1.100024.xyz/i/2021/01/03/kow1kz.png
用的DEV编译,然后报错,(第一行的关闭不安全函数已经删除后的代码) 砚堂_ 发表于 2020-12-19 03:53
看起来简单,但是写了两个小时
复制到单个文件中就可以编译运行了
(输出格式没有调好)
用vs运行带第一行的代码,显示如下:
https://i1.100024.xyz/i/2021/01/03/ksjdmk.png 愷龍 发表于 2021-1-3 12:52
用的DEV编译,然后报错,(第一行的关闭不安全函数已经删除后的代码)
哥哥,你看一下红色的报错的信息呀,说for循环中声明变量仅在c99或c11中可用
是不是没配好,用的c89 雷欧库珀 发表于 2020-12-29 19:17
强大,为何不用devc呢
之前用过,忘记什么原因卸载了。 愷龍 发表于 2021-1-3 12:57
用vs运行带第一行的代码,显示如下:
生成成功了,但是文件不见了,那应该是被删除了。。。
是360干的吗?。。
没装360的话,看一下windows defender有没有提示。
用结构体加链表你试一下 霸道larder 发表于 2021-1-8 11:58
用结构体加链表你试一下
就迭代的方式变了一下,别的都差不多。
页:
[1]