Skip to content

Commit

Permalink
+hello.c, +guess.c
Browse files Browse the repository at this point in the history
  • Loading branch information
hengxin committed Sep 19, 2024
1 parent 10d1ed0 commit 8b915ea
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 0 deletions.
91 changes: 91 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,94 @@ modules.order
Module.symvers
Mkfile.old
dkms.conf

CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
CMakeUserPresets.json

# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# AWS User-specific
.idea/**/aws.xml

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# SonarLint plugin
.idea/sonarlint/

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
10 changes: 10 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/2024-cpl-coding.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/codestream.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/git_toolbox_blame.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions CMakeLists.txt
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)
60 changes: 60 additions & 0 deletions 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;
}
32 changes: 32 additions & 0 deletions hello.c
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;
}

0 comments on commit 8b915ea

Please sign in to comment.