1018: improvement of calculator (simple simulation)

Keywords: PHP supervisor calculator

Title Source:

http://129.211.20.246/problem.php?id=1018

Title Description

NCL is a laboratory specializing in the improvement and upgrading of calculators. Recently, the laboratory received a task entrusted by a company: it needs to add the function of solving one yuan one order equation to a certain type of calculators of the company. The lab entrusted the task to a new novice Mr. ZL. In order to complete this task well, Mr. ZL first studied some examples of univariate equation of the first order:

   4+3x=8
   6a-5+1=2-2a
   -5+12y=0

Mr. ZL was told by the supervisor that only integers, lowercase letters and three mathematical symbols +, -, = are included in a one yuan one order equation typed in the calculator (of course, the symbol "-" can be used as either a minus sign or a minus sign). There are no parentheses or dividers in the equation, and the letters in the equation represent unknowns.  
Write a program to solve the input one-dimensional equation, and output the result (accurate to three decimal places) to the screen.
You can assume that the judgment on the correctness of the typed equation is made by another programmer, or you can think that the typed equation of one degree is legal and has a unique real solution.  

input

Each test file contains only a set of test data, each group of input a string, representing a one variable one order equation.  

output

For each group of input data, the result of solving the equation (accurate to three decimal places) is output to the screen.  

sample input

6a-5+1=2-2a

Sample output

a=0.750

Source / classification

2000 NOIP National League popularization group  

Reference code:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<iostream>
#define N 200005
#define mod 1000007
using namespace std;
char s[N];
int main()
{
    while(~scanf("%s",s))
    {
        double k1=0,b1=0,k2=0,b2=0,a=0;
        int flag=0,p=1;
        char x;
        for(int i=0; i<strlen(s); i++)
        {
            if(s[i]=='=')
            {
                p=1;
                b1+=a;
                a=0;
                for(int j=i+1; j<strlen(s); j++)
                {
                    if(s[j]=='+')
                    {
                        p=1;
                        b2+=a;
                        a=0;
                        continue;
                    }
                    else if(s[j]=='-')
                    {
                        p=-1;
                        b2+=a;
                        a=0;
                        continue;
                    }
                    if(s[j]>='0'&&s[j]<='9')
                    {
                        a*=10;
                        a+=(s[j]-'0')*p;
                    }
                    else
                    {
                        x=s[j];
                        if(a==0) a=1*p;
                        k2+=a;
                        a=0;
                    }
                    if(j==strlen(s)-1)
                        b2+=a;
                }
                break;
            }
            if(s[i]=='+')
            {
                p=1;
                b1+=a;
                a=0;
                continue;
            }
            else if(s[i]=='-')
            {
                p=-1;
                b1+=a;
                a=0;
                continue;
            }

            if(s[i]>='0'&&s[i]<='9')
            {
                a*=10;
                a+=(s[i]-'0')*p;
            }
            else
            {
                x=s[i];
                if(a==0) a=1*p;
                k1+=a;
                a=0;
            }
        }
        if((b2-b1)/(k1-k2)==0)
            printf("%c=%.3lf\n",x,fabs((b2-b1)/(k1-k2)));
        else
            printf("%c=%.3lf\n",x,(b2-b1)/(k1-k2));
    }
    return 0;
}

 

Posted by kevinbarker on Tue, 19 Nov 2019 09:23:11 -0800