Simple Calculator with command-line GUI in C++


Running

Source Code

main.cpp

#include <iostream>
#include <stdlib.h>
#include <sstream>
#include "display.h"
#include "value.h"
/*
Checklist:
Display (done)
0-9, +, -, /, X, =, ., ON, OFF, C (done)
C button always resets display to 0 (done)
Calculator initially off (done)
When off, calculator has nothing on display (done)
When turned on, calculator shows 0 (done)
No operator displayed on display (done)
Ignore unexpected buttons (done)
*/
using namespace std;
void displayLine()
{
  cout << endl;
  for (int x = 0; x < 31; x++)
  {
    cout << (char)178;
  }
}
int main(int argc, char** argv)
{
  char lastSymbol, lastMathOperation;
  string response1, response2;
  Display display;
  Value value;
  double newValue;
  bool firstGo = true;
  system("cls");
  displayLine();
  cout << "\n" << (char)178 << "      Simple Calculator      " << (char)178;
  displayLine();
  cout << endl << (char)178 << "  C  " << (char)178 << "  1  " << (char)178 << "  2  " << (char)178 << "  +  " << (char)178 << " ON  " << (char)178;
  displayLine();
  cout << endl << (char)178 << "  3  " << (char)178 << "  4  " << (char)178 << "  5  " << (char)178 << "  -  " << (char)178 << " OFF " << (char)178;
  displayLine();
  cout << endl << (char)178 << "  6  " << (char)178 << "  7  " << (char)178 << "  8  " << (char)178 << "  /  " << (char)178 << "  =  " << (char)178;
  displayLine();
  cout << endl << (char)178 << "  9  " << (char)178 << "  0  " << (char)178 << "  .  " << (char)178 << "  X  ";
  for (int x = 0; x < 7; x++)
  {
    cout << (char)178;
  }
  displayLine();
  cout << "\n\nN = Turn calc on\nM = Turn calc off\n\n";
  while(true)
  {
    cout << "Display: " << (char)34;
    if (display.isCalcOn()) cout << display.getLine();
    cout << (char)34 << "\n";
    if (lastSymbol != '=')
    {
      cout << "Enter #: ";
      cin >> response1;
      if (!display.isCalcOn() && response1 != "N" && response1 != "n") continue;
    }
    if (response1 == "N" || response1 == "n")
    {
      firstGo = true;
      display.turnOn();
      continue;
    }
    else if (response1 == "M" || response1 == "m")
    {
      display.turnOff();
      continue;
    }
    else if (response1 == "C" || response1 == "c")
    {
      firstGo = true;
      lastMathOperation = lastSymbol = 'Z';
      display.resetDisplay();
      value.setValue(0);
      continue;
    }
    cout << "+, -, X, /, =, C, N = turn on, M = turn off --> ";
    cin >> response2;
    newValue = atof(response1.c_str());
    if (firstGo)
    {
      value.setValue(newValue);
      display.setLine(response1);
      firstGo = false;
    }
    if (response2 == "C" || response2 == "c")
    {
      firstGo = true;
      lastMathOperation = lastSymbol = 'Z';
      display.resetDisplay();
      value.setValue(0);
    }
    else if (response2 == "+")
    {
      lastMathOperation = lastSymbol = '+';
    }
    else if (response2 == "-")
    {
      lastMathOperation = lastSymbol = '-';
    }
    else if (response2 == "/")
    {
      lastMathOperation = lastSymbol = '/';
    }
    else if (response2 == "X" || response2 == "x" || response2 == "*")
    {
      lastMathOperation = lastSymbol = 'X';
    }
    else if (response2 == "N" || response2 == "n")  //ON
    {
      firstGo = true;
      lastSymbol = 'Z';
      display.turnOn();
    }
    else if (response2 == "M" || response2 == "m")  //OFF
    {
      display.turnOff();
    }
    else if (response2 == "=")
    {
      switch(lastMathOperation)
      {
        case '+':
          value.setValue(value.getValue() + newValue);
          break;
        case '-':
          value.setValue(value.getValue() - newValue);
          break;
        case '/':
          value.setValue(value.getValue() / newValue);
          break;
        case 'X':
          value.setValue(value.getValue() * newValue);
      }
      lastSymbol = '=';
      ostringstream s;
      s << value.getValue();
      display.setLine(s.str());
    }
  }
	return 0;
}

display.h

#ifndef DISPLAY_H
#define DISPLAY_H
#include <iostream>
using namespace std;
class Display
{
	public:
	  Display();
	  void resetDisplay();
	  void turnOff();
	  void turnOn();
	  bool isCalcOn();
	  string getLine();
	  void setLine(const string l);
	private:
    string line;
	  bool isOn;
};
#endif

display.cpp

#include "display.h"
Display::Display()
{
  resetDisplay();
  isOn = false;
}

void Display::resetDisplay()
{
  line = "0";
}

void Display::turnOff()
{
  isOn = false;
}

void Display::turnOn()
{
  resetDisplay();
  isOn = true;
}

bool Display::isCalcOn()
{
  return isOn;
}

string Display::getLine()
{
  return line;
}

void Display::setLine(const string l)
{
  line = l;
}

value.h

#ifndef VALUE_H
#define VALUE_H
#include <iostream>
using namespace std;
class Value
{
	public:
	  double getValue();
	  void setValue(const double v);
  private:
	  double value;
};
#endif

value.cpp

#include "value.h"

double Value::getValue()
{
  return value;
}

void Value::setValue(const double v)
{
  value = v;
}
,

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: