C++实现的命令行计算器。
2012-11-20 17:22
288 查看
只进行加减乘除运算,没有进行异常判断。没啥技术含量,直接贴代码。
#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;
}
}
}
本文出自 “怒放的生命” 博客,请务必保留此出处/article/4605655.html
#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;
}
}
}
本文出自 “怒放的生命” 博客,请务必保留此出处/article/4605655.html
相关文章推荐
- 第四次程序设计作业 C++计算器计算及命令行的使用 前缀表达式方法实现
- 用C++实现的命令行计算器
- 第四次程序设计作业 C++计算器计算及命令行的使用 前缀表达式方法实现
- 用C++实现的命令行计算器
- C++在命令行中回车实现默认输入
- C++-------实现计算器
- c++实现计算器
- 简单工厂模式----控制台计算器的设计(C++实现)
- 计算器——C++实现
- [转] 使用 gperf 实现高效的 C/C++ 命令行处理
- 使用 gperf 实现高效的 C/C++ 命令行处理
- 数据结构实验:一元多项式计算器(C++ 实现)
- 计算器——C++实现(CLR版本)
- 【C++】C++顺序栈实现的计算器
- 使用go实现基于命令行的计算器程序
- CSP考试 2014年03月第3题 命令行选项 C++实现
- 用C++实现中缀表达式求值即简单科学计算器的功能
- linux 中c/c++实现终端命令行命令
- 【C/C++】用回调函数实现计算器
- 计算器c++实现