Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.46 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <string>
  5. #include <stdlib.h>
  6. #include "mixed.h"
  7.  
  8. using namespace std;
  9.  
  10.  
  11. void welcome();
  12. void readline(string &line);
  13. void parse(string line, mixed &x, char &op, mixed &y);
  14. void perform(mixed &x, char op, mixed &y);
  15. string trim(string &line);
  16. mixed& readMixed(string &line);
  17. void getValues(string line, mixed &x, char &op, mixed &y, int where);
  18.  
  19. int main(int argc, char *argv[])
  20. {
  21.     mixed x, y;
  22.     char op;
  23.     string line;
  24.     welcome();
  25.     while(1)
  26.     {
  27.         readline(line);
  28.         parse(line, x, op, y);
  29.         perform(x, op, y);        
  30.     }
  31.     system("PAUSE");
  32.     return EXIT_SUCCESS;
  33. }
  34. stringstream ss (stringstream::in | stringstream::out);
  35.  
  36.  
  37. void welcome()
  38. {
  39.     cout<<"Welcome to the PCC mixed Calculator!"<<endl
  40.     <<endl
  41.     <<"This calculator will do simple mixed mathematics"<<endl
  42.     <<"that involve the operators +, -, *, /, and = and two"<<endl
  43.     <<"operands."<<endl<<endl;
  44. }
  45.  
  46. void readline(string &line)
  47. {
  48.     cout<<"Input: ";
  49.     getline(cin, line);
  50.     if(line == "")
  51.         exit(0);
  52. }
  53.  
  54. void parse(string line, mixed &x, char &op, mixed &y)
  55. {
  56.    
  57.     stringstream ss (stringstream::in | stringstream::out);
  58.     char *target = "+-*/=";
  59.     int firstOp = line.find_first_of(target),
  60.     lastOp = line.find_last_of(target),
  61.     opCount = 0,
  62.     found=line.find_first_of(target);
  63.     int opPluCount = 0, opMinCount = 0, opMulCount = 0;
  64.     string begin, end;
  65.     while (found!=string::npos)
  66.     {
  67.         opCount++;
  68.         found=line.find_first_of(target,found+1);
  69.     }
  70.     switch(opCount)
  71.     {
  72.         case 0 :
  73.             cout<<"nothing to do!!!"<<endl;
  74.             op = '+';
  75.             line.append("/1 + 0/1");
  76.             firstOp = line.find_first_of('+');
  77.             getValues(line, x, op, y, firstOp);
  78.             break;
  79.            
  80.         case 1 : /*One operator case with two integers
  81.                   One way of doing it would be to break things apart
  82.                  
  83.                   firstOp = line.find_first_of(target);
  84.                   op = line[firstOp];
  85.                   line = line.substr(0, op-1) + " 0/1 " +
  86.                   op + " "  +
  87.                   line.substr(op+1, string::npos) + " 0/1";  
  88.                  
  89.                   and then there is the "easy way"
  90.                   */
  91.            
  92.             if (line.at(firstOp) == '/') {
  93.                 line.insert(1,"/1");
  94.                 line.append("/1");
  95.                 int j = 0;
  96.                 for (size_t i=0; i < line.length(); i++)
  97.                 {
  98.                     if(line.at(i) == '/'){
  99.                         j++;
  100.                         if (j==2) {
  101.                             firstOp = i;
  102.                             break;
  103.                         }
  104.                     };
  105.                 }
  106.                 cout << line << endl;
  107.                 cout << firstOp << endl;
  108.                
  109.                 getValues(line, x, op, y, firstOp);
  110.                 //break; //del before going on
  111.             }
  112.             else {
  113.                 getValues(line, x, op, y, firstOp);
  114.             }
  115.  
  116.        
  117.             break;
  118.            
  119.         case 2 : /*
  120.                   Whole op -Whole
  121.                   -Whole op Whole
  122.                   Fac op Whole
  123.                   Whole op Frac
  124.                   Mixed op Whole
  125.                   Whole op Mixed
  126.                   */
  127.            
  128.             break;
  129.            
  130.         case 3: /*
  131.                  -Whole op -Whole
  132.                  -Frac op Whole
  133.                  Whole op -Frac
  134.                  -Mixed op Whole
  135.                  Whole op -Mix
  136.                  Frac op Frac
  137.                  Mixed op Mixed
  138.                  Frac op Mixed
  139.                  Mixed op Frac
  140.                  */
  141.            
  142.            
  143.             break;
  144.            
  145.            
  146.         case 4: /*
  147.                  -Frac op Frac
  148.                  Frac op -Frac
  149.                  Mixed op -Mixed
  150.                  -Mixed op Mixed
  151.                  -Mixed op Frac
  152.                  -Frac op Mixed
  153.                  Mixed op -Frac
  154.                  Frac op -Mixed
  155.                  -Whole op (-Frac or -Mixed)
  156.                  (-Mixed or -Frac) op -Whole
  157.                  */
  158.             break;
  159.            
  160.         case 5: /*
  161.                  -Mixed op -Mixed
  162.                  -Frac op -Frac
  163.                  -Frac op -Mixed
  164.                  -Mixed op -Frac
  165.                  */
  166.             /*
  167.              one way to read these would be:
  168.              
  169.              for(int i=0; i<3; i++)
  170.              firstOp = line.find_first_of(target);
  171.              
  172.              begin = line.substr(0, firstOp-1);
  173.              op =line[firstOp];
  174.              end = line.substr(firstOp+1,string::npos);
  175.              if(begin.find(" ") != string::npos)
  176.              begin = "0 " + begin;
  177.              if(end.find(" ") != string::npos)
  178.              end = "0 " + end;
  179.              line = begin + " " + op + " " + end;
  180.              */
  181.             getValues(line, x, op, y, 3);
  182.             break;
  183.     }
  184.    
  185. }
  186. /*
  187.  ss<<line;
  188.  ss>>x>>op>>y;
  189.  */    
  190.  
  191. void getValues(string line, mixed &x, char &op, mixed &y, int where)
  192. {
  193.     static char* target = "+-/*";
  194.    
  195.     int firstOp = where;
  196.     //int firstOp;
  197.     //for(int i=0; i<where; i++)
  198.     //  firstOp = line.find_first_of(target);
  199.     op = line[firstOp];
  200.     string begin = line.substr(0,firstOp),
  201.     end = line.substr(firstOp+1,string::npos);
  202.     //string begin = line.substr(0,firstOp-1),
  203.     //end = line.substr(0,firstOp+1);
  204.     trim(begin);
  205.     trim(end);
  206.     x = readMixed(begin);
  207.     y = readMixed(end);
  208.     /*
  209.      The above two lines translate to:
  210.      
  211.      begin = trim(line.substr(0,firstOp-1));
  212.      end = trim(line.substr(0,firstOp+1));
  213.      x = readMixed(begin);
  214.      y = readMixed(end);
  215.      */
  216. }
  217.  
  218. string trim(string &line)
  219. {
  220.     while(line[0] == ' ')
  221.         line.erase(0);
  222.     return line;
  223. }  
  224.  
  225. mixed& readMixed(string &line)
  226. {
  227.     static mixed ans;
  228.     char junk;
  229.     int w = 0, n = 0, d = 1;
  230.     stringstream ss (stringstream::in | stringstream::out);    
  231.     ss<<line;
  232.     if(line.find(" "))
  233.         ss>>w>>n>>junk>>d;
  234.     else
  235.         if(line.find("/") != string::npos)
  236.             ss>>n>>junk>>d;
  237.         else
  238.             ss>>w;
  239.     ans.set(w,n,d);
  240.     return ans;
  241. }
  242.  
  243.  
  244. void perform(mixed &x, char op, mixed &y)
  245. {
  246.     mixed ans;
  247.     cout<<x<<" "<<op<<" "<<y<<" = ";
  248.     switch(op)
  249.     {
  250.         case '+' :  ans = x + y;
  251.             break;
  252.            
  253.         case '-' :  ans = x - y;
  254.             break;
  255.            
  256.         case '*' :  ans = x * y;
  257.             break;
  258.            
  259.         case '/' :  ans = x / y;
  260.             break;
  261.         case 'e' :
  262.             op = '0';
  263.             break; 
  264.         case '=' :  if(x == y)
  265.             cout<<"TRUE"<<endl;
  266.         else
  267.             cout<<"FALSE"<<endl;
  268.             return;
  269.            
  270.         default  :  cout<<"Unknown operator!"<<endl;
  271.             return;
  272.     }
  273.     cout<<ans<<endl;
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement