patterncppMinor
Simple mathematical console application
Viewed 0 times
consolesimplemathematicalapplication
Problem
I am new to C++ and with my basic knowledge of the language, I have attempted to create a simple console application filled with lots of useful functions involving math related stuff.
The following is the only version of it that I have created thus far: (also available here).
```
#include
#include
#include
using namespace std;
string cmd;
bool moduleRun = true;
//Decorations
void finline(string appName){cout> n1 >> op >> n2;
switch(op){
//case '%' :
case '+' : cout' : (n1>n2)?cout> ";
cin>>arr[i];
total+=arr[i];
}
return total;
}
long double mean(int n){ return ( sum(n) / n ); }
long double prod(int n){
int arr[n];
long double product = 1;
for(int i = 0; i> ";
cin>>arr[i];
product*=arr[i];
}
cout> func >> param;
//Trigo
if(func == "sin") cout>n>>u1;
cout>u2;
if(u1 == u2){cout> ";
cin>>a;
cout> ";
cin>>b;
cout> ";
cin>>c;
double d = pow(b,2) - (4ac);
if( d >ans;
switch(sel){
case 0:
if( ans == (a + b) ){ cout100){
if(score>=50){cout> cmd;
if(cmd == "n" || cmd == "no"){cout>guess;
tries++;
if(guess>number){cout>cmd;
if( cmd =="n" || cmd =="no"){gameRun = false;} //ends game loop
else{ cout";
cin>>cmd;
if(cmd == "guessthenumber"){ guessthenumber(); }
else if(cmd == "solve"){ solve(); }
else if(cmd=="exit" || cmd =="quit"){ moduleRun = false; }
else{cout>";
cin >> cmd;
if( cmd == "cmdlist" || cmd=="commands" ){
cout << "\n\tcalc - Starts Arithmetic Calculator\n\tconv - Convert values to different units\n\tfunc - Scientific functions including sin, cos & tan\n\tquad - Finds the roots of a Quadratic Equation of the form ax^2 + bx + c = 0\n\tgames - Activates the games module\n\n";
}
else if( cmd == "calc" || cmd == "ca
The following is the only version of it that I have created thus far: (also available here).
```
#include
#include
#include
using namespace std;
string cmd;
bool moduleRun = true;
//Decorations
void finline(string appName){cout> n1 >> op >> n2;
switch(op){
//case '%' :
case '+' : cout' : (n1>n2)?cout> ";
cin>>arr[i];
total+=arr[i];
}
return total;
}
long double mean(int n){ return ( sum(n) / n ); }
long double prod(int n){
int arr[n];
long double product = 1;
for(int i = 0; i> ";
cin>>arr[i];
product*=arr[i];
}
cout> func >> param;
//Trigo
if(func == "sin") cout>n>>u1;
cout>u2;
if(u1 == u2){cout> ";
cin>>a;
cout> ";
cin>>b;
cout> ";
cin>>c;
double d = pow(b,2) - (4ac);
if( d >ans;
switch(sel){
case 0:
if( ans == (a + b) ){ cout100){
if(score>=50){cout> cmd;
if(cmd == "n" || cmd == "no"){cout>guess;
tries++;
if(guess>number){cout>cmd;
if( cmd =="n" || cmd =="no"){gameRun = false;} //ends game loop
else{ cout";
cin>>cmd;
if(cmd == "guessthenumber"){ guessthenumber(); }
else if(cmd == "solve"){ solve(); }
else if(cmd=="exit" || cmd =="quit"){ moduleRun = false; }
else{cout>";
cin >> cmd;
if( cmd == "cmdlist" || cmd=="commands" ){
cout << "\n\tcalc - Starts Arithmetic Calculator\n\tconv - Convert values to different units\n\tfunc - Scientific functions including sin, cos & tan\n\tquad - Finds the roots of a Quadratic Equation of the form ax^2 + bx + c = 0\n\tgames - Activates the games module\n\n";
}
else if( cmd == "calc" || cmd == "ca
Solution
The command pattern is your friend.
You can then use a std::map to convert a user input string to a command during execution.
Your conversions can be done by a matrix:
So the table above defines the power conversion from one unit to another.
You can then use a std::map to convert a user input string to a command during execution.
#include
#include
#include
#include
#include
int main(int argc, char* argv[])
{
std::map> commands;
commands["sin"] = [](double d) {return std::sin(d);};
commands["cos"] = [](double d) {return std::cos(d);};
//... etc.
std::string command = argv[1];
double value = std::atof(argv[2]);
std::cout << commands[command](value) << "\n";
}Your conversions can be done by a matrix:
Unit
m c d h k
mili 0 1 2 3 4 5
cente -1 0 1 2 3 4
-2 -1 0 1 2 3
deca -3 -2 -1 0 1 2
hecto -4 -3 -2 -1 0 1
kilo -5 -4 -3 -2 -1 0So the table above defines the power conversion from one unit to another.
int conversion[6][6] = {{0,1,2,3,4,5}, {-1,0,1,2,3,4}, .... };
std::map data = {{ "m", 0 }, {"c", 1}, {"", 2}, {"d", 3}, {"h", 4}, {"k", 5}};
std::string srcType = "d"; // src deca
std::string dstType = "k"; // kilo
int src = data[srcType];
int dst = data[dstType];
int result = n * pow(10, conversion[src][dst]);Code Snippets
#include <map>
#include <string>
#include <functional>
#include <cmath>
#include <iostream>
int main(int argc, char* argv[])
{
std::map<std::string, std::function<double(double)>> commands;
commands["sin"] = [](double d) {return std::sin(d);};
commands["cos"] = [](double d) {return std::cos(d);};
//... etc.
std::string command = argv[1];
double value = std::atof(argv[2]);
std::cout << commands[command](value) << "\n";
}Unit
m c <none> d h k
mili 0 1 2 3 4 5
cente -1 0 1 2 3 4
<none> -2 -1 0 1 2 3
deca -3 -2 -1 0 1 2
hecto -4 -3 -2 -1 0 1
kilo -5 -4 -3 -2 -1 0int conversion[6][6] = {{0,1,2,3,4,5}, {-1,0,1,2,3,4}, .... };
std::map<std::string, int> data = {{ "m", 0 }, {"c", 1}, {"", 2}, {"d", 3}, {"h", 4}, {"k", 5}};
std::string srcType = "d"; // src deca
std::string dstType = "k"; // kilo
int src = data[srcType];
int dst = data[dstType];
int result = n * pow(10, conversion[src][dst]);Context
StackExchange Code Review Q#31813, answer score: 7
Revisions (0)
No revisions yet.