gpt4 book ai didi

c - C语言中如何调用函数

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

我正在为我的编程入门类(class)做最后一个项目,任务是创建一个代码,该代码将显示一个菜单并要求用户输入一个选择。之后,它将显示订单总计以及用户选择的商品名称。

这是我的代码:

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

#define TEQUENOS_PRICE 5.00
#define AREPA_PRICE 3.00
#define CACHAPA_PRICE 2.50
#define EMPANADA_PRICE 2.00
#define SODA_PRICE 0.80
#define QUESILLO_PRICE 1.70
#define DULCE_DE_LECHE_PRICE 1.20

int main()
{
int choice = 0, tequenos = 0, arepa = 0, cachapa = 0, empanada = 0;
int soda = 0, quesillo = 0, dulceDeLeche = 0;
double sum = 0.0, totalPrice;

printf("Welcome to Daniela's Venezuelan emporium.\n");
printf("\nMay I take your order please?\n");

do
{
printf("\n\tMenu");
printf("\n1. Tequenos (5 ct.)\t $%5.2lf", TEQUENOS_PRICE);
printf("\n2. Arepa\t\t $%5.2lf", AREPA_PRICE);
printf("\n3. Cachapa\t\t $%5.2lf", CACHAPA_PRICE);
printf("\n4. Empanada\t\t $%5.2lf", EMPANADA_PRICE);
printf("\n5. Soda \t\t $%5.2lf", SODA_PRICE);
printf("\n6. Quesillo\t\t $%5.2lf", QUESILLO_PRICE);
printf("\n7. Dulce de Leche\t $%5.2lf", DULCE_DE_LECHE_PRICE);
printf("\n8. Done with this order.\n");
printf("\nEnter Item: ");
scanf("%i", &choice);

switch (choice)
{
case 1:
sum += TEQUENOS_PRICE;
tequenos++;
break;
case 2:
sum += AREPA_PRICE;
arepa++;
break;
case 3:
sum += CACHAPA_PRICE;
cachapa++;
break;
case 4:
sum += EMPANADA_PRICE;
empanada++;
break;
case 5:
sum += SODA_PRICE;
soda++;
break;
case 6:
sum += QUESILLO_PRICE;
quesillo++;
break;
case 7:
sum += DULCE_DE_LECHE_PRICE;
dulceDeLeche++;
break;
case 8:
break;
default:
printf("\n\t***ERROR: This is not on the menu.\n");
}
printf("\nTotal so far: $%.2lf\n", sum);
} while (choice != 8);

printf("\nThat's: ");

if (choice = 1)
{
printf(" %i Tequenos\n", tequenos);
}
if (choice = 2)
{
printf("\t %i Arepa\n", arepa);
}
if (choice = 3)
{
printf("\t %i Cachapa\n", cachapa);
}
if (choice = 4)
{
printf("\t %i Empanada\n", empanada);
}
if (choice = 5)
{
printf("\t %i Soda\n", soda);
}
if (choice = 6)
{
printf("\t %i Quesillo\n", quesillo);
}
if (choice = 7)
{
printf("\t %i Dulce De Leche\n", dulceDeLeche);
}

totalPrice = sum;
printf("\nYour total is $%.2lf\n", totalPrice);
printf("\nThank you for coming to Daniela's Venezuelan Emporium.");
printf("\nCome back soon!\n");

system("pause");
return 0;
}

代码工作正常,但我的教授告诉我,我需要在整个代码中调用函数,而 main 应该只调用其他函数。我一直在尝试,但对我来说真的很困惑,我不知道该怎么做。如果你们能帮助我,我将非常感激。谢谢!

最佳答案

这是您的代码,稍微重构以将程序的主要部分分解为单独的函数。

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

#define TEQUENOS_PRICE 5.00
#define AREPA_PRICE 3.00
#define CACHAPA_PRICE 2.50
#define EMPANADA_PRICE 2.00
#define SODA_PRICE 0.80
#define QUESILLO_PRICE 1.70
#define DULCE_DE_LECHE_PRICE 1.20

int tequenos = 0,
arepa = 0,
cachapa = 0,
empanada = 0,
soda = 0,
quesillo = 0,
dulceDeLeche = 0;

double sum = 0.0;

void print_header()
{
printf("Welcome to Daniela's Venezuelan emporium.\n");
printf("\nMay I take your order please?\n");
}

int get_menu_choice()
{
int choice;

printf("\n\tMenu");
printf("\n1. Tequenos (5 ct.)\t $%5.2lf", TEQUENOS_PRICE);
printf("\n2. Arepa\t\t $%5.2lf", AREPA_PRICE);
printf("\n3. Cachapa\t\t $%5.2lf", CACHAPA_PRICE);
printf("\n4. Empanada\t\t $%5.2lf", EMPANADA_PRICE);
printf("\n5. Soda \t\t $%5.2lf", SODA_PRICE);
printf("\n6. Quesillo\t\t $%5.2lf", QUESILLO_PRICE);
printf("\n7. Dulce de Leche\t $%5.2lf", DULCE_DE_LECHE_PRICE);
printf("\n8. Done with this order.\n");
printf("\nEnter Item: ");

scanf("%i", &choice);
return choice;
}

void process_menu_choice(int choice)
{
switch (choice)
{
case 1:
sum += TEQUENOS_PRICE;
tequenos++;
break;
case 2:
sum += AREPA_PRICE;
arepa++;
break;
case 3:
sum += CACHAPA_PRICE;
cachapa++;
break;
case 4:
sum += EMPANADA_PRICE;
empanada++;
break;
case 5:
sum += SODA_PRICE;
soda++;
break;
case 6:
sum += QUESILLO_PRICE;
quesillo++;
break;
case 7:
sum += DULCE_DE_LECHE_PRICE;
dulceDeLeche++;
break;
case 8:
break;
default:
printf("\n\t***ERROR: This is not on the menu.\n");
}

printf("\nTotal so far: $%.2lf\n", sum);
}

void take_order()
{
do
{
choice = get_menu_choice();

process_menu_choice(choice);
} while (choice != 8);
}

void print_total_for(char *item_type, int item_count)
{
if(item_count > 0)
printf(" %i %s\n", item_count, item_type);
}

int print_total(double totalPrice)
(
printf("\nYour total is $%.2lf\n", totalPrice);
printf("\nThank you for coming to Daniela's Venezuelan Emporium.");
printf("\nCome back soon!\n");
}

void print_total_header()
{
printf("\nThat's: ");
}

int main()
{
int choice = 0;

print_header();

take_order();

print_total_header();

print_total_for("Tequenos", tequenos);
print_total_for("Arepa", arepa);
print_total_for("Cachapa", cachapa);
print_total_for("Empanada", empanada);
print_total_for("Soda", soda);
print_total_for("Quesillo", quesillo);
print_total_for("Dulce De Leche", dulceDeLeche);

print_total(sum);

system("pause");
return 0;
}

注意:未编译或测试。如果您复制此代码,并将其作为您自己的代码上交,您的导师会因此而让您不及格 - 这是您的责任。

祝你好运。

关于c - C语言中如何调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49827243/

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