gpt4 book ai didi

c - 带有 C 语言开关的用户菜单 [包含代码]

转载 作者:行者123 更新时间:2023-11-30 20:38:12 25 4
gpt4 key购买 nike

我开发了一个小程序,它从文件中读取数据,进行一些特定的计算并将结果打印到屏幕和另一个文件上。该程序在执行此操作时工作正常,但是当我尝试使用“开关”添加用户菜单时,我遇到了问题。带有用户菜单的代码可以编译正常,但是当我运行它时,它会崩溃或无法正常工作,具体取决于您选择的选项。所以我的问题是:你能帮忙找出我哪里出了问题吗?我对此比较陌生,并且对“switch”命令了解不多。

我知道我应该使用函数,接下来就是!我将包含两组代码:

节目 1 - 无菜单

#include <stdlib.h>
#include <stdio.h>

// define structure
typedef struct car
{
char brand[20];
char model[20];
int engine;
float price;
int year;
} car_type;

int main(void)
{

car_type car[20];
int year, i;
char brand[20], model[20], space;
int engine;
float price, new_price;
i = 0;

// read from file
FILE* fp;
fp = fopen("indata.txt", "r");
FILE* fp_out;
fp_out = fopen("outdata.txt", "w");
if (fp == NULL)
{
printf("Opening of file failed\n");
return 0;
}
else
{
printf("Brand \t Model \t Engine \t Price \t Old(0)/New(1) \t New Price\n\n");
fprintf(fp_out, "Brand \t Model \t Engine \t Price \t Old(0)/New(1) \t New Price\n");

while (!feof(fp))
{
fscanf(fp, "%s%c%s%c%d%c%f%c%d\n", &car[i].brand, &space, &car[i].model, &space, &car[i].engine, &space, &car[i].price, &space, &car[i].year);
//display info
printf("%7s\t %6s\t %4d cc\t %1.1f euro\t %d\t ", car[i].brand, car[i].model, car[i].engine, car[i].price, car[i].year);
fprintf(fp_out, "%7s\t %6s\t %4d cc\t %1.1f euro\t %d\t ", car[i].brand, car[i].model, car[i].engine, car[i].price, car[i].year);
// Compute new price
if (car[i].year > 0)
{
printf("N/A\n");
fprintf(fp_out, "N/A\n");
}

else
{
if (car[i].engine >= 1600 && car[i].price>= 20000)
{
new_price = (car[i].price)*(0.85);
printf(" %1.1f euro \n", new_price);
fprintf(fp_out," %1.1f euro \n", new_price);
}
else
{
if (car[i].engine>=1600)
{
new_price = (car[i].price)*(0.95);
printf(" %1.1f euro \n", new_price);
fprintf(fp_out," %1.1f euro \n", new_price);
}
else
{
if (car[i].price >= 20000)
{
new_price = (car[i].price)*(0.9);
printf(" %1.1f euro \n", new_price);
fprintf(fp_out," %1.1f euro\n", new_price);
}
else
{
printf("N/A \n");
fprintf(fp_out, "N/A\n");
}
}
}
}
i++;
}
}
return(EXIT_SUCCESS);
}

节目 2 - 带菜单

#include <stdlib.h>
#include <stdio.h>

// define structure
typedef struct car
{
char brand[20];
char model[20];
int engine;
float price;
int year;
} car_type;

int main(void)
{

car_type car[20];
int year, i, option;
char brand[20], model[20], space;
int engine;
float price, new_price;
i = 0;

// read from file
FILE* fp;
fp = fopen("indata.txt", "r");
FILE* fp_out;
fp_out = fopen("outdata.txt", "w");

//user menu
option = 0;
while (option != 4)
{
printf("To have output printed on to screen only - select option 1 \n");
printf("To have output printed to file only - select option 2 \n");
printf("to have output printed to both - select option 3 \n");
printf("To exit - press 4 \ncc32");
scanf("%d", &option);
switch (option)
{
//Case 1
case 1:
{
if (fp == NULL)
{
printf("Opening of file failed\n");
return 0;
}

else
{
printf("Brand \t Model \t Engine \t Price \t Old(0)/New(1) \t New Price\n\n");
while (!feof(fp))
{
//display info
printf("%7s\t %6s\t %4d cc\t %1.1f euro\t %d\t ", car[i].brand, car[i].model, car[i].engine, car[i].price, car[i].year);

// Compute new price
if (car[i].year > 0)
{
printf("N/A\n");
}
else
{
if (car[i].engine >= 1600 && car[i].price>= 20000)
{
new_price = (car[i].price)*(0.85);
printf(" %1.1f euro \n", new_price);
}
else
{
if (car[i].engine>=1600)
{
new_price = (car[i].price)*(0.95);
printf(" %1.1f euro \n", new_price);
}
else
{
if (car[i].price >= 20000)
{
new_price = (car[i].price)*(0.9);
printf(" %1.1f euro \n", new_price);
}
else
{
printf("N/A \n");
}
}
}
}
i++;
}
}
break;
}

//Case 2
case 2:
{
if (fp == NULL)
{
printf("Opening of file failed\n");
return 0;
}
else
{
fprintf(fp_out, "Brand \t Model \t Engine \t Price \t Old(0)/New(1) \t New Price\n");

while (!feof(fp))
{
fscanf(fp, "%s%c%s%c%d%c%f%c%d\n", &car[i].brand, &space, &car[i].model, &space, &car[i].engine, &space, &car[i].price, &space, &car[i].year);
//display info
fprintf(fp_out, "%7s\t %6s\t %4d cc\t %1.1f euro\t %d\t ", car[i].brand, car[i].model, car[i].engine, car[i].price, car[i].year);
// Compute new price
if (car[i].year > 0)
{
fprintf(fp_out, "N/A\n");
}

else
{
if (car[i].engine >= 1600 && car[i].price>= 20000)
{
new_price = (car[i].price)*(0.85);
fprintf(fp_out," %1.1f euro \n", new_price);
}
else
{
if (car[i].engine>=1600)
{
new_price = (car[i].price)*(0.95);
fprintf(fp_out," %1.1f euro \n", new_price);
}
else
{
if (car[i].price >= 20000)
{
new_price = (car[i].price)*(0.9);
fprintf(fp_out," %1.1f euro\n", new_price);
}
else
{
fprintf(fp_out, "N/A\n");
}
}
}
}
i++;
}
}
break;
}

//Case 3
case 3:
{
if (fp == NULL)
{
printf("Opening of file failed\n");
return 0;
}
else
{
printf("Brand \t Model \t Engine \t Price \t Old(0)/New(1) \t New Price\n\n");
fprintf(fp_out, "Brand \t Model \t Engine \t Price \t Old(0)/New(1) \t New Price\n");

while (!feof(fp))
{
fscanf(fp, "%s%c%s%c%d%c%f%c%d\n", &car[i].brand, &space, &car[i].model, &space, &car[i].engine, &space, &car[i].price, &space, &car[i].year);
//display info
printf("%7s\t %6s\t %4d cc\t %1.1f euro\t %d\t ", car[i].brand, car[i].model, car[i].engine, car[i].price, car[i].year);
fprintf(fp_out, "%7s\t %6s\t %4d cc\t %1.1f euro\t %d\t ", car[i].brand, car[i].model, car[i].engine, car[i].price, car[i].year);
// Compute new price
if (car[i].year > 0)
{
printf("N/A\n");
fprintf(fp_out, "N/A\n");
}

else
{
if (car[i].engine >= 1600 && car[i].price>= 20000)
{
new_price = (car[i].price)*(0.85);
printf(" %1.1f euro \n", new_price);
fprintf(fp_out," %1.1f euro \n", new_price);
}
else
{
if (car[i].engine>=1600)
{
new_price = (car[i].price)*(0.95);
printf(" %1.1f euro \n", new_price);
fprintf(fp_out," %1.1f euro \n", new_price);
}
else
{
if (car[i].price >= 20000)
{
new_price = (car[i].price)*(0.9);
printf(" %1.1f euro \n", new_price);
fprintf(fp_out," %1.1f euro\n", new_price);
}
else
{
printf("N/A \n");
fprintf(fp_out, "N/A\n");
}
}
}
}
i++;
}
}
break;
}
}

}
return(EXIT_SUCCESS);
}

最佳答案

它可以比使用 switch 语句和所有重复更简单地完成。如果您首先清除退出(和无效)选项,则您只能执行一个或两个选项权限:

if (option & 1)         // to terminal
printf("%7s\t %6s\t %4d cc\t %1.1f euro\t %d\t ", car[i].brand, car[i].model, car[i].engine, car[i].price, car[i].year);
if (option & 2) { // to file
if ((fp_out = fopen("outdata.txt", "w")) == NULL)
exit(1); // improve this
fprintf(fp_out, "%7s\t %6s\t %4d cc\t %1.1f euro\t %d\t ", car[i].brand, car[i].model, car[i].engine, car[i].price, car[i].year);
fclose(fp_out);
}

您可以通过首先准备(相同的)输出来进一步减少重复。

char data_str[500];
sprintf(data_str, "%7s\t %6s\t %4d cc\t %1.1f euro\t %d\t ", car[i].brand, car[i].model, car[i].engine, car[i].price, car[i].year);
if (option & 1) // to terminal
printf("%s\n", data_str);
if (option & 2) { // to file
if ((fp_out = fopen("outdata.txt", "w")) == NULL)
exit(1); // improve this
fprintf(fp_out, "%s\n", data_str);
fclose(fp_out);
}

关于c - 带有 C 语言开关的用户菜单 [包含代码],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30197150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com