Skip to content

Commit 8af44b2

Browse files
author
Quincy
authored
Create main.cpp
1 parent 590e0f9 commit 8af44b2

File tree

1 file changed

+255
-0
lines changed

1 file changed

+255
-0
lines changed

4.0/main.cpp

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
#include <iostream>
2+
#include <cmath>
3+
#include <string>
4+
#include <vector>
5+
#include <fstream>
6+
#include "calculate.h"
7+
8+
double calculation(double numA, double numB, char oper);
9+
static double question();
10+
calculate cal;
11+
std::string fileName = "history.txt";
12+
extern std::ofstream writeHistory;
13+
std::ifstream readHistory;
14+
int HistoryShow();
15+
double IntroductionInput();
16+
17+
int mainMenu()
18+
{
19+
int choice;
20+
std::cout <<"\tMAIN MENU\n\n"<< "1. Use Calculator.\n" << "2. Show History.\n" << "3. Exit\n";
21+
while (true)
22+
{
23+
std::cout << "What is your choice? (0 - 2) ";
24+
std::cin >> choice;
25+
switch (choice)
26+
{
27+
case 1:
28+
system("cls");
29+
IntroductionInput();
30+
break;
31+
case 2:
32+
system("cls");
33+
HistoryShow();
34+
case 3:
35+
writeHistory.close();
36+
readHistory.close();
37+
exit(0);
38+
default:
39+
break;
40+
}
41+
}
42+
}
43+
44+
void Introduction()
45+
{
46+
std::cout << "\n\tHow to use the calculator: \n\n"<<"Just simply enter the first number of your problem then the operator and then the second number of your problem." << std::endl;
47+
std::cout << "Operators are: \"+ for adding, - for subtracting, * for multiplying, / for dividing, ^ for power, s for square root.\"" << std::endl << "\n";
48+
}
49+
50+
int HistoryShow()
51+
{
52+
int choiceHistory;
53+
std::string readHistoryTo;
54+
if (!readHistory.is_open())
55+
{
56+
readHistory.open(fileName);
57+
}
58+
else if(readHistory.is_open())
59+
{
60+
readHistory.close();
61+
readHistory.open(fileName);
62+
}
63+
while (!readHistory.eof())
64+
{
65+
std::getline(readHistory, readHistoryTo);
66+
std::cout << readHistoryTo<<std::endl;
67+
}
68+
while (true)
69+
{
70+
std::cout << "\n1. Clear History\n" << "2. Return to main menu\n\n";
71+
std::cout << "Your Choice? (1 - 2) ";
72+
std::cin >> choiceHistory;
73+
switch (choiceHistory)
74+
{
75+
case 1:
76+
if (writeHistory.is_open())
77+
{
78+
writeHistory.close();
79+
writeHistory.open(fileName, std::ios::out | std::ios::trunc);
80+
}
81+
else
82+
{
83+
writeHistory.open(fileName, std::ios::out | std::ios::trunc);
84+
}
85+
std::cout << "\nHistory Deleted!!\n";
86+
system("pause");
87+
system("cls");
88+
return mainMenu();
89+
case 2:
90+
system("cls");
91+
return mainMenu();
92+
break;
93+
default:
94+
if (!isdigit(choiceHistory))
95+
{
96+
std::cout << "\nINVALID INPUT\n";
97+
std::cout << "\nYou entered a letter. Only NUMBERS are allowed. To prevent computer damage, we will return you to main menu\n";
98+
system("pause");
99+
system("cls");
100+
return mainMenu();
101+
}
102+
else if (isdigit(choiceHistory))
103+
{
104+
std::cout << "\nINVALID INPUT\n";
105+
}
106+
107+
break;
108+
}
109+
}
110+
}
111+
112+
double IntroductionInput()
113+
{
114+
double numA;
115+
double numB;
116+
char oper;
117+
bool CINFail = std::cin.fail();
118+
Introduction();
119+
std::cout << "Enter the first number: ";
120+
std::cin >> numA;
121+
try
122+
{
123+
if (std::cin.fail())
124+
{
125+
throw 1;
126+
}
127+
}
128+
catch (int error)
129+
{
130+
system("cls");
131+
std::cout << "ERROR NUMBER: " << error << std::endl;
132+
system("pause");
133+
return error;
134+
}
135+
std::cout << std::endl;
136+
std::cout << "Enter the operator: ";
137+
std::cin >> oper;
138+
std::cout << std::endl;
139+
if (oper == 's')
140+
{
141+
std::cin.ignore();
142+
return calculation(numA, 0, oper);
143+
}
144+
else
145+
{
146+
std::cout << "Enter the second number: ";
147+
std::cin >> numB;
148+
try
149+
{
150+
if (std::cin.fail())
151+
{
152+
throw 1;
153+
}
154+
}
155+
catch (int error)
156+
{
157+
system("cls");
158+
std::cout << "ERROR NUMBER: " << error << std::endl;
159+
system("pause");
160+
exit(error);
161+
}
162+
std::cout << std::endl;
163+
return calculation(numA, numB, oper);
164+
}
165+
}
166+
167+
int main()
168+
{
169+
writeHistory.open(fileName, std::ios::app);
170+
mainMenu();
171+
}
172+
173+
double calculation(double numA, double numB, char oper)
174+
{
175+
if (writeHistory.is_open())
176+
{
177+
if (oper == '+' || oper == '-' || oper == '*' || oper == '/' || oper == '^' || oper == 's')
178+
{
179+
if (oper == '+')
180+
{
181+
std::cout << cal.add(numA, numB);
182+
}
183+
else if (oper == '-')
184+
{
185+
std::cout << cal.subtract(numA, numB);
186+
}
187+
else if (oper == '*')
188+
{
189+
std::cout << cal.multiply(numA, numB);
190+
}
191+
else if (oper == '/')
192+
{
193+
std::cout << cal.divide(numA, numB);
194+
195+
}
196+
else if (oper == '^')
197+
{
198+
std::cout << cal.power(numA, numB);
199+
}
200+
else if (oper == 's')
201+
{
202+
std::cout << cal.square(numA, numB);
203+
}
204+
return question();
205+
}
206+
else
207+
{
208+
std::cout << "\nYou have put in an invalid operator/number!\n\a";
209+
return question();
210+
}
211+
}
212+
else
213+
{
214+
try
215+
{
216+
throw "ERROR WRITING HISTORY TO FILE";
217+
}
218+
catch (const char* e)
219+
{
220+
system("cls");
221+
std::cout << e << std::endl;
222+
}
223+
}
224+
}
225+
226+
double question()
227+
{
228+
while (true)
229+
{
230+
std::string yn;
231+
std::cout << "\nDo you want to use the calculator again? ";
232+
std::cin >> yn;
233+
std::cout << std::endl;
234+
if (yn == "Yes" || yn == "No" || yn == "YES" || yn == "NO" || yn == "YEs" || yn == "nO" || yn == "yes" || yn == "no")
235+
{
236+
if (yn == "Yes" || yn == "YES" || yn == "YEs" || yn == "yes")
237+
{
238+
return IntroductionInput();
239+
}
240+
else if (yn == "No" || yn == "NO" || yn == "nO" || yn == "no")
241+
{
242+
writeHistory.close();
243+
writeHistory.open(fileName, std::ios::app);
244+
system("cls");
245+
mainMenu();
246+
}
247+
}
248+
else
249+
{
250+
std::cout << "Invalid answer, try again.\a\n";
251+
}
252+
}
253+
254+
255+
}

0 commit comments

Comments
 (0)