Getting command-line user inputs and dealing with stacks (push and pop) in C++


Running

Source Code

main.cpp

#include <iostream>
#include <stack>
#include "subtract.h"

int main(int argc, char** argv)
{
  Subtract sub;
  sub.getInputs();
	sub.insertPrecedingZeros();
	sub.fillStacks();
	sub.subtract();
	sub.display();
  return 0;
}

subtract.h

#ifndef SUBTRACT_H
#define SUBTRACT_H
#include <stack>
#include <iostream>
using namespace std;

class Subtract
{
  private:
    stack<int> num1;
    stack<int> num2;
    stack<int> result;
    string firstNum;
    string secondNum;
    string firstNumBackup;
    string secondNumBackup;
    bool negate;
    int getIntFromChar(char c);
    char getCharFromInt(int i);
    void negateIfNecessary();
    string getReverse(string s);
  public:
    Subtract();
    void insertPrecedingZeros();
    void fillStacks();
    void display();
    void getInputs();
    void subtract();
};

#endif

subtract.cpp

#include <string>
#include <iostream>
#include <string>
#include "subtract.h"
using namespace std;

Subtract::Subtract()
{
}

int Subtract::getIntFromChar(char c)
{
  int i;
  switch(c)
  {
    case '0':
      i = 0;
      break;
    case '1':
      i = 1;
      break;
    case '2':
      i = 2;
      break;
    case '3':
      i = 3;
      break;
    case '4':
      i = 4;
      break;
    case '5':
      i = 5;
      break;
    case '6':
      i = 6;
      break;
    case '7':
      i = 7;
      break;
    case '8':
      i = 8;
      break;
    case '9':
      i = 9;
      break;
  }
  return i;
}

char Subtract::getCharFromInt(int i)
{
  char c;
  switch(i)
  {
    case 0:
      c = '0';
      break;
    case 1:
      c = '1';
      break;
    case 2:
      c = '2';
      break;
    case 3:
      c = '3';
      break;
    case 4:
      c = '4';
      break;
    case 5:
      c = '5';
      break;
    case 6:
      c = '6';
      break;
    case 7:
      c = '7';
      break;
    case 8:
      c = '8';
      break;
    case 9:
      c = '9';
      break;
  }
  return c;
}

void Subtract::negateIfNecessary()
{
  if (negate)
  {
    int x = num1.size();
    for (int i = 0; i <= x; i++)
    {
      num1.pop();
    }
    x = num2.size();
    for (int i = 0; i < x; i++)
    {
      num2.pop();
    }
    x = result.size();
    for (int i = 0; i < x; i++)
    {
      result.pop();
    }
    firstNum = secondNumBackup;
    secondNum = firstNumBackup;
    insertPrecedingZeros();
    fillStacks();
    subtract();
    firstNum.swap(secondNum);
  }
}

string Subtract::getReverse(string s)
{
  string str;
  for (int i = s.size() - 1; i >= 0; i--)
  {
    str += s[i];
  }
  return str;
}

void Subtract::insertPrecedingZeros()
{
  if (firstNum.length() != secondNum.length())
  {
    int t = secondNum.length() - firstNum.length();
    if (t < 0) t *= -1;
    for (int i = 0; i < t; i++)
    {
      if (firstNum.length() < secondNum.length())
      {
        firstNum = "0" + firstNum;
      }
      else
      {
        secondNum = "0" + secondNum;
      }
    }
  }
}

void Subtract::fillStacks()
{
  int x;
  for (int i = firstNum.length() - 1; i >= 0; i--)
  {
    x = getIntFromChar(firstNum[i]);
    num1.push(x);
  }
  for (int i = secondNum.length() - 1; i >= 0; i--)
  {
    x = getIntFromChar(secondNum[i]);
    num2.push(x);
  }
}

void Subtract::display()
{
  cout << firstNum << " - " << secondNum << " = ";
  int top;
  string resultString;
  while (!result.empty())
  {
    resultString = resultString + getCharFromInt(result.top());
    result.pop();
  }
  resultString = getReverse(resultString);
  if (negate) resultString = "-" + resultString;
  cout << resultString;
}

void Subtract::getInputs()
{
  cout << "Enter first #: ";
  cin >> firstNum;
  firstNumBackup = firstNum;
  cout << "Enter second #: ";
  cin >> secondNum;
  secondNumBackup = secondNum;
}

void Subtract::subtract()
{
  int difference, z;
  int x, y;
  while (!num1.empty())
  {
    x = num1.top();
    num1.pop();
    y = 0;
    if (!num2.empty())
    {
      y = num2.top();
      num2.pop();
    }
    difference = x - y;
    if (difference < 0)
    {
      if (num1.empty())
      {
        negate = true;
      }
      else
      {
        z = num1.top();
        num1.pop();
        z--;
        num1.push(z);
        difference += 10;
        result.push(difference);
      }
    }
    else
    {
      result.push(difference);
    }
  }
  negateIfNecessary();
}
,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: