File tree Expand file tree Collapse file tree 3 files changed +54
-0
lines changed
Unix_System_examples/Basic Calculator (TUI) Expand file tree Collapse file tree 3 files changed +54
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Basic Calculator (TUI)
2
+
3
+ ## Compile:
4
+ Use the ` m̀ake ` command to compile
5
+
6
+ ## Usage
7
+ You have 4 operatios ` plus ` ` minus ` ` times ` ` devide `
8
+
9
+ ```
10
+ # The hardest math problem.
11
+ ./calculator 6 times 3
12
+ ```
13
+
14
+ the output should be a simple ` 18 `
15
+
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < string>
3
+ #include < sstream>
4
+
5
+ // Parse Float
6
+ float parseFloat ( const std::string &str ) {
7
+
8
+ std::stringstream ss (str);
9
+ float num;
10
+ ss >> num;
11
+ return num;
12
+
13
+ }
14
+
15
+
16
+ int main ( int argc, char ** argv ) {
17
+
18
+ const std::string Operation (argv[2 ]); // take the second argument before the "./calculator"
19
+
20
+ if (Operation == " plus" ) std::cout << parseFloat (argv[1 ]) + parseFloat (argv[3 ]) << ' \n ' ; // Plus
21
+ else if (Operation == " minus" ) std::cout << parseFloat (argv[1 ]) - parseFloat (argv[3 ]) << ' \n ' ; // Minus
22
+ else if (Operation == " times" ) std::cout << parseFloat (argv[1 ]) * parseFloat (argv[3 ]) << ' \n ' ; // Times
23
+ else if (Operation == " divide" ) std::cout << parseFloat (argv[1 ]) / parseFloat (argv[3 ]) << ' \n ' ; // Divide
24
+ else std::cout << " Invalid operation\n " ; // Invalid operation message
25
+
26
+ return 0 ;
27
+
28
+ }
Original file line number Diff line number Diff line change
1
+ TARGET = calculator
2
+ FILE = main.cpp
3
+ COMPILER = /usr/bin/g++
4
+
5
+ .PHONY : all clean
6
+
7
+ all :
8
+ $(COMPILER ) $(FILE ) -o $(TARGET )
9
+
10
+ clean :
11
+ rm -rf $(TARGET )
You can’t perform that action at this time.
0 commit comments