#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<stack>
#include<cmath>
using namespace std;
int cba[3],epos;
char str[100];
void Read(int &co,int &eo,int &i){
if(str[i]==0) {eo=-1; return ;}
int flag=0;
if(str[i]=='=') {++i;}
if(str[i]=='-') {++i;++flag;}
if(str[i]=='+') ++i;
if(i>epos) ++flag;
if(str[i]=='x'){
co=1;
if(str[i+1]=='^'){i+=2; eo=str[i]-'0'; ++i;}
else {i++; eo=1;}
}
else{
co=0;
while(str[i]>='0'&&str[i]<='9')
{co=co*10+str[i]-'0'; ++i;}
if(str[i]=='x'){
if(str[i+1]=='^'){i+=2; eo=str[i]-'0';++i;}
else {i++; eo=1;}
}
else {eo=0;++i;}
}
if(flag%2){
co=0-co;
}
}
int main(){
while(scanf("%s",str)==1){
for(int i=0;;++i){
if(str[i]==0)break;
if(str[i]=='=') {epos=i; break;}
}
int co,eo,i=0;
memset(cba,0,sizeof(cba));
while(1){
Read(co,eo,i);
if(eo==-1) break;
cba[eo]+=co;
}
int a=cba[2],b=cba[1],c=cba[0];
// printf("a:%d,b:%d,c:%d\n",a,b,c);
double delta=b*b-4*a*c;
if(delta<0) printf("No Solution\n");
else {
double x1,x2;
x1=(0-b-sqrt(delta))/(2*a);
x2=(0-b+sqrt(delta))/(2*a);
if(x1>x2) swap(x1,x2);
printf("%.2f %.2f\n",x1,x2);
}
}
return 0;
}
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
int mat[5][5]={
1,0,0,0,0,
1,0,0,0,0,
1,0,0,0,0,
1,1,1,0,0,
1,1,1,0,0,
};
char s[1000];
stack<int> op;
stack<double> num;
void getop(bool &isop,int &retn,int &cur){
if(cur==0&&op.empty()||s[cur]==0){isop=1;retn=0; return ;}
char ch=s[cur];
if(ch>='0'&&ch<='9'){
isop=0;
}
else{
isop=1;
if(ch=='+') retn=1;
else if(ch=='-') retn =2;
else if(ch=='*') retn=3;
else if(ch=='/') retn = 4;
cur+=2;
return ;
}
retn=0;
while(s[cur]!=' '&&s[cur]!=0){
retn=retn*10+s[cur++]-'0';
}
if(s[cur]==' ') ++cur;
}
int main(){
while(gets(s)){
while(op.size()) op.pop();
while(num.size()) num.pop();
if(s[0]=='0'&&s[1]==0) break;
bool isop;
int retn,cur=0;
while(1){
getop(isop,retn,cur);
if(isop){
if(op.size()==0||mat[retn][op.top()]) op.push(retn);
else{
while(!mat[retn][op.top()]){
int tmpop=op.top(); op.pop();
double b=num.top(); num.pop();
double a=num.top(); num.pop();
double tmpnum;
if(tmpop==1) tmpnum=a+b;
if(tmpop==2) tmpnum=a-b;
if(tmpop==3)tmpnum=a*b;
if(tmpop==4) tmpnum=a/b;
num.push(tmpnum);
//printf("%f\n",tmpnum);
}
op.push(retn);
}
}
else num.push(retn);
if(op.size()==2&&op.top()==0) break;
}
printf("%.2f\n",num.top());
}
re
如何寻找单位读取单元,抽象出公共范式,是关键!