| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2407 人关注过本帖
标题:[求助]数据结构一个问题,解决不了,有理数四则运算……
取消只看楼主 加入收藏
willon
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2007-6-14
收藏
 问题点数:0 回复次数:2 
[求助]数据结构一个问题,解决不了,有理数四则运算……

我做的一个数据结构综合性算法设计题目:内容是有理数的四则运算,实现两个有理数的相加,相减,相乘以及求分子分母的运算,我已经完成,可是不知道为什么结果一直都是零。我想可能是全局变量定义有问题,可是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;
}
}


搜索更多相关主题的帖子: 数据结构 有理数 运算 double 变量 
2007-06-14 14:23
willon
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2007-6-14
收藏
得分:0 

多谢各位了,我已经找到问题并解决了~~


2007-06-20 15:01
willon
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2007-6-14
收藏
得分:0 
高手啊~~

2007-06-20 15:10
快速回复:[求助]数据结构一个问题,解决不了,有理数四则运算……
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036318 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved