C语言简单计算器-实现四则运算-可带括号

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

/*1、只能进行整数运算,2、可以有括号的嵌套,如(a+b*(c+d)-e)-f,但对(a+((b+c)+d*e)-f)形如的算式无法计算*/#includeiostream#includecstdlib#includecstring#includecstdiousingnamespacestd;structElement{doubledata;charsign;charbracket_left;charbracket_right;Element*next;};intcheck(char*s){intcount_bracket=0;inti=0;while(istrlen(s)){if(s[i]=='(')count_bracket++;elseif(s[i]==')')count_bracket--;if(count_bracket0)return0;if(!(s[i]='0'&&s[i]='9'||s[i]==')'||s[i]=='('||s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/'))return0;i++;}if(count_bracket)return0;return1;}voidcreate_expression(char*s,Element*head){Element*p=head;p-next=(Element*)malloc(sizeof(Element));p=p-next,p-next=NULL;p-bracket_left=p-bracket_right='\0';p-sign='\0',p-data=0;intx=0;while(xstrlen(s)){if(s[x]='0'&&s[x]='9'){while(s[x]='0'&&s[x]='9'&&s[x]!='\0')p-data*=10,p-data+=s[x++]-'0';x--;}elseif(s[x]=='+'||s[x]=='-'||s[x]=='*'||s[x]=='/'){p-next=(Element*)malloc(sizeof(Element));if(!p-next){coutERRORendl;exit(0);}p=p-next,p-next=NULL;p-bracket_left=p-bracket_right='\0';p-sign=s[x],p-data=0;}elseif(s[x]==')'){p-bracket_right=')';p-bracket_left='\0';}elseif(s[x]=='('){p-bracket_left='(';p-bracket_right='\0';}x++;}p=head-next;while(p-next)p=p-next;p-next=(Element*)malloc(sizeof(Element));p=p-next;p-data=0,p-sign='+',p-next=NULL;p-bracket_left=p-bracket_right='\0';}/*doublechang_int(char*s){doublesum=0;intx=0,mark=0;while(xstrlen(s)){if(s[x]=='.'){mark=1;break;}sum*=10,sum+=s[x]-'0';x++;}if(mark){doublem=0;for(inti=strlen(s)-1;ix;i--)m+=s[i]-'0',m/=10;sum+=m;}returnsum;}*/doublecalculate(Element*start,Element*end){Element*p=start;while(p!=end){if(p-next-sign=='*'){p-data*=p-next-data;p-next=p-next-next;p=start;continue;}elseif(p-next-sign=='/'){p-data/=p-next-data*1.0;p-next=p-next-next;p=start;continue;}p=p-next;}doublesum=start-data;start=start-next;while(start!=end){if(start-sign=='+')sum+=start-data;elsesum-=start-data;start=start-next;}returnsum;}voidcancel_bracket(Element*head){Element*start=head-next;Element*p=head;while(start){intflag=0;if(start-bracket_left){Element*end=start;intcount_bracket=1;while(end){if(end-bracket_right){count_bracket--;if(!count_bracket){start-data=calculate(start,end-next);start-bracket_left=start-bracket_right='\0';start-next=end-next;}}elseif(end-bracket_left){flag=1;count_bracket=1;start=end;}end=end-next;}}if(flag)start=p;start=start-next;}}intmain(){chars[100];while(1){do{system(cls);puts(\n请输入正确的表达式);cin.getline(s,100);}while(!check(s));Element*head=(Element*)malloc(sizeof(Element));head-next=NULL;create_expression(s,head);Element*p=head-next;cancel_bracket(head);while(p-next){if(p-sign)coutp-sign;if(p-bracket_left)coutp-bracket_left;coutp-data;if(p-bracket_right)coutp-bracket_right;p=p-next;}cout'='calculate(head-next,p)endl;puts(\n按任意键继续计算\n);system(pause);}}/*1+2-(8+9)+(3+4)-6*//*1+2-(8+9)+(3+(3+3)+4)-6=-7*//*1+2-(8*9)+(3*4)-6*//*1-(3-(3+3)+4)-6=-6*/

1 / 5
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功