Analysis of the improved problem of Luogu p1022 calculator

Topic link:

https://www.luogu.org/problem/show?pid=P1022

Basic idea: The main difficulty of this problem lies in the processing of characters. My initial idea was to calculate after all the input is completed, but I found that it is not right, because the program I wrote is written according to the sample, but the sample ( 6a-5+1=2a) The given numbers are all one character, so there is no need to accumulate the numbers, but when I found that if the sample is (67a-5+1=24a), my method is wrong . Resolutely gave up my idea, and then started to think about the processing of characters one by one. First define two variables a1, a2 used to store the sum of the constant term and the sum of the coefficients of the variables, the final result is -b1/a1;

code show as below:

#include<bits/stdc++.h>
using namespace std;
char op;
char op1;
int opp=1; //Indicates the left or right side of the equal sign. Left: 1, Right: -1.
int a1=0; //store the coefficient sum
int b1=0; //store the sum of the constant term
int cur=0; //accumulate each coefficient or each constant term
double jieguo; //result
int sign=1; //represents the sign
int main()
{
    op=getchar();
    int c=1;
    while (1)
    {
        if(c==1&&op>='a'&&op<='z') //must be added This is a judgment, because when the first character is a variable, the coefficient in front of the variable is one,
        {
            a1=1; //At this time, a1 must be increased by 1 to save the coefficient
            op=getchar(); //If not The above judgment goes directly to the third judgment after the loop comes in. At this time, cur is equal to 0, a1 is equal to 0, and the 1 in front of a is not added, which is obviously not correct for
            c++;
            continue;
        }
        c++;
        if(op>='0'&&op<='9') //This judgment is used to calculate the constant term or coefficient
        {
             cur=cur*10+sign*(op-'0')* opp; //When two digital characters are connected together, this calculation is correct, sign represents the positive and negative sign, when the digital character is in front of
        } //When it is a negative sign, the sign will become -1, the following code has , the calculated coefficient or constant term is negative and correct.
        else if(op>='a'&&op<='z')
        {
            op1=op; //record the variable, use
            a1+=cur in the final output; //accumulate the coefficient in front of the variable
            cur=0; //if encountered A variable, indicating that the coefficient in front of the variable has been calculated. At this time, set cur to 0 to calculate the coefficient in front of the following constant item or variable
        } //The coefficient in front of this variable has nothing to do with any number behind it
        else if(op= ='-')
        {
            b1+=cur;//If a minus sign is encountered, it means that there may be a constant item in front of it, and the calculation has been completed. At this time, it needs to be accumulated into b1. If there is a variable in front of the minus sign, don't care, because the above judgment has already put the variable in front of this variable. The coefficients are accumulated into a1 to
            sign=-1; //make the sign negative, because if there is a constant term after the negative sign, then the constant term must be negative
            cur=0; //and the above cur=0 The explanation of the operation is the same.
        }
        else if(op=='+')
        {
            b1+=cur; // Exactly the same as the interpretation of the minus sign.
            sign=1;
            cur=0;
        }
        else if(op=='=')
        {
            b1+=cur; //If the equal sign is encountered, then the constant term is still accumulated into b1, and the explanation of the positive and negative signs The same
            opp=-1; //At this time, opp must be set to negative, because when we calculate the equation, the negative sign changes after shifting the item, which is equivalent to the shifting operation, changing the number that was originally a positive number on the left side of the equal sign. A negative number, a number that was originally a negative number becomes a positive number
            cur=0;
            sign=1;//This operation must have, because if the equal sign is preceded by a negative constant term and the equal sign is followed by a positive constant term, the sign must be set to positive in order to calculate the coefficient of the following positive constant term, if not This operation, then the negative sign of the coefficient of the constant term obtained below is reversed, because sign is negative, opp is negative, and negative and negative are positive (this is on the right side of the equal sign, the coefficient displayed by the expression is positive, then the calculated result must be is negative, the coefficient displayed by the expression is negative, the result of the calculation must be positive), the result is wrong
        }           //If you still don't understand, use the example of (6a+5-1=45+6a) to try
        else
        {
            b1+ =cur; //If you reach here, it means that the end is reached. If the last character is a letter, then the above judgment condition will be entered, and this will not be entered. If the last character is not a letter, then this judgment will be entered. Accumulate the constant term into b1
            break ;
        }
        op=getchar();
    }
    //cout<<a1<<" "<<b1<<endl;
    if(a1==0||b1==0)
        cout<< op1<<'='<<"0.000";
    else
    {
        jieguo=-(1.0*b1)/a1; //A negative sign must be added here, or the problem of shifting items
        cout<<op1<<'=';
        printf("%.3f",jieguo);
    }

}


There are a lot of pitfalls in this question, you need to pay attention

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325767931&siteId=291194637