1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
// Add string of whole numbers from command line
// Version 2.0
// - added help and added remainder
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace 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;
}
else if (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;
}
}
else if (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;
}
|