| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 612 人关注过本帖
标题:[求助]请求修改一下这个计算器的C程序
取消只看楼主 加入收藏
hsb008
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-6-29
收藏
 问题点数:0 回复次数:0 
[求助]请求修改一下这个计算器的C程序

问题:设计一个模拟计算器的程序,要求对包括加、减、乘、除、括号运算及SQRABS函数的任意整型表达式的求解。
以下程序中a表示绝对值,计算时放在后面 如(3-4)a表示求3-4的绝对值,s表示平方,如(4-3)s表示4-3的平方.
但是本程序还有错误,输入(5-9/3)结果为4,不知道为什么, 请求高手帮忙修改!!谢谢

#include<math.h>

#include<ctype.h>

#include<stdio.h>

typedef struct list

{ double data;

struct list *next;

}NODE;

typedef NODE *LLIST;

LLIST operater=NULL;

LLIST number=NULL;

LLIST oper=NULL;

LLIST ber=NULL;

int empty(LLIST operater)

{

if(operater==NULL)return 1;

else return 0;

}

LLIST stackin(LLIST operater,double b) /*进栈*/

{

LLIST ptr;

ptr=(LLIST)malloc(sizeof(NODE));

ptr->data=b;

ptr->next=operater;

operater=ptr;

return operater;

}

LLIST stackout(LLIST number,double *num) /*出栈*/

{

LLIST ptr=number;

if(number!=NULL)

{

number=number->next;

*num=ptr->data;

free(ptr);

return number;

}

}

int max(char sum) /*设定优先级*/

{

switch(sum)

{

case '+':

case '-':return 2;

case '*':

case '/':return 3;

case '(':

case ')':return 1;

case 'a':return 4;

case 's':return 4;

default:return 0;

}

}

double tatal(double num1,double num2,char sum) /*计算*/

{

int i;

double s;

switch(sum)

{

case '+':return(num2+num1);

case '-':return(num2-num1);

case '*':return(num2*num1);

case '/':return(num2/num1);

case 'a':return(abs(num2));

case 's':return(num2*num2) ;

}

}

Contrary(double num1,double num2,char sum)

{

double s;

int i;

switch(sum)

{

case '-':return(num1-num2);

case '/':return(num1/num2);

}

}

outdata(LLIST operater) /*输出数据*/

{

LLIST ber=operater;

while(ber!=NULL)

{

printf("%lf ",ber->data);

ber=ber->next;

}

printf("\n");

}

outoper(LLIST operater) /*输出计算符*/

{

LLIST oper=operater;

while(oper!=NULL)

{

printf("%c",oper->data);

oper=oper->next;

}

printf("\n");

}

char *filter(char *c) /*读取表达式*/

{

int j=0,i=0;

char *s;

while(c[i]!='\0'&&c[i]!='\n')

{

if(c[i]!=' '&&max(c[i])||isdigit(c[i])||c[i]=='.')

{

if(c[i]=='a'||c[i]=='s'){ if(isdigit(c[i-1])||c[i-1]==')')s[j++]=c[i]; }

else if(c[i]=='.'){ if(isdigit(c[i-1]))s[j++]=c[i]; }

else s[j++]=c[i];

}

i++;

}

s[j]='\0';

return s;

}

Resolve(char *s,double *a,char *t) /*对读取的表达式进行分析处理,分开数据和操作符*/

{

int k=1,i=0,l=0;

while(s[i]!='\0'&&s[i]!='\n')

{

char p[50]={0}; int j=0;

if(max(s[i])){ if(s[i]=='('&&s[i+1]=='-')

{

i+=2;

while(isdigit(s[i])||s[i]=='.')p[j++]=s[i++];

a[k++]=0-atof(p);

i++;

}

else t[l++]=s[i++];

}

else {

while(isdigit(s[i])||s[i]=='.')p[j++]=s[i++];

a[k++]=atof(p);

}

}

t[l]='\0';

a[0]=k-1;

}

main() /*主函数*/

{

double k,j=0,i=0,a[50];

double num,num1,num2,sum,kern=1;

char b[50],c[100],*s;

printf("please input the Expression :\n ");

gets(c);

s=filter(c);

Resolve(s,a,b);

while(b[i]!='\0'&&b!='\n')

{

if(b[i]!='('){ if(kern<=a[0])number=stackin(number,a[kern++]); }

if(b[i]=='a'||b[i]=='s'){

number=stackout(number,&num);

number=stackin(number,tatal(0,num,b[i]));

}

else if(operater==NULL||b[i]=='(')operater=stackin(operater,(double)b[i]);

else if(b[i]==')')

{

if((char)operater->data=='(')operater=stackout(operater,&sum);

else { while((char)operater->data!='(')

{

number=stackout(number,&num);

ber=stackin(ber,num);

operater=stackout(operater,&sum);

if(oper==NULL)oper=stackin(oper,sum);

else {

if(max((char)sum)<=max((char)oper->data)&&ber->next!=NULL)

{

ber=stackout(ber,&num1);

ber=stackout(ber,&num2);

oper=stackout(oper,&num);

ber=stackin(ber,tatal(num1,num2,(char)num));

}

oper=stackin(oper,sum);

}

}

number=stackout(number,&num1);

ber=stackin(ber,num1);

operater=stackout(operater,&sum);

ber=stackout(ber,&num1);

ber=stackout(ber,&num2);

oper=stackout(oper,&num);

if((char)num=='-'||(char)num=='/')number=stackin(number,Contrary(num1,num2,(char)num));

else { number=stackin(number,tatal(num1,num2,(char)num)); }

}

}

else {

if(max(b[i])<=max((char)operater->data)&&number->next!=NULL)

{

number=stackout(number,&num1);

number=stackout(number,&num2);

operater=stackout(operater,&sum);

num=tatal(num1,num2,(char)sum);

number=stackin(number,num);

}

operater=stackin(operater,(double)b[i]);

}

i++;

}

if(kern<=a[0])number=stackin(number,a[kern]);

while(operater!=NULL)

{

number=stackout(number,&num1);

number=stackout(number,&num2);

operater=stackout(operater,&sum);

number=stackin(number,tatal(num1,num2,(char)sum));

}

for(k=0;s[k]!='\0';k++)printf("%c",s[k]);

number=stackout(number,&num2);

printf("=%lf\n",num2);

printf("\nSuccess! Press any key to exit!\n");

getch();

}

搜索更多相关主题的帖子: 计算器 Roman 
2006-06-30 11:51
快速回复:[求助]请求修改一下这个计算器的C程序
数据加载中...
 
   



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

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