added second valgrind test and finished math game
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2020-03-05 06:04:38 +01:00
parent 989f3fba88
commit a16521fa87
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
4 changed files with 148 additions and 5 deletions

@ -13,13 +13,29 @@ steps:
- cmake CMakeLists.txt
- make
- name: valgrind-test
pull: always
- name: valgrind-test0
pull: if-not-exists
image: immawanderer/archlinux-cdev
commands:
- valgrind --log-file=valgrind_output ./pjc-0x02 < ./test_input > output
- valgrind --log-file=valgrind_output ./pjc-0x02 < ./test_input0 > /dev/null
- cat valgrind_output
- grep "no leaks are possible" valgrind_output
when:
status:
- success
- failure
- name: valgrind-test1
pull: if-not-exists
image: immawanderer/archlinux-cdev
commands:
- valgrind --log-file=valgrind_output ./pjc-0x02 < ./test_input1 > /dev/null
- cat valgrind_output
- grep "no leaks are possible" valgrind_output
when:
status:
- success
- failure
---

114
main.c

@ -1,6 +1,7 @@
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
void print_menu(){
printf("\n[*] guess a number from 0 to 10\n");
@ -33,6 +34,7 @@ int get_userguess(){
ok = scanf("%d", &x);
if(ok == EOF){
printf("[*] please, come on...\n");
return 9000;
}
if(ok == 0){
while (fgetc(stdin) != '\n'){
@ -73,6 +75,7 @@ void guess(){
ok = scanf("%d", &userchoice);
if(ok == EOF){
printf("[*] please, come on...\n");
return;
}
if(ok == 0){
while (fgetc(stdin) != '\n'){
@ -126,9 +129,116 @@ void print_rand_bs(){
}
}
int main()
void read_selection(int selection){
int ok;
while(ok != 1) {
ok = scanf("%d", &selection);
if(ok == EOF){
printf("[*] please, come on...\n");
return;
}
if(ok == 0){
while (fgetc(stdin) != '\n'){
}
lemmego();
return;
}
}
}
void default_flow(char operator, int a, int b, int result){
int answer = 0;
printf("a: %d\tb: %d\n", a, b);
printf("what is %d %c %d?\n>> ", a, operator, b);
answer = get_userguess();
if (answer == result){
printf("splendid. you win %d czk!\n", rand());
}
else {
printf("incorrect, %d %c %d is %d.\n", a, operator, b, result);
}
}
void another_bs(){
int a, b, result, answer, selection;
int upper_limit = 10;
srand(time(NULL));
printf("\n\nwelcome to the math game\n");
while ( selection != 8 ) {
printf("\npick a number\n");
printf("1 - addition\n"
"2 - subtraction\n"
"3 - multiplication\n"
"4 - division\n"
"5 - modulo\n"
"6 - set upper_limit for x = rand() mod upper_limit\n"
"7 - XOR\n"
"8 - quit\n"
">> ");
selection = get_userguess();
a= 1 + (rand() % upper_limit);
b = 1 + (rand() % upper_limit);
switch ( selection ) {
case 1:
result = a + b;
default_flow('+', a, b, result);
break;
case 2:
result = a - b;
default_flow('-', a, b, result);
break;
case 3:
result = a * b;
default_flow('*',a , b, result);
break;
case 4:
result = a / b;
default_flow('/', a, b, result);
break;
case 5:
printf("a: %d\tb: %d\n", a, b);
result = a % b;
printf("what is %d mod %d?\n>> ", a, b);
answer = get_userguess();
if (answer == result)
printf("splendid. you win %d czk!\n", rand());
else
printf("incorrect, %d mod %d je %d.\n", a, b, result);
break;
case 6:
printf(" default limit: 10\n"
" * current limit: %d\n", upper_limit);
printf(" new limit\t>> ");
upper_limit = get_userguess();
printf("\n * new limit: %d\n", upper_limit);
break;
case 7:
result = a ^ b;
default_flow('^',a, b, result);
break;
case 8:
printf("quitting...\n");
lemmego();
return;
default:
printf("invalid option\n");
selection = 0;
break;
}
}
}
int main(int argc, char *argv[])
{
guess();
print_rand_bs();
guess();
another_bs();
return 0;
}

@ -11,3 +11,4 @@ ok1
2
2
ktxbai
8

16
test_input1 Normal file

@ -0,0 +1,16 @@
q
1
323
2
3
2
4
1
5
4
6
9000
7
9001
gibberish
8