c - 问题::C 中字符串运算的算术表达式

标签 c math expression

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/

相关文章:

math - 对称级别索引算法(替代 float )的好处是什么?

Javascript 表达式 : object array;

C - 使用 htonl 和 ntohl 编码/取消编码结构

C 并发进程和静态变量

c - 使用isupper函数

PHP 数学 : Why is mod (%) not working with fac(13)?

c++ - 如何将 OpenCV 的 Mat 结构的部分图像矩阵加载到我自己的 3x3 数组中?

python - 使用来自 FLD 的最高特征值为 2D 数据投影多个集群

r - 如何用变量调用 lm?

sql - 字符串是否可以由一组字母组成的正则表达式