c語言作業,,輸入表示式並計算表示式的值
阿新 • • 發佈:2019-02-01
題目概述:表示式只含+,-運算子,運算元為整數,以封號結尾,例:233-67+89;
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <ctype.h> #define maxn 10000 int main() { char expn[maxn]; scanf("%s",expn); double tmp1=0,tmp2=0; char ch1[10],ch2[10],op; int i=0,j=0; while(expn[i]!='+'&&expn[i]!='-'){ ch1[j++]=expn[i++]; } tmp1=atof(ch1);//得到表示式首個運算元 while(1){ j=0; op=expn[i++]; while(expn[i]!='+'&&expn[i]!='-'&&expn[i]!=';'){ ch2[j++]=expn[i++]; } tmp2=atof(ch2);//得到第二個運算元 //根據操作符更新第一個運算元 if(op=='+')tmp1+=tmp2; else if(op=='-')tmp1-=tmp2; if(expn[i]==';')break; } printf("=%.2f\n",tmp1); //system("pause"); return 0; }