注意:没有合并多项式的功能,且未知数必须只在左边,右边是常数!!!
好用记得点赞!
代码如下:
#include<iostream>
#include<stdio.h>
using namespace std;
string q,f;double a,b,c;bool bsz=0;
/*string addSame(string ba){
for(int i=0;i<int(ba.length());i++)
if(ba[i]>='a'&&ba[i]<='z')
ba[i]='x';
if(ba[0]!='-')ba=string("+")+ba;
string side,nba=ba;
for(int i=0;i<int(nba.length());i++){
if(nba[i]=='-'||nba[i]=='+'){
side+=string(1,nba[i]);
}
if(nba[i]=='-'){
nba[i]='+';
}
}
string vs[side.length()];
int df=-1;
for(int i=0;i<int(nba.length());i++){
if(nba[i]=='+'){
df++;
}else{
vs[df]+=string(1,nba[i]);
}
}
double xnum=0,cnum=0;double j;
for(int i=0;i<int(side.length());i++){
if(vs[i][vs[i].length()-1]=='x'){
sscanf(vs[i].data(),"%lfx",&j);
xnum+=j;
}else{
sscanf(vs[i].data(),"%lf",&j);
cnum+=j;
}
}
char cstr[64];
sprintf(cstr,"%c%lfx%c%lf",(xnum>=0?'+':0),xnum,(cnum>=0?'+':0),cnum);
return string(cstr);
}*/
string getS(string &str){
string ret="";
if(str[0]=='+'){
str[0]=' ';
}else if(str[0]=='-'){
str[0]=' ';
bsz=1;
}
for(int i=0;i<int(str.length());i++){
if(str[i]=='+'||str[i]=='-'){
ret+=string(1,str[i]);
}else if(str[i]=='='){
return ret;
}else if(str[i]>='a'&&str[i]<='z'){
ret+=string(1,'x');
str[i]='x';
}
}
throw "can not know";
}
void getD(string str){
if(str=="x+"){
sscanf(q.data(),"%lfx+%lf=%lf",&a,&b,&c);
if(bsz)a=-a;
}else if(str=="+x"){
sscanf(q.data(),"%lf+%lfx=%lf",&b,&a,&c);
if(bsz)b=-b;
}else if(str=="x"){
if(q[0]=='x'){
sscanf(q.data(),"x=%lf",&c);
a=1;b=0;
if(bsz)a=-a;
return;
}
sscanf(q.data(),"%lfx=%lf",&a,&c);
b=0;
if(bsz)a=-a;
}else if(str=="x-"){
sscanf(q.data(),"%lfx-%lf=%lf",&a,&b,&c);
b=-b;
if(bsz)a=-a;
}else if(str=="-x"){
sscanf(q.data(),"%lf-%lfx=%lf",&b,&a,&c);
a=-a;
if(bsz)b=-b;
}
}
double know(double a,double b,double c){
return (c-b)/a;
}
/*double solution(string bk){
string bs="",es="";bool isnis=1;
for(int i=0;i<int(bk.length());i++){
if(bk[i]=='='){
isnis=0;
}
if(isnis){
bs+=string(1,bk[i]);
}else{
es+=string(1,bk[i]);
}
}
bs=addSame(bs);
bk=bs+es;
getD(getS(bk));
return -know(a,b,c);
}*/
double solution(string bk){
getD(getS(bk));
return know(a,b,c);
}
char getnk(string bf){
char r=0;
for(int i=0;i<int(bf.length());i++){
if(bf[i]>='a'&&bf[i]<='z'){
r=bf[i];
break;
}
}
return r;
}
int main(){
cin>>q;
cout<<getnk(q)<<'='<<solution(q);
}