#include <iostream>
#include <cmath>
usingnamespace std;
int main() {
cout << "\tWelcome to Jacky\'s simple Calculator" << endl;
double x, y, z;
char u, u2;
cin >> x >> u >> y >> u2 >> z;
if (u == '+') {
if (u2 == '+') {
cout << x + y + z << endl;
}elseif (u2 == '-') {
cout << x + y - z << endl;
}elseif (u2 == '*') {
cout << (x + y) * z << endl;
}elseif (u2 == '/') {
cout << (x + y) / z << endl;
}else {
cout << "Does not fit..." << endl;
}
}elseif (u == '-') {
if (u2 == '+') {
cout << x - y + z << endl;
}elseif (u2 == '-') {
cout << x - y - z << endl;
}elseif (u2 == '*') {
cout << (x - y) * z << endl;
}elseif (u2 == '/') {
cout << (x - y) / z << endl;
}else {
cout << "Does not fit..." << endl;
}
}elseif (u == '*') {
if (u2 == '+') {
cout << (x * y) + z << endl;
}elseif (u2 == '-') {
cout << (x * y) - z << endl;
}elseif (u2 == '*') {
cout << (x * y) * z << endl;
}elseif (u2 == '/') {
cout << (x * y) / z << endl;
}else {
cout << "Does not fit..." << endl;
}
}elseif (u == '/') {
if (u2 == '+') {
cout << (x / y) + z << endl;
}elseif (u2 == '-') {
cout << (x / y) - z << endl;
}elseif (u2 == '*') {
cout << (x / y) * z << endl;
}elseif (u2 == '/') {
cout << (x / y) / z << endl;
}else {
cout << "Does not fit..." << endl;
}
}else {
cout << "Uhh... this doesn\'t work, sorry..." << endl;
}
return 0;
}
And it is fine for certain problems.
But what if for the cin cin >> x >> u >> y >> u2 >> z;
They don't want to fill every space? For example, they only want to input "2 + 2" instead of "2 + 2 + 2"
So How would you make a 'cin' that is optional and you can just press the 'enter' instead of having to add "+ 0" at the end of "2 + 2"?
I have been thinking about this too. I needed a program to add a string of numbers. Since then I have been thinking about how to subtract and multiply, so forth...
BTW< yours does not * or / before it + or -'s so the answer is wrong.
1 2 3 4
C:\Temp>testxxx3
Welcome to Jacky's simple Calculator
2+6/3
2.66667
The answer should be 4
I will look at this more later, but for now I'll just post my add program. Currently it only adds but, it will add from 2 to millions of numbers as long as they don't total over 2,147,483,647. Maybe you can find something in it to use.
// Add string of whole numbers from command line
// Version 2.0
// - added help and added remainder
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
usingnamespace std;
int remainder1;
// char *s2 = "/?";
void commandhelp ()
{
cout << "Add is a command line calculator to add multiple numbers." << endl;
cout << "Counts the number of entries and gives Average/Remainder." << endl;
cout << "The maximum size is 2,147,483,647" << endl;
cout << "---------------------How to use---------------------" << endl;
cout << "Type " << '"' << "add" << '"' << " followed by a string of numbers." << endl;
cout << "Example Useage: c:\\> add 1 2 3 4" << endl;
cout << "-------------------Aditional Notes------------------" << endl;
cout << "These charctres are ignored (A-Z a-z + - / * <> , % $ # )" << endl;
cout << "Using a charcter above that is ignored will increase the " << '"' << "Count of entries" << '"'<< endl;
cout << "c:\\> add 1,000 2,000 will result in a total of 3 since everything after the , is ignored" << endl;
cout << "Extra spaces are ok, c:\\> add 111 222 555 is the same as c:\\> add 111 222 555." << endl;
}
int main(int argc, char* argv[])
{
// this allows you to pass the values to another function if desired
char *s0 = argv[0];
char *s1 = argv[1];
if (argc == 1)
{
cout << "For Help Type : c:\\> add /?" << endl;
cout << "Example Useage: c:\\> add 1 2 3 4" << endl;
}
elseif (argc == 2)
{
if (!strncmp(s1, "/e", 2))
// if (strcmp (s1,"/e") == 0)
{
commandhelp();
}
else
{
cout << "For Help Type : c:\\> add /?" << endl;
cout << "Example Useage: c:\\> add 1 2 3 4" << endl;
}
}
elseif (argc > 2)
{
string line;
int count = 0; // Count of numbers + command add
int total = 0; // Sum of numbers
while (count+1 <= argc) // zero relative
{
count = count + 1; // add 1 to ignore command add
int x = atoi(argv[count]); // convert to int
total = total + x; // while there is another number add one to total
}
// if total is larger than 2147483647 return value will be negative
if (total < 0) {
cout << "Error, Number is negative" << endl;
}
else
{
remainder1 = (total % (argc-1));
cout << "Total = " << total << endl;
cout << "Count of entries) = " << (argc-1) << endl;
cout << "Average = " << (total/(count-1)) << " Remainder = " << remainder1 << endl;
}
}
return 0;
}
Thank you, I will be able to take a more detailed look at your code after I figure out how to run it. Because on line 63 int x = atoi(argv[count]); // convert to int
I cannot figure out what you meant "atoi" to be. Seems like a simple syntax error to me
Try using a bool type variable. Create an if statement that states that if the bool variable is true, take in 2 numbers. Otherwise, take in three. Try experimenting with that. Also, you should use system("CLS") to clear the screen after every problem. Hope I helped!