垃圾值正在分配

时间:2014-07-19 09:43:49

标签: c

这基本上将表达式作为输入并从左到右进行评估。没有优先权。当我编辑变量的名字时,我弄坏了什么。它不适用于简单表达式x。 输入示例: X + 3-4 * X 1 63 3 33 1000

#include<stdio.h>
#include<math.h>

int result=0;
char op;



//This function computes the operations and returns an integer
int compute(int r, int x, char op)
{
    switch (op)
    {   
        case '+':return r+x;
            break;  
        case '-':return r-x;
            break;
        case '/':return r/x;
            break;
        case '*':return r*x;
            break;
        case '^':return (int) pow((double) r,x);
            break;

    }
}

//This function breaks the expression into tokens and computes result in result variable
void solveExpression(char expression[],int n, int x)
{
    int i;
    char temp[10];
    int pos=0;
    int check = 0;
    //int result=0;
    for(i=0;i<=n;i++)
    {
        //store first number
        if((check==0)&&(expression[i]>='0')&&(expression[i]<='9'))
        {
            pos=0;
            temp[pos]=expression[i];
            temp[pos+1] = '\0';
            pos++;
            check = 1;
        }
        //store second number 
        else if ((check==1)&&(expression[i]>='0')&&(expression[i]<='9'))
        {
            temp[pos]=expression[i];
            temp[pos+1] = '\0';
            pos++;

        }
        //check character
        else
        {
            //if exists, store char temp[] as int op            
            if(pos>0) 
            {
                //if it is first number
                if (op=='\0') 
                    result =atoi(temp);
                //if it is number in the centre
                else result=compute(result, atoi(temp), op);
            }

            //reset values
            pos=0; 
            check = 0;

            //Check for letter
            if (expression[i]=='x')
            {   
                if (op=='\0') 
                      result=x;
                else 
                result = compute(result,x,op);
            }

            //Check for operator            
            else if ((expression[i]=='+')
                    ||(expression[i]=='-')
                    ||(expression[i]=='*')
                    ||(expression[i]=='/')
                    ||(expression[i]=='^'))         

            {
                op=expression[i];
            }
        }
    }

}


int main(void)
{
    int vali=0;
    char expression[80] ;
    int arr_value[100]; 
    int x;

    //Scan Expression
    scanf("%[^\n]s", expression);

    //Fill array of values until input value=1000
    while(1)
    {
    scanf("%d", &arr_value[vali]);
    if(arr_value[vali]==1000)
        break;  
    else    
        vali++;   
    }


    vali=0;
    //Compute for each value
    while(arr_value[vali]!=1000)
    {   
        //Set global x value
        x=arr_value[vali];
        //Solve expression 
        //Print result
        solveExpression(expression,80,x) ;  
        printf("\n result %d\n", result);       
        //Next Value        
        vali++; 
        result=0;

    }   
    printf("\n");
}   

2 个答案:

答案 0 :(得分:0)

对于while循环的每次迭代,都需要重置全局变量op和result。

while(arr_value[vali]!=1000)
{   
    //Set global x value
    x=arr_value[vali];
    //Solve expression 
    //Print result
    solveExpression(expression,80,x) ;  
    printf("\n result %d\n", result);       
    //Next Value        
    vali++; 
    result=0;
    op='\0'; // add this line to reset global variable
}   

答案 1 :(得分:0)

void solveExpression(char expression[], int exp_len, int x){
    static const char *ops = "+-/*^";
    char op, num[16];
    size_t  pos = 0;
    int acc = 0, n;

    pos = strcspn(expression, ops);//strcspn in <string.h>
    memcpy(num, expression, pos);
    num[pos] = 0;
    acc = strcmp(num, "x")==0 ? x : atoi(num);

    while(1){
        if('\0'== (op = expression[pos]))
            break;
        expression += pos+1;
        pos = strcspn(expression, ops);
        memcpy(num, expression, pos);
        num[pos] = 0;
        n = strcmp(num, "x")==0 ? x : atoi(num);
        acc = compute(acc, n, op);
    }
    result = acc;
}