-
Notifications
You must be signed in to change notification settings - Fork 164
feat: do a main operator with user input #459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LeottaAlberto
wants to merge
4
commits into
UNICT-Quality-Development:main
Choose a base branch
from
LeottaAlberto:calculator_LA
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+81
−0
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1a5c0a9
feat: do a main operator with user input
LeottaAlberto 56d8bca
style: erase whitespace
LeottaAlberto 4da429a
refactor(calculator.cpp): add namespace and refactor insertNumber's f…
LeottaAlberto a72a67c
style(calculator.cpp): improve readability and naming consistency
LeottaAlberto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,3 +10,81 @@ | |
| Multiplication: 8 | ||
| Division: 2 | ||
| */ | ||
|
|
||
| #include <iostream> | ||
| #include <string> | ||
|
|
||
| using namespace std; | ||
|
|
||
| const string ERROR_COLOR = "\033[31m"; | ||
| const string NORMAL_COLOR = "\033[0m"; | ||
|
|
||
| const int ZERO = 48; | ||
| const int NINE = 57; | ||
| const int MAX_DIGIT = 10; | ||
| const int MINUS = 45; | ||
|
|
||
| int insertNumber(string position); | ||
| bool checkStringIsNumber(string in); | ||
|
|
||
| float doSum(float n1, float n2); | ||
| float doSub(float n1, float n2); | ||
| float doMolt(float n1, float n2); | ||
| float doDiv(float n1, float n2); | ||
|
|
||
| void doOperation(float n1, float n2); | ||
|
|
||
| int main(){ | ||
| float num1 = insertNumber("first"); | ||
| float num2 = insertNumber("second"); | ||
|
|
||
| doOperation(num1, num2); | ||
| return EXIT_SUCCESS; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| int insertNumber(string position){ | ||
| string insert; | ||
| bool isCorrect = true; | ||
| do { | ||
| if(!isCorrect) cout << ERROR_COLOR+("[Insert a valid input] ")+NORMAL_COLOR; | ||
| cout << "Insert "<< position <<" number: "; | ||
| getline(cin, insert); | ||
|
|
||
| isCorrect = checkStringIsNumber(insert); | ||
| } while(!(isCorrect)); | ||
|
|
||
| return stoi(insert); | ||
| } | ||
|
|
||
| bool checkStringIsNumber(string in){ | ||
| if(in.empty()) return false; | ||
| if(in.size() > MAX_DIGIT) return false; | ||
|
||
|
|
||
| for(int i = 0; i < in.size(); i++) | ||
| if((in.at(i) < ZERO || in.at(i) > NINE) && in.at(i) != MINUS) | ||
| return false; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| float doSum(float n1, float n2) { | ||
| return n1+n2; | ||
| } | ||
| float doSub(float n1, float n2) { | ||
| return n1-n2; | ||
| } | ||
| float doMolt(float n1, float n2) { | ||
| return n1*n2; | ||
| } | ||
| float doDiv(float n1, float n2) { | ||
| return (n2 != 0) ? n1/n2 : -1; | ||
| } | ||
|
|
||
| void doOperation(float n1, float n2) { | ||
| cout << "SUM: " << doSum(n1, n2) << endl; | ||
| cout << "Difference: " << doSub(n1, n2) << endl; | ||
| cout << "Moltiplication: " << doMolt(n1, n2) << endl; | ||
| cout << "Division: " << doDiv(n1, n2) << endl; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
manca
return EXIT_SUCCESS;