垃圾值越来越分配

垃圾值越来越分配

本文介绍了垃圾值越来越分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这基本上需要一个前pression作为输入,并从左至右计算它。没有precedence。我打破了东西时,我编辑我的变量的名称。它不为简单的前pression点¯xEEN工作。
例如输入:
X + 3-4 * X
1
63
3
33
1000

 #包括LT&;&stdio.h中GT;
#包括LT&;&MATH.H GT;INT结果为0;
字符运算;//这个函数计算的操作,并返回一个整数
INT计算(INT R,诠释的x,焦运)
{
    开关(OP)
    {
        案例'+':回归R + X;
            打破;
        案例' - ':回归R-X;
            打破;
        案例'/':回归R / X;
            打破;
        案例'*':回归R * X;
            打破;
        案例'^':回报(INT)POW((双)R,X);
            打破;    }
}//这个函数打破了前pression为标记,并在结果变量计算结果
虚空solveEx pression(字符前pression [],诠释N,INT X)
{
    INT I;
    炭温度[10];
    INT POS = 0;
    INT检查= 0;
    // INT结果为0;
    对于(i = 0; I< = N;我++)
    {
        //存储第一个数字
        如果((检查== 0)及及(前pression [1] - GT ='0')及及(前pression [1] - ='9'))
        {
            POS = 0;
            临时[POS] =前pression [I]
            温度[POS + 1] ='\\ 0';
            POS ++;
            检查= 1;
        }
        //商店第二个数字
        否则如果((检查== 1)及及(前pression [1] - GT ='0')及及(前pression [1] - ='9'))
        {
            临时[POS] =前pression [I]
            温度[POS + 1] ='\\ 0';
            POS ++;        }
        //检查字符
        其他
        {
            //如果存在,店内温度炭[]为int运
            如果(POS大于0)
            {
                //如果它是第一个数字
                如果(OP =='\\ 0')
                    结果=与atoi(临时);
                //如果它是在中心数
                否则结果=计算(结果,与atoi(TEMP),OP);
            }            //复位值
            POS = 0;
            检查= 0;            //检查信
            如果(如pression [I] =='X')
            {
                如果(OP =='\\ 0')
                      结果= X;
                其他
                结果=计算(结果,X,OP);
            }            //检查操作员
            否则,如果((如pression [I] =='+')
                    ||(如pression [我] ==' - ')
                    ||(例如pression由[i] =='*')
                    ||(如pression [I] =='/')
                    ||(如pression [I] =='^'))            {
                OP =前pression [I]
            }
        }
    }}
INT主要(无效)
{
    INT瓦利= 0;
    焦恩pression [80];
    INT arr_value [100];
    INT X;    //扫描前pression
    scanf函数(%[^ \\ n]的,前pression);    //填充值数组,直到输入值= 1000
    而(1)
    {
    scanf函数(%d个,&安培; arr_value [瓦利]);
    如果(arr_value [瓦利] == 1000)
        打破;
    其他
        瓦利++;
    }
    瓦利= 0;
    //计算每个值
    而(arr_value [瓦利]!= 1000)
    {
        //设置全局的x值
        X = arr_value [瓦利]
        //解决前pression
        //打印结果
        solveEx pression(如pression,80,X);
        的printf(\\ n结果%d个\\ N,结果);
        // NEXT值
        瓦利++;
        结果= 0;    }
    的printf(\\ n);
}


解决方案

这两个全局变量运算和结果需要进行重置,而每次循环。

 而(arr_value [瓦利]!= 1000)
{
    //设置全局的x值
    X = arr_value [瓦利]
    //解决前pression
    //打印结果
    solveEx pression(如pression,80,X);
    的printf(\\ n结果%d个\\ N,结果);
    // NEXT值
    瓦利++;
    结果= 0;
    OP ='\\ 0'; //添加此行重置全局变量
}

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*x1633331000

#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");
}   
解决方案

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("\n result %d\n", result);       
    //Next Value        
    vali++; 
    result=0;
    op='\0'; // add this line to reset global variable
}   

这篇关于垃圾值越来越分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 02:29