| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 689 人关注过本帖
标题:这个程序我调了几天了,实在没办法了,还是不通..
只看楼主 加入收藏
fujian26
Rank: 1
等 级:新手上路
帖 子:131
专家分:0
注 册:2007-4-3
收藏
 问题点数:0 回复次数:3 
这个程序我调了几天了,实在没办法了,还是不通..
/* Note:Your choice is C IDE */
/* Note:Your choice is C IDE */
#include<stdio.h>
#include <string.h>
#include <conio.h>
#define PLUS 0
#define MINUS 1
#define POWER 2
#define DIVIDE 3
#define LEFTP 4
#define RIGHP 5
#define STARTEND 6
#define DIGIT 7
#define POINT 8
#define NUM 7
#define NO 32767
#define STACKSIZE 20
/*//运算符*/
char a[]={'+','-','*','/','(',')','#'};
/*//优先关系矩阵,规定:=为0,>为1,<为-1,空为NO*/
int PriorityTable[7][7]={{ 1, 1,-1,-1,-1, 1, 1},
{ 1, 1,-1,-1,-1, 1, 1},
{ 1, 1, 1, 1,-1, 1, 1},
{ 1, 1, 1, 1,-1, 1, 1},
{-1,-1,-1,-1,-1, 0, NO},
{ 1, 1, 1, 1,NO, 1, 1},
{-1,-1,-1,-1,-1,NO, 0}
};
int menu(void);
/***************************/
/* 输入表达式函数 */
/***************************/
void InputExpression(char str[])
{ int len;
printf("Input expression string:\n");
scanf("%s",str);
len=strlen(str);
str[len]='#';
str[len+1]='\0';
}
/**************操作函数*****************************/
/**************************************************/
void operate(double a,int b,double c)
{ if(b==0)
a=a+c;
else if(b==1)
a=a-c;
else if(b==2)
a=a*c;
else if(b==3)
a=a/c;
else return;
}
/*************************************************/
/* 确定当前字符类型 */
/*************************************************/
/* 若为运算符,则返回运算符在运算符数组中的序号 */
/* 若为数字字符,则返回DIGIT */
/* 若为小数点字符,则返回POINT */
/* 否则为非法字符,返回-1 */
/*************************************************/
int GetCharType(char ch)
{ int i;
for(i=0;i<NUM;i++) if(ch==a[i]) return(i);
if(ch>='0' && ch<='9') return(DIGIT);
if(ch=='.') return(POINT);
return(-1);
}
/*************************************************/
/* 表达式计算 */
/*************************************************/
/* 输入参数:char *str,表达式字符串 */
/* 输出参数:double *Result,表达式计算结果 */
/* 函数返回值:1 计算正确 */
/* 0 表达式有错 */
/*************************************************/
int EXCUTE(char *str,double *Result)
{
int pp,strlength,topTr,topNd,CharType,OPTR[STACKSIZE];
double number,temp,OPND[STACKSIZE];
OPTR[0]=STARTEND;
topTr=1;
topNd=0;
pp=0;
while((str[pp]))
{ CharType=GetCharType(str[pp]); /*//确定字符类型*/
switch(CharType)
{ case -1: /*//不是表达式中的字符,表达式有错*/
return(0);
case DIGIT: /*//数字字符,实数的开始字符*/
number=0;
while(str[pp]>='0' && str[pp]<='9') /*//处理实数的整数部分*/
{ number=number*10+(str[pp]-48);
pp++;
}
if(str[pp]=='.') /*//遇到小数点*/
{ temp=10.0;
pp++;
while(str[pp]>='0' && str[pp]<='9') /*//处理实数的小数部分*/
{ number=number+(str[pp]-48)/temp;
temp=temp*10;
pp++;
}
}
OPND[topNd]=number;
topNd++;
break;
case POINT: /*//小数点,以小数点开头的实数*/
number=0;
temp=10.0;
pp++;
while(str[pp]>='0' && str[pp]<='9')
{ number=number+(str[pp]-48)/temp;
temp=temp*10;
pp++;
}
OPND[topNd]=number;
topNd++;
break;
case PLUS:
case MINUS:
case POWER:
case DIVIDE:
if(PriorityTable[OPTR[topTr-1]][CharType]==-1)
{OPTR[topTr]=GetCharType(str[pp]);
topTr++; pp++;
}
else if(PriorityTable[OPTR[topTr]][CharType]==1)
{operate(OPND[topNd-2],OPTR[topTr-1],OPND[topNd-1]);
topNd--; topTr--; pp++;
}
break;
case LEFTP:
OPTR[topTr]=GetCharType(str[pp]);
topTr++; pp++;
break;
case RIGHP:
if(OPTR[topTr-1]==4)
{topTr--; pp++;}
else if(OPTR[topTr-1]>-1&&OPTR[topTr-1]<4)
{operate(OPND[topNd-2],OPTR[topTr-1],OPND[topNd-1]);
topNd--; topTr--; pp++;
}
else if(OPTR[topTr-1]==6) return(0);
break;
case STARTEND: /*//遇到表达式结束符*/
while(OPTR[topTr-1]!=6)
{operate(OPND[topNd-2],OPTR[topTr-1],OPND[topNd-1]);
topNd--; topTr--;
}
if(topNd==1)
{ *Result=OPND[0];
return(1);
}
else return(0);
}
}
return(1);
}
void main()
{ int num,flag;
double result;
char str[256];
str[0]=0;
while(1)
{ num=menu();
switch(num)
{
case 1: /*//输入表达式*/
InputExpression(str);
flag=0;
printf("%s\n",str);
getchar();
break;
case 2: /*//计算表达式*/
if(str[0]==0)
{ printf("Expresson is Empty!");
getchar();
break;
}
if(!EXCUTE(str,&result))
{ printf("The expression has error!\n");
getchar();
}
else
{ printf("calulation has finished!\n");
getchar();
flag=1;
}
break;
case 3: /*//打印计算结果*/
if(flag)
{ printf("#%s=%lf\n",str,result);
getchar();
}
break;
case 4: /* //退出系统*/
break;
}
if(num==4) break;
}
}
int menu(void)
{ int num;
clrscr();
printf("%20c1--input expression\n",' ');
printf("%20c2--calculation expression\n",' ');
printf("%20c3--print result\n",' ');
printf("%20c4--Quit\n",' ');
printf(" please select 1,2,3,4:");
do{
scanf("%d",&num);
}while(num<1 || num>4);
return(num);
}


这是一道数据结构的题,是利用栈来实现算术表达式的求值,输入例如3+6*6, 但我调出来的时候选采单2的时候什么都没出来,不知道怎么回事,哪位能帮我调一下 顺便把我错的地方给我说下,,谢谢了
搜索更多相关主题的帖子: define IDE 办法 include 
2007-11-12 17:41
otacon
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2007-10-28
收藏
得分:0 
scanf() gets()
等函数输入数据后 只输入了回车符前面的字符 而留下回车字符
所以你选2后输出结果 再运行到getchar() 的时候自动读入了回车符 所以继续循环执行了清屏 清掉了结果
在getchar()前面加上 fflush(stdin)刷新缓冲区 就行了


When people run in circles!It is a very very mad world!
2007-11-12 19:25
fujian26
Rank: 1
等 级:新手上路
帖 子:131
专家分:0
注 册:2007-4-3
收藏
得分:0 
谢谢了 那个问题解决了 ,我这程序还有问题,我还得下来再调调

退一步海阔天空.......
2007-11-12 19:34
eakcon
Rank: 1
等 级:新手上路
帖 子:754
专家分:0
注 册:2007-11-7
收藏
得分:0 
C是不是代表运算符+,-,*,/
scanf("%c",&c);
2007-11-13 11:45
快速回复:这个程序我调了几天了,实在没办法了,还是不通..
数据加载中...
 
   



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

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