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

我做的一个数据结构综合性算法设计题目:内容是有理数的四则运算,实现两个有理数的相加,相减,相乘以及求分子分母的运算,我已经完成,可是不知道为什么结果一直都是零。我想可能是全局变量定义有问题,可是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
duyanchao
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2007-6-12
收藏
得分:0 

我也写了一个能运行的!你看看能又帮助吗!(用的是堆栈的方法!只能一位运算)
#include <iostream.h>
#include <cstdio>
#include <stdlib.h>
#include <malloc.h>
#define STATCK_INTI_SIZE 100
#define stackingcreement 10
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int selemtype;
typedef int status;
typedef struct {
selemtype *base;
selemtype *top;
int stacksize;
} sqstack;//定义栈;
typedef struct {
char *base;
char *top;
int stacksize;
} sqstack1;//定义栈;

status initstack(sqstack &s){
//构造一个栈
s.base=(selemtype *)malloc((STATCK_INTI_SIZE)*sizeof(selemtype));
if(!s.base) exit(OVERFLOW);
s.top=s.base;
s.stacksize=STATCK_INTI_SIZE;
return OK;
}
//初始化一个栈

void push(sqstack &s,selemtype e){

if((s.top-s.base)>=s.stacksize){
//栈满追加空间
s.base=(selemtype *)realloc (s.base,(s.stacksize+STATCK_INTI_SIZE)*sizeof(selemtype));
if(!s.base) exit(OVERFLOW);
s.top=s.base+s.stacksize;
s.stacksize+=stackingcreement;
}
*s.top++=e;
}
//出棧

int pop(sqstack &s,selemtype e){
e=*(--(s.top));
return e;
}

//入棧
int gettop(sqstack s){
int e;
e=*(--(s.top));
return e;
}

//以下是用于处理字符的堆栈

status initstack1(sqstack1 &s){
//构造一个栈
s.base=(char *)malloc((STATCK_INTI_SIZE)*sizeof(char));
if(!s.base) exit(OVERFLOW);
s.top=s.base;
s.stacksize=STATCK_INTI_SIZE;
return OK;
}

void push1(sqstack1 &s,char e){

if((s.top-s.base)>=s.stacksize){
//栈满追加空间
s.base=(char *)realloc (s.base,(s.stacksize+STATCK_INTI_SIZE)*sizeof(char));
if(!s.base) exit(OVERFLOW);
s.top=s.base+s.stacksize;
s.stacksize+=stackingcreement;
}
*s.top++=e;
}
//出棧

char pop1(sqstack1 &s,char e){
e=*(--(s.top));
return e;
}

char gettop1(sqstack1 s){
char e;
e=*(--(s.top));
return e;
}


status display(sqstack s)
{
int *p=s.base;
int i=0;
for(i=0;i<(s.top-s.base);i++)
{

printf("%d",*p);
p++;
}
return OK;

}

status display1(sqstack1 s)
{
char *p=s.base;
int i=0;
for(i=0;i<(s.top-s.base);i++)
{

printf("%c",*p);
p++;
}
return OK;

}

//结束

////////以下是关于计算的算法(仅仅能计算一位算数)


status isin(char c)//判断是否是运算符
{
int a;
switch(c)
{
case '+':a=1;break;
case '-':a=1;break;
case '*':a=1;break;
case '/':a=1;break;
case '(':a=1;break;
case ')':a=1;break;
case '#':a=1;break;

default:a=0;break;
}
return a;
}

char precede(char a,char b)//比较运算符的优先级(加法有问题)
{
char c;
switch(a)
{
case '+':
{
switch(b)
{
case '+':c='>';break;
case '-':c='>';break;
case '*':c='<';break;
case '/':c='<';break;
case '(':c='<';break;
case ')':c='>';break;
case '#':c='>';break;
default:printf("输入错误\n");break;
}
}break;
case '-':
{
switch(b)
{
case '+':c='>';break;
case '-':c='>';break;
case '*':c='<';break;
case '/':c='<';break;
case '(':c='<';break;
case ')':c='>';break;
case '#':c='>';break;
default:printf("输入错误\n");break;
}
}break;
case '*':
{
switch(b)
{
case '+':c='>';break;
case '-':c='>';break;
case '*':c='>';break;
case '/':c='>';break;
case '(':c='<';break;
case ')':c='>';break;
case '#':c='>';break;
default:printf("输入错误\n");break;
}
}break;
case '/':
{
switch(b)
{
case '+':c='>';break;
case '-':c='>';break;
case '*':c='>';break;
case '/':c='>';break;
case '(':c='<';break;
case ')':c='>';break;
case '#':c='>';break;
default:printf("输入错误\n");break;
}
}break;
case '(':
{
switch(b)
{ case '+':c='<';break;
case '-':c='<';break;
case '*':c='<';break;
case '/':c='<';break;
case '(':c='<';break;
case ')':c='=';break;
}
}break;
case ')':
{
switch(b)
{ case '+':c='>';break;
case '-':c='>';break;
case '*':c='>';break;
case '/':c='<';break;
case ')':c='>';break;
case '#':c='>';break;
default:printf("输入错误\n");break;
}
}break;
case '#':
{switch(b)
{
case '+':c='<';break;
case '-':c='<';break;
case '*':c='<';break;
case '/':c='<';break;
case '(':c='<';break;
case '#':c='=';break;
default:printf("输入错误\n");break;
}
}break;
default:printf("输入错误\n");break;
}
return c;
}


int operate(int a,char thetc,int b)//数字1对应的ASCLL码是48
{
int result;
switch(thetc)
{
case '+':
result=a+b;
break;
case '-':
result=a-b;
break;
case '*':
result=a*b;
break;
case '/':
{
if(b==0)
printf("除数为零,错误\n");
else
result=a/b;
}
}
return result;
}

int evaluateexpression()//算数运算式的解决方案
{
char c;//接受数字
int a,b,e,f;//负责接受需要运算时的数字
char x;//x是专门用于脱去括号
char theta;//theta专门负责用于运算时接受运算符的
sqstack opnd;
sqstack1 optr;
initstack1(optr);push1(optr,'#');
initstack(opnd);c=getchar();
while(c!='#' || gettop1(optr)!='#')
{
if(!isin(c)){

push(opnd,(c-48));
c=getchar();
}//不是运算符就进站
else
switch(precede(gettop1(optr),c))//有问题
{
case '<':{
push1(optr,c);
c=getchar();
}
break;
case '=':{
pop1(optr,x);
c=getchar();
}
break;
case '>':{
theta=pop1(optr,theta);
e=pop(opnd,a);
f=pop(opnd,b);
push(opnd,operate(f,theta,e));
}//有问题
break;


}//switch
}//while
return gettop(opnd);
}

//////////////////算法结束
void main()
{
int k;
k=evaluateexpression();
printf("%d\n",k);
}


新手!老纯啦...
2007-06-14 23:00
yixiliuyun
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-6-16
收藏
得分:0 

太长了 不看了 给你看看和你一样的程序吧 应该是你要的
#include<stdio.h>
#include<malloc.h>
#include<string.h>

typedef struct Fnode/*符号单链表*/
{
int yxj;
char fh;
struct Fnode *next;
}Fnode;

void F_push(Fnode *h,char c)/*尾插*/
{
Fnode *p;
p=(Fnode*)malloc(sizeof(Fnode));
p->fh=c;
p->next=NULL;
if(c=='+'||c=='-')
p->yxj=1;
else if(c=='*'||c=='/')
p->yxj=2;
else if(c=='('||c==')')
p->yxj=0;
else
{ free(p); return ; }
while(h->next!=NULL)
h=h->next;
p->next=h->next;
h->next=p;
}

char F_pop(Fnode *h)/*尾删*/
{
char c;
while(h->next->next!=NULL)
h=h->next;
c=h->next->fh;
free(h->next);
h->next=NULL;
return c;
}
int F_look(Fnode *h)
{
while(h->next!=NULL)
h=h->next;
return h->yxj;
}

/*------------------------------------------------------------*/
typedef struct Snode/*数字单链表*/
{
double data;
struct Snode *next;
}Snode;

void S_push(Snode *h,double c)/*尾插*/
{
Snode *p;
p=(Snode*)malloc(sizeof(Snode));
p->data=c;
p->next=NULL;
while(h->next!=NULL)
h=h->next;
p->next=h->next;
h->next=p;
}

double S_pop(Snode *h)/*尾删*/
{
double i;
while(h->next->next!=NULL)
h=h->next;
i=h->next->data;
free(h->next);
h->next=NULL;
return i;
}
/*-------------------------------------------------------*/

char *change(char *str)/*字符串前加(,后加 )*/
{
char *r;
*(str-1)='(';
str--;
r=str;
strcat(r,")");
return r;
}
/*;-------------------------------------------------*/
double final(char *str,Fnode *fh,Snode *sh)/*计算表达式结果*/
{
double i,temp,emp;
while(*str != '\0')
{
if(*str=='(')
{
F_push(fh,*str);
str++;
continue;
}
else if(*str>='0' && *str<='9')
{
emp=0;
while(*str>='0' && *str<='9')
{
emp=emp*10+(*str-'0');
str++;
}
S_push(sh,emp);
continue;
}
else if(*str=='.')
{
i=0.1; str++; emp=0;
if(*str>='0'&&*str<='9')
{
while(*str>='0'&&*str<='9')
{
emp+=(*str-'0')*i;
i*=0.1;
str++;
}
S_push(sh,(S_pop(sh)+emp));
}
}
else if(*str==')')
{
while(F_look(fh)!=0)
{
switch(F_pop(fh))
{
case '+':emp=S_pop(sh);S_push(sh,(S_pop(sh)+emp));printf("%f ",emp);break;
case '-':emp=S_pop(sh);S_push(sh,(S_pop(sh)-emp));printf("%f ",emp);break;
case '*':emp=S_pop(sh);S_push(sh,(S_pop(sh)*emp));printf("%f ",emp);break;
case '/':emp=S_pop(sh);S_push(sh,(S_pop(sh)/emp));printf("%f ",emp);break;
default : break;
}
}
F_pop(fh);
str++;
continue;
}
else
{
if(*str=='+'||*str=='-')
temp=1;
else
temp=2;
if(temp>F_look(fh))
F_push(fh,*str);
else
{
switch(F_pop(fh))
{
case '+':emp=S_pop(sh);S_push(sh,(S_pop(sh)+emp));printf("%f ",emp);break;
case '-':emp=S_pop(sh);S_push(sh,(S_pop(sh)-emp));printf("%f ",emp);break;
case '*':emp=S_pop(sh);S_push(sh,(S_pop(sh)*emp));printf("%f ",emp);break;
case '/':emp=S_pop(sh);S_push(sh,(S_pop(sh)/emp));printf("%f ",emp);break;
default : break;
}
if(temp==F_look(fh))
str--;
else
F_push(fh,*str);

}
str++;continue;
}
}
return S_pop(sh);
}

main()
{
Fnode *fh;
Snode *sh;
char *str;
fh=(Fnode*)malloc(sizeof(Fnode));
fh->next=NULL;
sh=(Snode*)malloc(sizeof(Snode));
sh->next=NULL;
str=change(gets(str));
printf("%lf",final(str,fh,sh));
system("pause");
}


我的qq算法群 供大家讨论哦 请各位喜欢算法的朋友加入哦 qq群号 40320457 嘿嘿
2007-06-17 00:01
duyanchao
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2007-6-12
收藏
得分:0 
果然比我 的简洁很多啊...高手啊

新手!老纯啦...
2007-06-18 17:34
zkkpkk
Rank: 2
等 级:论坛游民
威 望:5
帖 子:489
专家分:28
注 册:2006-6-17
收藏
得分:0 
算符优先算法

Viva,espana!
2007-06-18 19:58
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
duyanchao
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2007-6-12
收藏
得分:0 
恩继续加油哈。。。

新手!老纯啦...
2007-06-20 22:56
快速回复:[求助]数据结构一个问题,解决不了,有理数四则运算……
数据加载中...
 
   



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

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