解方程工具

· · 个人记录

把以前写过的双栈模板和分数模板,拿出来整合了一下,制作了一个解方程工具。虽然有 300 行,但是很多都是重复和冗余。而且全是bug

只能解一元一次

#pragma GCC optimize(3,"Ofast","inline")
#pragma G++ optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
#include<windows.h>
#define int long long
#define Color(c) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c)
using namespace std;

struct frac { // 分数
    private:
        int x,y; // x/y
        void simplify() {
            if(y<0) x=-x, y=-y;
            int c = __gcd(x, y);
            if(c != 0)
                x/=c, y/=c;
        }
    public:
        frac(int _x=0,int _y=1) {
            x=_x, y=_y;
        }
        frac & operator = (const frac cp) { // 拷贝时自动化简
            x=cp.x, y=cp.y;
            simplify();
            return *this;
        }
        frac operator - () {
            return {-x,y};
        }
        friend frac operator + (frac a, frac b) {
            return {a.x*b.y+a.y*b.x, a.y*b.y};
        }
        friend frac operator - (frac a, frac b) {
            return {a.x*b.y-a.y*b.x, a.y*b.y};
        }
        friend frac operator * (frac a, frac b) {
            return {a.x*b.x, a.y*b.y};
        }
        friend frac operator / (frac a, frac b) {
            return {a.x*b.y, a.y*b.x};
        }
        bool operator < (frac b) const {
            return x*b.y < b.x*y;
        }
        bool operator <= (frac b) const {
            return x*b.y <= b.x*y;
        }
        bool operator > (frac b) const {
            return x*b.y > b.x*y;
        }
        bool operator >= (frac b) const {
            return x*b.y >= b.x*y;
        }
        bool operator == (frac b) const {
            return x*b.y == b.x*y;
        }
        bool operator != (frac b) const {
            return x*b.y != b.x*y;
        }
        friend ostream & operator << (ostream &os, frac a) {
            a.simplify();
            if(a.y == 0) os << "NaN";
            else if(a.y == 1) os << a.x;
            else os << "(" << a.x << "/" << a.y << ")";
            return os;
        }
};
class node {
#define const_val 3
        inline void clear() {
            x=0, val=0;
            for(int i=0; i<const_val; ++i) a[i]=0, ax[i]=0;
        }
    public:
        frac x,val; //一次系数,常数
        frac ax[const_val]; // ax,bx,cx,... 的系数
        frac a[const_val]; // a,b,c,... 的系数
        node() {
            clear();
        }
        node(const frac _x, const frac _val) {
            clear(), x=_x, val=_val;
        }
        node(frac _x, frac _val, frac _a) {
            clear(), x=_x, val=_val;
            a[0]=_a;
        }
        node(const node &cp) {
            x=cp.x, val=cp.val;
            for(int i=0; i<const_val; ++i) a[i]=cp.a[i], ax[i]=cp.ax[i];
        }
        node & operator = (const node cp) { // 拷贝
            x=cp.x, val=cp.val;
            for(int i=0; i<const_val; ++i) a[i]=cp.a[i], ax[i]=cp.ax[i];
            return *this;
        }
        friend node operator + (node a, node b) {
            node c = {a.x+b.x, a.val+b.val};
            for(int i=0; i<const_val; ++i) c.a[i]=a.a[i]+b.a[i], c.ax[i]=a.ax[i]+b.ax[i];
            return c;
        }
        friend node operator - (node a, node b) {
            node c = {a.x-b.x, a.val-b.val};
            for(int i=0; i<const_val; ++i) c.a[i]=a.a[i]-b.a[i], c.ax[i]=a.ax[i]-b.ax[i];
            return c;
        }
        friend node operator * (node a, node b) {
            node c = {a.x*b.val+a.val*b.x, a.val*b.val};
            for(int i=0; i<const_val; ++i) c.a[i]=a.a[i]*b.val+a.val*b.a[i], c.ax[i]=a.a[i]*b.x+a.x*b.a[i]+a.ax[i]*b.val+a.val*b.ax[i];
            return c;
        }
        friend node operator / (node a, node b) { // 不考虑分式方程
            return {a.x/b.val, a.x/b.x+a.val/b.val};
        }
};
// ukn = unknown number
void ukn(frac x,char ch[],int first){ // 输出未知数
    if(x==0) return;
    if(x==1) {
        if(first==0) cout<<"+";
        cout<<ch;
    } else if(x==-1) {
        cout<<"-"<<ch;
    } else if(x>0) {
        if(first==0) cout<<"+";
        cout<<x<<ch;
    } else {
        cout<<x<<ch;
    }
}
void ukn_op(frac x,char ch[],int &first){ // 输出未知数并操作 first
    ukn(x,ch,first);
    if(x != 0) first=0; 
}
int find(char x) {
    if(x=='+'||x=='-') return 1;
    if(x=='*'||x=='/') return 2;
    return 0;
}
#define isdigit(c) (((c)>='0'&&(c)<='9')||(c)=='.')
bool eval(stack<node>&num, stack<char>&op) {
    if(num.empty()) {
        Color(12),cout<<"error : 缺少操作数\n",Color(15);
        return 1;
    }
    node b = num.top();
    num.pop();
    if(num.empty()) {
        Color(12),cout<<"error : 缺少操作数\n",Color(15);
        return 1;
    }
    node a = num.top();
    num.pop();
    if(op.empty()) {
        Color(12),cout<<"error : 缺少运算符\n",Color(15);
        return 1;
    }
    char p = op.top();
    op.pop();
    if (p == '+') num.push(a+b);
    else if (p == '-') num.push(a-b);
    else if (p == '*') num.push(a*b);
    else if (p == '/') num.push(a/b);
    return 0;
}
node calc(string s) {
    stack <node> num;
    stack <char> op;
    for (int i=0; i<(int)s.size(); i++) {
        char c = s[i];
        if(c == ' ') continue;
        else if(c == 'x' || c == 'X') {
            num.push({1,0});
            if(i+1<(int)s.size() && (s[i+1]=='a' || s[i+1]=='x' || s[i+1]=='(')) {
                while (op.size() && find(op.top())>=find('*')) if(eval(num,op)) return {0,0};
                op.push('*'); // 字母和字母之间,自动补一个乘号
            }
        } else if(c == 'a' || c == 'A') {
            num.push({0,0,1}); // 常数
            if(i+1<(int)s.size() && (s[i+1]=='a' || s[i+1]=='x' || s[i+1]=='(')) {
                while (op.size() && find(op.top())>=find('*')) if(eval(num,op)) return {0,0};
                op.push('*'); // 字母和字母之间,自动补一个乘号
            }
        } else if (isdigit(c)) {
            int x=0,j=i;
            int point=-1;
            while (j < (int)s.size() && isdigit(s[j])) {
                if(s[j] == '.') point=++j;
                else x = x*10+(s[j++]^48);
            }
            if(point == -1) num.push({0,x});
            else num.push({0,{x,(int)pow(10,j-point)}}); // 小数输入
            i = j-1;
            if(j<(int)s.size() && (s[j]=='x' || s[j]=='(')) {
                while (op.size() && find(op.top())>=find('*')) if(eval(num,op)) return {0,0};
                op.push('*'); // 数字和字母之间,自动补一个乘号
            }
        } else if((i==0 || s[i-1]=='(') && s[i]=='-') {
            num.push({0,0}); // 负号前补0
            while (op.size() && find(op.top())>=find(c)) if(eval(num,op)) return {0,0};
            op.push(c);
        } else if (c == '(') {
            op.push(c);
        } else if (c == ')') {
            if(op.empty()) {
                Color(12),cout<<"error : 错误的括号匹配\n",Color(15);
                return {1,0};
            }
            while (op.top()!='(') if(eval(num,op)) return {0,0};
            op.pop();
            if(i+1<(int)s.size() && (s[i+1]=='a' || s[i+1]=='x' || s[i+1]=='(')) {
                while (op.size() && find(op.top())>=find('*')) if(eval(num,op)) return {0,0};
                op.push('*'); // 数字和字母之间,自动补一个乘号
            }
        } else {
            while (op.size() && find(op.top())>=find(c)) if(eval(num,op)) return {0,0};
            op.push(c);
        }
    }
    while (op.size()) if(eval(num,op)) return {0,0};
    return num.top();
}
signed main() {
    string s;
    Color(11), cout<<"cbs 计算器           1223\n";
    Color(15), cout<<"输入表达式,方程或不等式:\n";
    while(getline(cin,s)) {
        if(s == "") continue;
        if(s == "help"){
            Color(11), cout<<"cbs 计算器           1223\n";
            Color(15), cout<<"发布时间:2023/12/23\n";
            cout<<"表达式化简\n  最高次数:1,支持字母:x、a,                      分式:不支持\n";
            cout<<"解方程\n  最高次数:1,支持未知数:x,支持常数:a(不完善),分式:不支持\n";
            cout<<"解不等式\n  最高次数:1,支持未知数:x,支持常数:不支持,     分式:不支持\n";
            continue;
        }
#define any Color(10),cout<<"any\n"
#define no Color(12),cout<<"no\n"
        Color(14);
        int mid = s.find("<=");
        if(~mid) { // <=
            node ans = calc(s.substr(0,mid))-calc(s.substr(mid+2,s.size()-mid-2));
            if(ans.x > 0) cout << "x<=" << (-ans.val/ans.x) << endl;
            else if(ans.x == 0 && ans.val <= 0) any;
            else if(ans.x == 0) no;
            else cout << "x>=" << (-ans.val/ans.x) << endl;
        } else if(~(mid=s.find(">="))) { // >=
            node ans = calc(s.substr(0,mid))-calc(s.substr(mid+2,s.size()-mid-2));
            if(ans.x > 0) cout << "x>=" << (-ans.val/ans.x) << endl;
            else if(ans.x == 0 && ans.val >= 0) any;
            else if(ans.x == 0) no;
            else cout << "x<=" << (-ans.val/ans.x) << endl;
        } else if(~(mid=s.find("="))) { // =
            node ans = calc(s.substr(0,mid))-calc(s.substr(mid+1,s.size()-mid-1));
            if(ans.ax[0]!=0) {
                Color(14);
                cout<<"当a="<<-ans.x/ans.ax[0]<<"时,";
                if(-ans.a[0]-ans.val == 0) any;
                else no;
                Color(14);
                cout<<"当a!="<<-ans.x/ans.ax[0]<<"时,x="<<(-ans.a[0]-ans.val)<<"/(";
                ukn(ans.ax[0],"a",1), ukn(ans.x,"",0);
                cout<<")\n";
            } else {
                if(ans.x == 0 && ans.val == 0) any;
                else if(ans.x == 0) no;
                else cout<<(-ans.val/ans.x)<<endl;
            }
        } else if(~(mid=s.find("<"))) { // <
            node ans = calc(s.substr(0,mid))-calc(s.substr(mid+1,s.size()-mid-1));
            if(ans.x > 0) cout << "x<" << (-ans.val/ans.x) << endl;
            else if(ans.x == 0 && ans.val < 0) any;
            else if(ans.x == 0) no;
            else cout << "x>" << (-ans.val/ans.x) << endl;
        } else if(~(mid=s.find(">"))) { // >
            node ans = calc(s.substr(0,mid))-calc(s.substr(mid+1,s.size()-mid-1));
            if(ans.x > 0) cout << "x>" << (-ans.val/ans.x) << endl;
            else if(ans.x == 0 && ans.val > 0) any;
            else if(ans.x == 0) no;
            else cout << "x<" << (-ans.val/ans.x) << endl;
        } else if(~(mid=s.find("!="))) { // !=
            node ans = calc(s.substr(0,mid))-calc(s.substr(mid+2,s.size()-mid-2));
            if(ans.x == 0 && ans.val != 0) any;
            else if(ans.x == 0) no;
            else cout << "x!=" << (-ans.val/ans.x) << endl;
        } else {
            node ans = calc(s);
            int first = 1;
            ukn_op(ans.x,"x",first), ukn_op(ans.a[0],"a",first), ukn_op(ans.val,"",first);
            cout << endl;
        }
        Color(15);
    }
    return 0;
}