我做的一个数据结构综合性算法设计题目:内容是有理数的四则运算,实现两个有理数的相加,相减,相乘以及求分子分母的运算,我已经完成,可是不知道为什么结果一直都是零。我想可能是全局变量定义有问题,可是c语言书上明明说,全局变量在某个函数中改变了值在其他函数中也可以使用这个已经改变的值啊……哪位大虾帮助下把……急啊……!!
#include "stdio.h"
#include "conio.h"
#define yx 10000
int choice1,choice2;
double xiaoshuA;
double xiaoshuB;
double result;
long fenshuA1,fenshuA2;
long fenshuB1,fenshuB2;
long result_fenzi,result_fenmu;
void main()
{void choice_input();
void print_opening();
void input_xiaoshu();
void input_a_xiaoshu();
void input_fenshu();
void input_a_fenshu();
void xiaoshu_add();
void xiaoshu_sub();
void xiaoshu_mul();
void xiaoshu_fenzifenmu(double a);
void fenshu_fenzifenmu();
void fenshu_add();
void fenshu_sub();
void fenshu_mul();
long gongyueshu(long m,long n);
choice_input();
getch();
}
void choice_input(){
printf("***************Rational Number Operation Program****************\n");
printf("what type data do you want to input?Xiao shu or Fenshu?\n");
printf("1.input xiao shu\n");
printf("2.input fenshu\n");
printf("0.exit\n");
printf("please input you choice 1:\n");
scanf("%d",&choice1);
switch(choice1)
{ case 1:print_opening(); break;
case 2:print_opening();break;
case 0:exit(); break;
default :printf("error!\n");choice_input();}
}
void print_opening(){
printf("***************Rational Number Operation Program****************\n");
printf("***************What operation do you want to do?****************\n");
printf("1. Two rational number addition\n");
printf("2. Two rational number subtraction\n");
printf("3. Two rational number multiplication\n");
printf("4. Undo The rational number's numerator and denominator\n");
printf("5. exit\n");
printf("please input you choice 2:");
scanf("%d",&choice2);
if(choice1==1){
switch(choice2){
case 1: input_xiaoshu();xiaoshu_add();break;
case 2: input_xiaoshu();xiaoshu_sub();break;
case 3: input_xiaoshu();xiaoshu_mul();break;
case 4: input_a_xiaoshu();xiaoshu_fenzifenmu(xiaoshuA);break;
case 5: exit();
default :printf("error\n");choice_input();}
}
else {
switch(choice2){
case 1: input_fenshu();fenshu_add();break;
case 2: input_fenshu();fenshu_sub();break;
case 3: input_fenshu();fenshu_mul();break;
case 4: input_a_fenshu();fenshu_fenzifenmu(fenshuA1,fenshuA2);break;
case 5: exit(0);
default :printf("error\n");choice_input();}
}
}
void input_xiaoshu(){
printf("please input the first XiaoShu A:\n");
scanf("%f",&xiaoshuA);
printf("please input the second XiaoShu B:\n");
scanf("%f",&xiaoshuB);
}
void input_a_xiaoshu(){
printf("please input the xiaoshu:\n");
scanf("%f",&xiaoshuA);
}
void input_fenshu(){
printf("please input the first FenShu A:\n");
scanf("%d/%d",&fenshuA1,&fenshuA2);
printf("please input the second FenShu B:\n");
scanf("%d/%d",&fenshuB2,&fenshuB2);}
void input_a_fenshu(){
printf("please input a FenShu:\n");
scanf("%d/%d",&fenshuA1,&fenshuA2);}
void xiaoshu_add() {
result=xiaoshuA+xiaoshuB;
printf("Addition result=%f\n",result);
printf("\n");
printf("\n");
printf("\n");
choice_input();}
void xiaoshu_sub() {
result=xiaoshuA-xiaoshuB;
printf("subtration result=%f\n",result);
printf("\n");
printf("\n");
printf("\n");
choice_input();}
void xiaoshu_mul(){
result=xiaoshuA*xiaoshuB;
printf("multiplication result=%f\n",result);
printf("\n");
printf("\n");
printf("\n");
choice_input();}
void xiaoshu_fenzifenmu(double a){
int n;
a=a*yx;
n=gongyueshu(a,yx);
printf(" result=%d/%d\n",a/n,yx/n);
printf("\n");
printf("\n");
printf("\n");
choice_input();}
void fenshu_fenzifenmu(){
int n;
n=gongyueshu(fenshuA1,fenshuA2);
printf("result=%d/%d\n",fenshuA1/n,fenshuA2/n);
printf("\n");
printf("\n");
printf("\n");
choice_input();
}
void fenshu_add(){
long gfm,n;
gfm=fenshuA2*fenshuB2;
result_fenzi=fenshuA1*fenshuB2+fenshuA2*fenshuB1;
n=gongyueshu(result_fenzi,gfm);
result_fenzi=result_fenzi/n;
result_fenmu=gfm/n;
printf("addition result is:%d/%d\n",result_fenzi,result_fenmu);
printf("\n");
printf("\n");
printf("\n");
choice_input();}
void fenshu_sub(){
long gfm,n;
gfm=fenshuA2*fenshuB2;
result_fenzi=fenshuA1*fenshuB2-fenshuA2*fenshuB1;
n=gongyueshu(result_fenzi,gfm);
result_fenzi=result_fenzi/n;
result_fenmu=gfm/n;
printf("subtraction result is:%d/%d\n",result_fenzi,result_fenmu);
printf("\n");
printf("\n");
printf("\n");
choice_input();}
void fenshu_mul(){
int n;
result_fenzi=fenshuA1*fenshuB1;
result_fenmu=fenshuA2*fenshuB2;
n=gongyueshu(result_fenzi,result_fenmu);
result_fenzi/=n;
result_fenmu/=n;
printf("multiplitation result is:%d/%d\n",result_fenzi,result_fenmu);
printf("\n");
printf("\n");
printf("\n");
choice_input();}
long gongyueshu(long m,long n){ \\*求公约数*\\
long r;
m=m>n?m:n;
r=n;
while(r!=0) {
r=m%n;
if(r=0) return(n);
m=n;
n=r;
}
}