Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // operator.h
- #include <iostream>
- #include <cmath>
- #include <conio.h>
- #include <string>
- #ifndef OPERATOR_H_
- #define OPERATOR_H_
- class Operator
- {
- public:
- double x,y;
- Operator () {};
- Operator (double, double);
- Operator operator + (Operator);
- Operator operator ^ (Operator);
- };
- #endif
- // operator.cpp
- #include "operator.h"
- Operator::Operator (double a, double b)
- {
- x = a;
- y = b;
- }
- Operator Operator::operator+ (Operator param)
- {
- Operator temp;
- temp.x = x + param.x;
- temp.y = y + param.y;
- return (temp);
- }
- Operator Operator::operator^ (Operator param)
- {
- Operator temp;
- temp.x = pow(x, y);
- //temp.y = pow(y, x);
- return (temp);
- }
- // main.cpp
- #include "operator.h"
- #include <vector>
- using namespace std;
- string testString;
- char *pointer;
- // vectors being used
- vector<string>strVec;
- vector<string>upperLetters;
- vector<string>lowerLetters;
- vector<string>specialCharacters;
- vector<string>numbers;
- vector<string>op;
- bool w_bool=false, o_bool = false, _continue = true;
- short int i, j, total_lower, total_upper, total_special, total_whitespace, length, runAgain, k, l, val=0, time=0, count=0;
- double first, second;
- #define NUMLETTER 29
- #define SPECIAL 38
- #define MAX 1024
- #define quote ""
- #define NUMBERS 10
- #define OP_COUNT 6
- 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"};
- 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"};
- string special[] = {"|","§","!","#","¤","%","&","/","(",")","=","+","?","`","\"","¨","<",">","*",".",",","_",":",";", "'","@","£","$","{","[","]","}","´","~","€","+","-", quote};
- string operators[] = {"/", "-", "+", "*", "^", "="};
- string numbersStr[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
- string whitespace[] = {" "};
- string g_str;
- string intStr="";
- string intStr2="";
- string opStr="";
- int choice;
- Operator c;
- void running();
- void run()
- {
- cout << "Enter x^y to calculate x * x a total of y-times. Ex: 2^10 = 1024.";
- cout << "\n\nYour expression: ";
- getline(cin, testString);
- pointer = new char[testString.length() + 1];
- strcpy(pointer, testString.c_str());
- //strcpy_s(pointer,128, testString.c_str());
- while( *pointer != '\0')
- {
- g_str = *pointer;
- strVec.push_back(g_str);
- *pointer++;
- length++;
- }
- for(i=0; i<(int)strVec.size(); i++)
- {
- for(j=0, k=0; j<NUMBERS; j++, k++)
- {
- if(k>=6)
- k=0;
- // combine the numbers to a string until there is a whitespace, after a dash there comes a new strig etc.
- // slå sammen tallene til en string inntil det er et mellomrom, etter et mellomrom kommer en ny string etc.
- if( numbers[j].compare(strVec[i])==0 || strVec[i] == "-")
- {
- if(strVec[i] == "-")
- if(time == 0)
- {
- intStr += "-";
- j = NUMBERS;
- }
- else
- {
- intStr2 += "-";
- j = NUMBERS;
- }
- else if(time == 0 && count == 0)
- {
- intStr += numbers[j];
- j = NUMBERS;
- }
- else if(time == 1 && count > 0)
- {
- intStr2 += numbers[j];
- j = NUMBERS;
- }
- }
- // read a new string or continue on the current string
- // lese etter en ny string eller fortsettelse på nåværende string
- else if( whitespace[0].compare(strVec[i])==0)
- {
- // read the next character as long as it is a number
- if( time == 0 && strVec[i+1].compare(numbers[j]) && (!w_bool))
- {
- count++;
- time = 1;
- }
- else if( time == 1 && (strVec[i+1].compare(numbers[j])) && (w_bool) && (count != 1))
- {
- time = 0;
- count = 0;
- }
- else
- {
- time = 1, count = 1;
- }
- j=NUMBERS;
- }
- // read the opeartor related to whitespaces etc.
- // lese operatoren i forhold til mellomrom etc.
- else if( operators[k].compare(strVec[i])==0 )
- {
- if(time == 0)
- {
- w_bool = true;
- time=1;
- count=1;
- }
- else if(time == 1)
- {
- w_bool = false;
- }
- j = NUMBERS;
- }
- }
- }
- if(intStr.size() > 0)
- {
- first = atoi(intStr.c_str());
- // cout << "\n\nfirst: " << first;
- }
- if(intStr2.size() > 0)
- {
- second = atoi(intStr2.c_str());
- /* if(intStr.size() > 0)
- cout << ", second: " << second;
- else
- cout << "\n\nsecond: " << second;
- */
- if(intStr.size() > 0 && intStr2.size() > 0)
- o_bool = true;
- }
- if(o_bool)
- {
- c = ( Operator(first, second) ^ (Operator()) );
- if(c.x > 1.7*pow(10.0, 308.0) || c.x < 1.7*pow(10.0, -308.0))
- cout << "\n\nCatching overflow\n\n";
- else
- cout << "\n\n" << first << "^" << second << " = " << c.x;
- }
- running();
- }
- void running()
- {
- cout << "\n\nPress T or t to try again, otherwise any key to quit.\n";
- choice = _getch();
- if(choice == 't' || choice == 'T')
- {
- while(strVec.size() > 0)
- strVec.pop_back();
- first = second = NULL;
- intStr = intStr2 = "";
- time = count = 0;
- _continue = true;
- system("CLS");
- run();
- }
- else
- {
- pointer = NULL;
- while(strVec.size() > 0)
- strVec.pop_back();
- while(upperLetters.size() > 0)
- upperLetters.pop_back();
- while(lowerLetters.size() > 0)
- lowerLetters.pop_back();
- while(specialCharacters.size() > 0)
- specialCharacters.pop_back();
- while(numbers.size() > 0)
- numbers.pop_back();
- while(op.size() > 0)
- op.pop_back();
- pointer = NULL;
- _continue = false;
- }
- }
- int main ()
- {
- for(j=0; j<NUMLETTER; j++)
- {
- upperLetters.push_back(upper[j]);
- total_upper++;
- }
- for(j=0; j<NUMLETTER; j++)
- {
- lowerLetters.push_back(lower[j]);
- total_lower++;
- }
- for(i=0; i<length; i++)
- {
- for(j=0; j<SPECIAL; j++)
- {
- if(strVec[i].compare(special[j]) == 0)
- {
- specialCharacters.push_back(special[j]);
- total_special++;
- }
- }
- }
- for(i=0; i<length; i++)
- {
- for(j=0; j<1; j++)
- {
- if(strVec[i].compare(whitespace[j]) == 0)
- {
- total_whitespace++;
- }
- }
- }
- for(i=0; i<length; i++)
- {
- for(j=0; j<OP_COUNT; j++)
- {
- op.push_back(operators[j]);
- }
- }
- for(i=0; i<NUMBERS; i++)
- {
- numbers.push_back(numbersStr[i]);
- }
- while(_continue)
- {
- run();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement