📄 main.cpp
字号:
#include <iostream>
#include "UInt.h"
#include "IoUtils.h"
using namespace std;
/* 让用户输入一个任意长整数
* 由于UInt类型是无符号整数, 用返回值指示符号
* 返回值: true表示负数, false表示正数
* 0 的返回值不确定
*/
bool inputInt(UInt& i) {
bool is_neg; // 是否是负数 (或0)
while (true) {
string str = getString();
char *ch = str.begin();
if (*ch == '-') { // 判断符号
is_neg = true;
++ch;
} else
if (str[0] == '+') {
is_neg = false;
++ch;
} else {
is_neg = false;
}
try {
UInt val(ch); // 构造
i = val; // 传回
return is_neg; // 正常返回
} catch (const std::invalid_argument&) { // 构造失败
cout << "输入有误! 请输入一个整数值 (任意长): ";
}
}
} // inputInt(UInt&)
int main() {
int noErrors = 0;
cout << "输入任意长整数并求积:\n"
<< "\t一次输入两个任意长整数, 程序算出它们的积并输出\n"
<< endl;
try {
UInt i, j;
bool ni, nj;
cout << "输入第一个整数 (任意长): ";
ni = inputInt(i);
cout << endl;
cout << "输入第二个整数 (任意长): ";
nj = inputInt(j);
cout << endl;
if (ni) {
cout << "-"; // 输出负号
}
i.print();
cout << " * ";
if (nj) {
cout << "-"; // 输出负号
}
j.print();
cout << endl;
i.mul(j);
ni ^= nj; // 计算结果的符号
cout << "\t== ";
if (ni) {
cout << "-";
}
i.print();
cout << endl;
pause();
} catch (const std::exception& e) {
cerr << "捕捉到异常!" << endl;
cerr << e.what() << endl;
++noErrors;
}
return noErrors;
} // main()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -