如何在C中调用函数

时间:2018-04-14 02:17:11

标签: c function

我正在为我的编程类介绍完成我的最终项目,并且分配是创建一个显示菜单的代码,并要求用户输入一个选项。之后,它将显示订单的总数和用户选择的项目的名称。

这是我的代码:

#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应该只调用其他函数。我一直在尝试,但对我来说真的很困惑,我不知道该怎么做。如果你们能帮助我,我将非常感激。 谢谢!

1 个答案:

答案 0 :(得分:0)

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

#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;
}

注意:未编译或测试。如果您复制此代码,请将其作为您自己的代码交付,并且您的教师会让您这样做 - 这就是您自己。

祝你好运。