写好了
不能算小数,根据你的题意来的,如果输入小数,则只取整数部分运算,结果不能为负数
#include
#include
#include
intdeal(intop1,intop2,charop)//运算
{
switch(op)
{
case'+':op1+=op2;break;
case'-':op1-=op2;break;
case'*':op1*=op2;break;
case'/':op1/=op2;break;
case'^':
{
inttemp=op2;
for(inti=1;i
break;
}
}
returnop1;
}
char*replace(char*source,char*sub,char*rep)//字符串替换
{
char*result;
char*pc1,*pc2,*pc3;
intisource,isub,irep;
isub=strlen(sub);
irep=strlen(rep);
isource=strlen(source);
if(NULL==*sub)
returnstrdup(source);
result=(char*)malloc(((irep>isub)?(float)strlen(source)/isub*irep+1:isource)*sizeof(char));
pc1=result;
while(*source!=NULL)
{
pc2=source;
pc3=sub;
while(*pc2==*pc3&&*pc3!=NULL&&*pc2!=NULL)
pc2++,pc3++;
if(NULL==*pc3)
{
pc3=rep;
while(*pc3!=NULL)
*pc1++=*pc3++;
pc2--;
source=pc2;
}
else
*pc1++=*source;
source++;
}
*pc1=NULL;
returnresult;
}
intmain()
{
chars[1000],op1[100],op2[100],*p,*temp,*back;
scanf("%s",&s);
temp=s;
back=s;
intpos=-1;
intfin,oper1,oper2,tmp,size;
charop;
while(1)
{
size=(int)strlen(temp);
for(inti=0;i
if(*temp=='(')
{
p=(temp+1);
pos=(i+1);//找出最后一个(的位置
}
}
intk=0;
if(pos!=-1)
{
temp=back;
tmp=pos;
while(*p!=')')
{
p++;
tmp++;
if(*p=='*'||*p=='/')
{
tmp--;
for(;(temp[tmp]>='0'&&temp[tmp]<='9')||temp[tmp]=='.';)
{
tmp--;
break;
}
}
else
continue;
}
if(tmp!=pos&&*p!=')')
{
pos=++tmp;
}
intstart=pos;
while(temp[pos]>='0'&&temp[pos]<='9')
{
op1[k]=temp[pos];
pos++;
k++;
}
op1[k]='';
while((temp[pos]>='0'&&temp[pos]<='9')||temp[pos]=='.')
pos++;
op=temp[pos];
oper1=atoi(op1);
pos++;
k=0;
while(temp[pos]>='0'&&temp[pos]<='9')
{
op2[k]=temp[pos];
pos++;
k++;
}
op2[k]='';
while((temp[pos]>='0'&&temp[pos]<='9')||temp[pos]=='.')
pos++;
oper2=atoi(op2);
intend;
if(temp[pos]!=')')
end=--pos;
else
{
if(temp[start-1]=='(')
{
end=pos;
start--;
}
}
charrep[100];
k=0;
for(;start<=end;start++,k++)
{
rep[k]=temp[start];
}
rep[k]='';
fin=deal(oper1,oper2,op);
charfinal[100];
itoa(fin,final,10);
temp=replace(temp,rep,final);
back=temp;
pos=-1;
p=temp;
k=0;
*rep=NULL;
*final=NULL;
printf("%s
",replace(temp,rep,final));
}
else
{
temp=back;
p=temp;
tmp=pos;
while(*p!='')
{
p++;
tmp++;
if(*p=='*'||*p=='/')
{
tmp--;
for(;(temp[tmp]>='0'&&temp[tmp]<='9')||temp[tmp]=='.';)
{
tmp--;
break;
}
}
else
continue;
break;
}
if(tmp!=pos&&tmp!=(int)strlen(temp)-1)
{
pos=++tmp;
}
else
pos=0;
intstart=pos;
while(temp[pos]>='0'&&temp[pos]<='9')
{
op1[k]=temp[pos];
pos++;
k++;
}
while((temp[pos]>='0'&&temp[pos]<='9')||temp[pos]=='.')
pos++;
op1[k]='';
op=temp[pos];
oper1=atoi(op1);
pos++;
k=0;
while(temp[pos]>='0'&&temp[pos]<='9')
{
op2[k]=temp[pos];
pos++;
k++;
}
op2[k]='';
while((temp[pos]>='0'&&temp[pos]<='9')||temp[pos]=='.')
{
pos++;
if(pos>=(int)strlen(temp))
break;
}
oper2=atoi(op2);
intend=--pos;
charrep[100]={''};
k=0;
for(;start<=end;start++)
{
rep[k]=temp[start];
k++;
}
fin=deal(oper1,oper2,op);
charfinal[100];
itoa(fin,final,10);
temp=replace(temp,rep,final);
back=temp;
for(k=0;k<(int)strlen(temp);k++)
{
if(temp[k]<'0'||temp[k]>'9'&&temp[k]!='.')
break;
}
if(k==(int)strlen(temp))
break;
pos=-1;
p=temp;
k=0;
printf("%s
",temp);
}
}
printf("%d
",fin);
return0;
}