我该如何将其分解为函数?

时间:2012-12-06 15:04:54

标签: c function

这个程序现在正在运行而不使用任何功能,但我想实现它们以使其更加模块化。

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

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

3 个答案:

答案 0 :(得分: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

}

这是将碎片分开并重构的一种方法。请放心,还有很多其他方法。想一想原来的任务和被认为是“工作单位”的东西。您可能想要重用/重新执行的任何内容都是函数的一个很好的例子。

答案 1 :(得分: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. 输出给用户
  4. 如果你愿意,你可以做三个功能。 (对于这样一个简单的程序来说有点矫枉过正,但这对于教育目的来说是可行的。)

    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);
    

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

答案 2 :(得分: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);
}
相关问题