如何用c++做一个较为复杂的计算器

结合之前几个版本的计算器,现在我将计算器进行了又一次升级,当数字输入的为其它字符的时候我们会要求重新输入,具体代码如下:

 

#include <iostream>
#include <limits> // 包含用于处理输入流的 std::numeric_limits
#include <cstdlib> // 包含用于清屏的 std::system
#include <cmath> // 包含用于平方根的 std::sqrt
using namespace std;
// 函数声明
void sssm();
void show_results();
// 全局变量
float result_arr[2] = { 0 }; // 存放两次结果的数组
int arr_index = 0;          // 当前存放结果的数组索引
int main() {
    float num1 = 0, num2 = 0, final;
    char fh = 0;
    char select = 0;
    cout << "欢迎使用简易计算器" << endl;
    cout << "-----------------" << endl;
    cout << "|    1、进入    |" << endl;
    cout << "-----------------" << endl;
    cout << "|    2、说明    |" << endl;
    cout << "-----------------" << endl;
    cin >> select;
    if (select == '1') {
        system("cls");
        cout << "请输入" << endl;
        sssm();
        goto Z;
    }
    else
        sssm();
Z:cin >> num1;
    while (true) {
        if (cin.fail()) {
            cin.clear(); // 清除错误标志
            cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 忽略输入缓冲区中的内容
            cout << "输入无效,请重新输入一个数字:" << endl;
            

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值