Objective: Here the codes should read any arbitrary long arithmetic expression like, 233+200. Then it should print in the postfix and prefix formats and calculate it i.e 433.
Problems: output shows anomalous data like 98, 101 etc. I think there might be any mistake in the code. please have a look and let me know it.
#include<stdio.h>
void copy(char* t, char* s, int n)
{
int i;
for(i=0;i<n;i++)
t[i]=s[i];
t[n]='\n';
}
int fix(char* s, int length, int task)
{
int i;
for( i=length-1;i>=0;i--)
{
if((s[i]=='+')||(s[i]=='-'))
{
char s1[i+1];
char s2[length-i];
copy(s1,s,i);
copy(s2,s+i+1,length-i-1);
if(task==2)
{
return fix(s1,i,task)+fix(s2,i,task);
}
else
{
return fix(s1,i,task)-fix(s2,length-1,task);
}
if(task==0)printf("%c",s[i]);;
fix(s1,i,task);
fix(s2,length-i-1,task);
if(task==1)printf("%c",s[i]);
return 0;
}
}
}
int main(){
char s[80];
int i;
for(i=0;(s[i]=getchar())!='\n';i++);
fix(s,i,0);
printf("\n");
fix(s,i,1);
printf("\n");
printf("%d\n",fix(s,i,2));
return 0;
}
最佳答案
嗯,是的,代码中存在一些问题。我们通常不会在这里运行免费的调试服务,但对于初学者来说,什么是:
(s,i,0);
应该做什么?
关于c - 问题::C 中字符串运算的算术表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6205615/