1 # include<iostream> 2 # include<string> 3 # include<iomanip> 4 # include<cmath> 5 using namespace std; 6 int a=0,b=0,c=0; 7 void getPar(string s,int flag) 8 { 9 if(s=="0") return ; 10 int l=s.size(); 11 bool neg=true; 12 int i=0; 13 for(i=0;i<l;i++) 14 { 15 int sum=0; 16 if(s[i]==‘-‘) {neg=false;i++;} 17 else if(s[i]==‘+‘){ neg=true;i++;} 18 while(s[i]>=‘0‘ && s[i]<=‘9‘) 19 { 20 sum=sum*10+s[i]-‘0‘; 21 i++; 22 } 23 if(sum==0){ sum=1;} 24 if(!neg) sum=-1*sum; 25 if(i==l) 26 { 27 if(flag) c=c-sum; 28 else c=c+sum; 29 } 30 else if(s[i]==‘x‘) 31 { 32 if(i+1<l && s[i+1]==‘^‘) 33 { 34 if(flag) a=a-sum; 35 else a=a+sum; 36 //a=a+flag?-1*sum:sum; 37 i=i+2; 38 } 39 else { 40 //b=b+flag? -1*sum:sum; 41 if(flag) b=b-sum; 42 else b=b+sum; 43 } 44 } 45 } 46 } 47 int main(){ 48 string s; 49 while(cin>>s){ 50 a=b=c=0; 51 int pos=s.find(‘=‘); 52 int l=s.size(); 53 string s1=s.substr(0,pos); 54 string s2=s.substr(pos+1,l-1); 55 getPar(s1,0); 56 getPar(s2,1); 57 double dot=b*b-4*a*c; 58 //cout<<a<<" "<<b<<" "<<c<<endl; 59 if(dot<0) cout<<"No Solution"<<endl; 60 else 61 { 62 double b1=(-b-sqrt(dot))/(2.0*a); 63 double b2=(-b+sqrt(dot))/(2.0*a); 64 if(b1>b2) 65 {cout<<fixed<<setprecision(2)<<b2<<" "<<b1<<endl;} 66 else cout<<fixed<<setprecision(2)<<b1<<" "<<b2<<endl; 67 } 68 } 69 return 0; 70 }不知道错那了 标记一下
时间: 2024-12-10 06:29:13