C++实现的命令行计算器。

只进行加减乘除运算,没有进行异常判断。没啥技术含量,直接贴代码。
 
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
void main() {
 
 cout << "          ********************************************************" << endl;
 cout << "                         命令行计算器" << endl << endl;
 cout << "             1.用法介绍:第一次先输入一个数字,然后回车" << endl;
 cout << "             2.输入运算符号,然后回车" << endl;
 cout << "             3.再次输入一个数字,然后回车,程序会自动运算出结果" << endl;
 cout << "             4.输入exit退出程序" << endl;
 cout << "          ********************************************************" << endl;
 string leftParStr;
 int leftParInt;
 int rightParInt;
 string rightParStr;
 string calculator;
 while(true) {
  
  cout << "1.请输入运算左值:";
 cin >> leftParStr;
 if(leftParStr == "exit")
  return;
 cout << "2.请输入运算符:";
 cin >> calculator;
 if(calculator == "exit")
  return;
 cout << "3.请输入运算右值:";
 cin >> rightParStr;
 if(rightParStr == "exit")
  return;
 leftParInt = atoi(leftParStr.c_str());
 rightParInt = atoi(rightParStr.c_str());
 if(calculator == "+") {
  cout << " The result of this calculator is: " << leftParInt + rightParInt << endl;
 }
 ///*
 if(calculator == "-") {
  cout << " The result of this calculator is: " << leftParInt - rightParInt << endl;
 }
 if(calculator == "*") {
  cout << " The result of this calculator is: " << leftParInt * rightParInt << endl;
 }
 if(calculator == "/") {
  cout << " The result of this calculator is: " << leftParInt / rightParInt << endl;
 }
 }
 
}

本文出自 “怒放的生命” 博客,请务必保留此出处http://shane.blog.51cto.com/824878/339922

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值