永发信息网

用简单的C语言实现带括号的四则运算?

答案:2  悬赏:0  手机版
解决时间 2021-12-04 00:10
用简单的C语言实现带括号的四则运算?
最佳答案
#include
#include
#include

int check(char *c)
{
int k=0;
while(*c!='')
{
if((*c>='0' && *c<='9') || *c=='+' || 
*c=='-' || *c=='*' || *c=='/' || 
*c=='.' || *c=='(' ||  *c==')' )
{
}
else
{

printf("input error, there have the char not the math expression char!
");

return 0;
}
if(*c=='(')
k++;
else if(*c==')')
k--;
c++;
}
if(k!=0)
{

printf("input error, there is not have correct bracket '()'!
");

return 0;
}
return 1;
}
全部回答
#include
#include
#include


int check(char *c)
{
int k=0;
while(*c!='\0')
{
if((*c>='0' && *c<='9') || *c=='+' ||
*c=='-' || *c=='*' || *c=='/' ||
*c=='.' || *c=='(' || *c==')' )
{

}
else
{
printf("input error, there have the char not the math expression char!\n");
return 0;
}

if(*c=='(')
k++;
else if(*c==')')
k--;

c++;
}
if(k!=0)
{
printf("input error, there is not have correct bracket '()'!\n");
return 0;
}
return 1;
}



void move(char *f, double *s,int p)
{
int i=0,len=strlen(f);
for(i=p; i {
f[i]=f[i+1];
s[i]=s[i+1];
}
s[i]=s[i+1];
f[len-1]='\0';
}

double convnum(char *c)
{
double num=0.0;
double a=1.0;
int i=0,p=0,len=0;
char temp[100];
int tempi=0;
int start=0;
int f=1;

len=strlen??;

if(c[0]=='-')
{
start=1;
f=-1;
}
for(i=start; i {
if(c[i]=='.')
{
p=i;
break;
}
temp[tempi++]=c[i];
}
temp[tempi]='\0';

if(p!=0)
{
for(i=p+1;i {
if(c[i]=='.')
{
printf("there is more that one dot '.' in number!error!\n");
exit(0);
}
a=a*0.1;
num+=(a*(c[i]-48));
}
}

a=1.0;

len=strlen(temp);
for(i=len-1;i>=0; i--)
{
num=num+(a*(temp[i]-48));
a*=10;
}

num=num*f;
return num;
}


double good(char *c)
{
char g[100],number[30];
char f[80];
int fi=0;
double s[80];
int si=0;
int k=0;
int num=0,i=0;
int cc=0;
int jj=0;

while(*c!='\0')
{
k=0;
num=0;

switch(*c)
{
case '+':
case '-':
case '*':
case'/':
f[fi++]=*c;
if(*c=='*' || *c=='/')
cc++;
else
jj++;
if(*(c-1)!=')')
{
number[i]='\0';
i=0;

s[si++]=convnum(number);
}
break;
case'(':
k++;
while(k>0)
{
c++;
g[num]=*c;
num++;
if(*c==')')
{
k--;
}
else if(*c=='(')
{
k++;
}
}
g[num-1]='\0';
num=0;
s[si++]=good(g);
break;
default:
number[i++]=*c;

if(*(c+1)=='\0')
{ number[i]='\0';
s[si++]=convnum(number);
}
break;
}

c++;
}

f[fi]='\0';

i=0;
while(cc>0)
{
switch(f[i])
{
case '*': cc--;
s[i+1]=s[i]*s[i+1];
move(f,s,i);
break;
case '/': cc--;
s[i+1]=s[i]/(float)s[i+1];
move(f,s,i);
break;
default:
i++;
break;
}
}

i=0;
while(jj>0)
{
switch(f[i])
{
case '+': s[i+1]=s[i]+s[i+1];
jj--;
move(f,s,i);
break;
case '-': s[i+1]=s[i]-s[i+1];
jj--;
move(f,s,i);
break;
default:
printf("operator error!");
break;
}
}

return s[0];
}

void main()
{
char str[100];
double sum=0;
int p=1;

while(1)
{
printf("enter expression: enter 'exit' end of program\n");
scanf("%s",str);
p=strcmp(str,"exit");
if(p==0)
break;
p=check(str);

if(p==0)
continue;
sum=good(str);
printf("%s=%f",str,sum);
printf("\n");
}
printf("good bye!\n");
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
众绅士们都去哪找色气满满的图?
我想梦游,怎么样才能梦游?
福特10万左右车
冷弯内卷边槽钢是C型钢么
2017武汉落户人口多少?
厚6毫米89无缝钢管一米多重
我想问下五零四零三九一五七三二零四的快递走
洛阳精怀川能搁不住巩县一咕哝。这个顺口溜是
全新拆机耳机是什么意思?
作者是男人还是女人
中国古代吃肉的马叫什么
广元高铁车站设在哪里
求个IPAD的网络电话!好用的
秀湖香颂湾怎么样?好不好?值不值得买?
质量大师帮忙分析一下什么情况会出现CPK为0.7
推荐资讯
箱梁顶面预留方孔叫什么
弹跳力跟脚踝粗细有关吗
长31厘米,宽16厘米,高31厘米,这个包大吗
将十进制IP地址217.51.14.24转换成二进制形式
向日葵晚上头朝哪个方向?
神仙道真天地无用几级可列
以“当危险来临时”为题,写一篇看电影或电视
海关协管办公地址在什么地方?我要处理点事
请问从【成都】如何去【罗马尼亚雅西】?
雅马哈电钢P70和P85哪个性价比更高,到底有什
电陶炉升温到300度,对应功率是多少
屋面光伏发电屋面租金是多少
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?