一点尝试

257次阅读
没有评论

共计 2916 个字符,预计需要花费 8 分钟才能阅读完成。

#include <bits/stdc++.h>

using namespace std;


string fc;
string tps;
int devide[1000000];

bool is_zimu(char f) {
    if ('a' <= f && f <= 'z') return true;
    return false;
}

bool is_fuhao(char f) {
    if (f == '-' || f == '+') {
        return true;
    }
    return false;
}

bool is_num(char f) {
    if ('0' <= f && f <= '9') return true;
    return false;
}

void qianyi(int yi_x) {
    for (int i=yi_x+1; i < fc.length(); i++) {
        fc[i-1] = fc[i];
        tps[i-1] = tps[i];
    }
}

void houyi(int yi_x) {
    for (int i=fc.length()-1; i > yi_x; i--) {
        fc[i] = fc[i-1];
        tps[i] = tps[i-1];
    }
}

void qiandiao(int diao_x) {
	fc += " ";
	tps += " ";
	houyi(0);
	fc[0] = fc[diao_x+1];
	tps[0] = tps[diao_x+1];
	qianyi(diao_x+1);
	fc.erase(fc.end()-1);
}

int num_str_to_int(string num_str) {
    string str_num = num_str.substr(1, num_str.length()-1);
    return atoi(str_num.c_str());
}

int main() {
    cin >> fc;
    tps = fc;
    
    string wei="";
    
    // 判断是否需要添加 + 号 
    if (!is_fuhao(fc[0])) {
		fc += " ";
		tps += " ";
		houyi(0);
		fc[0] = '+';
    }
    
    // 区分类型 
    for (int i=0; i < fc.length(); i++) {
        tps[i] = 'n';
        if (is_fuhao(fc[i])) tps[i] = 'f';
        if (i != 0) {
            if (is_zimu(fc[i])) {
                wei = fc[i];
                tps[i] = 'a';
                int ix=i-1;
                for (int j=i-1; is_num(fc[j]); j--) {
                    tps[j] = 'x';
                    ix = j-1;
                }
                if (ix >= 0 && fc[ix] != '=') {
                    tps[ix] = 'x';
                }
            }
            if (fc[i] == '=') {
                tps[i] = '=';
            }
        }
    }
    
    // 移项 
    bool flag=true;
    for (int i=0; i < fc.length(); i++) {
        if (tps[i] == '=') {
            flag = false;
            // 看等号右边第一个是否有加号 
            if (!is_fuhao(fc[i+1])) {
				fc += " ";
				tps += " ";
				houyi(i+1);
				fc[i+1] = '+';
				if (tps[i+2] == 'a' || tps[i+2] == 'x') {
					tps[i+1] = 'x';
				}
				else {
					tps[i+1] = 'f';
				}
            }
			
            continue;
        }
        if (flag) {
            // 等号左边 
            if (tps[i] == 'f') {
                char yi=fc[i];
                if (yi == '+') {
                    yi = '-';
                }
                else yi = '+';
                qianyi(i);
                fc[fc.length() - 1] = yi;
                tps[fc.length() - 1] = 'f';
                i--;
            }
            if (tps[i] == 'n') {
                char yi=fc[i];
                qianyi(i);
                fc[fc.length() - 1] = yi;
                tps[fc.length() - 1] = 'n';
                i--;
            }
        }
        else {
			// 等号右边 
			if (tps[i] == 'a') {
				qiandiao(i);
				// 接着移动前面的系数 
				while (tps[i] == 'x') {
					qiandiao(i);
					if (is_fuhao(fc[0])) {
    					if (fc[0] == '+') {
    						fc[0] = '-';
    					}
    					else {
    						fc[0] = '+';
    					}
					}
				}
			} 
        }
    }
    
    // cout << endl << fc << endl << tps << endl << endl;
    
    // 合并同类项 
    double zong=0, xi=0;
    // int left[count(tps.begin(), tps.end(), 'a') + 1];
    // int right[count(tps.begin(), tps.end(), 'f') + 1];
    int cnt=0;
    string tmp="";
    flag = true;
    for (int i=0; i < fc.length(); i++) {
        if (tps[i] == '=') {
            flag = false;
            cnt = -1;
            tmp = "";
            continue;
        }
        if (flag) {
            // 等号左边 
            if (tps[i] == 'a') {
				// cout << tmp << endl;
                if (tmp[0] == '+') {
                    if (tmp.length() == 1) {
                        // left[cnt] = 1;
                        xi++;
                    }
                    else {
                        // left[cnt] = num_str_to_int(tmp);
                        xi += num_str_to_int(tmp);
                    }
                }
                else {
                    if (tmp.length() == 1) {
                        // left[cnt] = -1;
                        xi--;
                    }
                    else {
                        // left[cnt] = num_str_to_int(tmp) * (-1);
                        xi -= num_str_to_int(tmp);
                    }
                }
                cnt++;
                tmp = "";
            }
            else tmp += fc[i];
        }
        else {
            // 等号右边 
            if (tps[i] == 'f' && cnt != -1) {
				// cout << tmp << endl;
                if (tmp[0] == '+') {
                    // right[cnt] = num_str_to_int(tmp);
                    zong += num_str_to_int(tmp);
                }
                else {
                    // right[cnt] = num_str_to_int(tmp) * (-1);
                    zong -= num_str_to_int(tmp);
                }
                cnt++;
                tmp = fc[i];
            }
            else {
                if (tps[i] == 'f' && cnt == -1) {
                    cnt = 0;
                }
                tmp += fc[i];
            }
        }
    }
    if (tmp[0] == '+') {
        // right[cnt] = num_str_to_int(tmp);
        zong += num_str_to_int(tmp);
    }
    else {
        // right[cnt] = num_str_to_int(tmp) * (-1);
        zong -= num_str_to_int(tmp);
    }
    
    
    // 求值 
    // for (int i=0; i < count(tps.begin(), tps.end(), 'a'); i++) {
        // xi += left[i];
    // }
    // for (int i=0; i < count(tps.begin(), tps.end(), 'f'); i++) {
        // zong += right[i];
    // }
    
    // cout << endl << xi << endl << zong << endl;
    
    cout << wei << "=";
    
    double a=zong / xi;
    double fu_z = 0;
    if (a == fu_z) {
		a = 0;
    }
    cout << fixed << setprecision(3) << a;
    
    return 0;
}
正文完
 0
syh
版权声明:本站原创文章,由 syh 于2025-08-12发表,共计2916字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
本站总字数:7.2w