四则运算总结
经历了一个礼拜的编程之后,我终于做完了四则运算的作业,在编写过程中也遇到过一些问题,但最后还是顺利解决了,例如:
1、首先遇到的问题是中缀转后缀的问题,查书之后最终用系统栈解决了这个问题。
2、第二个问题就是分数类的编写,最后实现方法如下
class fenshu { public: int fz; int fm; fenshu(int a, int b) { fz = a; fm = b; yuefen(); } void yuefen() { int t; int a = fz; int b = fm; if (a < b) { t = a; a = b; b = t; } while (t = a % b) { a = b; b = t; } if (b != 1) { fz /= b; fm /= b; } } void print() { cout << fz << "/" << fm; } fenshu operator +(fenshu &fs) { fenshu f(fz*fs.fm + fs.fz*fm, fm*fs.fm); f.yuefen(); return f; } fenshu operator -(fenshu &fs) { fenshu f(fz*fs.fm - fs.fz*fm, fm*fs.fm); f.yuefen(); return f; } fenshu operator *(fenshu &fs) { fenshu f(fz*fs.fz, fs.fm*fm); f.yuefen(); return f; } fenshu operator /(fenshu &fs) { fenshu f(fz*fs.fm, fm*fs.fz); f.yuefen(); return f; } void operator =(fenshu &fs) { fz = fs.fz; fm = fs.fm; } bool operator ==(fenshu &fs) { return fz == fs.fz&&fm == fs.fm; } };
3、关于分号是分号还是除法的问题也进行了区分
for (int i = 0; i < exp.size(); i++){ if (exp[i] == '.') { jiance = 0; break; } else { jiance = 1; } }
最后程序结果如下: