Advertisement
MegaTor

overlastet operator ^

Jul 20th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.25 KB | None | 0 0
  1. // operator.h
  2.  
  3. #include <iostream>
  4. #include <cmath>
  5. #include <conio.h>
  6. #include <string>
  7.  
  8. #ifndef OPERATOR_H_
  9. #define OPERATOR_H_
  10.  
  11. class Operator
  12. {
  13. public:
  14.     double x,y;
  15.     Operator () {};
  16.     Operator (double, double);
  17.     Operator operator + (Operator);
  18.     Operator operator ^ (Operator);
  19. };
  20.  
  21. #endif
  22.  
  23.  
  24. // operator.cpp
  25.  
  26. #include "operator.h"
  27.  
  28. Operator::Operator (double a, double b)
  29. {
  30.     x = a;
  31.     y = b;
  32. }
  33.  
  34. Operator Operator::operator+ (Operator param)
  35. {
  36.     Operator temp;
  37.     temp.x = x + param.x;
  38.     temp.y = y + param.y;
  39.  
  40.     return (temp);
  41. }
  42.  
  43. Operator Operator::operator^ (Operator param)
  44. {
  45.     Operator temp;
  46.     temp.x = pow(x, y);
  47.     //temp.y = pow(y, x);
  48.  
  49.     return (temp);
  50. }
  51.  
  52.  
  53. // main.cpp
  54.  
  55. #include "operator.h"
  56. #include <vector>
  57.  
  58. using namespace std;
  59.  
  60. string testString;
  61. char *pointer;
  62.  
  63.  
  64. // vectors being used
  65. vector<string>strVec;
  66. vector<string>upperLetters;
  67. vector<string>lowerLetters;
  68. vector<string>specialCharacters;
  69. vector<string>numbers;
  70. vector<string>op;
  71.  
  72.  
  73. bool w_bool=false, o_bool = false, _continue = true;
  74.  
  75. short int i, j, total_lower, total_upper, total_special, total_whitespace, length, runAgain, k, l, val=0, time=0, count=0;
  76. double first, second;
  77.  
  78. #define NUMLETTER 29
  79. #define SPECIAL 38
  80. #define MAX 1024
  81. #define quote ""
  82. #define NUMBERS 10
  83. #define OP_COUNT 6
  84.  
  85. string upper[] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","\x92","\x9D","\x8F"};
  86. string lower[] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","\x91","\x9B","\x86"};
  87. string special[] = {"|","§","!","#","¤","%","&","/","(",")","=","+","?","`","\"","¨","<",">","*",".",",","_",":",";", "'","@","£","$","{","[","]","}","´","~","€","+","-", quote};
  88. string operators[] = {"/", "-", "+", "*", "^", "="};
  89. string numbersStr[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
  90. string whitespace[] = {" "};
  91. string g_str;
  92. string intStr="";
  93. string intStr2="";
  94. string opStr="";
  95.  
  96. int choice;
  97. Operator c;
  98.  
  99. void running();
  100. void run()
  101. {
  102.     cout << "Enter x^y to calculate x * x a total of y-times. Ex: 2^10 = 1024.";
  103.     cout << "\n\nYour expression: ";
  104.     getline(cin, testString);
  105.  
  106.     pointer = new char[testString.length() + 1];
  107.     strcpy(pointer, testString.c_str());
  108.     //strcpy_s(pointer,128, testString.c_str());
  109.  
  110.     while( *pointer != '\0')
  111.     {
  112.         g_str = *pointer;
  113.         strVec.push_back(g_str);
  114.         *pointer++;
  115.         length++;
  116.     }
  117.  
  118.     for(i=0; i<(int)strVec.size(); i++)
  119.     {
  120.         for(j=0, k=0; j<NUMBERS; j++, k++)
  121.         {
  122.             if(k>=6)
  123.                 k=0;
  124.             // combine the numbers to a string until there is a whitespace, after a dash there comes a new strig etc.
  125.             // slå sammen tallene til en string inntil det er et mellomrom, etter et mellomrom kommer en ny string etc.
  126.             if( numbers[j].compare(strVec[i])==0 || strVec[i] == "-")
  127.             {
  128.                 if(strVec[i] == "-")
  129.                     if(time == 0)
  130.                     {
  131.                         intStr += "-";
  132.                         j = NUMBERS;
  133.                     }
  134.                     else
  135.                     {
  136.                         intStr2 += "-";
  137.                         j = NUMBERS;
  138.                     }
  139.                 else if(time == 0 && count == 0)
  140.                 {
  141.                     intStr += numbers[j];
  142.                     j = NUMBERS;
  143.                 }
  144.                 else if(time == 1 && count > 0)
  145.                 {
  146.                     intStr2 += numbers[j];
  147.                     j = NUMBERS;
  148.                 }
  149.             }
  150.  
  151.             // read a new string or continue on the current string
  152.             // lese etter en ny string eller fortsettelse på nåværende string
  153.             else if( whitespace[0].compare(strVec[i])==0)
  154.             {
  155.                 // read the next character as long as it is a number
  156.                 if( time == 0 && strVec[i+1].compare(numbers[j]) && (!w_bool))
  157.                 {
  158.                     count++;
  159.                     time = 1;
  160.                 }
  161.                 else if( time == 1 && (strVec[i+1].compare(numbers[j])) && (w_bool) && (count != 1))
  162.                 {
  163.                     time = 0;
  164.                     count = 0;
  165.                 }
  166.                 else
  167.                 {
  168.                     time = 1, count = 1;
  169.                 }
  170.                 j=NUMBERS;
  171.             }
  172.  
  173.             // read the opeartor related to whitespaces etc.
  174.             // lese operatoren i forhold til mellomrom etc.
  175.             else if( operators[k].compare(strVec[i])==0 )
  176.             {
  177.                 if(time == 0)
  178.                 {
  179.                     w_bool = true;
  180.                     time=1;
  181.                     count=1;
  182.                 }
  183.                 else if(time == 1)
  184.                 {
  185.                     w_bool = false;
  186.                 }
  187.  
  188.                 j = NUMBERS;
  189.             }
  190.         }
  191.     }
  192.  
  193.     if(intStr.size() > 0)
  194.     {
  195.         first = atoi(intStr.c_str());
  196. //      cout << "\n\nfirst: " << first;
  197.     }
  198.  
  199.     if(intStr2.size() > 0)
  200.     {
  201.         second = atoi(intStr2.c_str());
  202. /*      if(intStr.size() > 0)
  203.             cout << ", second: " << second;
  204.         else
  205.             cout << "\n\nsecond: " << second;
  206. */
  207.         if(intStr.size() > 0 && intStr2.size() > 0)
  208.             o_bool = true;
  209.     }
  210.  
  211.     if(o_bool)
  212.     {
  213.         c = ( Operator(first, second) ^ (Operator()) );
  214.         if(c.x > 1.7*pow(10.0, 308.0) || c.x < 1.7*pow(10.0, -308.0))
  215.             cout << "\n\nCatching overflow\n\n";
  216.         else
  217.             cout << "\n\n" << first << "^" << second << " = " << c.x;
  218.     }
  219.  
  220.     running();
  221. }
  222.  
  223. void running()
  224. {
  225.     cout << "\n\nPress T or t to try again, otherwise any key to quit.\n";
  226.  
  227.     choice = _getch();
  228.  
  229.     if(choice == 't' || choice == 'T')
  230.     {
  231.         while(strVec.size() > 0)
  232.             strVec.pop_back();
  233.  
  234.         first = second = NULL;
  235.         intStr = intStr2 = "";
  236.  
  237.         time = count = 0;
  238.  
  239.         _continue = true;
  240.         system("CLS");
  241.         run();
  242.     }
  243.     else
  244.     {
  245.         pointer = NULL;
  246.        
  247.         while(strVec.size() > 0)
  248.             strVec.pop_back();
  249.        
  250.         while(upperLetters.size() > 0)
  251.             upperLetters.pop_back();
  252.        
  253.         while(lowerLetters.size() > 0)
  254.             lowerLetters.pop_back();
  255.        
  256.         while(specialCharacters.size() > 0)
  257.             specialCharacters.pop_back();
  258.  
  259.         while(numbers.size() > 0)
  260.             numbers.pop_back();
  261.  
  262.         while(op.size() > 0)
  263.             op.pop_back();
  264.  
  265.         pointer = NULL;
  266.  
  267.         _continue = false;
  268.     }
  269. }
  270.  
  271. int main ()
  272. {
  273.  
  274.     for(j=0; j<NUMLETTER; j++)
  275.     {
  276.         upperLetters.push_back(upper[j]);
  277.         total_upper++;
  278.     }
  279.  
  280.     for(j=0; j<NUMLETTER; j++)
  281.     {
  282.         lowerLetters.push_back(lower[j]);
  283.         total_lower++;
  284.     }
  285.  
  286.     for(i=0; i<length; i++)
  287.     {
  288.         for(j=0; j<SPECIAL; j++)
  289.         {
  290.             if(strVec[i].compare(special[j]) == 0)
  291.             {
  292.                 specialCharacters.push_back(special[j]);
  293.                 total_special++;
  294.             }
  295.         }
  296.     }
  297.  
  298.     for(i=0; i<length; i++)
  299.     {
  300.         for(j=0; j<1; j++)
  301.         {
  302.             if(strVec[i].compare(whitespace[j]) == 0)
  303.             {
  304.                 total_whitespace++;
  305.             }
  306.         }
  307.     }
  308.  
  309.     for(i=0; i<length; i++)
  310.     {
  311.         for(j=0; j<OP_COUNT; j++)
  312.         {
  313.             op.push_back(operators[j]);
  314.         }
  315.     }
  316.  
  317.     for(i=0; i<NUMBERS; i++)
  318.     {
  319.         numbers.push_back(numbersStr[i]);
  320.     }
  321.  
  322.     while(_continue)
  323.     {
  324.         run();
  325.     }
  326.  
  327.     return 0;
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement