Luo Gu P1022 Calculator improvement

--- --- restore content begins

 

Luo Gu P1022 Calculator improvement

Topic background

NCL is a specialized calculator improvement and upgrading of the laboratory, the lab recently received a task entrusted to a company: the need to add solving a linear equation functions on a particular model of the company's calculator. This laboratory will be the task to a novice just entering the Mr. ZL.

Title Description

In order to accomplish this task well, ZL the Z- Mr L first studied a number of instances of one dollar equation:

4 + 3x = 8

6a-5 + 1 = 2-2a

-5+12y=0

ZL of the Z Mr. L is competent Advertisement, typing on a one dollar calculator equation containing only integers, and lower case letters +, -, = three mathematical symbols (of course, the symbol "-" can be minus , but also for the minus sign). Equation and no parentheses, no other numbers, letters unknowns in the equation.

You can assume the correctness of the judgment of the type of equation is done by another programmer, or that can be considered a linear equation typing are legitimate, and there is the only real solution.

Input Format

A one dollar equation.

Output Format

Results (After accurate to three decimal places) solution of the equation.

Sample input and output

Input # 1
6a-5 + 1 = 2-2a
Output # 1
a=0.750

#include<bits/stdc++.h>
using namespace std;
string sentence;
int dl1,dl2,ta;//系数&常数项 
int xs(int n,int t)
{
    if(sentence[n]>='0'&&sentence[n]<='9')
        return xs(n+1,t*10+sentence[n]-'0');
    ta=n;
    return t;
}
int main()
{

    getline(cin,sentence);
    char x;
    int zf=1,w=0,t=-1,fh;
    while(sentence[++t]!='\0')
    {
        fh=1;
        if(sentence[t]=='=') 
        {
            zf=-1;
            continue;
        }
        if(sentence[t]=='-')
        {
            fh=-1;
            t++;
        }
        if(sentence[t]>='a'&&sentence[t]<='z')
        {
            x=sentence[t];
            dl1+=zf*fh;
            continue;
        }
        if(sentence[t]<='9'&&sentence[t]>='0')
        {
            int n=xs(t,0);
            t=ta;
            if(sentence[t]>=' A ' && sentence [T] <= ' Z ' ) 
            { 
                DL1 + = n-* ZF * FH; 
                X = sentence [T]; 
            }             
            the else 
            { 
                DL2 + = n-* ZF * FH; 
                T - ; 
            } 
        } 
        
    } 
    COUT the X-<< << " = " ;
     // Double-0 will be the case! 
    Double ANS = -dl2 / (DL1 * 1.0 );
     IF (ANS == - 0.000) ans=0.000;
    printf("%.3lf",ans); 
    return 0;
}

solution

First-time into the equation

Then one by one judge

Encounter equal sign put zf (positive and negative) is set to -1

(Zf before the cycle is set to 1)

Encountered minus / minus sign is put FH (symbols) set to -1

(Fh at the beginning of each cycle to be initialized to 1)

Digital encountered on entering the function that returns real value of this string of digits

At the same time look at a letter is not, then it is put dl1 (coefficient), otherwise put dl2 (constant terms) in

Finally transposition (* -1)

Coefficients into a

There is a double-0! ! !

This is why there is always a point of trouble, the answer is 0.000, the general procedure is output -0.000

So add a special sentence

If the ans is -0.000, ans = 0.000

The End

Very important! ! !

 

Guess you like

Origin www.cnblogs.com/send-off-a-friend/p/11260084.html