# Improvement of P1022 Calculator (Simulation)

Posted by kusarigama on Sat, 22 Feb 2020 17:25:48 +0100

It's simulation, plus the elementary school solution of the univariate linear equation

Actually, many of the duplicates can be used with functions, but I always like to copy and paste!(x)

Improvement of P1022 Calculator

## Topic Background

NCLNCL is a lab specializing in the improvement and upgrade of calculators. Recently, the lab received a task entrusted by a company: the ability to solve one-variable first-order equations needs to be added to the calculator of a certain model of the company.The lab delegated the task to a new beginner, Mr. ZL.

## Title Description

In order to accomplish this task well, Mr. ZLZL first studied some examples of univariate linear equations:

4+3x=84+3x=8

6a-5+1=2-2a6a−5+1=2−2a

-5+12y=0−5+12y=0

Mr. ZLZL was informed by his supervisor that only three mathematical symbols, integer, lowercase letters and +, -, =, were included in a unary one-time equation typed on the computer (of course, the symbol'-'can be used as either a minus sign or a minus sign).There are no parentheses or division signs in the equation, and the letters in the equation represent unknown numbers.

You can assume that another programmer is making a judgement of the correctness of the equations you type, or that the one-time equations you type are valid and have a unique real solution.

## Input and Output Formats

Input format:

An equation of one degree with one variable.

Output format:

The result of solving the equation (exactly three decimal places).

## Input and Output Samples

Input Sample #1:copy
```6a-5+1=2-2a
```
Output Sample #1:copy
`a=0.750`

```#include <iostream>
#include <cmath>
#include <stdio.h>
#include <cstring>
using namespace std;
int main()
{
char a={0};

cin>>a;

double s=0,g=0;//Result, constant before unknown number

int i;

char y;

for(i=0;i<strlen(a);){

if(a[i]=='=') break;

if(a[i]=='+'||i==0&&a!='-'){

if(i!=0) i++;

int t=0;

while(a[i]>='0'&&a[i]<='9'){

t=t*10+(a[i]-'0');

i++;
}

if(a[i]>='a'&&a[i]<='z'){

y=a[i];

if(t==0) t=1;

g+=t;

i++;
}else{

s-=t;

}

}

if(a[i]=='-'){

i++;

int t=0;

while(a[i]>='0'&&a[i]<='9'){

t=t*10+(a[i]-'0');

i++;
}

if(a[i]>='a'&&a[i]<='z'){

y=a[i];

if(t==0) t=-1;

g-=t;

i++;

}else{

s+=t;
}

}

}

int h=++i;

for(;i<strlen(a);i++){

if(a[i]=='+'||i==h&&a[h]!='-'){

if(i!=h) i++;

int t=0;

while(a[i]>='0'&&a[i]<='9'){

t=t*10+(a[i]-'0');

i++;
}

if(a[i]>='a'&&a[i]<='z'){

y=a[i];

if(t==0) t=1;

g-=t;

}else{

s+=t;

}

}

if(a[i]=='-'){

i++;

int t=0;

while(a[i]>='0'&&a[i]<='9'){

t=t*10+(a[i]-'0');

i++;
}

if(a[i]>='a'&&a[i]<='z'){

y=a[i];

if(t==0) t=-1;

g+=t;

}else{

s-=t;
}

}

}

double x=s/g;

//cout<<s<<" "<<g<<endl;

printf("%c=%.3f\n",y,x);

return 0;
}```

Topics: calculator supervisor