Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 2019/12/22(日)
- // 2019/12/25(水) L8改変
- #include <errno.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <limits.h>
- #include <time.h>
- #define MAXBUF 3
- bool geti(int * n);
- bool my_getline(char * line, size_t size);
- int main()
- {
- srand((unsigned)time(0UL));
- int answer = rand() % 10;
- bool isAtari = false;
- printf("***数当てゲーム(Lv1)***\n");
- printf("回答のチャンスは4回まで\n");
- for (int i = 1; i <= 4; i++) {
- int num;
- bool isSuccess;
- do {
- printf("1桁の数を入力:");
- isSuccess = geti(&num);
- } while (!isSuccess);
- if (num == answer) {
- printf("当たり!(%d回目)\n", i);
- isAtari = true;
- break;
- }
- printf("残念、はずれです(入力:%d、%d回目)\n", num, i);
- if (num < answer) {
- printf("もっと大きな数です\n\n");
- }
- else {
- printf("もっと小さな数です\n\n");
- }
- }
- if (!isAtari) {
- printf("正解は%dでした\n", answer);
- }
- return 0;
- }
- bool geti(int * n)
- {
- char line[MAXBUF];
- char * endp;
- long val;
- if (!my_getline(line, sizeof(line))) {
- return false;
- }
- errno = 0;
- val = strtol(line, &endp, 0);
- if (val == 0 && errno != 0) {
- return false;
- }
- if (endp == line || *endp != '\0') {
- return false;
- }
- *n = val;
- return true;
- }
- bool my_getline(char * line, size_t size)
- {
- char * s;
- int c;
- if (fgets(line, (int)size, stdin) == NULL) {
- return false;
- }
- s = strchr(line, '\n');
- if (s == line) {
- return false;
- }
- if (s == NULL) {
- if (strlen(line) + 1 == size) {
- while ((c = getchar()) != EOF && c != '\n') {
- }
- return false;
- }
- }
- *s = '\0';
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement