From d9a2fa3a91acc23fb82772c9c3be5a7a3a062f90 Mon Sep 17 00:00:00 2001 From: KenNN99 <1292781066@qq.com> Date: Wed, 27 Feb 2019 20:53:26 +0800 Subject: [PATCH 01/10] 2/27 20:53 --- level1/p01_runningLetter/Run.cpp | 38 ++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 level1/p01_runningLetter/Run.cpp diff --git a/level1/p01_runningLetter/Run.cpp b/level1/p01_runningLetter/Run.cpp new file mode 100644 index 00000000..823f0eb1 --- /dev/null +++ b/level1/p01_runningLetter/Run.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +#define WIDE 80 +#define SLEEP 5 + +void Run(int i); + +int main() +{ + system("mode con cols=80 lines=30"); + int i; + while(true) + { + for (i = 1; i < WIDE; i++) + { + Run(i); + } + for (i; i >= 0; i--) + { + Run(i); + } + } + return 0; +} + +void Run(int i) +{ + for (int j = 0; j < i; j++) + { + printf(" "); + } + printf("R"); + Sleep(SLEEP); + system("cls"); +} \ No newline at end of file From 01e8d7162d7b95beae5ab263f0c78af804f008d8 Mon Sep 17 00:00:00 2001 From: KenNN99 <1292781066@qq.com> Date: Wed, 27 Feb 2019 21:06:03 +0800 Subject: [PATCH 02/10] =?UTF-8?q?2/27=2021=EF=BC=9A05?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- level1/p02_isPrime/_isPrime.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 level1/p02_isPrime/_isPrime.cpp diff --git a/level1/p02_isPrime/_isPrime.cpp b/level1/p02_isPrime/_isPrime.cpp new file mode 100644 index 00000000..d50b5e2e --- /dev/null +++ b/level1/p02_isPrime/_isPrime.cpp @@ -0,0 +1,31 @@ +#include +#include + +void NotPrime(); + +int main() +{ + int i; + scanf("%d", &i); + if (i == 1) + { + NotPrime(); + } + for (int n = 2; n*n < i; n++) + { + if (i%n == 0) + { + NotPrime(); + return 0; + } + } + printf("This is a Prime."); + system("pause"); + return 0; +} + +void NotPrime() +{ + printf("This is not a Prime!!!"); + system("pause"); +} \ No newline at end of file From 5fbdb2aacada0bb1550a5e42323708c2d86e7c88 Mon Sep 17 00:00:00 2001 From: KenNN99 <1292781066@qq.com> Date: Wed, 27 Feb 2019 21:06:03 +0800 Subject: [PATCH 03/10] =?UTF-8?q?2/27=2021=EF=BC=9A05?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- level1/p02_isPrime/_isPrime.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 level1/p02_isPrime/_isPrime.cpp diff --git a/level1/p02_isPrime/_isPrime.cpp b/level1/p02_isPrime/_isPrime.cpp new file mode 100644 index 00000000..c4601bdd --- /dev/null +++ b/level1/p02_isPrime/_isPrime.cpp @@ -0,0 +1,31 @@ +#include +#include + +void NotPrime(); + +int main() +{ + int i; + scanf("%d", &i); + if (i == 1) + { + NotPrime(); + } + for (int n = 2; n*n <= i; n++) + { + if (i%n == 0) + { + NotPrime(); + return 0; + } + } + printf("This is a Prime."); + system("pause"); + return 0; +} + +void NotPrime() +{ + printf("This is not a Prime!!!"); + system("pause"); +} \ No newline at end of file From 2b6d759c8e10cfe17d315e90703734d5f6ddb2ff Mon Sep 17 00:00:00 2001 From: KenNN99 <47114155+KenNN99@users.noreply.github.com> Date: Thu, 28 Feb 2019 16:36:10 +0800 Subject: [PATCH 04/10] Update _isPrime.cpp --- level1/p02_isPrime/_isPrime.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/level1/p02_isPrime/_isPrime.cpp b/level1/p02_isPrime/_isPrime.cpp index d50b5e2e..8ae06888 100644 --- a/level1/p02_isPrime/_isPrime.cpp +++ b/level1/p02_isPrime/_isPrime.cpp @@ -11,7 +11,7 @@ int main() { NotPrime(); } - for (int n = 2; n*n < i; n++) + for (int n = 2; n*n <= i; n++) { if (i%n == 0) { @@ -28,4 +28,4 @@ void NotPrime() { printf("This is not a Prime!!!"); system("pause"); -} \ No newline at end of file +} From 551663ca203869d02e4cfc07c62e8f288a502b14 Mon Sep 17 00:00:00 2001 From: KenNN99 <1292781066@qq.com> Date: Thu, 28 Feb 2019 17:10:51 +0800 Subject: [PATCH 05/10] =?UTF-8?q?2/28=2017=EF=BC=9A10?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- level1/p03_Diophantus/p03_Diophantus.cpp | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 level1/p03_Diophantus/p03_Diophantus.cpp diff --git a/level1/p03_Diophantus/p03_Diophantus.cpp b/level1/p03_Diophantus/p03_Diophantus.cpp new file mode 100644 index 00000000..b76d587b --- /dev/null +++ b/level1/p03_Diophantus/p03_Diophantus.cpp @@ -0,0 +1,25 @@ +#include +#include + +double Form(double x); + +int main() +{ + double x = 10; + while (true) + { + if (Form(x) == x) + { + printf("Diophantus is %d years old when his son died.", (int)x-4); + system("pause"); + return 0; + } + x++; + } +} + +double Form(double x) +{ + double y = x / 6 + x / 12 + x / 7 + 5 + x / 2 + 4; + return y; +} \ No newline at end of file From afc0199e568ae577699f90940209a0c3748de9a7 Mon Sep 17 00:00:00 2001 From: KenNN99 <1292781066@qq.com> Date: Thu, 28 Feb 2019 22:35:36 +0800 Subject: [PATCH 06/10] =?UTF-8?q?2/28=2022=EF=BC=9A35?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- level1/p04_ narcissus/p04_ narcissus.cpp | 32 ++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 level1/p04_ narcissus/p04_ narcissus.cpp diff --git a/level1/p04_ narcissus/p04_ narcissus.cpp b/level1/p04_ narcissus/p04_ narcissus.cpp new file mode 100644 index 00000000..a0dd01c0 --- /dev/null +++ b/level1/p04_ narcissus/p04_ narcissus.cpp @@ -0,0 +1,32 @@ +#include +#include + +int Just(int x, int y, int z); + +int main() +{ + int i, j, k; + for (i = 1; i <= 9; i++) + { + for (j = 0; j <= 9; j++) + { + for (k = 0; k <= 9; k++) + { + if (Just(i, j, k) == i * 100 + j * 10 + k) + { + printf("%d ", i * 100 + j * 10 + k); + } + else if (i == 9 && j == 9 && k == 9) + { + system("pause"); + return 0; + } + } + } + } +} + +int Just(int x, int y, int z) +{ + return x * x*x + y * y*y + z * z*z; +} \ No newline at end of file From 9dd5bff4bb66d6e2432f0ad1e8dda2539fa06913 Mon Sep 17 00:00:00 2001 From: KenNN99 <1292781066@qq.com> Date: Tue, 5 Mar 2019 21:23:52 +0800 Subject: [PATCH 07/10] 19/3/5 21:35 --- level1/p05_allPrimes/p05_allPrimes.cpp | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 level1/p05_allPrimes/p05_allPrimes.cpp diff --git a/level1/p05_allPrimes/p05_allPrimes.cpp b/level1/p05_allPrimes/p05_allPrimes.cpp new file mode 100644 index 00000000..dbcbaf57 --- /dev/null +++ b/level1/p05_allPrimes/p05_allPrimes.cpp @@ -0,0 +1,37 @@ +#include +#include +#include + +int main() +{ + clock_t start, end; + start = clock(); + bool Prime[1001]; + int i; + for (i = 1; i <= 1000; i++) + { + Prime[i] = true; + } + for (i = 2; i*i <= 1000; i++) + { + if (Prime[i]) + { + for (int j = 2; j*i <= 1000; j++) + { + Prime[j*i] = false; + } + } + } + for (i = 1; i <= 1000; i++) + { + if (Prime[i]) + { + printf("%d\n", i); + } + } + end = clock(); + double duration = (double)(end - start) / CLOCKS_PER_SEC * 1000; + printf("Used %f ms", duration); + system("pause"); + return 0; +} \ No newline at end of file From 0e271375e5bb599b15ffa8660166675b7ffe7a32 Mon Sep 17 00:00:00 2001 From: KenNN99 <47114155+KenNN99@users.noreply.github.com> Date: Tue, 5 Mar 2019 21:38:55 +0800 Subject: [PATCH 08/10] Update p05_allPrimes.cpp --- level1/p05_allPrimes/p05_allPrimes.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/level1/p05_allPrimes/p05_allPrimes.cpp b/level1/p05_allPrimes/p05_allPrimes.cpp index dbcbaf57..a6f8a45e 100644 --- a/level1/p05_allPrimes/p05_allPrimes.cpp +++ b/level1/p05_allPrimes/p05_allPrimes.cpp @@ -22,7 +22,7 @@ int main() } } } - for (i = 1; i <= 1000; i++) + for (i = 2; i <= 1000; i++) { if (Prime[i]) { @@ -34,4 +34,4 @@ int main() printf("Used %f ms", duration); system("pause"); return 0; -} \ No newline at end of file +} From 489a4ea51899324a773430243f426892213bd279 Mon Sep 17 00:00:00 2001 From: "1292781066@qq.com" <1292781066@qq.com> Date: Wed, 27 Mar 2019 17:45:03 +0800 Subject: [PATCH 09/10] p06-p08 --- level1/p06_Goldbach/p06_Goldbach.cpp | 59 ++++++++++++ .../p07_encrypt_decrypt.cpp | 58 ++++++++++++ level1/p08_hanoi/p08_hanoi.cpp | 92 +++++++++++++++++++ 3 files changed, 209 insertions(+) create mode 100644 level1/p06_Goldbach/p06_Goldbach.cpp create mode 100644 level1/p07_encrypt_decrypt/p07_encrypt_decrypt.cpp create mode 100644 level1/p08_hanoi/p08_hanoi.cpp diff --git a/level1/p06_Goldbach/p06_Goldbach.cpp b/level1/p06_Goldbach/p06_Goldbach.cpp new file mode 100644 index 00000000..0981847a --- /dev/null +++ b/level1/p06_Goldbach/p06_Goldbach.cpp @@ -0,0 +1,59 @@ +#include +#include +#include + +bool Pri[101]; + +void FindPrime(void); + +int main() +{ + int n, i, j; + bool done; + FindPrime(); + for (n = 4; n <= 100; n + 2) + { + done = false; + for (i = 2; i <= n / 2; i++) + { + if (Pri[i]) + { + for (j = n - 2; j >= n / 2; j--) + { + if (Pri[j]) + { + if (i + j == n) + { + printf("%d = %d + %d\n", n, i, j); + done = true; + break; + } + } + } + } + if (done) + { + n = n + 2; + break; + } + } + } + system("pause"); + return 0; +} + +void FindPrime(void) +{ + int i; + memset(Pri, 1, sizeof(bool) * 101); + for (i = 2; i*i <= 100; i++) + { + if (Pri[i]) + { + for (int j = 2; i*j <= 100; j++) + { + Pri[i*j] = false; + } + } + } +} \ No newline at end of file diff --git a/level1/p07_encrypt_decrypt/p07_encrypt_decrypt.cpp b/level1/p07_encrypt_decrypt/p07_encrypt_decrypt.cpp new file mode 100644 index 00000000..0bcce860 --- /dev/null +++ b/level1/p07_encrypt_decrypt/p07_encrypt_decrypt.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include + +#define SEED 300 + +int Encrypt(char *a); + +void Decrypt(int *b, int x); + +char a[128]; + +int b[128]; + +char c[128]; + +int main() +{ + fgets(a, 128, stdin); + int seed = Encrypt(a); + printf("After encrypting:"); + int len = strlen(a); + int i; + for (i = 0; i < len; i++) + { + printf("%d ", b[i]); + } + printf("\n"); + Decrypt(b, seed); + printf("Now we decrypt it: %s\n", c); + system("pause"); + return 0; +} + +int Encrypt(char *a) +{ + int len = strlen(a) - 1; + srand(time(NULL)); + int seed = rand(); + int i = 0; + for (i = 0; i < len; i++) + { + b[i] = a[i] * (i + 1)*(seed % SEED); + } + return seed; +} + +void Decrypt(int *b, int x) +{ + int len = strlen(a) - 1; + int i; + for (i = 0; i < len; i++) + { + c[i] = b[i] / (i + 1) / (x % SEED); + } + c[len + 1] = '\0'; +} \ No newline at end of file diff --git a/level1/p08_hanoi/p08_hanoi.cpp b/level1/p08_hanoi/p08_hanoi.cpp new file mode 100644 index 00000000..64528620 --- /dev/null +++ b/level1/p08_hanoi/p08_hanoi.cpp @@ -0,0 +1,92 @@ +#include +#include + +char A = 'A'; +char B = 'B'; +char C = 'C'; + +void Hanio(char t, int n, char *p); + +void SetPosition(char t, int n, char *p); + +int main() +{ + int n; + scanf("%d", &n); + char *p = (char*)malloc(sizeof(char)*n); + for (int i = 0; i < n; i++) + { + p[i] = A; + } + Hanio(C, n, p); + system("pause"); + return 0; +} + +void Hanio(char t, int n, char *p) +{ + if (n > 1) + { + SetPosition(t, n, p); + } + else + { + printf("No.%d %c -> %c\n", n, p[n - 1], t); + p[n - 1] = t; + } +} + +void SetPosition(char t, int n, char *p) +{ + if (t == A) + { + if (p[n - 1] == B) + { + Hanio(C, n - 1, p); + printf("No.%d %c -> %c\n", n, p[n - 1], t); + p[n - 1] = t; + Hanio(A, n - 1, p); + } + else + { + Hanio(B, n - 1, p); + printf("No.%d %c -> %c\n", n, p[n - 1], t); + p[n - 1] = t; + Hanio(A, n - 1, p); + } + } + else if (t == B) + { + if (p[n - 1] == A) + { + Hanio(C, n - 1, p); + printf("No.%d %c -> %c\n", n, p[n - 1], t); + p[n - 1] = t; + Hanio(B, n - 1, p); + } + else + { + Hanio(A, n - 1, p); + printf("No.%d %c -> %c\n", n, p[n - 1], t); + p[n - 1] = t; + Hanio(B, n - 1, p); + } + } + else if (t == C) + { + if (p[n - 1] == B) + { + Hanio(A, n - 1, p); + printf("No.%d %c -> %c\n", n, p[n - 1], t); + p[n - 1] = t; + Hanio(C, n - 1, p); + } + else + { + Hanio(B, n - 1, p); + printf("No.%d %c -> %c\n", n, p[n - 1], t); + p[n - 1] = t; + Hanio(C, n - 1, p); + } + } +} \ No newline at end of file From a668b7aa67187c8ef44bdefe5e7813d665084ff3 Mon Sep 17 00:00:00 2001 From: Ken_99 <1292781066@qq.com> Date: Wed, 24 Apr 2019 19:45:41 +0800 Subject: [PATCH 10/10] correct p08. push p09-p12 --- level1/p08_hanoi/p08_hanoi.cpp | 76 +--- level1/p09_maze/Maze.txt | 15 + level1/p09_maze/p09_maze.cpp | 276 +++++++++++++ level1/p10_pushBoxes/1.txt | 6 + level1/p10_pushBoxes/2.txt | 8 + level1/p10_pushBoxes/3.txt | 11 + level1/p10_pushBoxes/4.txt | 11 + level1/p10_pushBoxes/5.txt | 7 + level1/p10_pushBoxes/Gaming.cpp | 285 ++++++++++++++ level1/p10_pushBoxes/InitCursor.cpp | 9 + level1/p10_pushBoxes/LoadGame.cpp | 19 + level1/p10_pushBoxes/LoadMap.cpp | 90 +++++ level1/p10_pushBoxes/Print.cpp | 34 ++ level1/p10_pushBoxes/Save.cpp | 19 + level1/p10_pushBoxes/Save.txt | 1 + level1/p10_pushBoxes/SaveScore.cpp | 27 ++ level1/p10_pushBoxes/SetPosition.cpp | 11 + level1/p10_pushBoxes/StartGame.cpp | 81 ++++ level1/p10_pushBoxes/game.cpp | 51 +++ level1/p10_pushBoxes/game.h | 29 ++ level1/{p08_hanoi => p10_pushBoxes}/hanoi.jpg | Bin level1/p10_pushBoxes/main.cpp | 9 + level1/p10_pushBoxes/score1.txt | 1 + level1/p10_pushBoxes/score2.txt | 1 + level1/p10_pushBoxes/score3.txt | 1 + level1/p10_pushBoxes/score4.txt | 0 level1/p10_pushBoxes/score5.txt | 0 level1/p11_linkedList/p11_linkedList.cpp | 143 +++++++ level1/p12_warehouse/List.txt | 2 + level1/p12_warehouse/main.cpp | 370 ++++++++++++++++++ 30 files changed, 1525 insertions(+), 68 deletions(-) create mode 100644 level1/p09_maze/Maze.txt create mode 100644 level1/p09_maze/p09_maze.cpp create mode 100644 level1/p10_pushBoxes/1.txt create mode 100644 level1/p10_pushBoxes/2.txt create mode 100644 level1/p10_pushBoxes/3.txt create mode 100644 level1/p10_pushBoxes/4.txt create mode 100644 level1/p10_pushBoxes/5.txt create mode 100644 level1/p10_pushBoxes/Gaming.cpp create mode 100644 level1/p10_pushBoxes/InitCursor.cpp create mode 100644 level1/p10_pushBoxes/LoadGame.cpp create mode 100644 level1/p10_pushBoxes/LoadMap.cpp create mode 100644 level1/p10_pushBoxes/Print.cpp create mode 100644 level1/p10_pushBoxes/Save.cpp create mode 100644 level1/p10_pushBoxes/Save.txt create mode 100644 level1/p10_pushBoxes/SaveScore.cpp create mode 100644 level1/p10_pushBoxes/SetPosition.cpp create mode 100644 level1/p10_pushBoxes/StartGame.cpp create mode 100644 level1/p10_pushBoxes/game.cpp create mode 100644 level1/p10_pushBoxes/game.h rename level1/{p08_hanoi => p10_pushBoxes}/hanoi.jpg (100%) mode change 100755 => 100644 create mode 100644 level1/p10_pushBoxes/main.cpp create mode 100644 level1/p10_pushBoxes/score1.txt create mode 100644 level1/p10_pushBoxes/score2.txt create mode 100644 level1/p10_pushBoxes/score3.txt create mode 100644 level1/p10_pushBoxes/score4.txt create mode 100644 level1/p10_pushBoxes/score5.txt create mode 100644 level1/p11_linkedList/p11_linkedList.cpp create mode 100644 level1/p12_warehouse/List.txt create mode 100644 level1/p12_warehouse/main.cpp diff --git a/level1/p08_hanoi/p08_hanoi.cpp b/level1/p08_hanoi/p08_hanoi.cpp index 64528620..150202f0 100644 --- a/level1/p08_hanoi/p08_hanoi.cpp +++ b/level1/p08_hanoi/p08_hanoi.cpp @@ -5,88 +5,28 @@ char A = 'A'; char B = 'B'; char C = 'C'; -void Hanio(char t, int n, char *p); +void Hanio(int n, char A, char B, char C); -void SetPosition(char t, int n, char *p); int main() { int n; scanf("%d", &n); - char *p = (char*)malloc(sizeof(char)*n); - for (int i = 0; i < n; i++) - { - p[i] = A; - } - Hanio(C, n, p); + Hanio(n, A, B, C); system("pause"); return 0; } -void Hanio(char t, int n, char *p) +void Hanio(int n, char A, char B, char C) { - if (n > 1) + if (n == 1) { - SetPosition(t, n, p); + printf("No.%d %c -> %c\n", n, A, C); } else { - printf("No.%d %c -> %c\n", n, p[n - 1], t); - p[n - 1] = t; - } -} - -void SetPosition(char t, int n, char *p) -{ - if (t == A) - { - if (p[n - 1] == B) - { - Hanio(C, n - 1, p); - printf("No.%d %c -> %c\n", n, p[n - 1], t); - p[n - 1] = t; - Hanio(A, n - 1, p); - } - else - { - Hanio(B, n - 1, p); - printf("No.%d %c -> %c\n", n, p[n - 1], t); - p[n - 1] = t; - Hanio(A, n - 1, p); - } - } - else if (t == B) - { - if (p[n - 1] == A) - { - Hanio(C, n - 1, p); - printf("No.%d %c -> %c\n", n, p[n - 1], t); - p[n - 1] = t; - Hanio(B, n - 1, p); - } - else - { - Hanio(A, n - 1, p); - printf("No.%d %c -> %c\n", n, p[n - 1], t); - p[n - 1] = t; - Hanio(B, n - 1, p); - } - } - else if (t == C) - { - if (p[n - 1] == B) - { - Hanio(A, n - 1, p); - printf("No.%d %c -> %c\n", n, p[n - 1], t); - p[n - 1] = t; - Hanio(C, n - 1, p); - } - else - { - Hanio(B, n - 1, p); - printf("No.%d %c -> %c\n", n, p[n - 1], t); - p[n - 1] = t; - Hanio(C, n - 1, p); - } + Hanio(n - 1, A, C, B); + printf("No.%d %c -> %c\n", n, A, C); + Hanio(n - 1, B, A, C); } } \ No newline at end of file diff --git a/level1/p09_maze/Maze.txt b/level1/p09_maze/Maze.txt new file mode 100644 index 00000000..87e27fff --- /dev/null +++ b/level1/p09_maze/Maze.txt @@ -0,0 +1,15 @@ +###O############# +# # # +# ##### # ##### # +# # # # +# # ########### # +# # # # # # +# # # ##### # # +### # # ##### +# # # ####### # +# # # # D +# ##### ##### # # +# # # # +# ############# # +# # +################# \ No newline at end of file diff --git a/level1/p09_maze/p09_maze.cpp b/level1/p09_maze/p09_maze.cpp new file mode 100644 index 00000000..938e8c5f --- /dev/null +++ b/level1/p09_maze/p09_maze.cpp @@ -0,0 +1,276 @@ +#include +#include +#include +#include + +char map[30][30]; + +bool wall[30][30]; + +struct Player +{ + int x; + int y; +}player; + +struct Des +{ + int x; + int y; +}DesPos; + +int StartGame(); + +void SetPosition(int x, int y); + +void PrintSpace(int x, int y); + +void PrintX(int x, int y); + +void PrintO(int x, int y); + +void PrintD(int x, int y); + +int LoadMap(char Map[][30]); + +void Gaming(char Map[][30], int i); + +int main() +{ + CONSOLE_CURSOR_INFO cursor = { 100, false }; + SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor); + + int line; +BackToMenu: + int SON = StartGame(); + if (SON == 0) + { + return 0; + } + line = LoadMap(map); + Gaming(map, line); + system("cls"); + goto BackToMenu; +} + +int StartGame() +{ + system("cls"); + printf("---------------------------------\n"); + printf("\n\n\n"); + printf(" Push Boxes\n\n"); + printf(" 1. NEW GAME\n"); + printf(" 2. EXIT GAME\n"); + printf("\n\n\n"); + printf("---------------------------------\n"); + printf("Press Enter to choose...\n"); + SetPosition(5, 6); + printf("->"); + clock_t t = clock(); + const int x = 5; + int y = 6; + while (GetKeyState(VK_RETURN) >= 0) + { + if (GetKeyState(VK_UP) < 0) + { + if (clock() - t > 200) + { + t = clock(); + if (y != 6) + { + PrintSpace(x, y); + PrintSpace(x + 1, y); + --y; + SetPosition(x, y); + printf("->"); + } + else + { + PrintSpace(x, y); + PrintSpace(x + 1, y); + y = 7; + SetPosition(x, y); + printf("->"); + } + } + } + else if (GetKeyState(VK_DOWN) < 0) + { + if (clock() - t > 200) + { + t = clock(); + if (y != 7) + { + PrintSpace(x, y); + PrintSpace(x + 1, y); + ++y; + SetPosition(x, y); + printf("->"); + } + else + { + PrintSpace(x, y); + PrintSpace(x + 1, y); + y = 6; + SetPosition(x, y); + printf("->"); + } + } + } + } + switch (y) + { + case 6:return 1; break; + case 7:return 0; + } +} + +void SetPosition(int x, int y) +{ + COORD coord; + coord.X = x; + coord.Y = y; + SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); +} + +void PrintSpace(int x, int y) +{ + SetPosition(x, y); + printf(" "); +} + +void PrintX(int x, int y) +{ + SetPosition(x, y); + printf("X"); +} + +void PrintO(int x, int y) +{ + SetPosition(x, y); + printf("O"); +} + +void PrintD(int x, int y) +{ + SetPosition(x, y); + printf("D"); +} + +int LoadMap(char Map[][30]) +{ + int i = 0; + FILE *fp; + fp = fopen("Maze.txt", "r"); + if (fp == NULL) + { + printf("Something wrong!\n"); + system("pause"); + return 0; + } + while (!feof(fp)) + { + fgets(Map[i], 30, fp); + ++i; + } + fclose(fp); + return i; +} + +void Gaming(char Map[][30], int i) +{ + int x, y; + int k; + int j; + memset(wall, 0, sizeof(wall)); + system("cls"); + for (j = 0; j < i; ++j) + { + printf("%s", Map[j]); + for (k = 0; k < 30; ++k) + { + switch (Map[j][k]) + { + case '#': + wall[j][k] = true; + break; + case 'D': + DesPos.x = k; + DesPos.y = j; + break; + case 'O': + player.x = k; + player.y = j; + break; + } + } + } + printf("\n---------------\n"); + printf("Use directtion keys to control...\n"); + printf("Press Esc back to the menu...\n"); + clock_t t = clock(); + while (GetKeyState(VK_ESCAPE) >= 0) + { + if (GetKeyState(VK_UP) < 0) + { + if (clock() - t > 200) + { + t = clock(); + + if (!wall[player.y - 1][player.x] && player.y != 0) + { + PrintO(player.x, player.y - 1); + PrintSpace(player.x, player.y); + --player.y; + } + } + } + else if (GetKeyState(VK_DOWN) < 0) + { + if (clock() - t > 200) + { + t = clock(); + if (!wall[player.y + 1][player.x]) + { + PrintO(player.x, player.y + 1); + PrintSpace(player.x, player.y); + ++player.y; + } + } + } + else if (GetKeyState(VK_LEFT) < 0) + { + if (clock() - t > 200) + { + t = clock(); + if (!wall[player.y][player.x - 1]) + { + PrintO(player.x - 1, player.y); + PrintSpace(player.x, player.y); + --player.x; + } + } + } + else if (GetKeyState(VK_RIGHT) < 0) + { + if (clock() - t > 200) + { + t = clock(); + if (!wall[player.y][player.x + 1]) + { + PrintO(player.x + 1, player.y); + PrintSpace(player.x, player.y); + ++player.x; + } + } + } + if (player.x == DesPos.x&&player.y == DesPos.y) + { + system("cls"); + printf("You win!!!!\nPress Shift to back to the menu...\n"); + while (GetKeyState(VK_SHIFT) >= 0); + return; + } + } + return; +} \ No newline at end of file diff --git a/level1/p10_pushBoxes/1.txt b/level1/p10_pushBoxes/1.txt new file mode 100644 index 00000000..da103e19 --- /dev/null +++ b/level1/p10_pushBoxes/1.txt @@ -0,0 +1,6 @@ + #### +### #### +# X # +# # #X # +# D D#O # +######### \ No newline at end of file diff --git a/level1/p10_pushBoxes/2.txt b/level1/p10_pushBoxes/2.txt new file mode 100644 index 00000000..81a6c2db --- /dev/null +++ b/level1/p10_pushBoxes/2.txt @@ -0,0 +1,8 @@ + ##### +#### # +#DD#XX # +# DDX ##### +# O# # # # +# X X # +# #######D# +#### ### \ No newline at end of file diff --git a/level1/p10_pushBoxes/3.txt b/level1/p10_pushBoxes/3.txt new file mode 100644 index 00000000..11eefc71 --- /dev/null +++ b/level1/p10_pushBoxes/3.txt @@ -0,0 +1,11 @@ + ### + ## # #### + ## ### # +## X # +# OX # # +### X### # + # #DD # + ## ##D ## + # ## + # ## + ####### \ No newline at end of file diff --git a/level1/p10_pushBoxes/4.txt b/level1/p10_pushBoxes/4.txt new file mode 100644 index 00000000..575aa8ed --- /dev/null +++ b/level1/p10_pushBoxes/4.txt @@ -0,0 +1,11 @@ + ######## + # # +#### #### # +# X X DD# # +# # D# # # +# # ### # # +# #D# X # +# #DXOD ## +# #X### # +# D # +########## \ No newline at end of file diff --git a/level1/p10_pushBoxes/5.txt b/level1/p10_pushBoxes/5.txt new file mode 100644 index 00000000..fff106fe --- /dev/null +++ b/level1/p10_pushBoxes/5.txt @@ -0,0 +1,7 @@ +########## +# # +# X X X # +##O##### # +# D D D# +# # +########## \ No newline at end of file diff --git a/level1/p10_pushBoxes/Gaming.cpp b/level1/p10_pushBoxes/Gaming.cpp new file mode 100644 index 00000000..274cd238 --- /dev/null +++ b/level1/p10_pushBoxes/Gaming.cpp @@ -0,0 +1,285 @@ +#include +#include +#include +#include +#include "game.h" + +struct Player +{ + int x; + int y; +}player; + +struct Des +{ + int x; + int y; +}DesPos[10]; + +bool wall[15][15]; +bool des[15][15]; +bool box[15][15]; + +int Gaming(char Map[][15], int i, bool *back, int level); + +int Gaming(char Map[][15], int i, bool *back, int level) +{ + int x, y; + Restart: + memset(wall, 0, sizeof(wall)); + memset(des, 0, sizeof(des)); + memset(box, 0, sizeof(box)); + system("cls"); + int j, k; + int m; + int win; + int score = 5000; + int NumOfBox = 0; + bool found = false; + for (j = 0; j < i; ++j) + { + printf("%s", Map[j]); + for (k = 0; k < 15; ++k) + { + switch (Map[j][k]) + { + case '#': + wall[j][k] = true; + break; + case 'D': + des[j][k] = true; + DesPos[NumOfBox].x = k; + DesPos[NumOfBox].y = j; + ++NumOfBox; + break; + case 'O': + player.x = k; + player.y = j; + break; + case 'X': + box[j][k] = true; + } + } + } + printf("\n---------------\n"); + printf("Use directtion keys to control...\n"); + printf("Press R to restart...\n"); + printf("Press S to save your level...\n"); + printf("Press Esc back to the menu...\n"); + + CONSOLE_SCREEN_BUFFER_INFO cInfo; + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cInfo); + x = cInfo.dwCursorPosition.X; + y = cInfo.dwCursorPosition.Y; + + clock_t t = clock(); + while (GetKeyState(VK_ESCAPE) >= 0) + { + win = 0; + if (GetKeyState(82) < 0) + { + goto Restart; + } + if (GetKeyState(VK_UP) < 0) + { + if (clock() - t > 200) + { + t = clock(); + if (!wall[player.y - 1][player.x]) + { + if (box[player.y - 1][player.x]) + { + if (!wall[player.y - 2][player.x] && !box[player.y - 2][player.x]) + { + PrintX(player.x, player.y - 2); + PrintO(player.x, player.y - 1); + if (des[player.y][player.x]) + { + PrintD(player.x, player.y); + } + else + { + PrintSpace(player.x, player.y); + } + box[player.y - 1][player.x] = false; + box[player.y - 2][player.x] = true; + --player.y; + score -= 10; + } + } + else + { + PrintO(player.x, player.y - 1); + if (des[player.y][player.x]) + { + PrintD(player.x, player.y); + } + else + { + PrintSpace(player.x, player.y); + } + --player.y; + score -= 10; + } + } + } + } + if (GetKeyState(VK_RIGHT) < 0) + { + if (clock() - t > 200) + { + t = clock(); + if (!wall[player.y][player.x + 1]) + { + if (box[player.y][player.x + 1]) + { + if (!wall[player.y][player.x + 2] && !box[player.y][player.x + 2]) + { + PrintX(player.x + 2, player.y); + PrintO(player.x + 1, player.y); + if (des[player.y][player.x]) + { + PrintD(player.x, player.y); + } + else + { + PrintSpace(player.x, player.y); + } + box[player.y][player.x + 1] = false; + box[player.y][player.x + 2] = true; + ++player.x; + score -= 10; + } + } + else + { + PrintO(player.x + 1, player.y); + if (des[player.y][player.x]) + { + PrintD(player.x, player.y); + } + else + { + PrintSpace(player.x, player.y); + } + ++player.x; + score -= 10; + } + } + } + } + if (GetKeyState(VK_LEFT) < 0) + { + if (clock() - t > 200) + { + t = clock(); + if (!wall[player.y][player.x - 1]) + { + if (box[player.y][player.x - 1]) + { + if (!wall[player.y][player.x - 2] && !box[player.y][player.x - 2]) + { + PrintX(player.x - 2, player.y); + PrintO(player.x - 1, player.y); + if (des[player.y][player.x]) + { + PrintD(player.x, player.y); + } + else + { + PrintSpace(player.x, player.y); + } + box[player.y][player.x - 1] = false; + box[player.y][player.x - 2] = true; + --player.x; + score -= 10; + } + } + else + { + PrintO(player.x - 1, player.y); + if (des[player.y][player.x]) + { + PrintD(player.x, player.y); + } + else + { + PrintSpace(player.x, player.y); + } + --player.x; + score -= 10; + } + } + } + } + if (GetKeyState(VK_DOWN) < 0) + { + if (clock() - t > 200) + { + t = clock(); + if (!wall[player.y + 1][player.x]) + { + if (box[player.y + 1][player.x]) + { + if (!wall[player.y + 2][player.x] && !box[player.y + 2][player.x]) + { + PrintX(player.x, player.y + 2); + PrintO(player.x, player.y + 1); + if (des[player.y][player.x]) + { + PrintD(player.x, player.y); + } + else + { + PrintSpace(player.x, player.y); + } + box[player.y + 1][player.x] = false; + box[player.y + 2][player.x] = true; + ++player.y; + score -= 10; + } + } + else + { + PrintO(player.x, player.y + 1); + if (des[player.y][player.x]) + { + PrintD(player.x, player.y); + } + else + { + PrintSpace(player.x, player.y); + } + ++player.y; + score -= 10; + } + } + } + } + if (GetKeyState(83)) + { + Save(level); + SetPosition(x, y); + printf("Saved!!!\n"); + } + for (m = 0; m < NumOfBox; ++m) + { + if (box[DesPos[m].y][DesPos[m].x]) + { + ++win; + } + } + if (win == NumOfBox) + { + system("cls"); + SetPosition(5, 4); + printf("You win!!!\n"); + SaveScore(level, score); + printf("Press Enter to continue...\n"); + return score; + } + } + back[0] = true; + printf("%d\n", back[0]); + return score; +} \ No newline at end of file diff --git a/level1/p10_pushBoxes/InitCursor.cpp b/level1/p10_pushBoxes/InitCursor.cpp new file mode 100644 index 00000000..d0edbb56 --- /dev/null +++ b/level1/p10_pushBoxes/InitCursor.cpp @@ -0,0 +1,9 @@ +#include + +void InitCursor(); + +void InitCursor() +{ + CONSOLE_CURSOR_INFO cursor = { 100, false }; + SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor); +} \ No newline at end of file diff --git a/level1/p10_pushBoxes/LoadGame.cpp b/level1/p10_pushBoxes/LoadGame.cpp new file mode 100644 index 00000000..20ad7a15 --- /dev/null +++ b/level1/p10_pushBoxes/LoadGame.cpp @@ -0,0 +1,19 @@ +#include +#include + +int LoadGame(); + +int LoadGame() +{ + FILE *fp; + fp = fopen("Save.txt", "r"); + if (fp == NULL) + { + printf("Something wrong!\n"); + system("pause"); + return 0; + } + int i = (int)fgetc(fp) - 48; + fclose(fp); + return i; +} \ No newline at end of file diff --git a/level1/p10_pushBoxes/LoadMap.cpp b/level1/p10_pushBoxes/LoadMap.cpp new file mode 100644 index 00000000..c109a4c4 --- /dev/null +++ b/level1/p10_pushBoxes/LoadMap.cpp @@ -0,0 +1,90 @@ +#include +#include +#include "game.h" + +int LoadMap(int n, char Map[][15]); + +int LoadMap(int n, char Map[][15]) +{ + int i = 0; + switch (n) + { + case 1: + FILE *fp; + fp = fopen("1.txt", "r"); + if (fp == NULL) + { + printf("Something wrong!\n"); + system("pause"); + return 0; + } + while (!feof(fp)) + { + fgets(Map[i], 15, fp); + ++i; + } + fclose(fp); + return i; + break; + case 2: + fp = fopen("2.txt", "r"); + if (fp == NULL) + { + printf("Something wrong!\n"); + return 0; + } + while (!feof(fp)) + { + fgets(Map[i], 15, fp); + ++i; + } + fclose(fp); + return i; + break; + case 3: + fp = fopen("3.txt", "r"); + if (fp == NULL) + { + printf("Something wrong!\n"); + return 0; + } + while (!feof(fp)) + { + fgets(Map[i], 15, fp); + ++i; + } + fclose(fp); + return i; + break; + case 4: + fp = fopen("4.txt", "r"); + if (fp == NULL) + { + printf("Something wrong!\n"); + return 0; + } + while (!feof(fp)) + { + fgets(Map[i], 15, fp); + ++i; + } + fclose(fp); + return i; + break; + case 5: + fp = fopen("5.txt", "r"); + if (fp == NULL) + { + printf("Something wrong!\n"); + return 0; + } + while (!feof(fp)) + { + fgets(Map[i], 15, fp); + ++i; + } + fclose(fp); + return i; + break; + } +} \ No newline at end of file diff --git a/level1/p10_pushBoxes/Print.cpp b/level1/p10_pushBoxes/Print.cpp new file mode 100644 index 00000000..a1f7e40f --- /dev/null +++ b/level1/p10_pushBoxes/Print.cpp @@ -0,0 +1,34 @@ +#include +#include "game.h" + +void PrintSpace(int x, int y); + +void PrintX(int x, int y); + +void PrintO(int x, int y); + +void PrintD(int x, int y); + +void PrintSpace(int x, int y) +{ + SetPosition(x, y); + printf(" "); +} + +void PrintX(int x, int y) +{ + SetPosition(x, y); + printf("X"); +} + +void PrintO(int x, int y) +{ + SetPosition(x, y); + printf("O"); +} + +void PrintD(int x, int y) +{ + SetPosition(x, y); + printf("D"); +} \ No newline at end of file diff --git a/level1/p10_pushBoxes/Save.cpp b/level1/p10_pushBoxes/Save.cpp new file mode 100644 index 00000000..93abbe66 --- /dev/null +++ b/level1/p10_pushBoxes/Save.cpp @@ -0,0 +1,19 @@ +#include +#include + +void Save(int i); + +void Save(int i) +{ + FILE *fp; + fp = fopen("Save.txt", "w"); + if (fp == NULL) + { + printf("Something wrong!\n"); + system("pause"); + return; + } + fputc(i + 48, fp); + fclose(fp); + return; +} \ No newline at end of file diff --git a/level1/p10_pushBoxes/Save.txt b/level1/p10_pushBoxes/Save.txt new file mode 100644 index 00000000..56a6051c --- /dev/null +++ b/level1/p10_pushBoxes/Save.txt @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/level1/p10_pushBoxes/SaveScore.cpp b/level1/p10_pushBoxes/SaveScore.cpp new file mode 100644 index 00000000..263c9b1c --- /dev/null +++ b/level1/p10_pushBoxes/SaveScore.cpp @@ -0,0 +1,27 @@ +#include +#include + +void SaveScore(int i, int score); + +void SaveScore(int i, int score) +{ + FILE *fp; + switch (i) + { + case 1:fp = fopen("score1.txt", "w"); break; + case 2:fp = fopen("score2.txt", "w"); break; + case 3:fp = fopen("score3.txt", "w"); break; + case 4:fp = fopen("score4.txt", "w"); break; + case 5:fp = fopen("score5.txt", "w"); break; + } + if (fp == NULL) + { + printf("Something wrong!\n"); + system("pause"); + return; + } + fprintf(fp, "Your score in level %d is %d...", i, score); + printf("Your score in level %d is %d...\n", i, score); + fclose(fp); + return; +} \ No newline at end of file diff --git a/level1/p10_pushBoxes/SetPosition.cpp b/level1/p10_pushBoxes/SetPosition.cpp new file mode 100644 index 00000000..3039b7e4 --- /dev/null +++ b/level1/p10_pushBoxes/SetPosition.cpp @@ -0,0 +1,11 @@ +#include + +void SetPosition(int x, int y); + +void SetPosition(int x, int y) +{ + COORD coord; + coord.X = x; + coord.Y = y; + SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); +} \ No newline at end of file diff --git a/level1/p10_pushBoxes/StartGame.cpp b/level1/p10_pushBoxes/StartGame.cpp new file mode 100644 index 00000000..0689a59c --- /dev/null +++ b/level1/p10_pushBoxes/StartGame.cpp @@ -0,0 +1,81 @@ +#include +#include "game.h" +#include +#include + +int StartGame(); + +int StartGame() +{ + system("cls"); + InitCursor(); + printf("---------------------------------\n"); + printf("\n\n\n"); + printf(" Push Boxes\n\n"); + printf(" 1. NEW GAME\n"); + printf(" 2. LOAD GAME\n"); + printf(" 3. EXIT GAME\n"); + printf("\n\n\n"); + printf("---------------------------------\n"); + printf("Press Enter to choose...\n"); + SetPosition(5, 6); + printf("->"); + clock_t t = clock(); + const int x = 5; + int y = 6; + while (GetKeyState(VK_RETURN) >= 0) + { + if (GetKeyState(VK_UP) < 0) + { + if (clock() - t > 200) + { + t = clock(); + if (y != 6) + { + PrintSpace(x, y); + PrintSpace(x + 1, y); + --y; + SetPosition(x, y); + printf("->"); + } + else + { + PrintSpace(x, y); + PrintSpace(x + 1, y); + y = 8; + SetPosition(x, y); + printf("->"); + } + } + } + else if (GetKeyState(VK_DOWN) < 0) + { + if (clock() - t > 200) + { + t = clock(); + if (y != 8) + { + PrintSpace(x, y); + PrintSpace(x + 1, y); + ++y; + SetPosition(x, y); + printf("->"); + } + else + { + PrintSpace(x, y); + PrintSpace(x + 1, y); + y = 6; + SetPosition(x, y); + printf("->"); + } + } + } + } + switch (y) + { + case 6:return 1; + case 7:return LoadGame(); + case 8:return 0; + } +} \ No newline at end of file diff --git a/level1/p10_pushBoxes/game.cpp b/level1/p10_pushBoxes/game.cpp new file mode 100644 index 00000000..631f4485 --- /dev/null +++ b/level1/p10_pushBoxes/game.cpp @@ -0,0 +1,51 @@ +#include "game.h" +#include +#include +#include + + +void game(); + +char Map[15][15]; +bool back[2]; + +void game() +{ + int i = 1; + int line; + BackToMenu: + int score = 0; + int ScoreSum = 0; + back[0] = false; + int SON = StartGame(); + if (SON == 0) + { + return; + } + i = SON; + for (i; i <= 3; i++) + { + back[0] = false; + line = LoadMap(i, Map); + score = Gaming(Map, line, back, i); + ScoreSum += score; + if (!back[0]) + { + while (GetKeyState(VK_RETURN) >= 0); + } + else + { + break; + } + } + if (back[0]) + { + system("cls"); + goto BackToMenu; + } + system("cls"); + printf("Your final score is %d\n", ScoreSum); + system("pause"); + goto BackToMenu; + return; +} \ No newline at end of file diff --git a/level1/p10_pushBoxes/game.h b/level1/p10_pushBoxes/game.h new file mode 100644 index 00000000..4903dea6 --- /dev/null +++ b/level1/p10_pushBoxes/game.h @@ -0,0 +1,29 @@ +void game(); + +int LoadMap(int n, char Map[][15]); + +int StartGame(); + +void EndGame(); + +void InitCursor(); + +void SetPosition(int x, int y); + +void PrintSpace(int x, int y); + +void PrintX(int x, int y); + +void PrintO(int x, int y); + +void PrintD(int x, int y); + +int LoadGame(); + +int ScoreBoard(); + +int Gaming(char Map[][15], int i, bool *back, int level); + +void SaveScore(int i, int score); + +void Save(int i); \ No newline at end of file diff --git a/level1/p08_hanoi/hanoi.jpg b/level1/p10_pushBoxes/hanoi.jpg old mode 100755 new mode 100644 similarity index 100% rename from level1/p08_hanoi/hanoi.jpg rename to level1/p10_pushBoxes/hanoi.jpg diff --git a/level1/p10_pushBoxes/main.cpp b/level1/p10_pushBoxes/main.cpp new file mode 100644 index 00000000..13344e65 --- /dev/null +++ b/level1/p10_pushBoxes/main.cpp @@ -0,0 +1,9 @@ +#include "game.h" +#include + +int main() +{ + game(); + return 0; +} + diff --git a/level1/p10_pushBoxes/score1.txt b/level1/p10_pushBoxes/score1.txt new file mode 100644 index 00000000..a73abccc --- /dev/null +++ b/level1/p10_pushBoxes/score1.txt @@ -0,0 +1 @@ +Your score in level 1 is 4590... \ No newline at end of file diff --git a/level1/p10_pushBoxes/score2.txt b/level1/p10_pushBoxes/score2.txt new file mode 100644 index 00000000..2ad79c2f --- /dev/null +++ b/level1/p10_pushBoxes/score2.txt @@ -0,0 +1 @@ +Your score in level 2 is 4270... \ No newline at end of file diff --git a/level1/p10_pushBoxes/score3.txt b/level1/p10_pushBoxes/score3.txt new file mode 100644 index 00000000..72fcb0a8 --- /dev/null +++ b/level1/p10_pushBoxes/score3.txt @@ -0,0 +1 @@ +Your score in level 3 is 3680... \ No newline at end of file diff --git a/level1/p10_pushBoxes/score4.txt b/level1/p10_pushBoxes/score4.txt new file mode 100644 index 00000000..e69de29b diff --git a/level1/p10_pushBoxes/score5.txt b/level1/p10_pushBoxes/score5.txt new file mode 100644 index 00000000..e69de29b diff --git a/level1/p11_linkedList/p11_linkedList.cpp b/level1/p11_linkedList/p11_linkedList.cpp new file mode 100644 index 00000000..917e4040 --- /dev/null +++ b/level1/p11_linkedList/p11_linkedList.cpp @@ -0,0 +1,143 @@ +#include +#include +#include + +int count = 0; + +typedef struct linkedList +{ + int value; + linkedList *next; +}; + +linkedList *mark; + +linkedList *reverse(linkedList *head); + +void LoadLinkedList(linkedList* head); + +void DeleteLinkedList(linkedList *head); + +int FindFive(linkedList *note); + +int main() +{ + int n = 1; + int a; + int j; + linkedList *head; + linkedList *pnew; + linkedList *tail; + pnew = (linkedList*)malloc(sizeof(linkedList)); + linkedList *p; + head = pnew; + while (true) + { + printf("Enter your number in No.%d linkedList.\nEnter 0 to quit.\n", n); + scanf("%d", &a); + if (a == 0) + { + tail->next = NULL; + free(pnew); + break; + } + pnew->value = a; + p = (linkedList *)malloc(sizeof(linkedList)); + pnew->next = p; + tail = pnew; + pnew = p; + ++n; + } + LoadLinkedList(head); + head = reverse(head); + printf("\nNow our linkedlist is reversed...\n\n"); + LoadLinkedList(head); + j = FindFive(head); + if (j != -1) + { + printf("5 is in No.%d note!!!\n", j); + } + while (j != -1 && mark != NULL) + { + j = FindFive(mark); + if (j != -1) + { + printf("5 is in No.%d note!!!\n", j); + } + } + DeleteLinkedList(head); + system("pause"); + return 0; +} + +void LoadLinkedList(linkedList* head) +{ + linkedList* read; + read = head; + int n = 1; + while (true) + { + printf("No.%d %d\n", n, read->value); + if (read->next == NULL) + { + break; + } + read = read->next; + ++n; + } +} + +linkedList *reverse(linkedList *head) +{ + linkedList *note; + linkedList *TrueHead; + TrueHead = head; + while (true) + { + note = TrueHead->next; + TrueHead->next = note->next; + note->next = head; + head = note; + if (TrueHead->next == NULL) + { + break; + } + } + return head; +} + +int FindFive(linkedList *note) +{ + while (true) + { + if (note->value == 5) + { + mark = note->next; + return ++count; + } + if (note->next == NULL) + { + return -1; + } + note = note->next; + count++; + } +} + +void DeleteLinkedList(linkedList *head) +{ + linkedList *note; + linkedList *p1; + note = head; + while (true) + { + if (note->next == NULL) + { + free(note); + return; + } + p1 = note->next; + free(note); + note = p1; + } +} \ No newline at end of file diff --git a/level1/p12_warehouse/List.txt b/level1/p12_warehouse/List.txt new file mode 100644 index 00000000..97c764d2 --- /dev/null +++ b/level1/p12_warehouse/List.txt @@ -0,0 +1,2 @@ +sdfadsfsfsfsd +33244 \ No newline at end of file diff --git a/level1/p12_warehouse/main.cpp b/level1/p12_warehouse/main.cpp new file mode 100644 index 00000000..abc368b6 --- /dev/null +++ b/level1/p12_warehouse/main.cpp @@ -0,0 +1,370 @@ +#include +#include +#include +#include +#include +#include + +typedef struct List +{ + char name[30]; + int quantity; + List *next; +}; + +int sum; + +int menu(); + +void SetPosition(int x, int y); + +int LoadInfo(List *head); + +void PrintList(List *head); + +List *Add(List *head); + +List *Delete(List *head); + +void Save(List *head); + +void FreeLoaded(List *head); + +int main() +{ + CONSOLE_CURSOR_INFO cursor = { 100, false }; + SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor); + int i; + List *head; + head = (List*)malloc(sizeof(List)); + sum = LoadInfo(head); + if (sum == 0) + { + goto po2; + } + po1: + i = menu(); + system("cls"); + switch (i) + { + case 1:PrintList(head); break; + case 2:po2: head = Add(head); break; + case 3:head = Delete(head); break; + case 4:Save(head); break; + case 5:FreeLoaded(head); return 0; + } + system("cls"); + goto po1; +} + +int LoadInfo(List *head) +{ + List *node; + List *p; + List *tail; + int len; + int n = 1; + char ch; + node = head; + FILE *fp; + fp = fopen("List.txt", "r"); + if (fgetc(fp)==EOF) + { + printf("Empty warehouse! Add somrthing now!\n"); + return 0; + } + fseek(fp, 0, SEEK_SET); + while (true) + { + if (fgets(node->name, 30, fp) == NULL) + { + tail->next = NULL; + free(p); + --n; + return n; + } + fscanf(fp, "%d\n", &node->quantity); + len = strlen(node->name); + node->name[len - 1] = '\0'; + p = (List*)malloc(sizeof(List)); + node->next = p; + tail = node; + node = p; + ++n; + } + fclose(fp); +} + +int menu() +{ + SetPosition(8, 2); + printf("Goods List!"); + SetPosition(5, 4); + printf("1. Print List"); + SetPosition(5, 5); + printf("2. Add Goods"); + SetPosition(5, 6); + printf("3. Remove Goods"); + SetPosition(5, 7); + printf("4. Save List"); + SetPosition(5, 8); + printf("5. Exit"); + SetPosition(2, 4); + printf("->"); + int x = 2; + int y = 4; + clock_t t = clock(); + while (GetKeyState(VK_RETURN) >= 0) + { + if (GetKeyState(VK_UP) < 0) + { + if (clock() - t > 200) + { + t = clock(); + if (y != 4) + { + SetPosition(x, y); + printf(" "); + --y; + SetPosition(x, y); + printf("->"); + } + else + { + SetPosition(x, y); + printf(" "); + y = 8; + SetPosition(x, y); + printf("->"); + } + } + } + else if (GetKeyState(VK_DOWN) < 0) + { + if (clock() - t > 200) + { + t = clock(); + if (y != 8) + { + SetPosition(x, y); + printf(" "); + ++y; + SetPosition(x, y); + printf("->"); + } + else + { + SetPosition(x, y); + printf(" "); + y = 4; + SetPosition(x, y); + printf("->"); + } + } + } + } + switch (y) + { + case 4:return 1; break; + case 5:return 2; break; + case 6:return 3; break; + case 7:return 4; break; + case 8:return 5; break; + } +} + +void SetPosition(int x, int y) +{ + COORD coord; + coord.X = x; + coord.Y = y; + SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); +} + +void PrintList(List *head) +{ + int i = 1; + List *node; + clock_t t = clock(); + node = head; + while (true) + { + if (node->next == NULL) + { + printf("No.%d goods is %s. Its quantity is %d\n", i, node->name, node->quantity); + printf("Press Esc to continue...\n"); + while (GetKeyState(VK_ESCAPE) >= 0); + return; + } + printf("No.%d goods is %s. Its quantity is %d\n", i, node->name, node->quantity); + node = node->next; + ++i; + } +} + +void FreeLoaded(List *head) +{ + List *note; + List *p; + note = head; + while (true) + { + if (note->next == NULL) + { + free(note); + return; + } + p = note->next; + free(note); + note = p; + } +} + +List *Add(List *head) +{ + List *node; + List *p; + int i, n, len; + printf("Now we have %d kind(s) of goods\n", sum); + printf("Enter the Position you want to add...\n"); +po2: + node = head; + scanf("%d", &n); + if (n > sum + 1) + { + printf("Wrong! Try again...\n"); + goto po2; + } + for (i = 1; i < n - 1; ++i) + { + if (i == sum) + { + break; + } + node = node->next; + } + p = (List*)malloc(sizeof(List)); + printf("Please Enter the name of good you want to add...\n"); + while (getchar() != '\n'); + fgets(p->name, 30, stdin); + len = strlen(p->name); + p->name[len - 1] = '\0'; + printf("Please Enter the quantity of good you want to add...\n"); + scanf("%d", &p->quantity); + if (n == 1) + { + if (sum == 0) + { + head = p; + p->next = NULL; + sum++; + goto po3; + } + p->next = head; + head = p; + ++sum; + goto po3; + } + else if (n == sum + 1) + { + p->next = NULL; + } + else + { + p->next = node->next; + } + node->next = p; + ++sum; +po3: + printf("Press Shift to add another good...\nPress Esc to back to the Menu...\n"); + while (true) + { + if (GetKeyState(VK_ESCAPE) < 0) + { + return head; + } + if (GetKeyState(VK_SHIFT) < 0) + { + printf("Now we have %d kind(s) of goods\n", sum); + printf("Enter the Position you want to add...\n"); + goto po2; + } + } +} + +List *Delete(List *head) +{ + int n, i; + n - 0; + List *node1, *p, *node2; + printf("Now we have %d kind(s) of goods\n", sum); + printf("Enter the node you want to delete..."); +po1: + node1 = head; + node2 = head->next; + scanf("%d", &n); if (n > sum + 1) + { + printf("Wrong! Try again...\n"); + goto po1; + } + for (i = 1; i < n - 2; ++i) + { + node2 = node2->next; + node1 = node1->next; + } + if (n == 1) + { + free(node1); + head = node2; + sum--; + } + else + { + node1->next = node2->next; + free(node2); + sum--; + } + printf("Done!\n"); + printf("Press Shift to delete another good...\nPress Esc to back to the Menu...\n"); + while (true) + { + if (GetKeyState(VK_ESCAPE) < 0) + { + return head; + } + if (GetKeyState(VK_SHIFT) < 0) + { + printf("Now we have %d kind(s) of goods\n", sum); + printf("Enter the node you want to delete...\n"); + goto po1; + } + } +} + +void Save(List *head) +{ + FILE *fp; + List *node; + node = head; + fp = fopen("List.txt", "w"); + if (fp == NULL) + { + printf("GG\n"); + return; + } + while (true) + { + if (node->next == NULL) + { + fprintf(fp, "%s\n", node->name); + fprintf(fp, "%d", node->quantity); + printf("Done\n"); + clock_t t = clock(); + while (clock() - t < 1000); + return; + } + fprintf(fp, "%s\n", node->name); + fprintf(fp, "%d\n", node->quantity); + node = node->next; + } +} \ No newline at end of file