-
Title Description:
-
Design a two-time equation calculator
-
Input:
-
Each case is a two-time equation of expression for x, and for simplicity, each coefficient is an integer form.
-
Output:
-
Each case outputs two real numbers (from small to large output, separated by spaces), retains two decimal places, and outputs "no solution" if no solution is provided.
-
Sample input:
-
X^2+x=3x+4
-
Sample output:
-
-1.24 3.24
#include <iostream>#include<cstring>#include<cmath>#include<iomanip>using namespacestd;voidOperatestringStrint&a,int&b,int&c) { for(intI=0; I<str.length (); i++){ if(str[i]=='+')Continue; Else if(str[i]=='-'){ } Else if(str[i]=='x'){ if(i+1<str.length () &&str[i+1]=='^'){ if(I-1>=0&&str[i-1]=='-') A=a-1; Elsea+=1; I+=3; } Else { if(I-1>=0&&str[i-1]=='-') b--; Elseb++; I++; } } Else { inttemp=0; intj=i; while(IsDigit (Str[i])) {temp=temp*Ten+str[i]-'0'; I++; } if(J-1>=0&&str[j-1]=='-') Temp=0-temp; if(I<str.length () &&str[i]=='x'){ if(i+1<str.length () &&str[i+1]=='^') {a+=temp; I+=3; } Else{b+=temp; I++; } } Elsec+=temp; } }}intMain () {stringstr; while(cin>>str) { intj=0; for(intI=0; I<str.length (); i++){ if(str[i]=='=') J=i; } stringStrleft,strright; Strleft=STR.SUBSTR (0, J); Strright=str.substr (j+1); inta1=0, b1=0, c1=0; intA2=0, b2=0, c2=0; Operate (STRLEFT,A1,B1,C1); Operate (STRRIGHT,A2,B2,C2); intA=a1-A2; intB=b1-B2; intC=c1-C2; inttemp=b*b-4*a*C; if(temp<0) cout<<"No Solution"<<Endl; Else { Doublex1,x2; X1=(Double)(0-B+SQRT (temp))/(2*a); X2=(Double)(0-B-SQRT (temp))/(2*a); if(x1<x2) cout<<fixed<<setprecision (2) <<x1<<" "<<x2<<Endl; Elsecout<<fixed<<setprecision (2) <<x2<<" "<<x1<<Endl; } } return 0;}
1103. Two-time equation calculator