-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
cmake_minimum_required(VERSION 3.25) | ||
project(2024-cpl-coding C) | ||
|
||
set(CMAKE_C_STANDARD 17) | ||
|
||
add_executable(hello hello.c) | ||
add_executable(guess guess.c) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// Created by hfwei on 2024/9/19. | ||
// | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
|
||
int main(void) { | ||
int high = 100; | ||
int chance = 7; | ||
|
||
/* | ||
* print the rule of the game | ||
*/ | ||
printf("The computer will generate a random number between 1 and %d\n" | ||
"You have %d chances.\n", | ||
high, chance); | ||
/* | ||
* generate a random number | ||
*/ | ||
srand(time(NULL)); // use current time as seed for random generator | ||
// 0 .. RAND_MAX | ||
// 1 .. high | ||
int secret = rand() % high + 1; | ||
printf("secret = %d\n", secret); | ||
|
||
while (chance > 0) { | ||
|
||
/* | ||
* let the player enter his/her guess number | ||
*/ | ||
printf("Enter your guess.\n"); | ||
|
||
/* | ||
* store the guess number, | ||
* compare it with the secret, | ||
* and inform the player of the result | ||
*/ | ||
int guess; | ||
scanf("%d", &guess); | ||
|
||
if (guess == secret) { | ||
printf("You Win!\n"); | ||
break; | ||
} else if (guess > secret) { | ||
printf("guess > secret\n"); | ||
} else { | ||
printf("guess < secret\n"); | ||
} | ||
|
||
/* | ||
* loop: repeat until the player wins or loses | ||
*/ | ||
chance--; | ||
// chance = chance - 1; | ||
} | ||
|
||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// Created by hfwei on 2024/9/19. | ||
// | ||
|
||
// single-line comment | ||
// Created by hfwei on 2023/9/15. | ||
// | ||
|
||
/* | ||
* this is a multi-line comment | ||
* this is a multi-line comment | ||
*/ | ||
|
||
// directive | ||
// .h: header file | ||
// stdio: standard input/output | ||
#include <stdio.h> | ||
|
||
/* | ||
* main function | ||
* y <- f(x) | ||
* int: integer (return) | ||
*/ | ||
int main(void) { | ||
// "hello world\n": string | ||
// printf: print + f (format) | ||
printf("Hello World\n"); | ||
|
||
// return statement (return to operating system) | ||
// 0: exit code | ||
return 0; | ||
} |