-1

该程序现在可以在不使用任何功能的情况下运行,但我想实现它们以使其更加模块化。

我的问题是我不知道如何使用函数,如何调用函数,如何实现它。现在我真的很困惑如何理解它们。

#include <stdio.h>
#include <conio.h>
#define BERBER 27.95
#define PILE 15.95

void main () {
    int budget,lenh,wah,ftl,ftw;
    float convert,area,costl,costw;

    printf("WELCOME TO OUR SHOP!");
    printf("\nWE SALE CARPETS");
    printf("\nLength in <feet and inches>:\n");
    scanf_s("%d %d",&lenh,&ftl);fflush(stdin);
    printf("\nWidth in <feet and inches>:\n");
    scanf_s("%d %d",&wah,&ftw);fflush(stdin);

    costl=lenh(ftl/12.0);
    costw=wah(ftw/12.0);
    area=costl*costw;
    printf("\nTotal cost of the length : %.2f",costl);
    printf("\nTotal cost of the width : %.2f",costw);

    printf("\nYour budget:");scanf("%d",&budget);

    if (budget<BERBER) {
        convert=area*PILE;
        printf("\nYOu can only afford PILE");
    } else if (budget>PILE) {
        convert=area*BERBER;
        printf("\nYou can afford BERBER");
    } else {
        printf("\nThe choose is in you if you choose BERBER or PILE");
    }
    getch();
}
4

3 回答 3

1

在不为您编写解决方案的情况下,我将提供应该将您推向正确方向的伪代码(然后由您来实施):

float GetArea(){
  float area;

  // - output the greeting
  // - prompt user for inputs (width/length
  // - total area

  return area;
}

float GetBudget(){
  float budget;

  // - prompt user for budget

  return budget;
}

void CheckBudget(float area, float budget) {
  // -check your area vs budget
  // - output appropriate response
}

void main(){ // Starting Point
  float area, budget;

  // - call GetArea() and store it
  // - call GetBudget() and store it
  // - call CheckBudget() referencing both of these values

  // done

}

这是拆散和重构的一种方法。请放心,还有很多其他方法。想想最初的任务和什么被认为是“工作单元”。您可能想要重用/重新执行的任何内容都是函数的一个很好的例子。

于 2012-12-06T15:29:23.447 回答
0

使用函数的一个要点是将您的代码模块化为可重用的小块。如果您的代码没有“可重用”部分,则可能不需要制作函数。例如:

int main()
{
    printf("The color is blue\n");
    printf("I like blue\n");

    printf("The color is red\n");
    printf("I like red\n");

    printf("The color is green\n");
    printf("I like green\n");
    return 0;
}

在这里我一遍又一遍地做同样的事情,只改变一个部分,我们可以把它变成一个函数:

void printit(const char * color)
{
    printf("The color is %s\n", color);
    printf("I like %s\n", color);
}

int main()
{
    printit("blue");
    printit("red");
    printit("green");
    return;
}

main()现在我只是通过使用一个函数来处理常见任务来清理我的。每次都不同的部分(颜色)我变成了一个变量并将其传递给函数以允许对打印的内容进行一些自定义。这当然是一个非常简化的例子。


回顾一下,您的代码实际上并没有任何“可重用”部分......但您可能想使用函数来简单地保持它的清洁度,您的程序包含三个部分:

  1. 来自用户的输入
  2. 做数学
  3. 输出给用户

因此,如果您愿意,您可以执行三个功能。(对于这样一个简单的程序来说有点矫枉过正,但对于教育目的来说没关系)。

void welcome_and_input(int * lenh, int * wah, int *ftl, int *ftw)
    printf("WELCOME TO OUR SHOP!");
    printf("\nWE SALE CARPETS");
    printf("\nLength in <feet and inches>:\n");
    scanf_s("%d %d\\n",lenh,ftl);
    printf("\nWidth in <feet and inches>:\n");
    scanf_s("%d %d\\n",wah,ftw);
}

void main () {
    int budget,lenh,wah,ftl,ftw;
    float convert,area,costl,costw;

    welcome_and_input(&lenh, &wah, &ftl, &ftw);

看看您是否能理解该代码,它会接收您的欢迎消息和输入请求并将其分离为一个函数。

于 2012-12-06T15:36:39.857 回答
0

Holly,在发送问题之前:在谷歌中使用“c + function + tutorials”之类的搜索教程,你会发现很多网站可以回答你的大部分问题。然后,如果您有特定问题,您可以在此处发布您的问题以及详细信息。

因为我很好......这是一个简单的函数示例(它使用函数 area_calculation 进行面积计算):

#include <stdio.h>
#include <conio.h>
#define BERBER 27.95
#define PILE 15.95

//Prototype 
float area_calculation (float costl, float costw);

void main () {
    int budget,lenh,wah,ftl,ftw;
    float convert,area,costl,costw;

    printf("WELCOME TO OUR SHOP!");
    printf("\nWE SALE CARPETS");
    printf("\nLength in <feet and inches>:\n");
    scanf_s("%d %d",&lenh,&ftl);fflush(stdin);
    printf("\nWidth in <feet and inches>:\n");
    scanf_s("%d %d",&wah,&ftw);fflush(stdin);

    costl=lenh(ftl/12.0);
    costw=wah(ftw/12.0);
    //Call to the function area_calculation 
    area=area_calculation (costl,costw);
    printf("\nTotal cost of the length : %.2f",costl);
    printf("\nTotal cost of the width : %.2f",costw);

    printf("\nYour budget:");scanf("%d",&budget);

    if (budget<BERBER) {
        convert=area*PILE;
        printf("\nYOu can only afford PILE");
    } else if (budget>PILE) {
        convert=area*BERBER;
        printf("\nYou can afford BERBER");
    } else {
        printf("\nThe choose is in you if you choose BERBER or PILE");
    }
    getch();
}

//definition of the function area_calculation
float area_calculation (float costl, float costw)
{
    return (costl * costw);
}
于 2012-12-06T15:36:43.843 回答