Expand|Select|Wrap|Line Numbers
- class Juggernaut
- {
- public:
- friend Juggernaut operator +(Juggernaut a, Juggernaut b);
- friend Juggernaut operator -(Juggernaut a, Juggernaut b);
- friend Juggernaut operator *(Juggernaut a, Juggernaut b);
- friend long int operator /(Juggernaut a, Juggernaut b);
- Juggernaut(string a);
- Juggernaut();
- void output();
- int size();
- void reverse();
- bool Negative();
- void setNegative();
- void flip();
- private:
- vector<int> Jugg;
- bool isNeg;
- };
- Juggernaut::Juggernaut()
- {}
- Juggernaut::Juggernaut(string temp)
- {
- int hold = temp[0];
- if(hold != 45) //checks ASCII to see if there is a negative sign in the front
- {
- isNeg = false;
- for(int i = 0; i < temp.length(); i++)
- {
- Jugg.push_back(temp[i] - 48); // the -48 converts ASCII to int
- }
- cout << endl;
- }
- else
- {
- isNeg = true;
- for(int j = 1; j < temp.length(); j++)
- {
- Jugg.push_back(temp[j] - 48);
- }
- cout << endl;
- }
- }
- void Juggernaut::flip()
- {
- Juggernaut temp;
- for(int i=Jugg.size()-1; i>=0; i--)
- {
- temp.Jugg.push_back(Jugg[i]);
- }
- for(int j=0; j<Jugg.size(); j++)
- {
- Jugg[j] = temp.Jugg[j];
- }
- }
- void Juggernaut::reverse()
- {
- if(isNeg == true)
- cout << "-";
- for(int i=Jugg.size()-1; i>=0; i--)
- cout << Jugg[i];
- }
- int Juggernaut::size()
- {
- return (Jugg.size());
- }
- //Precondition: isNeg is set
- //Postcondition: returns isNeg to non friendly functions use
- bool Juggernaut::Negative()
- {
- return isNeg;
- }
- void Juggernaut::setNegative()
- {
- isNeg = true;
- }
- void Juggernaut::output()
- {
- if(isNeg == true)
- cout << "-";
- for(int i=0; i<Jugg.size(); i++)
- cout << Jugg[i];
- }
- class menu
- {
- public:
- menu();
- void output();
- void implement();
- void getNums();
- private:
- int choice;
- Juggernaut a;
- Juggernaut b;
- Juggernaut c;
- };
- menu::menu()
- {
- choice = 0;
- }
- void menu::getNums()
- {
- string temp, temp2;
- cout << "\nEnter the first number: ";
- cin >> temp;
- a = Juggernaut(temp);
- cout << "Enter the second number: ";
- cin >> temp2;
- b = Juggernaut(temp2);
- }
- void menu::output()
- {
- cout << "Welcome to the JuggernautInteger Calculator!";
- while(choice !=5)
- {
- cout << "\n\n1. addition\n2. subtraction\n3. multiplication\n4. division\n5. exit\n\n"
- << "Please make a selection: ";
- cin >> choice;
- implement();
- }
- cout << "\nSee Ya!";
- }
- Juggernaut operator +(Juggernaut a, Juggernaut b)
- {
- Juggernaut c;
- int carry = 0;//holds the carry over from one int to the next
- if(a.size() == b.size())//This is to check numbers of the same length
- {
- for(int i = (a.size()-1); i>=0 ; i--)
- {
- if((a.Jugg[i] + b.Jugg[i] + carry)<10) // this is if there is no carry needed
- {
- c.Jugg.push_back(a.Jugg[i]+b.Jugg[i] + carry);
- carry = 0;
- }
- else
- {
- c.Jugg.push_back((a.Jugg[i]+b.Jugg[i] + carry)-10);
- carry = 1;
- }
- }
- if(carry == 1)//outputs carry if the is nothing for it to be added to.
- c.Jugg.push_back(carry);
- }
- else if(a.size() > b.size())//checks to see if a is longer the b
- {
- int j = a.size()-1;
- for(int i = b.size()-1; i>=0; i--)
- {
- if((a.Jugg[j] + b.Jugg[i] + carry)<10)
- {
- c.Jugg.push_back(a.Jugg[j]+b.Jugg[i] + carry);
- carry = 0;
- }
- else
- {
- c.Jugg.push_back((a.Jugg[j]+b.Jugg[i] + carry)-10);
- carry = 1;
- }
- j--;
- }
- while(j>=0)
- {
- c.Jugg.push_back(a.Jugg[j]+carry);
- carry = 0;
- j--;
- }
- }
- else
- {
- int j = b.size()-1;
- for(int i = a.size()-1; i>=0; i--)
- {
- if((a.Jugg[i] + b.Jugg[j] + carry)<10)
- {
- c.Jugg.push_back(a.Jugg[i]+b.Jugg[j] + carry);
- carry = 0;
- }
- else
- {
- c.Jugg.push_back((a.Jugg[i]+b.Jugg[j] + carry)-10);
- carry = 1;
- }
- j--;
- }
- while(j>=0)
- {
- c.Jugg.push_back(b.Jugg[j]+carry);
- carry = 0;
- j--;
- }
- }
- return c;
- }
- Juggernaut operator -(Juggernaut a, Juggernaut b)
- {
- Juggernaut c;
- char smaller = 'Z';
- int i = 0;
- if(a.size() == b.size())//When they are the same size this bit figures out which is bigger
- { // because it makes a difference in the sign given
- while(smaller == 'Z' && i < a.size())
- {
- if(a.Jugg[i] > b.Jugg[i])
- smaller = 'B';
- else if(a.Jugg[i] < b.Jugg[i])
- smaller = 'A';
- else
- i++;
- }
- if(smaller == 'Z')
- c.Jugg.push_back(0);
- }
- if(a.size()>b.size() || smaller == 'B')
- {
- int j = a.size()-1;
- for(int i = b.size()-1; i >=0; i--)
- {
- if(a.Jugg[j] >= b.Jugg[i])
- c.Jugg.push_back(a.Jugg[j]-b.Jugg[i]);
- else
- {
- bool cont = true;
- int k =1;
- while(cont == true)
- {
- if(a.Jugg[j-k] != 0)
- {
- a.Jugg[j-k] = a.Jugg[j-k]-1;
- cont = false;
- }
- else
- {
- a.Jugg[j-k] = 9;
- k++;
- }
- }
- a.Jugg[j] = a.Jugg[j] + 10;
- c.Jugg.push_back(a.Jugg[j] - b.Jugg[i]);
- }
- j--;
- }
- while(j>=0)
- {
- c.Jugg.push_back(a.Jugg[j]);
- j--;
- }
- }
- else if(a.size()<b.size()|| smaller == 'A')
- {
- c.isNeg = true;
- int j = b.size()-1;
- for(int i = a.size()-1; i >=0; i--)
- {
- if(b.Jugg[j] >= a.Jugg[i])
- c.Jugg.push_back(b.Jugg[j]-a.Jugg[i]);
- else
- {
- bool cont = true;
- int k =1;
- while(cont == true)
- {
- if(b.Jugg[j-k] != 0)
- {
- b.Jugg[j-k] = b.Jugg[j-k]-1;
- cont = false;
- }
- else
- {
- b.Jugg[j-k] = 9;
- k++;
- }
- }
- b.Jugg[j] = b.Jugg[j] + 10;
- c.Jugg.push_back(b.Jugg[j] - a.Jugg[i]);
- }
- j--;
- }
- while(j>=0)
- {
- c.Jugg.push_back(b.Jugg[j]);
- j--;
- }
- }
- return c;
- }
- //Precondition: takes in a, b which are user defined
- //Postcondition: returns and anonymous Juggernaut that as the values of a*b
- Juggernaut operator *(Juggernaut a, Juggernaut b)
- {
- char smaller = 'Z';
- long int counter = 0;
- int i = 0;
- Juggernaut sum = Juggernaut("0");
- while(smaller == 'Z' && i < a.size())
- {
- if(a.Jugg[i] > b.Jugg[i])
- smaller = 'B';
- else if(a.Jugg[i] < b.Jugg[i])
- smaller = 'A';
- else
- i++;
- }
- if(a.size() >= b.size() || smaller == 'B')
- {
- for(int i = 0; i<b.size(); i++)
- counter += (b.Jugg[i] * pow(10, (b.size()-1-i)));
- while(counter > 0)
- {
- sum = sum + a;
- counter --;
- sum.flip();
- }
- }
- else if(a.size() < b.size() || smaller == 'A')
- {
- for(int i=0; i<a.size(); i++)
- counter += (a.Jugg[i] * pow(10, (a.size()-1-i)));
- while(counter > 0)
- {
- sum = sum + b;
- counter --;
- sum.flip();
- }
- }
- return sum;
- }
- //Precondition: takes in user defined Juggernauts a and b
- //Postcondition: returns Juggernaut object with the values of a/b
- long int operator /(Juggernaut a, Juggernaut b)
- {
- char smaller = 'Z';
- long int counter = 0;
- int i = 0;
- Juggernaut temp;
- while(smaller == 'Z' && i < a.size())
- {
- if(a.Jugg[i] > b.Jugg[i])
- smaller = 'B';
- else if(a.Jugg[i] < b.Jugg[i])
- smaller = 'A';
- else
- i++;
- }
- if(a.size() >= b.size() || smaller == 'B')
- {
- temp = a;
- bool Help = temp.isNeg;
- cout << temp.isNeg;
- while(Help == false)
- {
- temp = temp - b;
- Help = temp.Negative();
- counter ++;
- temp.flip();
- temp.output();
- }
- }
- else if(a.size() < b.size() || smaller == 'A')
- {
- temp = b;
- while(b.Negative() == false)
- {
- temp.flip();
- temp = temp - a;
- counter ++;
- }
- }
- cout << "escape";
- return counter;
- }
- //Precondition: this is called after choice has been defined
- void menu::implement()
- {
- if(choice == 1)//adds numbers, how this is done depends on the signs of the given numbers
- {
- getNums(); // ask user for values
- if(a.Negative()==true && b.Negative() == true)
- {
- c=(a+b);//-a+-b
- c.setNegative();// adding to negatives gives you a negative
- }
- else if(a.Negative() == true)
- {
- c=b-a; //-a+b
- }
- else if(b.Negative()==true)
- {
- c=a-b; // a+-b
- }
- else
- c = a+b;
- cout << "Your result is: ";
- c.reverse(); //c is backwards so this flips them and displays them properly
- }
- else if(choice == 2)
- {
- getNums();
- if(a.Negative()==true && b.Negative()==true)
- c=b-a; // -a--b
- else if(a.Negative()==true)
- {
- c=a+b;//-a-b
- c.setNegative();//subtracting a negative with a positive gives you a super negative
- }
- else if(b.Negative()==true)
- c=a+b;//a--b
- else
- c = a-b;
- cout << "Your result is: ";
- c.reverse();//c is still backwards so flip it
- }
- else if(choice ==3)
- {
- getNums();
- c=a*b;
- if((a.Negative()==true && b.Negative()==false) || (a.Negative() == false && b.Negative() == true))
- c.setNegative();
- cout << "Your result is: ";
- c.output();
- }
- else if(choice==4)
- {
- long int count;
- getNums();
- count=a/b;
- cout << "Your result is: " << count;
- }
- else if(choice==5){} // if choice is five then nothing happens and when it returns to menu
- else //the program ends
- cout << "\nInvailid Entry, Try again."; //error message
- }
- //Precondition: Nothing has happened yet
- //Postcondition:It makes the program kick off and go
- int main()
- {
- menu New; //creates a new menu
- New.output(); // starts the interface
- }