使用 getchar() 读入字符,本地可正常运行,提交洛谷全 RE。
代码:
#include<bits/stdc++.h>
using namespace std;
struct Node{
int f,num;
bool dir,alpha;
}terms[110];
int i,j,s,l,r;
double x;
char alpha;
void input(){ // 输入
char c=getchar();
while(c!='='){ // =前
if(c=='-') terms[++i].f=-1,c=getchar(); // 符号
else if(c=='+') terms[++i].f=1,c=getchar();
else terms[++i].f=1;
int t=0; // 数字
while(isdigit(c)){
t=t*10+c-48;
c=getchar();
}
terms[i].num=t;
if(isalpha(c)) terms[i].alpha=1,alpha=c,c=getchar(); // 字母
else terms[i].alpha=0;
terms[i].dir=0;
}
c=getchar();
while(c!='\n'){ // =后
if(c=='-') terms[++j+i].f=-1,c=getchar(); // 符号
else if(c=='+') terms[++j+i].f=1,c=getchar();
else terms[++j+i].f=1;
int t=0; // 数字
while(isdigit(c)){
t=t*10+c-48;
c=getchar();
}
terms[i+j].num=t;
if(isalpha(c)) terms[i+j].alpha=1,alpha=c,c=getchar(); // 字母
else terms[i+j].alpha=0;
terms[i+j].dir=1;
}
}
void move(){ // 移项
for(int _=1;_<=i+j;_++){
if(terms[_].alpha&&terms[_].dir||!terms[_].alpha&&!terms[_].dir){
terms[_].dir^=1;
terms[_].f*=-1;
}
}
}
void combine(){ // 合并
for(int _=1;_<=i+j;_++){
if(terms[_].alpha) l+=terms[_].f*terms[_].num;
else r+=terms[_].f*terms[_].num;
}
}
void div(){ // 系数化为1
x=1.0*r/l;
}
int main(){
input();
move();
combine();
div();
cout<<alpha<<"="<<fixed<<setprecision(3)<<x;
return 0;
}