@ZControls_Games/

NiftyDeafeningMiddleware

C++

No description

fork
loading
Files
  • main.cpp
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <cmath>
#include <cstdio>
#include <bits/stdc++.h>

using namespace std;

#define put(string) cout << string
#define nlput(string) cout << endl << string
#define putnl(string) put(string) << endl
#define nlputnl(string) nlput(string) << endl
#define nlputdnl(string) nlputnl(string) << endl

#define setMinMax(a, b)   \
  if (num1 < 0) {         \
    numMin = a - num1;    \
  } else if (num1 > 0) {  \
    numMax = b - num1;    \
  } break

#define cinReset() cin.clear(); cin.ignore(10000, '\n')

int readNumber(string num, int min, int max, int zeroValid) {
  int r = 0;
  int isValid = false;

  do {
    cinReset();
    
    nlput("Enter your " << num << " number");
    
    nlput("[" << min << " to ");
    if (!zeroValid) put(" -1 and 1 to ");
    putnl(max << "]:");

    cin >> r;

    if (!cin.fail() && (r >= min && r <= max)) {
      if (zeroValid == false) {
        if (r != 0) isValid = true;
      } else
        isValid = true;
    }

    if (!isValid) putnl("Not a valid number.");
  } while (!isValid);

  return r;
}

int main() {
  string name;
  nlputnl("Please enter your name:"); cin >> name;
  nlputnl("Hello " << name);

  const char *operations[] = {
    "Add", "Subtract", "Divide", "Multiply"
  };

  int option;
  while (true) {
    nlputdnl("Would you like to:");
    for (int i = 0; i < 4; ++i) {
      putnl((i + 1) << " - " << operations[i]);
    }
    put(endl);

    int isValid = false;
    do {
      cinReset();
      putnl("Select an operation [1-4]:"); cin >> option;
    
      if (!cin.fail())
        if (option > 0 && option < 5)
          isValid = true;
     
      if (!isValid) nlputnl("Invalid option. ");
    } while (!isValid);

    nlputnl(operations[option-1] << " has been confirmed.");

    int numMin = INT_MIN;
    int numMax = INT_MAX;
    int isZeroValid = true;

    int num1 = readNumber("first", numMin, numMax, isZeroValid);
    switch (option) {
      case 1: setMinMax(INT_MIN, INT_MAX);
      case 2: setMinMax(INT_MAX, INT_MIN);
      case 3: isZeroValid = false; break;
      case 4: {
        int t = INT_MAX / num1;
        t = abs(t);

        numMin = -t;
        numMax = t;
      } break;
    }

    int num2 = readNumber("second", numMin, numMax, isZeroValid);

    switch (option) {
      case 1: num1 += num2; break;
      case 2: num1 -= num2; break;
      case 3: {
        putnl("Integer maths is used. Rounding happens.");
        num1 /= num2;
      } break;
      case 4: num1 *= num2; break;
    }

    nlputnl("Your answer is " << num1);

    putnl("Calculate again? [Y]es or any other quits");
    cinReset();
    int playAgain; cin >> playAgain;
    if (playAgain != 'Y' && playAgain != 'y') break;
  }

  nlputnl("END OF LINE");
  return 0;
}