Using C++ Write a command line calculator in C++. The calculator supports the fo
ID: 3877970 • Letter: U
Question
Using C++
Write a command line calculator in C++. The calculator supports the following mathmatical operations:
• Multiplication m
• Divison d
• Subtraction s
• Addition a
• Percentage p
The following example show cases how this calculator can be used
The calculator should also support a commandline option --history, which displays the list of all previous calculations. Check out the following example:
$ ccalc p 2 10 Ans: 20
$ calc --history History:
p 2 10
20
a23
5
m 2 40
80
By necessity you’ll have to use a file to save/load the history. For the purposes of this exercise, lets assume that the history is stored in file ccalc-history.txt sitting in the local directory.
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv)
{
cout << "Hello World" << endl;
float a, b;
string code;
float result;
code = argv[1];
a = stoi(argv[2]);
b = stoi(argv[3]);
if(code.compare("m") == 0){
result = a * b;
cout<<result;
}
else if(code.compare("a") == 0){
result = a+b;
cout<<result;
}
else if(code.compare("s") == 0){
result = a-b;
cout<<result;
}
else if(code.compare("d") == 0){
result = a/b;
cout<<result;
}
else if(code.compare("p") == 0){
result = (a/b)*100;
cout<<result;
}
else{
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.