11// Include libraries for the calculator program
2- #include < iostream>
3- #include < string>
4- #include < stdexcept>
5- #include < exception>
6- #include < vector>
7- #include < utility>
8- #include " calculator.hpp"
2+ #include < iostream> // For output to the user
3+ #include < sstream> // For converting whitespace into elements for a std::vector
4+ #include < string> // For std::string (used a lot in this program)
5+ #include < vector> // For std::vector (storing numbers yet to be calculated)
6+ #include < utility>
7+ #include " calculator.hpp" // For the actual Calculating meat
98
109// Include additional libraries for the Switch program
11- #include < cstdio>
12- #include < cstdlib>
13- #include < switch.h>
14-
15- char * SoftwareKeyboard (Result rc, const char * guideText) {
16- SwkbdConfig kbd;
17- char * tmpoutstr;
18-
19- rc = swkbdCreate (&kbd, 0 );
20- // printf("swkbdCreate(): 0x%x\n", rc);
21- if (R_SUCCEEDED (rc)) {
22- // Select a Preset to use, if any.
23- swkbdConfigMakePresetDefault (&kbd);
24-
25- // Optional, set any text if you want (see swkbd.h).
26- // swkbdConfigSetOkButtonText(&kbd, "Submit");
27- // swkbdConfigSetLeftOptionalSymbolKey(&kbd, "a");
28- // swkbdConfigSetRightOptionalSymbolKey(&kbd, "b");
29- // swkbdConfigSetHeaderText(&kbd, "Header");
30- // swkbdConfigSetSubText(&kbd, "Sub");
31- swkbdConfigSetGuideText (&kbd, guideText);
32-
33- // Optional, can be removed if not using TextCheck.
34-
35- // Set the initial string if you want.
36- // swkbdConfigSetInitialText(&kbd, "Initial");
37-
38- // You can also use swkbdConfigSet*() funcs if you want.
39-
40- // printf("Running swkbdShow...\n");
41- rc = swkbdShow (&kbd, tmpoutstr, sizeof (tmpoutstr));
42- // printf("swkbdShow(): 0x%x\n", rc);
43-
44- /* if (R_SUCCEEDED(rc)) {
45- printf("out str: %s\n", tmpoutstr);
46- }*/
47- swkbdClose (&kbd);
48- }
49-
50- return tmpoutstr;
51- }
52-
53- void calculateVectorInts (std::string operation, MathCalculator& calculator, Result& rc) {
54- std::string number;
55- char * tmpoutstr;
56- bool inputComplete = false ;
57-
58- while (!inputComplete) {
59- tmpoutstr = SoftwareKeyboard (rc, " Enter one number at a time (q to stop)" );
60- number = std::string (tmpoutstr);
61-
62- if (number == " q" ) {
63- inputComplete = true ;
64- } else {
65- if (calculator.contains_number (number)) {
66- calculator.setVector (std::stoi (number));
67- } else inputComplete = true ;
68- }
69-
70- int answer = calculator.CalculateMoreInt (operation);
71- std::cout << " The answer is: " << answer << " \n " ;
72- }
73- }
74-
75- void calculateTwoInts (std::string operation, MathCalculator& calculator, Result& rc) {
76- std::string number1;
77- std::string number2;
78-
79- char * tmpoutstr = SoftwareKeyboard (rc, " Enter two numbers with a space in-between" );
80- std::string keyboardInput (tmpoutstr);
81-
82- int pos = keyboardInput.find (" " );
83-
84- number1 = keyboardInput.substr (0 , pos);
85- number2 = keyboardInput.substr (pos + 1 );
86-
87- int num1;
88- int num2;
89-
90- if (calculator.contains_number (number1) && calculator.contains_number (number2)) {
91- num1 = std::stoi (number1);
92- num2 = std::stoi (number2);
93-
94- int answer = calculator.CalculateInt (operation, num1, num2);
95-
96- std::cout << " The answer to " << num1 << " " << operation << " " << num2 << " is " << answer << " \n " ;
97- } else return ;
98- }
10+ #include < cstdio> // C library (might be unused)
11+ #include < cstdlib> // C library (might be unused)
12+ #include < switch.h> // The important header for working the Switch
9913
10014int main (int argc, char * argv[]) {
10115 consoleInit (NULL );
@@ -109,13 +23,12 @@ int main(int argc, char* argv[]) {
10923
11024 Result rc = 0 ;
11125
112- std::vector<int > StoredInts{};
26+ std::vector<float > vec;
27+ std::string operation;
28+ char tmpoutstr[16 ] = {0 };
11329
114- MathCalculator calculator{StoredInts};
115- std::string operation{};
116-
117- std::cout << " Press up for Addition, \n down for Subtraction, \n left for Mutiplication, \n and right for Division." << " \n " ;
118- std::cout << " L to Calculate, Plus to exit" << " \n " ;
30+ std::cout << " Press up for Addition, \n down for Subtraction, \n left for Mutiplication, \n and right for Division." << std::endl;
31+ std::cout << " L to Calculate, Plus to exit" << std::endl;
11932
12033 // Main loop
12134 while (appletMainLoop ())
@@ -149,17 +62,52 @@ int main(int argc, char* argv[]) {
14962 }
15063
15164 if (kDown & HidNpadButton_L && !operation.empty ()) {
152- std::cout << " A for calculating more than 2 numbers, B for calculating only 2 numbers\n " ;
153- if (kDown & HidNpadButton_A) {
154- calculateVectorInts (operation, calculator, rc);
155- } else if (kDown & HidNpadButton_B) {
156- calculateTwoInts (operation, calculator, rc);
65+ std::string fullNums;
66+
67+ SwkbdConfig kbd;
68+ rc = swkbdCreate (&kbd, 0 );
69+
70+ if (R_SUCCEEDED (rc)) {
71+ // Select a Preset to use, if any.
72+ swkbdConfigMakePresetDefault (&kbd);
73+ // swkbdConfigMakePresetPassword(&kbd);
74+ // swkbdConfigMakePresetUserName(&kbd);
75+ // swkbdConfigMakePresetDownloadCode(&kbd);
76+
77+ // Optional, set any text if you want (see swkbd.h).
78+ // swkbdConfigSetOkButtonText(&kbd, "Submit");
79+ // swkbdConfigSetLeftOptionalSymbolKey(&kbd, "a");
80+ // swkbdConfigSetRightOptionalSymbolKey(&kbd, "b");
81+ // swkbdConfigSetHeaderText(&kbd, "Header");
82+ // swkbdConfigSetSubText(&kbd, "Sub");
83+ swkbdConfigSetGuideText (&kbd, " Enter all numbers with a space in-between: " );
84+
85+ // Set the initial string if you want.
86+ // swkbdConfigSetInitialText(&kbd, "Initial");
87+
88+ // You can also use swkbdConfigSet*() funcs if you want.
89+
90+ // printf("Running swkbdShow...\n");
91+ rc = swkbdShow (&kbd, tmpoutstr, sizeof (tmpoutstr));
92+ // printf("swkbdShow(): 0x%x\n", rc);
93+
94+ swkbdClose (&kbd);
15795 }
15896
97+ std::istringstream iss (tmpoutstr);
98+ std::string Num;
99+
100+ while (iss >> Num) {
101+ vec.emplace_back (std::stof (Num));
102+ }
103+
104+ std::cout << " The answer is: " << Calculator::solve (vec, operation) << std::endl;
105+
159106 // I just noticed that the string below says \nand at some point
160107 std::cout << " Press up for Addition, \n down for Subtraction, \n left for Mutiplication, \n and right for Division." << " \n " ;
161108 std::cout << " L to Calculate, Plus to exit" << " \n " ;
162109
110+ vec.clear ();
163111 }
164112
165113 // std::cout << operation << "\n";
0 commit comments