8000 Create the first unix example · Benjam007/C-CPP-Programming@b235a6a · GitHub
[go: up one dir, main page]

Skip to content

Commit b235a6a

Browse files
Create the first unix example
1 parent adbb86f commit b235a6a

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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)

0 commit comments

Comments
 (0)
0