【发布时间】: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;
}
我不了解整个源代码文件并使程序模块化。有人可以帮忙吗,任何事情都非常感谢!!!谢谢
【问题讨论】:
-
这看起来像是你要求别人为你做的家庭作业。
-
“在一个模块中完成输入,在第二个模块中完成输出,在第三个模块中完成计算”。这已经是关于如何使代码模块化的非常好的指南。这意味着在单独的函数中为每个操作编写代码。