这份代码提交提示第一行多个回车
查看原帖
这份代码提交提示第一行多个回车
225964
entity楼主2020/11/5 23:22
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
/*
20+3x=-18
x=-12.667
*/
string str;
double ans=0,c,v;//常数,字母系数 
char mark,letter;
int len=0,tmp=0,neg=1;
bool foundIt=true;
int main(){
//	freopen("in.txt","r",stdin);
	getline(cin,str);
	len=str.length();
	tmp=0;
	for(int i=0;i<len;i++){
		if(str[i]=='-'){
			if(foundIt) neg=0-neg;
			tmp*=neg;
			neg=-1;
			c+=tmp;
			tmp=0;
		}else{
			if(str[i]=='+'){
				if(foundIt) neg=0-neg;
				tmp*=neg;
				neg=1;
				c+=tmp;
				tmp=0;
			}else{
				if(str[i]>='0'&&str[i]<='9'){
					tmp=tmp*10+str[i]-'0';
				}else{
					if(str[i]=='='){
						foundIt=false;
						tmp*=neg;
						neg=1;
						c+=tmp;
						tmp=0;
					}else{
						if(!foundIt) neg=0-neg;
						mark=str[i];
						tmp*=neg;
						neg=1;
						v+=tmp;
						tmp=0;
					}
				}
			}
		}
	}
	if(tmp){
		tmp*=neg;
		c+=tmp;
	}
	ans=c/v;
//	cout<<c<<' '<<v<<endl;
	printf("%c=%.3lf",mark,ans);
	return 0;
}
2020/11/5 23:22
加载中...