【问题标题】:Fraction Arithmetic Program in CC中的分数算术程序
【发布时间】:2015-12-07 00:02:34
【问题描述】:

程序应如下所示(USER INPUT IN BOLD):

    Welcome to the Fraction Arithmetic Program.
    -------------------------------------------
    Your problems with fractions can be solved here. Enter a fraction arithmetic problem (Example 2/5 -4/7).

1/2 + 1/4

    The answer is 6/8.

提示:您的程序应处理加法、减法、乘法和除法运算。对于这个版本的程序,答案不必是最低限度的。

计划您的程序,使其模块化。在一个模块中完成输入,在第二个模块中完成输出,在第三个模块中完成计算。将每个模块放在单独的源代码文件中。 main() 函数应该驻留在自己的模块中,并且是不使用任何全局变量的程序。

到目前为止,我得到的是:

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

int main( void )
{   
    int a,b,c,d,e,f,g,h;
    int oper;
    float n1, n2, d1, d2;
    float n11, n22, d11, d22;
    int problem; 
    int restart;
    char operation;

    printf("Welcome to the FRACTION ARITHMETIC PROGRAM\n-------- ---------- -------\n");
    OUT:
    printf("Indicate which operation ( 1=Add , 2=Subtract , 3=Multiply , 4=Divide ): ");
    scanf("%d", &oper);

    if (oper== 1 | oper== 2 | oper== 3 | oper== 4){   
        printf("Enter your two fractions with one space in between: ");
        scanf("%f/%f %f/%f", &n1, &d1, &n2, &d2);
        switch(oper){   
            case 1:
                a=n1*d2+n2*d1;
                b=d1*d2;
                printf("The answer is %d/%d", a,b);
                if (b=0)
                    printf ("ILLEGAL INPUT");
                break;
            case 2:
                c=n1*d2-n2*d1;
                d=d1*d2;
                printf("The answer is %d/%d", c,d);
                if (d=0)
                    printf ("ILLEGAL INPUT");
                break;
            case 3:
                e=n1*d2;
                f=n2*d1;
                printf("The answer is %d/%d", e,f);
                if (f=0)
                    printf ("ILLEGAL INPUT"); 
                break;
            case 4:
                g=n1*d1;
                h=n2*d2;
                printf("The answer is %d/%d", g,h);
                if (h=0)
                    printf ("ILLEGAL INPUT");
                break;
        }
    }

    printf("\t Another Problem (1=Yes or 2=No)? ");
    scanf("%d", &restart);

    if(restart==1)
        goto OUT;
    if (restart==2)
        goto DONE;

    DONE: 
    printf("Goodbye and thank you.");
    return 0;
}

我不了解整个源代码文件并使程序模块化。有人可以帮忙吗,任何事情都非常感谢!!!谢谢

【问题讨论】:

  • 这看起来像是你要求别人为你做的家庭作业。
  • “在一个模块中完成输入,在第二个模块中完成输出,在第三个模块中完成计算”。这已经是关于如何使代码模块化的非常好的指南。这意味着在单独的函数中为每个操作编写代码。

标签: c modular


【解决方案1】:

使其模块化意味着您必须将此源代码分解为不同的文件。在这种情况下,您需要提供 3 个模块,以及一个包含 main() 方法的文件。这意味着您应该拥有以下文件:

fraction_arithmetic_program.c // Contains the main method.
fraction_arithmetic_input.c   // Handles all the user input.
fraction_arithmetic_input.h   // Header to the above file.
fraction_arithmetic_output.c  // Handles all the output back to the user.
fraction_arithmetic_output.h  // Header to the above file.
fraction_arithmetic_calc.c    // Does the necessary calculations.
fraction_arithmetic_calc.h    // Header to the above file.

fraction_arithmetic_program.c 文件中,您有自己的主要方法。此文件包括所有三个:fraction_arithmetic_input.hfraction_arithmetic_output.hfraction_arithmetic_calc.h。这意味着,在您的主文件(/Main 方法)中,您可以使用模块fraction_arithmetic_input.cfraction_arithmetic_output.cfraction_arithmetic_calc.c 提供给您的所有功能。

在这 3 个.c 文件中,您必须放置相应的方法。你想如何将这段代码分解成这 3 个文件取决于你,但你会受到一些非常具体的限制,比如你必须如何去做,所以我会坚持这些。如果您有任何问题,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-21
    • 2019-07-30
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 2010-09-11
    • 1970-01-01
    相关资源
    最近更新 更多