Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

c - Garbage values are getting assigned

This basically takes an expression as input and evaluates it from left to right. No precedence. I broke something when I edited the names of my variables. It does not een work for the simple expression x. Example input: 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] = '';
            pos++;
            check = 1;
        }
        //store second number 
        else if ((check==1)&&(expression[i]>='0')&&(expression[i]<='9'))
        {
            temp[pos]=expression[i];
            temp[pos+1] = '';
            pos++;

        }
        //check character
        else
        {
            //if exists, store char temp[] as int op            
            if(pos>0) 
            {
                //if it is first number
                if (op=='') 
                    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=='') 
                      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("%[^
]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("
 result %d
", result);       
        //Next Value        
        vali++; 
        result=0;

    }   
    printf("
");
}   
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Both global variables op and result need to be reset for each iteration of while loop.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...