Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- #include <sstream>
- #include <string>
- #include <stdlib.h>
- #include "mixed.h"
- using namespace std;
- void welcome();
- void readline(string &line);
- void parse(string line, mixed &x, char &op, mixed &y);
- void perform(mixed &x, char op, mixed &y);
- string trim(string &line);
- mixed& readMixed(string &line);
- void getValues(string line, mixed &x, char &op, mixed &y, int where);
- int main(int argc, char *argv[])
- {
- mixed x, y;
- char op;
- string line;
- welcome();
- while(1)
- {
- readline(line);
- parse(line, x, op, y);
- perform(x, op, y);
- }
- system("PAUSE");
- return EXIT_SUCCESS;
- }
- stringstream ss (stringstream::in | stringstream::out);
- void welcome()
- {
- cout<<"Welcome to the PCC mixed Calculator!"<<endl
- <<endl
- <<"This calculator will do simple mixed mathematics"<<endl
- <<"that involve the operators +, -, *, /, and = and two"<<endl
- <<"operands."<<endl<<endl;
- }
- void readline(string &line)
- {
- cout<<"Input: ";
- getline(cin, line);
- if(line == "")
- exit(0);
- }
- void parse(string line, mixed &x, char &op, mixed &y)
- {
- stringstream ss (stringstream::in | stringstream::out);
- char *target = "+-*/=";
- int firstOp = line.find_first_of(target),
- lastOp = line.find_last_of(target),
- opCount = 0,
- found=line.find_first_of(target);
- int opPluCount = 0, opMinCount = 0, opMulCount = 0;
- string begin, end;
- while (found!=string::npos)
- {
- opCount++;
- found=line.find_first_of(target,found+1);
- }
- switch(opCount)
- {
- case 0 :
- cout<<"nothing to do!!!"<<endl;
- op = '+';
- line.append("/1 + 0/1");
- firstOp = line.find_first_of('+');
- getValues(line, x, op, y, firstOp);
- break;
- case 1 : /*One operator case with two integers
- One way of doing it would be to break things apart
- firstOp = line.find_first_of(target);
- op = line[firstOp];
- line = line.substr(0, op-1) + " 0/1 " +
- op + " " +
- line.substr(op+1, string::npos) + " 0/1";
- and then there is the "easy way"
- */
- if (line.at(firstOp) == '/') {
- line.insert(1,"/1");
- line.append("/1");
- int j = 0;
- for (size_t i=0; i < line.length(); i++)
- {
- if(line.at(i) == '/'){
- j++;
- if (j==2) {
- firstOp = i;
- break;
- }
- };
- }
- cout << line << endl;
- cout << firstOp << endl;
- getValues(line, x, op, y, firstOp);
- //break; //del before going on
- }
- else {
- getValues(line, x, op, y, firstOp);
- }
- break;
- case 2 : /*
- Whole op -Whole
- -Whole op Whole
- Fac op Whole
- Whole op Frac
- Mixed op Whole
- Whole op Mixed
- */
- break;
- case 3: /*
- -Whole op -Whole
- -Frac op Whole
- Whole op -Frac
- -Mixed op Whole
- Whole op -Mix
- Frac op Frac
- Mixed op Mixed
- Frac op Mixed
- Mixed op Frac
- */
- break;
- case 4: /*
- -Frac op Frac
- Frac op -Frac
- Mixed op -Mixed
- -Mixed op Mixed
- -Mixed op Frac
- -Frac op Mixed
- Mixed op -Frac
- Frac op -Mixed
- -Whole op (-Frac or -Mixed)
- (-Mixed or -Frac) op -Whole
- */
- break;
- case 5: /*
- -Mixed op -Mixed
- -Frac op -Frac
- -Frac op -Mixed
- -Mixed op -Frac
- */
- /*
- one way to read these would be:
- for(int i=0; i<3; i++)
- firstOp = line.find_first_of(target);
- begin = line.substr(0, firstOp-1);
- op =line[firstOp];
- end = line.substr(firstOp+1,string::npos);
- if(begin.find(" ") != string::npos)
- begin = "0 " + begin;
- if(end.find(" ") != string::npos)
- end = "0 " + end;
- line = begin + " " + op + " " + end;
- */
- getValues(line, x, op, y, 3);
- break;
- }
- }
- /*
- ss<<line;
- ss>>x>>op>>y;
- */
- void getValues(string line, mixed &x, char &op, mixed &y, int where)
- {
- static char* target = "+-/*";
- int firstOp = where;
- //int firstOp;
- //for(int i=0; i<where; i++)
- // firstOp = line.find_first_of(target);
- op = line[firstOp];
- string begin = line.substr(0,firstOp),
- end = line.substr(firstOp+1,string::npos);
- //string begin = line.substr(0,firstOp-1),
- //end = line.substr(0,firstOp+1);
- trim(begin);
- trim(end);
- x = readMixed(begin);
- y = readMixed(end);
- /*
- The above two lines translate to:
- begin = trim(line.substr(0,firstOp-1));
- end = trim(line.substr(0,firstOp+1));
- x = readMixed(begin);
- y = readMixed(end);
- */
- }
- string trim(string &line)
- {
- while(line[0] == ' ')
- line.erase(0);
- return line;
- }
- mixed& readMixed(string &line)
- {
- static mixed ans;
- char junk;
- int w = 0, n = 0, d = 1;
- stringstream ss (stringstream::in | stringstream::out);
- ss<<line;
- if(line.find(" "))
- ss>>w>>n>>junk>>d;
- else
- if(line.find("/") != string::npos)
- ss>>n>>junk>>d;
- else
- ss>>w;
- ans.set(w,n,d);
- return ans;
- }
- void perform(mixed &x, char op, mixed &y)
- {
- mixed ans;
- cout<<x<<" "<<op<<" "<<y<<" = ";
- switch(op)
- {
- case '+' : ans = x + y;
- break;
- case '-' : ans = x - y;
- break;
- case '*' : ans = x * y;
- break;
- case '/' : ans = x / y;
- break;
- case 'e' :
- op = '0';
- break;
- case '=' : if(x == y)
- cout<<"TRUE"<<endl;
- else
- cout<<"FALSE"<<endl;
- return;
- default : cout<<"Unknown operator!"<<endl;
- return;
- }
- cout<<ans<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement