Commit 404c7c3c authored by Karine's avatar Karine

exercise_06_37, version2

parent 6fc26dc7
......@@ -20,8 +20,8 @@ $(progname): $(progname).o | .gitignore
clean:
rm -f *.ii *.s *.o *.out $(progname) .gitignore
test: $(progname)
./$(progname)
test%: $(progname)
./$(progname) < $@.in > $@.out 2>&1 || echo "$@ is executing..."
diff $@.out $@.expected && echo PASSED || echo FAILED
.PRECIOUS: $(progname).ii $(progname).s
......
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <ctime>
std::string
responseForCorrectAnswer(const int answer)
std::string
printAnswer(const bool answerStatus)
{
const int answerType = 1 + std::rand() % 4;
std::string response;
switch(answer) {
case 0: response = "Very good!"; break;
case 1: response = "Excellent!"; break;
case 2: response = "Nice work!"; break;
case 3: response = "Keep up the good work!"; break;
}
return response;
}
std::string
responseForIncorrectAnswer(const int answer)
{
std::string response;
switch(answer) {
case 0: response = "Please try again."; break;
case 1: response = "Wrong.Try once more."; break;
case 2: response = "Don't give up."; break;
case 3: response = "No.Keep trying."; break;
if (answerStatus) {
switch(answerType) {
case 1: response = "Very good!"; break;
case 2: response = "Excellent!"; break;
case 3: response = "Nice work!"; break;
case 4: response = "Keep up the good work!"; break;
default: response = "Good job"; break;
}
} else {
switch(answerType) {
case 1: response = "Please try again."; break;
case 2: response = "Wrong.Try once more."; break;
case 3: response = "Don't give up."; break;
case 4: response = "No.Keep trying."; break;
default: response = "Nooooo"; break;
}
}
return response;
}
int
main()
{
int correctAnswerCount = 0;
int incorrectAnswerCount = 0;
for(int i = 0; i < 10; ++i) {
int first = std::rand() % 10;
int second = std::rand() % 10;
std::cout << "How much is " << first << " times " << second << " ? ";
if (::isatty(STDIN_FILENO)) {
std::srand(std::time(0));
}
int first = 0;
int second = 0;
bool answerStatus = true;
int count = 10;
int incorrectAnswerCount = 10;
while (count != 0) {
if (answerStatus) {
first = 1 + std::rand() % 10;
second = 1 + std::rand() % 10;
}
if (::isatty(STDIN_FILENO)) {
std::cout << "How much " << first << " times " << second << "? ";
}
int answer;
std::cin >> answer;
int response_ = std::rand() % 4;
if(answer == first * second) {
std::cout << responseForCorrectAnswer(response_) << std::endl;
++correctAnswerCount;
if (-1 == answer) {
break;
}
else {
do {
std::cout << responseForIncorrectAnswer(response_) << std::endl;
++incorrectAnswerCount;
std::cout << "How much is " << first << " times " << second << " ? ";
std::cin >> answer;
if(answer == first * second) {
std::cout << "Good!" << std::endl;
}
} while(answer != first * second);
answerStatus = (first * second == answer);
if (!answerStatus) {
--incorrectAnswerCount;
}
std::cout << printAnswer(answerStatus) << std::endl;
--count;
}
const int result = incorrectAnswerCount * 10;
int result = correctAnswerCount * 10;
if(result < 75) {
std::cout << "Please ask y our instructor for extra help." << std::endl;
if (result < 75) {
std::cout << "Please ask your instructor for extra help." << std::endl;
return 1;
}
std::cout << "Good job!!!" << std::endl;
return 0;
}
Excellent!
No.Keep trying.
Nice work!
Wrong.Try once more.
Don't give up.
No.Keep trying.
Don't give up.
No.Keep trying.
No.Keep trying.
Don't give up.
Please ask your instructor for extra help.
Wrong.Try once more.
No.Keep trying.
Wrong.Try once more.
No.Keep trying.
Don't give up.
Please try again.
Wrong.Try once more.
Wrong.Try once more.
Nice work!
No.Keep trying.
Please ask your instructor for extra help.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment