用简单的C语言实现带括号的四则运算?
答案:2 悬赏:0 手机版
解决时间 2021-12-04 00:10
- 提问者网友:聂風
- 2021-12-03 17:47
用简单的C语言实现带括号的四则运算?
最佳答案
- 五星知识达人网友:三千妖杀
- 2021-12-03 19:08
#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
{
}
if(*c=='(')
k++;
else if(*c==')')
k--;
c++;
}
if(k!=0)
{
}
return 1;
}
#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!
");
}
if(*c=='(')
k++;
else if(*c==')')
k--;
c++;
}
if(k!=0)
{
printf("input error, there is not have correct bracket '()'!
");
}
return 1;
}
全部回答
- 1楼网友:纵马山川剑自提
- 2021-12-03 19:32
#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");
}
#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");
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯