-
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
1a5c0a9
56d8bca
4da429a
a72a67c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,3 +10,74 @@ | |||||||||||||||||||||||||||||||||||||||||||
| Multiplication: 8 | ||||||||||||||||||||||||||||||||||||||||||||
| Division: 2 | ||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #include <iostream> | ||||||||||||||||||||||||||||||||||||||||||||
| #include <string> | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const std::string ERROR_COLOR = "\033[31m"; | ||||||||||||||||||||||||||||||||||||||||||||
| const std::string NORMAL_COLOR = "\033[0m"; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const int ZERO = 48; | ||||||||||||||||||||||||||||||||||||||||||||
| const int NINE = 57; | ||||||||||||||||||||||||||||||||||||||||||||
| const int MAX_DIGIT = 10; | ||||||||||||||||||||||||||||||||||||||||||||
| const int MINUS = 45; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| int insertNumber(std::string position); | ||||||||||||||||||||||||||||||||||||||||||||
| bool checkStringIsNumber(std::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); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| int insertNumber(std::string position){ | ||||||||||||||||||||||||||||||||||||||||||||
| std::string insert; | ||||||||||||||||||||||||||||||||||||||||||||
| bool isCorrect = true; | ||||||||||||||||||||||||||||||||||||||||||||
| do { | ||||||||||||||||||||||||||||||||||||||||||||
| if(!isCorrect) std::cout << ERROR_COLOR+("[Insert a valid input] ")+NORMAL_COLOR; | ||||||||||||||||||||||||||||||||||||||||||||
| std::cout << "Insert "<< position <<" number: "; | ||||||||||||||||||||||||||||||||||||||||||||
| getline(std::cin, insert); | ||||||||||||||||||||||||||||||||||||||||||||
| } while(!(isCorrect = checkStringIsNumber(insert))); | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| return std::stoi(insert); | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| bool checkStringIsNumber(std::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; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
| bool checkStringIsNumber(std::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; | |
| } | |
| bool checkStringIsNumber(std::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; | |
| } |
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;