C++实现一个简单的控制台计算器

VS官网给新用户提供了一个C++控制台创建计算器的例子,用这个例子熟悉了一下VS,顺带做了一些个人改进
VS官网计算器例子的链接

官网的计算器有基本的 + - * / 功能,头文件创建的类中声明一个计算函数:

/*Calculator.h*/
#pragma once
class Calculator
{
public:
	double Calculate(double x, char oper, double y);
};

我在计算器的主体上添加了:
能识别正确的输入,输错会error并开始新一轮输入
当输入字符串aa时,将退出程序
要注意cin的原理
关于cin.sync()参考了该文:关于cin.sync()的解释

/*Calculator.cpp : 
此文件包含 "main" 函数。程序执行将在此处开始并结束*/

#include <iostream>
#include <string>
#include "Calculator.h"

using namespace std;

int main()
{
    double x = 0.0, y = 0.0;
    double res = 0.0;
    char oper = '+';
    string exit_a; //为了实现输入aa退出准备的字符串
    //建议用string,char字符数组会给自己找很多麻烦
    
    cout << "Calculator Console Application"<<endl<<endl;
    cout << "Please enter the operation to perform.Format: a+b | a-b | a*b | a/b" << endl;
    cout << "If you want to exit, please enter aa." << endl;

    Calculator c;//调用头文件中定义的类
    while (true) 
    {   
        /*判断输入是否符合变量的要求,符合则会被cin从输入流获取,
		cin成功获取会返回一个值,除了if(cin>>x)可行外,
		cin.rdstate()也可以直接获取这个值*/
        if (cin >> x) {
            if (cin >> oper) {
                if (cin >> y) {
                    //当三个值都能成功输入
                    cout << "Input success!" << endl;
                    //0不能被除
                    if (oper == '/' && y == 0)
                    {
                        cout << "Division by 0 exception" << endl;
                        continue;
                    }
                    else
                    {
                    	//符合所有条件,开始计算
                        res = c.Calculate(x, oper, y);
                    }
                    //输出结果
                    cout << "Result is:" << res << endl;
                }
                else {
                    cout << "Error!" << endl;
                }
            }
            else {
                cout << "Error!" << endl;
            }
        }
        else {
        	//输入不符合x的情况
        	//必须用clear()先恢复cin无法输入x的返回值
        	//否则cin不会继续从输入流进行读取
            cin.clear();
            cin >> exit_a;//cin读取字符串
            
            //仅当输入为aa时退出退出循环,无法再继续输入
            if (exit_a == "aa"){
                cout << "Your input is: " << exit_a << "." << endl;
                cout << "Now you will exit." << endl;
                break;//退出循环
            }
            else
                cout << "Error!" << endl;
        }
        cin.clear();
        //清除输入流中的内容
        //以下换成cin.sync();也可以
        cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
    }
    return 0;
}

/*类中的Calculate函数,实现+-*/*/
double Calculator::Calculate(double x, char oper, double y)
{
    switch (oper) {
    case '+':
        return x + y;
    case '-':
        return x - y;
    case '*':
        return x * y;
    case '/':
        return x / y;
    default:
        return 0.0;
    }
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值