Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions exercises/calculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

manca return EXIT_SUCCESS;




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)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

questo ciclo do-while sembra poco leggibile, potresti aggiungere parentesi e spaziature al primo if e spezzare la condizione dentro il while nel seguente modo

...
isCorrect = checkStringIsNumber(insert)
} while(!isCorrect)


return std::stoi(insert);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

potresti usare il namespace std per evitare di scrivere std:: nell'intero file

}

bool checkStringIsNumber(std::string in){
if(in.empty()) return false;
if(in.size() > MAX_DIGIT) return false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😢

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;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gli spazi non costano, puoi usarli ;-)

Suggested change
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;
}


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) {
std::cout << "SUM: " << doSum(n1, n2) << std::endl;
std::cout << "Difference: " << doSub(n1, n2) << std::endl;
std::cout << "Moltiplication: " << doMolt(n1, n2) << std::endl;
std::cout << "Division: " << doDiv(n1, n2) << std::endl;
}