diff --git a/practices/c/level1/p01_runningLetter/runningLetter.c b/practices/c/level1/p01_runningLetter/runningLetter.c new file mode 100644 index 00000000..320f6dcc --- /dev/null +++ b/practices/c/level1/p01_runningLetter/runningLetter.c @@ -0,0 +1,25 @@ +#include +#include +#define BOUNDRY 80 + +void move(int m); + +void main() +{ + for ( int i = 0;i < BOUNDRY;i++) { + move(i); + } + for (int i = BOUNDRY;i >= 0;i--) { + move(i); + } +} + +void move(int m) +{ + Sleep(50); + system("cls"); + for (int j = 0;j < m - 1;j++) { + printf(" "); + } + printf("R"); +} diff --git a/practices/c/level1/p02_isPrime/isPrime.c b/practices/c/level1/p02_isPrime/isPrime.c new file mode 100644 index 00000000..a43b25cd --- /dev/null +++ b/practices/c/level1/p02_isPrime/isPrime.c @@ -0,0 +1,36 @@ +#include + +int main() +{ + long int i, j; + + printf("Please input a number: \t"); + scanf_s("%d", &i); + + if (i == 2) { + printf("%d is a Prime.", i); + } + else if (i == 1) { + printf("%d is not a Prime.", i); + } + else { + j = 2; + while (i > j) { + if (i%j == 0) { + break; + } + else { + if (i == j + 1) { + printf("%d is a Prime.", i); + break; + } + j++; + + } + } + + if (i > j + 1) { + printf("%d is not a Prime.", i); + } + } +} \ No newline at end of file diff --git a/practices/c/level1/p03_Diophantus/Diophantus.c b/practices/c/level1/p03_Diophantus/Diophantus.c new file mode 100644 index 00000000..0b4692f8 --- /dev/null +++ b/practices/c/level1/p03_Diophantus/Diophantus.c @@ -0,0 +1,16 @@ +#include +int main() +{ + int i, j, k, l; + for (i = 100;i < 1000;i++) + { + k = i % 10; + j = (i % 100) / 10; + l = i / 100; + if (i == (k*k*k) + (j*j*j) + (l*l*l)) + { + printf("%5d", i); + } + } + return 0; +} \ No newline at end of file diff --git a/practices/c/level1/p04_ narcissus/.vs/ConsoleApplication5/v14/.suo b/practices/c/level1/p04_ narcissus/.vs/ConsoleApplication5/v14/.suo new file mode 100644 index 00000000..3dc0deeb Binary files /dev/null and b/practices/c/level1/p04_ narcissus/.vs/ConsoleApplication5/v14/.suo differ diff --git a/practices/c/level1/p04_ narcissus/narcissus.c b/practices/c/level1/p04_ narcissus/narcissus.c new file mode 100644 index 00000000..0b0564f2 --- /dev/null +++ b/practices/c/level1/p04_ narcissus/narcissus.c @@ -0,0 +1,17 @@ +#include + +void main() +{ + int one, ten, hun; + + for (int num= 100;num < 1000;num++) + { + one = num % 10; + ten = (num % 100) / 10; + hun = num / 100; + if (num == (one*one*one) + (ten*ten*ten) + (hun*hun*hun)) + { + printf("%5d", num); + } + } +} \ No newline at end of file diff --git a/practices/c/level1/p05_allPrimes/allPrimes.c b/practices/c/level1/p05_allPrimes/allPrimes.c new file mode 100644 index 00000000..af94adbe --- /dev/null +++ b/practices/c/level1/p05_allPrimes/allPrimes.c @@ -0,0 +1,31 @@ +#include +#include + +int main() +{ + clock_t start, finish; + double duration; + + start = clock(); + printf(" 2"); + for (int i = 2;i <= 1000;i++) + { + int j = 2; + while (i > j) { + if (i%j == 0) { + break; + } + else { + if (i == j + 1) { + printf("%5d", i); + break; + } + j++; + + } + } + } + finish = clock(); + duration = (finish - start)*1.0 / CLOCKS_PER_SEC; + printf("\n程序运行时间为:%lfs", duration); +} \ No newline at end of file diff --git a/practices/c/level1/p06_Goldbach/Godback.c b/practices/c/level1/p06_Goldbach/Godback.c new file mode 100644 index 00000000..98f298eb --- /dev/null +++ b/practices/c/level1/p06_Goldbach/Godback.c @@ -0,0 +1,40 @@ +#include; + +void main() +{ + int t = 1, m, n; + int num[101] = { 0 }; + int prime[26]; + + for (int a = 2;a <= 10;a++) { + if (num[a] == 0) { + for (int b = 2 * a;b <= 100;b += a) + num[b] = 1; + } + } + + for (int a = 2;a <= 100;a++) { + if (num[a] == 0) { + prime[t] = a; + t += 1; + } + } + + for (m = 1;m <= 50;m++) { + n = 2 * m; //nʾ100ڵż + for (int p = 1;p <= 26;p++) { + for (int d = 1;d <= p;d++) { + if (n == prime[p] + prime[d]) + printf("%2d=%2d+%2d\t", n, prime[p], prime[d]); + } + } + } + printf("\n\n"); + + if (m = 50) { + printf("ˣ100ڸ°ͺղȷ"); + } + else { + printf("ˣ100ڸ°ͺղ벻ȷ"); + } +} \ No newline at end of file diff --git a/practices/c/level1/p07_encrypt_decrypt/encrypt_decrypt.c b/practices/c/level1/p07_encrypt_decrypt/encrypt_decrypt.c new file mode 100644 index 00000000..4f111fec --- /dev/null +++ b/practices/c/level1/p07_encrypt_decrypt/encrypt_decrypt.c @@ -0,0 +1,68 @@ +#include +#include +void encrypt(); +void decrypt(); + +int L = 5; +char k, ch; +char* p; +int count = 0; + +int main() +{ + p = (char*)malloc(sizeof(char) * L); + printf("ѡܻǽ:a b\n"); + k = getch(); + if (k == 'a') { + encrypt(); + } + + + else if (k == 'b') { + decrypt(); + } + else { + printf("Ϣ."); + return 0; + } +} + + +void encrypt() +{ + printf("ַܵ: "); + do + { + ch = getchar(); + count++; + if (count >= L) { + p = (char*)realloc(p, sizeof(char*) * (++L)); + } + p[count - 1] = (char)((int)ch + 4); + } while (ch != '\n'); + printf("һ%dַ\nܺΪ", count - 1); + for (int i = 0;i < count;i++) { + printf("%c", p[i]); + } + free(p); +} + + +void decrypt() +{ + printf("ַܵ: "); + do + { + ch = getchar(); + count++; + if (count >= L) { + p = (char*)realloc(p, sizeof(char*) * (++L)); + } + p[count - 1] = (char)((int)ch - 4); + } while (ch != '\n'); + printf("һ%dַ\nܺΪ", count - 1); + for (int i = 0;i < count;i++) { + printf("%c", p[i]); + } + free(p); +} \ No newline at end of file diff --git a/practices/c/level1/p08_hanoi/hanoi.c b/practices/c/level1/p08_hanoi/hanoi.c new file mode 100644 index 00000000..21af806d --- /dev/null +++ b/practices/c/level1/p08_hanoi/hanoi.c @@ -0,0 +1,28 @@ +#include +#include +void gotoxy(int x, int y); + +int main() +{ + boundry(); + +} + +void boundry() +{ + gotoxy(20, 20); + printf("1"); + gotoxy(20, 22); + printf("2"); + gotoxy(20, 24); + printf("3"); + gotoxy(20, 26); + printf("4"); +} +void gotoxy(int x, int y)//ָ光标控制函数 +{ + COORD c; + c.X = x; + c.Y = y; + SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); +} \ No newline at end of file diff --git a/practices/c/level1/p08_hanoi/hanoi.exe b/practices/c/level1/p08_hanoi/hanoi.exe new file mode 100644 index 00000000..5a11b09a Binary files /dev/null and b/practices/c/level1/p08_hanoi/hanoi.exe differ diff --git a/practices/c/level1/p09_maze/maze.c b/practices/c/level1/p09_maze/maze.c new file mode 100644 index 00000000..8b260c58 --- /dev/null +++ b/practices/c/level1/p09_maze/maze.c @@ -0,0 +1,123 @@ +##define _CRT_SECURE_NO_WARNINGS//大神教的 +#include +#include + +void turn_up(); +void turn_down(); +void turn_left(); +void turn_right(); +void show(); + +char map[22][22]; +int x = 20, y = 0; +int point = 'R'; + +int main() +{ + int i=0, j=0; + char *p; + char step; + p = map[0]; + FILE *fp; + fp = fopen("mazemap.txt", "r"); + + if (fp == NULL) { + printf("can't open"); + exit(1); + } + + for (;p < map[0] + 484;p++) { + fscanf_s(fp, "%c", p); + if ((p - map[0]) % 22 == 0) { + i += 1; + j = 0; + } + j++; + } + map[x][y] = 'R';; + show(); + + while (x > 0 || y < 20) { + step = _getch(); + switch (step) { + case 'w': { + turn_up(); + break; + } + case 'a': { + turn_left(); + break; + } + case 'd': { + turn_right(); + break; + } + case 's': { + turn_down(); + break; + } + } + } + system("cls"); + printf(" That's cool , you have win!\n"); + getchar(); +} + + +void show() +{ + for (int i = 0;i < 22;i++) { + for (int j = 0;j < 22;j++) { + printf("%c", map[i][j]); + if (j == 21)printf("\n"); + } + } +} + + +void turn_up() +{ + if (map[x - 1][y] != '*') { + map[x][y] = ' '; + map[x - 1][y] = point; + system("cls"); + show(); + x = x - 1; + } +} + + +void turn_left() +{ + if (map[x][y - 1] != '*') { + map[x][y] = ' '; + map[x][y - 1] = point; + system("cls"); + show(); + y = y - 1; + } +} + + +void turn_right() +{ + if (map[x][y + 1] != '*') { + map[x][y] = ' '; + system("CLS"); + map[x][y + 1] = point; + show(); + y = y + 1; + } +} + + +void turn_down() +{ + if (map[x + 1][y] != '*') { + map[x][y] = ' '; + map[x + 1][y] = point; + system("cls"); + show(); + x += 1; + } +} diff --git a/practices/c/level1/p09_maze/mazemap.txt b/practices/c/level1/p09_maze/mazemap.txt new file mode 100644 index 00000000..f4b4a013 --- /dev/null +++ b/practices/c/level1/p09_maze/mazemap.txt @@ -0,0 +1 @@ +******************** ** ** * ** ***** * *** ***** ** * * ** * * ** * *** * * ***** **** * * ** * ***** *** ** * * * ***** *** ** **** * * * * * ** * * * * * ****** **** ******* * * ** * * *** ** *** ******** * * ** * * * * **** * * ******** * * ** * * * *** * ** * **** *** * * ** * * * *** * ***** ******* * * *** ** * * * * ***** ******* * * * * * *********************** \ No newline at end of file diff --git a/practices/c/level1/p10_pushBoxes/pushbox.c b/practices/c/level1/p10_pushBoxes/pushbox.c new file mode 100644 index 00000000..87e3fb8c --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/pushbox.c @@ -0,0 +1,179 @@ +#define _CRT_SECURE_NO_WARNINGS//̵ +#include +#include +#include + +//ļжȡͼʱͼĹ +#define w 219;//ǽ +#define b 254;// +#define l 255;//· +#define r 208;// + +void show(); +void turnup(); +void turndown(); +void turnleft(); +void turnright(); + + +char map[8][8]; +int x = 4, y = 4; +char step; + +int main() +{ + int i = 0; + char *p; + p = map; + FILE *fp; + fp = fopen("pushbox_0.txt", "rb"); + + if (fp == NULL) { + printf("can't open"); + exit(1); + } + while (fscanf_s(fp, "%c", p) != EOF) { + p++; + } + + + //Сˡӳʼλ + map[x][y] = r; + map[5][4] = b; + map[4][3] = b; + map[3][3] = b; + map[3][5] = b; + show(); + //ж + + do { + step = _getch(); + putchar(step); + switch (step) + { + case'w': + turnup(); + break; + case'a': + turnleft(); + break; + case's': + turndown(); + break; + case'd': + turnright(); + break; + } + } + //־ + while ((map[4][1] != (char)254) || (map[1][3] != (char)254) || (map[3][6] != (char)254) || (map[6][4] != (char)254)); + system("cls"); + printf("you have win!\n"); + system("pause"); +} + + +void show() +{ + system("chcp 437"); + for (int i = 0;i < 8;i++) { + for (int j = 0;j < 8;j++) { + if (map[i][j] == 'w') { + printf("%c", 219); + } + else if (map[i][j] == 'l' || map[i][j] == ' ') { + printf("%c", 255); + } + else + printf("%c", map[i][j]); + if (j == 7) { + printf("\n"); + } + } + } +} + + + + +void turnup() +{ + if ((map[x - 1][y] == (char)254) && (map[x - 2][y] == 'l')) + { + system("cls"); + map[x][y] = 'l'; + map[x - 1][y] = 208; + map[x - 2][y] = 254; + x -= 1; + show(); + } + else if (map[x - 1][y] == 'l') { + system("cls"); + map[x][y] = 'l'; + map[x - 1][y] = 208; + x -= 1; + show(); + } +} + +void turndown() +{ + if ((map[x + 1][y] == (char)254) && (map[x + 2][y] == 'l')) + { + putchar(step); + system("cls"); + map[x][y] = 'l'; + map[x + 1][y] = 208; + map[x + 2][y] = 254; + x += 1; + show(); + } + else if (map[x + 1][y] == 'l') { + system("cls"); + map[x][y] = 'l'; + map[x + 1][y] = 208; + x += 1; + show(); + } +} + +void turnleft() +{ + if ((map[x][y - 1] == (char)254) && (map[x][y - 2] == 'l')) + { + system("cls"); + map[x][y] = 'l'; + map[x][y - 1] = 208; + map[x][y - 2] = 254; + y -= 1; + show(); + } + else if (map[x][y - 1] == 'l') { + system("cls"); + map[x][y] = 'l'; + map[x][y - 1] = 208; + y -= 1; + show(); + } +} + +void turnright() +{ + if ((map[x][y + 1] == (char)254) && (map[x][y + 2] == 'l')) + { + system("cls"); + map[x][y] = 'l'; + map[x][y + 1] = 208; + map[x][y + 2] = 254; + y += 1; + show(); + } + else if (map[x][y + 1] == 'l') { + system("cls"); + map[x][y] = 'l'; + map[x][y + 1] = 208; + y += 1; + show(); + } +} + diff --git a/practices/c/level1/p10_pushBoxes/pushbox_0.txt b/practices/c/level1/p10_pushBoxes/pushbox_0.txt new file mode 100644 index 00000000..0580d88c --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/pushbox_0.txt @@ -0,0 +1 @@ + www wlw wlwwwwwwwllllwwllllwwwwwwwlw wlw www \ No newline at end of file diff --git a/practices/c/level1/p11_linkedList/lianbiao.c b/practices/c/level1/p11_linkedList/lianbiao.c new file mode 100644 index 00000000..09b51b51 --- /dev/null +++ b/practices/c/level1/p11_linkedList/lianbiao.c @@ -0,0 +1,152 @@ +#include +#include +int fan(); +void input(); +void show(); +void divided5(); +void Free(); + +static int i = 1, a; +int *K; +struct slist { + long int value; + int number; + struct slist*next; +}; +struct slist*head = NULL; +struct slist*prev = NULL, *cur; + + +int main(void) +{ + int k; + + printf("if you want to input data, please input any key,otherwise 2"); + while (getchar() != '2') { + printf("data:"); + input(); + } + + if (head == NULL) { + printf("No datas were inputd\n"); + } + else { + printf("Here are the datas you've inputted:\n"); + } + + cur = head; + show(); + + if (i > 2) { + fan(); + printf("Here are the datas :\n"); + show(); + } + else if (i == 2) { + printf("Here are the datas :\n"); + printf("1һΪ%d\n", prev->value); + } + + divided5(); + Free(); +} + + +int fan() +{ + struct slist*next = NULL; + if (NULL != head->next) { + prev = head; + cur = prev->next; + next = cur->next; + } + else if (NULL == head->next) { + prev = head; + cur = prev->next; + } + + i = i - 1; + prev->next = NULL; + prev->number = i; + + while (1) { + i--; + cur->number = i; + cur->next = prev; + + if (next) { + prev = cur; + cur = next; + next = next->next; + } + else { + return K = cur; + } + } +} + + +void input() +{ + cur = (struct slist*)malloc(sizeof(struct slist)); + if ((struct slist*)malloc(sizeof(struct slist)) == NULL) + return -1; + if (head == NULL) { + head = cur; + } + else { + prev->next = cur; + } + cur->next = NULL; + scanf_s("%d", &a); + cur->value = a; + cur->number = i; + printf("if you want to input another data, please input any key,otherwise 2"); + getchar(); + prev = cur; + ++i; +} + + +void show() +{ + while (cur != NULL) { + printf("%dΪ%d\n", cur->number, cur->value); + cur = cur->next; + } +} + +void divided5() +{ + if (i == 2) { + if (prev->value % 5 == 0) { + printf("ܱݵλΪ 1\n"); + } + else { + printf("ûݿԱ5"); + } + } + else { + printf("ܱ5λΪ\n"); + cur = K; + while (cur != NULL) { + if (cur->value % 5 == 0) { + printf("%5d", cur->number); + } + cur = cur->next; + } + + } +} + +void Free() +{ + if (head != NULL) { + cur = head; + while (cur != NULL) { + prev = cur; + cur = prev->next; + free(prev); + } + } +} \ No newline at end of file diff --git a/practices/c/level1/p12_warehouse/warehousu.c b/practices/c/level1/p12_warehouse/warehousu.c new file mode 100644 index 00000000..211dd357 --- /dev/null +++ b/practices/c/level1/p12_warehouse/warehousu.c @@ -0,0 +1,224 @@ +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include + +void gotoxy(int x, int y); +void read(); +void boundry(); +void show(); +void remote(); +void over(); +void add(); + +struct slist{ + char name[20]; + int value; + struct slist*next; +}; +struct slist*head = NULL,*tail=NULL; +struct slist*prev = NULL, *cur=NULL; +FILE *fp; + + +int main() +{ + char k; + read(); + while (1) { + boundry(); + k = getchar(); + switch (k) { + case '2': { + show(); + break; + } + case '3': { + add(); + break; + } + case '4': { + remote(); + break; + } + case '5': { + over(); + goto end; + } + } + } + end:return 0; +} + +void boundry() +{ + system("cls"); + gotoxy(20, 8); + printf("按数字键选择菜单功能"); + gotoxy(20, 10); + printf("2.显示存货列表"); + gotoxy(20, 12); + printf("3.入库"); + gotoxy(20, 14); + printf("4.出库"); + gotoxy(20, 16); + printf("5.退出程序"); + gotoxy(20, 18); +} +void gotoxy(int x, int y)//指定显示位置 +{ + COORD c; + c.X = x; + c.Y = y; + SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); +} + +void read() { + FILE *fp = NULL; + fp = fopen("warehouse.txt", "rb"); + if (fp == NULL) { + printf("can't open the file.\n"); + exit(1); + } + rewind(fp); + fseek(fp, 0L, SEEK_END); + if (ftell(fp) == 0) + { + return; + } + else{ + fseek(fp, 0, SEEK_SET); + cur = (struct slist*)malloc(sizeof(struct slist)); + head = cur; + cur->next = NULL; + do{ + fscanf(fp, "%s%d ", cur->name, &cur->value); + prev = cur; + cur = (struct slist*)malloc(sizeof(struct slist)); + prev->next = cur; + } while (!feof(fp)); + free(cur); + prev->next = NULL; + } + fclose(fp); +} + +void show() +{ + system("cls"); + if (head == NULL) { + printf("There's no products.\n"); + } + else { + cur = head; + while (cur != NULL) { + printf("%20s%20d", cur->name, cur->value); + printf("\n"); + cur = cur->next; + } + } + printf("Press any key to return.\n\n"); + system("pause"); +} + +void add() +{ + long int i; + char a[10]; + char *find=NULL; + system("cls"); + printf("Please input the product's name:"); + scanf("%s", &a); + printf("Please input the number that you want to add:"); + scanf("%d", &i); + if (head != NULL) { + cur = head; + while (cur != NULL) { + if (find = strstr(cur->name, a)) { + cur->value = cur->value + i; + goto next; + } + else { + cur = cur->next; + } + } + } + if(find == NULL){ + cur = (struct slist*)malloc(sizeof(struct slist)); + if (head == NULL) { + head = cur; + } + else { + prev->next = cur; + } + strcpy(cur->name,a); + cur->value = i; + } + next:; + printf("If you want to input another product,please input 1 ,otherwise,any key to return:"); + getchar(); + if (getchar() == '1') { + prev = cur; + prev->next = NULL; + add(); + } + else { + prev = cur; + prev->next = NULL; + } +} + +void remote() +{ + system("cls"); + char D[20]; + char *find = NULL; + int k = 0; + printf("Please input the product that you want remote. "); + scanf("%s", &D); + cur = head; + while (cur != NULL) { + if (find=strstr(cur->name,D)) { + printf("The number of the %s is %d\n", cur->name, cur->value); + break; + } + else { + cur = cur->next; + } + } + if (find) { + printf("Please input the number that you want remote ( don't be more than %d):", cur->value); + scanf("%d", &k); + if (k < cur->value) { + cur->value -= k; + } + else { + prev->next = cur->next; + free(cur); + } + } + else { + printf("There is no datas of what you want remote.\n"); + } + printf("Finished!"); + system("pause"); +} + +void over() +{ + FILE *fp = NULL; + fp = fopen("warehouse.txt", "w"); + if (fp == NULL) { + printf("can't open the file.\n"); + exit(1); + } + rewind(fp); + cur = head; + while (cur != NULL) { + fprintf(fp, "%s %d ", cur->name, cur->value); + prev = cur; + cur = prev->next; + free(prev); + } + fclose(fp); +} \ No newline at end of file diff --git a/practices/cpp/level1/p01_Queue/Queue.cpp b/practices/cpp/level1/p01_Queue/Queue.cpp new file mode 100644 index 00000000..00f0b019 --- /dev/null +++ b/practices/cpp/level1/p01_Queue/Queue.cpp @@ -0,0 +1,28 @@ +#include"Queue.h" + +using namespace std; +void Queue::show() { + cout << "Here are the datas in the queue:"; + + for (int i = head;(i % 20 + 1) <= tail;i++) { + cout << data[i]<<" "; + } +} + +void Queue::append(int num) { + data[tail] = num; + tail = (tail + 1) % 20; +} + +int Queue::pop() { + head = (head + 1) % 20; + return data[head]; +} + +bool Queue::isEmpty() { + return(head == tail); +} + +bool Queue::isFull() { + return((tail + 1) == head); +} \ No newline at end of file diff --git a/practices/cpp/level1/p01_Queue/Queue.h b/practices/cpp/level1/p01_Queue/Queue.h new file mode 100644 index 00000000..6919b315 --- /dev/null +++ b/practices/cpp/level1/p01_Queue/Queue.h @@ -0,0 +1,15 @@ +#include +class Queue { + +public: + void show(); + void append(int num); + int pop(); + bool isEmpty(); + bool isFull(); + +private: + int data[20]; + int head = 0; + int tail = 0; +}; diff --git a/practices/cpp/level1/p01_Queue/main.cpp b/practices/cpp/level1/p01_Queue/main.cpp new file mode 100644 index 00000000..09224144 --- /dev/null +++ b/practices/cpp/level1/p01_Queue/main.cpp @@ -0,0 +1,58 @@ +#include +#include"Queue.h" +/*todolist: + 1显示队列中元素:show + 2向队列中添加元素:append + 3从队列中弹出一个元素:pop + 4查看队列是否已满:isFull + 5退出*/ + +Queue queue; +void main() +{ + using namespace std; + while (1) { + system("cls"); + cout << "Please input the option you want to do:" << endl; + cout << "1.show\n2.append(int num)\n3.pop\n4.?isFull\n5.over" << endl; + int k; + scanf_s("%d", &k); + switch (k) { + case 1:if (!queue.isEmpty()) { + queue.show(); + } + else { + cout << "The queue is empty." << endl; + } + system("pause"); + break; + case 2:if (!queue.isFull()) { + cout << "Please input the number." << endl; + int n=0; + cin >> n; + queue.append(n); + } + else { + cout << "The queue has been full." << endl; + system("pause"); + } + break; + case 3:if (!queue.isEmpty()) { + int n; + n = queue.pop(); + } + system("pause"); + break; + case 4:if (queue.isFull()) { + cout << "The queue has been full" << endl; + } + else { + cout << "The queue has not been full" << endl; + } + system("pause"); + break; + case 5:goto END; + } + } + END:; +} \ No newline at end of file diff --git a/practices/cpp/level1/p02_Stack/Stack.cpp b/practices/cpp/level1/p02_Stack/Stack.cpp new file mode 100644 index 00000000..e428fb8b --- /dev/null +++ b/practices/cpp/level1/p02_Stack/Stack.cpp @@ -0,0 +1,35 @@ +#include"Stack.h" + +using namespace std; +void Stack::create(int n) +{ + size = n; + data = new int[size]; +} + +void Stack::show() +{ + cout << "Here are the datas in the stack:"; + + for (int i = 0;i < cur;i++) { + cout << *(data+i) << " "; + } +} + +void Stack::append(int num) { + *(data+cur) = num; + cur += 1; +} + +int Stack::pop() { + cur -= 1; + return *(data+cur+1); +} + +bool Stack::isFull() { + return(cur ==size); +} + +bool Stack::isEmpty() { + return(cur == 0); +} \ No newline at end of file diff --git a/practices/cpp/level1/p02_Stack/Stack.h b/practices/cpp/level1/p02_Stack/Stack.h new file mode 100644 index 00000000..91d183f2 --- /dev/null +++ b/practices/cpp/level1/p02_Stack/Stack.h @@ -0,0 +1,27 @@ +#include + +class Stack { + +public: + Stack(){ + data = NULL; + cur = 0; + size = 0; + }; + void create(int n); + void show(); + void append(int num); + int pop(); + bool isFull(); + bool isEmpty(); + ~Stack() { + if (data != NULL) { + delete[]data; + } + }; + +private: + int *data; + int cur; + int size; +}; \ No newline at end of file diff --git a/practices/cpp/level1/p02_Stack/main.cpp b/practices/cpp/level1/p02_Stack/main.cpp new file mode 100644 index 00000000..fd0bdfab --- /dev/null +++ b/practices/cpp/level1/p02_Stack/main.cpp @@ -0,0 +1,64 @@ +#include +#include"Stack.h" + +/*To do list + 1、显示栈中数据 + 2、向栈中添加元素 + 3、从栈中弹出一个元素 + 4、判断栈是否已满 + 5、退出程序 + */ + +Stack stack; +void main() +{ + using namespace std; + cout << "Input the size of stack:" << endl; + int n; + cin >> n; + stack.create(n); + while (1) { + system("cls"); + cout << "Please input the option you want to do:" << endl; + cout << "1.show\n2.append(int num)\n3.pop\n4.?isFull\n5.over" << endl; + int k; + scanf_s("%d", &k); + switch (k) { + case 1:if (!stack.isEmpty()) { + stack.show(); + } + else { + cout << "The stack is empty." << endl; + } + system("pause"); + break; + case 2:if (!stack.isFull()) { + cout << "Please input the number." << endl; + int n = 0; + cin >> n; + stack.append(n); + } + else { + cout << "The stack has been full." << endl; + system("pause"); + } + break; + case 3:if (!stack.isEmpty()) { + int n; + n = stack.pop(); + } + system("pause"); + break; + case 4:if (stack.isFull()) { + cout << "The stack has been full" << endl; + } + else { + cout << "The stack has not been full" << endl; + } + system("pause"); + break; + case 5:goto END; + } + } + END:; +} \ No newline at end of file diff --git a/practices/cpp/level1/p03_SafeArray/SafeArray.cpp b/practices/cpp/level1/p03_SafeArray/SafeArray.cpp new file mode 100644 index 00000000..1d91b32a --- /dev/null +++ b/practices/cpp/level1/p03_SafeArray/SafeArray.cpp @@ -0,0 +1,17 @@ +#include"SafeArray.h" + +void SafeArray::insert(int num, int No) +{ + array[No] = num; +} + +void SafeArray::look(int i) +{ + using namespace std; + cout< +class SafeArray +{ +private: + int array[100]; + +public: + void insert(int num,int No); + void look(int i); + bool judge(int n); + SafeArray() + { + for (int i = 0;i < 100;i++) { + array[i] = 0; + } + } +}; diff --git a/practices/cpp/level1/p03_SafeArray/main.cpp b/practices/cpp/level1/p03_SafeArray/main.cpp new file mode 100644 index 00000000..d32c279a --- /dev/null +++ b/practices/cpp/level1/p03_SafeArray/main.cpp @@ -0,0 +1,47 @@ +#include +#include"SafeArray.h" +#include + +SafeArray t; +using namespace std; + +int main(void) +{ + while (1) { + system("cls"); + cout << "请选择您想做的事情: 1.修改或添加数组元素 2.查看数组元素 3.结束" << endl; + int n; + cin >> n; + switch (n) { + case 1: + int No; + cout << "请输入您想操作的元素的下标:"; + cin >> No; + if (t.judge(No)) { + cout << "数组元素:"; + int num; + cin >> num; + t.insert(num, No); + } + else { + cout << "下标越界,操作无效"; + system("pause"); + } + break; + case 2: + cout << "请依次输入您想操作的元素的下标:"; + cin >> No; + if (t.judge(No)) { + t.look(No); + system("pause"); + } + else { + cout << "下标越界,操作无效"; + } + break; + case 3: + goto end; + } + } + end:; +} diff --git a/practices/cpp/level1/p04_cppScoreManagement/cppScoreManagement.cpp b/practices/cpp/level1/p04_cppScoreManagement/cppScoreManagement.cpp new file mode 100644 index 00000000..7df72409 --- /dev/null +++ b/practices/cpp/level1/p04_cppScoreManagement/cppScoreManagement.cpp @@ -0,0 +1,88 @@ +# include"cppScoreManagement.h" +#include + +void Student::display() +{ + using namespace std; + cout << setw(10) << name << setw(10) << num << setw(10) <name = name; + this->num = num; + this->cppmark = cppmark; +} + +void Class::add(string name) +{ + using namespace std; + for (int i = 0;i < 30;i++) { + if (list[i] == "n") { + list[i] = name; + No += 1; + break; + } + cout << "班级已满" << endl; + } +} + +void Class::add(Student name) +{ + for (int i = 0;i < 30;i++) { + if (str[i].name == "n") { + str[i] = name; + break; + } + } +} + +void Class::cut(string name) +{ + using namespace std; + for (int i = 0;i < 30;i++) { + if (list[i]==name){ + list[i] ="n"; + str[i].name = "n"; + No -= 1; + break; + } + if (i = 29) { + cout << "班级中无此人" << endl; + } + } +} + +void Class::display2() +{ + using namespace std; + cout << "共有" << No << "位学生选了该门课" << endl; + for (int i = 0;i < 30;i++) { + if (list[i] != "n") { + cout < +#include +using namespace std; +class Student +{ +public: + void display(); + void modify(string name,int num, int cppmark); + string name = "n"; + int cppmark = 0; + int num = 0; +}; + +class Class +{ +public: + void add(string name); + void add(Student); + void cut(string name); + void display2(); + void display(string name); + bool judge(string name); + Class() + { + for (int i = 0;i < 30;i++) { + list[i] = "n"; + } + } + +private: + string list[30]; + static int No; + Student str[30]; +}; diff --git a/practices/cpp/level1/p04_cppScoreManagement/main.cpp b/practices/cpp/level1/p04_cppScoreManagement/main.cpp new file mode 100644 index 00000000..38491a84 --- /dev/null +++ b/practices/cpp/level1/p04_cppScoreManagement/main.cpp @@ -0,0 +1,85 @@ +#include +#include"cppScoreManagement.h" +#include + +/*todolist: +1.对学生操作应当以班级为单位 +2.对成绩操作应当通过学生 +*/ +Class cpp; + +void main() +{ + while (1) { + system("cls"); + cout << "1、查看学生名单\t2、选课\t3、退课\t4、录入成绩\t5、查看已录入学生成绩\t6、退出" << endl; + int n; + cin >> n; + switch (n) { + case 1: + cpp.display2(); + system("pause"); + break; + case 2: { + string str; + cout << "请输入您的姓名:" << endl; + cin >> str; + cpp.add(str); + break; + } + case 3: { + string str1; + cout << "请输入您的姓名:" << endl; + cin >> str1; + cpp.cut(str1); + system("pause"); + break; + } + case 4: { + while (1) { + system("cls"); + string str2; + cout << "请输入您想录入成绩的学生姓名:" << endl; + cin >> str2; + string name = str2; + if (cpp.judge(str2)) { + Student str2; + int num; + cout << "请输入您该名学生的学号:"; + cin >> num; + int data; + cout << "请输入该名学生的成绩:"; + cin >> data; + str2.modify(name,num, data); + cpp.add(str2); + } + else { + cout << "无此人"; + } + cout << "是否要录入下一位学生的成绩:1.是 2.否 " << endl; + int choose; + cin >> choose; + if (choose == 1) { + continue; + } + else { + break; + } + } + break; + case 5: { + string str3; + cout << "请输入您想查看的学生的姓名:"; + cin >> str3; + if (cpp.judge(str3)) { + cpp.display(str3); + system("pause"); + } + } + case 6: + goto END; + } + } + } + END:; +} diff --git a/practices/cpp/level1/p11_Fighters/.vs/plane/v14/.suo b/practices/cpp/level1/p11_Fighters/.vs/plane/v14/.suo new file mode 100644 index 00000000..c01f932c Binary files /dev/null and b/practices/cpp/level1/p11_Fighters/.vs/plane/v14/.suo differ diff --git a/practices/cpp/level1/p11_Fighters/BoomArray.cpp b/practices/cpp/level1/p11_Fighters/BoomArray.cpp new file mode 100644 index 00000000..2089663d --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/BoomArray.cpp @@ -0,0 +1,37 @@ +#include "BoomArray.h" + +BoomArray::BoomArray(int num) +{ + this->num = num; + b = new Picture[num]; +} + +void BoomArray::produce(int i, int position_x, int position_y) +{ + for (int j = 0; j < num; j++) { + if (b[j].getx()<=0|| b[j].getx() >= 1080) { + b[j].produce(i, position_x,position_y); + break; + } + } +} + +sf::Sprite BoomArray::get(int No) +{ + return b[No].get(); +} + +bool BoomArray::exist(int No) +{ + if (b[No].exist()) { + return true; + } + return false; +} + +void BoomArray::initialize(int No, int time) +{ + if (time % 20 == 0) { + b[No].initialize(); + } +} \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/BoomArray.h b/practices/cpp/level1/p11_Fighters/BoomArray.h new file mode 100644 index 00000000..ef2b2812 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/BoomArray.h @@ -0,0 +1,16 @@ +#pragma once +#include +#include "Picture.h" + +class BoomArray +{ +public: + BoomArray(int bum); + void produce(int i, int position_x, int position_y); + sf::Sprite get(int No); + bool exist(int No); + void initialize(int No,int time); +private: + Picture *b; + int num; +}; \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/Enemy.cpp b/practices/cpp/level1/p11_Fighters/Enemy.cpp new file mode 100644 index 00000000..4a5c029c --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/Enemy.cpp @@ -0,0 +1,139 @@ +#include "Enemy.h" + +Enemy::Enemy(int type, int num, int bullet_num, float scanf):enemy_bullet(bullet_num) +{ + + this->num = num; + p = new Plane[num]; + for (int i = 0;i < num;i++) { + p[i].initialization(type, 0, 0, scanf, scanf); + } +} + +void Enemy::produce() +{ + int b = 1150, a = 20,randnum; + for (int i = 0;i < num;i++) { + if (p[i].gety() == 0) { + randnum = (rand() % (b - a + 1)) + a; + p[i].move(randnum, 0); + break; + } + } +} + +bool Enemy::bingo(sf::FloatRect b, int enemy_life) +{ + sf::FloatRect bullet_boundry; + for (int i = 0; i < num; i++) { + if (p[i].getx() != 0) { + bullet_boundry = p[i].get().getGlobalBounds(); + if (bullet_boundry.intersects(b)) { + if (p[i].is_bingo() == enemy_life) { + p[i].bingo_initialization(); + p[i].move(0, 0); + } + return true; + } + } + } + return false; +} + +void Enemy::destroy(int i) +{ + p[i].move(0, 0); +} + +int Enemy::getx(int No) +{ + return p[No].getx(); +} + +int Enemy::gety(int No) +{ + return p[No].gety(); +} + +void Enemy::setspeed(int enemy_speed, int bullet_speed) +{ + this->enemy_speed = enemy_speed; + enemy_bullet.setspeed(bullet_speed); +} + + +void Enemy::move(int &time) +{ + int b = num, a = 0, randnum; + randnum = (rand() % (b - a + 1)) + a; + for (int i = 0;i < num;i++) { + if (p[i].getx() != 0) { + p[i].move(p[i].getx(), p[i].gety() + enemy_speed); + if (randnum ==time) { + enemy_bullet.append(p[i].getx(), p[i].gety()); + } + } + if (p[i].gety() >= 1000) { + p[i].move(0, 0); + } + } + enemy_bullet.move(); +} + +void Enemy::move(int &time, int boundry_positon) +{ + static int flag=0; + for (int i = 0; i < num; i++) { + if (p[i].getx() != 0) { + if (p[i].gety() < 100) { + p[i].move(p[i].getx(), p[i].gety() + enemy_speed); + } + else { + if (boundry_positon > p[i].getx()) { + p[i].move(p[i].getx() + enemy_speed, 100); + } + else if (boundry_positon < p[i].getx()) { + p[i].move(p[i].getx() - enemy_speed, 100); + } + else { + if (p[i].getx() == p[i + 1].getx()) { + p[i].move(p[i].getx() - 200, 100 + i * 5); + p[i+1].move(p[i+1].getx() + 200, 100 + (i+1) * 5); + } + } + } + if (time%100==0) { + enemy_bullet.append(p[i].getx(), p[i].gety()); + } + } + } + enemy_bullet.move(); +} + +sf::Sprite &Enemy::draw_enemy(int i) +{ + return p[i].get(); +} + +sf::Sprite & Enemy::draw_bullet(int i) +{ + if (enemy_bullet.exist(i)) { + return enemy_bullet.draw(i); + } +} + +bool Enemy::exist(int i) +{ + if (p[i].getx() != 0) { + return true; + } + return false; +} + +bool Enemy::exist_bullet(int i) +{ + if (enemy_bullet.exist(i)) { + return true; + } + return false; +} \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/Enemy.h b/practices/cpp/level1/p11_Fighters/Enemy.h new file mode 100644 index 00000000..9d85676a --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/Enemy.h @@ -0,0 +1,28 @@ +#pragma once +#include"plane.h" +#include"bullet.h" +#include + +class Enemy +{ +public: + Enemy(int type, int num, int bullet_num, float scanf); + void produce(); + bool bingo(sf::FloatRect b, int enemy_life); + void move(int &time); + void move(int &time, int boundry_positon); + void move2(int &time, int boundry_positon); + sf::Sprite &draw_enemy(int i); + sf::Sprite &draw_bullet(int i); + bool exist(int i); + bool exist_bullet(int i); + void destroy(int i); + int getx(int No); + int gety(int No); + void setspeed(int enemy_speed, int bullet_speed); +private: + Plane *p; + int num; + Bullets enemy_bullet; + int enemy_speed; +}; \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/Picture.cpp b/practices/cpp/level1/p11_Fighters/Picture.cpp new file mode 100644 index 00000000..eab619c8 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/Picture.cpp @@ -0,0 +1,51 @@ +#include "Picture.h" + +Picture::Picture() +{ + x = 0; + y = 0; +} + +bool Picture::exist() +{ + if (x == 0) { + return false; + } + return true; +} + +void Picture::produce(int i, int position_x, int position_y) +{ + using namespace std; + string s; + s = to_string(i); + s = "xiao_" + s + ".png"; + x = position_x; + y = position_y; + boom_picture.loadFromFile(s); + boom.setTexture(boom_picture); + boom.setPosition(x, y); +} + +void Picture::produce(int texture_position_x, int texture_position_y) +{ + boom.setTexture(boom_picture); + boom.setTextureRect(sf::IntRect(texture_position_x, texture_position_y, 1200, 1080)); +} + +sf::Sprite Picture::get() +{ + return boom; +} + +int Picture::getx() +{ + return x; +} + +void Picture::initialize() +{ + x = 0; + y = 0; + boom.setPosition(x, y); +} \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/Picture.h b/practices/cpp/level1/p11_Fighters/Picture.h new file mode 100644 index 00000000..2f259457 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/Picture.h @@ -0,0 +1,18 @@ +#pragma once +#include + +class Picture +{ +public: + Picture(); + bool exist(); + void produce(int i, int position_x, int position_y); + void Picture::produce(int texture_position_x, int texture_position_y); + sf::Sprite get(); + int getx(); + void initialize(); +private: + int x, y; + sf::Texture boom_picture; + sf::Sprite boom; +}; \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/Text.cpp b/practices/cpp/level1/p11_Fighters/Text.cpp new file mode 100644 index 00000000..49b2e195 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/Text.cpp @@ -0,0 +1,75 @@ +#include "Text.h" + +Text::Text() +{ + font.loadFromFile("CroissantD.ttf"); + text.setFont(font); + text.setFillColor(sf::Color::Yellow); +} + +void Text::setmark(int type, std::string Str) +{ + std::string s; + if (type == 1) { + s = "The score is: "; + text.setPosition(850, 10); + text.setCharacterSize(50); + } + if (type == 2) { + s = "Your life left: "; + text.setPosition(10, 10); + text.setCharacterSize(50); + } + if (type == 3) { + s = "The time left: "; + text.setPosition(450, 10); + text.setCharacterSize(50); + } + if (type == 4) { + s = "mode: "; + text.setPosition(450,400); + text.setCharacterSize(50); + } + if (type == 5) { + s = "level:"; + text.setPosition(450, 500); + text.setCharacterSize(50); + } + if (type == 6) { + s = "direction: Fire:space "; + text.setPosition(400, 650); + text.setCharacterSize(50); + } + if (type == 7) { + s = " You have death,your scores is "; + s = s + Str; + Str = ". \nPlease press R if you want to play again, press Q if you want quit!"; + s = s + Str; + Str = ""; + text.setFillColor(sf::Color::Red); + text.setPosition(200, 400); + text.setCharacterSize(50); + } + if (type == 8) { + s = " Congradulations! You have death,your scores is "; + s = s + Str; + Str = ". \nPlease press R if you want to play again, press Q if you want quit! "; + s = s + Str; + Str = ""; + text.setFillColor(sf::Color::Red); + text.setPosition(200, 400); + text.setCharacterSize(50); + } + s = s + Str; + text.setString(s); +}; + +sf::Text Text::draw() +{ + return text; +} + +void Text::setsize(int i) +{ + text.setCharacterSize(i); +} diff --git a/practices/cpp/level1/p11_Fighters/Text.h b/practices/cpp/level1/p11_Fighters/Text.h new file mode 100644 index 00000000..c5cb6370 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/Text.h @@ -0,0 +1,15 @@ +#pragma once +#include +#include + +class Text +{ +public: + Text(); + void setmark(int type,std::string Str); + sf::Text draw(); + void setsize(int i); +private: + sf::Text text; + sf::Font font; +}; \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/TodoList.md b/practices/cpp/level1/p11_Fighters/TodoList.md index 497725f5..cfbb796a 100755 --- a/practices/cpp/level1/p11_Fighters/TodoList.md +++ b/practices/cpp/level1/p11_Fighters/TodoList.md @@ -1,23 +1,23 @@ | 任务(功能) | Value | Effort | 是否已完成 -----|-------------------------------|-----------|-----------|------------| -1 | 完成SFML配置,显示“SFML works” | 0 | | | -2 | 显示一架静止的飞机于屏幕底部 | 5 | | | -3 | 背景音乐 | 1 | | | -4 | 左右键,控制移动飞机 | 10 | | | -5 | 限制左右边界 | 1 | | | -6 | 空格键开炮,显示运动的炮弹 | 5 | | | -7 | 炮弹飞出边界处理 | 2 | | | -8 | 随机产生敌机,并向下运动 | 10 | | | -9 | 敌机飞出边界处理 | 2 | | | -10 | 碰撞处理(敌机与炮弹碰撞) | 10 | | | -11 | 显示敌机爆炸过程 | 10 | | | -12 | 爆炸声音 | 2 | | | -13 | 计分及显示 | 5 | | | -14 | 敌机炮弹处理 | 10 | | | -15 | 被敌机击中处理(炸毁、3条命) | 10 | | | -16 | 过关控制(过关需要计分、游戏速度控制)| 20 | | | -17 | | | | | -18 | | | | | +1 | 完成SFML配置,显示“SFML works” | 0 | | yes | +2 | 显示一架静止的飞机于屏幕底部 | 5 | | yes | +3 | 背景音乐 | 1 | | yes | +4 | 左右键,控制移动飞机 | 10 | | yes | +5 | 限制左右边界 | 1 | | yes | +6 | 空格键开炮,显示运动的炮弹 | 5 | | yes | +7 | 炮弹飞出边界处理 | 2 | | yes | +8 | 随机产生敌机,并向下运动 | 10 | | yes | +9 | 敌机飞出边界处理 | 2 | | yes | +10 | 碰撞处理(敌机与炮弹碰撞) | 10 | | yes | +11 | 显示敌机爆炸过程 | 10 | | yes | +12 | 爆炸声音 | 2 | | yes | +13 | 计分及显示 | 5 | | yes | +14 | 敌机炮弹处理 | 10 | | yes | +15 | 被敌机击中处理(炸毁、3条命) | 10 | | yes | +16 | 过关控制(过关需要计分、游戏速度控制)| 20 | | yes | +17 | 背景移动 | | | yes | +18 | 敌机追寻 | | | yes | 19 | | | | | 20 | | | | | 合计 | | | | | diff --git a/practices/cpp/level1/p11_Fighters/background.cpp b/practices/cpp/level1/p11_Fighters/background.cpp new file mode 100644 index 00000000..e4a16f19 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/background.cpp @@ -0,0 +1,20 @@ +#include "background.h" + + +Background::Background(int type) +{ + this->type = type; + background.produce(7, 0, 0); +} + +void Background::move() +{ + background.produce(0, 3070-1080-No); + No += 1; + if (No >= 910)No = 0; +} + +sf::Sprite Background::draw() +{ + return background.get(); +} diff --git a/practices/cpp/level1/p11_Fighters/background.h b/practices/cpp/level1/p11_Fighters/background.h new file mode 100644 index 00000000..9626ca3b --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/background.h @@ -0,0 +1,14 @@ +#pragma once +#include"Picture.h" + +class Background +{ +public: + Background(int type); + void move(); + sf::Sprite draw(); +private: + Picture background; + int type; + int No = 0; +}; \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/bullet.cpp b/practices/cpp/level1/p11_Fighters/bullet.cpp new file mode 100644 index 00000000..7b15dacf --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/bullet.cpp @@ -0,0 +1,85 @@ +#include "bullet.h" + +void Bullets::move() +{ + for (int i = 0;i < num;i++) { + if (p[i].getx() != 0) { + p[i].move(p[i].getx(), p[i].gety() - speed); + if ((p[i].gety() <= 0)||(p[i].gety()>=1000)) { + p[i].move(0, 0); + } + } + } +} + + +void Bullets::setpoint(int x, int y) +{ + this->x = x; + this->y = y; +} + +void Bullets::append2(int x, int y) +{ + int flag = 0; + for (int i = 0; i < num; i++) { + if (p[i].getx() == 0) { + flag += 1; + p[i].move(x + 10, y + 10); + if (flag == 10)break; + } + } +} + +Bullets::Bullets(int num) +{ + this->num = num; + p = new Plane[num]; + for (int i = 0;i < num;i++) { + p[i].initialization(4, 0, 0, 1, 1); + } + speed = 1; +} + +void Bullets::append(int x, int y) +{ + for (int i = 0;i < num;i++) { + if (p[i].getx() == 0) { + p[i].move(x+10, y+10); + break; + } + } +} + +sf::Sprite &Bullets::draw(int i) +{ + return p[i].get(); +} + +void Bullets::destroy(int No) +{ + p[No].move(0, 0); +} + +bool Bullets::exist(int i) +{ + if (p[i].getx() != 0) { + return true; + } + return false; +} + +int Bullets::getx(int No) +{ + return p[No].getx(); +} + +int Bullets::gety(int No) +{ + return p[No].gety(); +} + +void Bullets::setspeed(int speed) +{ + this->speed = speed; +} \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/bullet.h b/practices/cpp/level1/p11_Fighters/bullet.h new file mode 100644 index 00000000..ed6b3db9 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/bullet.h @@ -0,0 +1,24 @@ +#pragma once +#include +#include"plane.h" + +class Bullets +{ +public: + void move(); + void setpoint(int x,int y); + void append2(int x, int y); + Bullets(int num); + void append(int x,int y); + sf::Sprite &draw(int i); + void destroy(int No); + bool exist(int i); + int getx(int No); + int gety(int No); + void setspeed(int speed); +private: + Plane *p; + int speed; + int num; + int r = 0, a = 0, x, y; +}; \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/main.cpp b/practices/cpp/level1/p11_Fighters/main.cpp new file mode 100644 index 00000000..23c9d3b8 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/main.cpp @@ -0,0 +1,290 @@ +#include +#include +#include +#include +#include +#include"bullet.h" +#include"plane.h" +#include"Enemy.h" +#include"BoomArray.h" +#include "Text.h" +#include"background.h" +#include"start_text.h" + +void main() +{ + static int count = 0; + int enemy_number = 20, enemy_bullet_number = 20, my_bullet_num = 30; + int death_flag = 0, death_positionx, death_positiony, death_time = 0; + int marks = 0,life_left = 3, revive_flag = 0, revive_time = 0,produce_cause=200; + int time_left = 300; + bool over_flag = 0; + + std::string Str; + + sf::SoundBuffer buffer1,buffer2,buffer3; + buffer1.loadFromFile("collision2.flac"); + buffer2.loadFromFile("collision3.flac"); + buffer3.loadFromFile("background_music.flac"); + sf::Sound bingo1,bingo2; + sf::Sound music; + bingo1.setBuffer(buffer1); + bingo2.setBuffer(buffer2); + music.setBuffer(buffer3); + music.play(); + music.setLoop(true); + sf::Keyboard::Key; + sf::RenderWindow window(sf::VideoMode(1200, 1080), "Fighting!"); + sf::FloatRect EnemyBoundryBox; + + + Plane hero(3, 900, 800, 1, 1); + Bullets bullet(50); + bullet.setspeed(4); + Enemy enemy1(2, enemy_number, enemy_bullet_number, 1); + Enemy enemyboss2(6, enemy_number, enemy_bullet_number, 1); + Enemy enemyboss1(5, 2, enemy_bullet_number, 1); + BoomArray boom(enemy_number); + Picture start_background; + Background gamemap(1); + StartText start_text; + Text score; + Text life; + Text times; + Text start; + Text over1,over2; + + srand((unsigned)time(NULL)); + enemy1.setspeed(1, -3); + enemyboss1.setspeed(1, -3); + start_background.produce(5, 0, 0); + window.setFramerateLimit(200); + + + while (window.isOpen()) + { + sf::Event event; + window.clear(sf::Color::Black); + + static int k = 0; + while (0 == k || k == 2||k==3) { + window.draw(start_background.get()); + while (window.pollEvent(event)); + if (k == 0) { + window.draw(start_text.draw(0)); + window.draw(start_text.draw(1)); + window.draw(start_text.draw(2)); + window.display(); + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) { + k = 1; + } + } + if (k == 2) { + using namespace std; + string Str; + Str = to_string(marks); + over1.setmark(7, Str); + window.draw(over1.draw()); + death_flag = 0, death_time = 0; + life_left = 3, revive_flag = 0, revive_time = 0, produce_cause = 200; + time_left = 300, over_flag = 0; + window.display(); + if (sf::Keyboard::isKeyPressed(sf::Keyboard::R)) { + marks = 0; + goto restart; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) { + window.close(); + } + } + if (k == 3) { + over1.setmark(7, Str); + window.draw(over1.draw()); + window.display(); + if (sf::Keyboard::isKeyPressed(sf::Keyboard::R)) { + k = 2; + marks = 0; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) { + window.close(); + } + } + } + while (window.pollEvent(event)) + { + if (event.type == sf::Event::Closed) + window.close(); + } + + while (over_flag==0) { + restart: + if (count % produce_cause == 0) { + enemy1.produce(); + produce_cause -= 5; + if (produce_cause == 50)produce_cause = 200; + count = 0; + } + if (time_left / 100 < 15) { + enemyboss1.produce(); + } + int x = hero.getx(), y = hero.gety(); + if (death_flag == 0) { + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { + if (x > 0)x -= 5; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { + if (x < 1100)x += 5; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { + if (y > 0)y -= 5; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { + if (y < 900)y += 5; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) { + if (count % 15 == 0) { + bullet.append(x + 40, y + 10); + } + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { + window.close(); + } + } + + while (window.pollEvent(event)) + { + if (event.type == sf::Event::Closed) + window.close(); + } + window.clear(); + + window.draw(gamemap.draw()); + if(count%2)gamemap.move(); + hero.move(x, y); + enemyboss1.move(count, hero.getx()); + if (count % 1 == 0) { + enemy1.move(count); + bullet.move(); + } + + for (int i = 0; i < enemy_bullet_number; i++) { + if (bullet.exist(i)) { + EnemyBoundryBox = bullet.draw(i).getGlobalBounds(); + if (enemy1.bingo(EnemyBoundryBox,1)) { + boom.produce(4, bullet.getx(i) - 20, bullet.gety(i) - 50); + bingo1.play(); + bullet.destroy(i); + marks += 1; + time_left += 200; + } + if (enemyboss1.bingo(EnemyBoundryBox,10)) { + bullet.destroy(i); + bingo1.play(); + marks += 1; + time_left += 200; + } + } + + if (revive_flag == 0) { + if (enemy1.exist_bullet(i)) { + EnemyBoundryBox = enemy1.draw_bullet(i).getGlobalBounds(); + if (hero.get().getGlobalBounds().intersects(EnemyBoundryBox)) { + death_positionx = hero.getx(); + death_positiony = hero.gety(); + boom.produce(1, death_positionx + 20, death_positiony + 40); + hero.move(0, 0); + death_flag += 1; + } + } + if (i<10&&enemy1.exist(i)) { + EnemyBoundryBox = enemy1.draw_enemy(i).getGlobalBounds(); + if (hero.get().getGlobalBounds().intersects(EnemyBoundryBox)) { + death_positionx = hero.getx(); + death_positiony = hero.gety(); + boom.produce(1, death_positionx + 20, death_positiony + 40); + hero.move(0, 0); + death_flag += 1; + } + } + if (enemyboss1.exist_bullet(i)) { + if (enemyboss1.exist_bullet(i)) { + EnemyBoundryBox = enemyboss1.draw_bullet(i).getGlobalBounds(); + if (hero.get().getGlobalBounds().intersects(EnemyBoundryBox)) { + death_positionx = hero.getx(); + death_positiony = hero.gety(); + bingo2.play(); + boom.produce(1, death_positionx + 20, death_positiony + 40); + death_flag += 1; + } + } + } + } + if (i= 500) { + revive_flag = 0; + revive_time = 0; + } + } + + using namespace std; + Str = to_string(marks); + score.setmark(1, Str); + Str = to_string(life_left); + life.setmark(2, Str); + Str = to_string(time_left/100); + times.setmark(3, Str); + + window.draw(times.draw()); + window.draw(score.draw()); + window.draw(life.draw()); + window.display(); + count++,k=2; + time_left--; + if ( life_left == 0 ) { + over_flag = 1; + } + if (marks >= 200) goto quit; + } + + quit:k = 3; + } +} \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/plane.cpp b/practices/cpp/level1/p11_Fighters/plane.cpp new file mode 100644 index 00000000..8d668a60 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/plane.cpp @@ -0,0 +1,139 @@ +#include "plane.h" + +Plane::Plane() +{ + positionx = 0; + positiony = 0; +} + +void Plane::initialization(int i, int positionx, int positiony, float x, float y) +{ + switch (i) { + case 1: { + picture.loadFromFile("plane.jpg"); + break; + } + case 2: { + picture.loadFromFile("enemy1.png"); + break; + } + case 3: { + picture.loadFromFile("myplane1.png"); + break; + } + case 4: { + picture.loadFromFile("Fire1.png"); + break; + } + case 5: { + picture.loadFromFile("enemy4.png"); + break; + } + } + + + plane.setTexture(picture); + this->positionx = positionx; + this->positiony = positiony; + plane.setPosition(positionx, positiony); + plane.scale(x, y); +} + +Plane::Plane(int i, int positionx, int positiony, float x, float y) +{ + switch (i) { + case 1: { + picture.loadFromFile("plane.jpg"); + break; + } + case 2: { + picture.loadFromFile("enemy1.png"); + break; + } + case 3: { + picture.loadFromFile("myplane1.png"); + break; + } + case 4: { + picture.loadFromFile("Fire1.png"); + break; + } + case 5: { + picture.loadFromFile("enemy4.jpg"); + break; + } + } + + + plane.setTexture(picture); + this->positionx = positionx; + this->positiony = positiony; + plane.setPosition(positionx, positiony); + plane.scale(x, y); +} + +void Plane::move(int positionx, int positiony) +{ + this->positionx = positionx; + this->positiony = positiony; + plane.setPosition(positionx, positiony); +} + +bool Plane::Isdestroy(sf::FloatRect boundry) +{ + sf::FloatRect boundingBox = plane.getGlobalBounds(); + if (boundingBox.intersects(boundry)) + { + return true; + } + return false; +} + +int Plane::getx() +{ + return positionx; +} + +int Plane::gety() +{ + return positiony; +} + +sf::Sprite &Plane::get() +{ + return plane; +} + +sf::Texture &Plane::gettexture() +{ + return picture; +} + +void Plane::copy(Plane another) +{ + this->picture = another.gettexture(); + plane.setTexture(picture); + this->positionx = another.getx(); + this->positiony = another.gety(); +} + +void Plane::destroy() +{ + plane.move(0, 0); +} + +void Plane::setspeed(int speed) +{ + this->speed = speed; +} + +void Plane::bingo_initialization() +{ + bingo = 0; +} + +int Plane::is_bingo() +{ + bingo += 1; + return bingo; +} diff --git a/practices/cpp/level1/p11_Fighters/plane.h b/practices/cpp/level1/p11_Fighters/plane.h new file mode 100644 index 00000000..f100210b --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/plane.h @@ -0,0 +1,27 @@ +#pragma once +#include + +class Plane +{ +public: + Plane(); + Plane(int i, int positionx, int positiony, float x, float y); + void initialization(int i, int positionx,int positiony,float x, float y); + void move(int positionx, int positiony); + bool Isdestroy(sf::FloatRect boundry); + int getx(); + int gety(); + sf::Sprite &get(); + sf::Texture &gettexture(); + void copy(Plane another); + void destroy(); + void setspeed(int speed); + void bingo_initialization(); + int is_bingo(); +private: + int positionx, positiony; + sf::Texture picture; + sf::Sprite plane; + int speed; + int bingo = 0; +}; \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/start_text.cpp b/practices/cpp/level1/p11_Fighters/start_text.cpp new file mode 100644 index 00000000..c851d120 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/start_text.cpp @@ -0,0 +1,52 @@ +#include "start_text.h" + +StartText::StartText() +{ + a[0].setmark(4, " state"); + a[1].setmark(5, " newer"); + a[2].setmark(6, ""); +} + +sf::Text StartText::draw(int i) +{ + return (a[i].draw()); +} + +void StartText::change(int i, int j) +{ + using namespace std; + if (j == 0) { + if (i == 0) { + a[0].setmark(4, "state"); + a[1].setmark(5, "new"); + a[2].setmark(6, ""); + } + else if(i==1){ + a[0].setmark(4, "state"); + a[1].setmark(5, "narmal"); + a[2].setmark(6, ""); + } + else { + a[0].setmark(4, "state"); + a[1].setmark(5, "hard"); + a[2].setmark(6, ""); + } + } + if (j == 1) { + if (i == 0) { + a[0].setmark(4, "time"); + a[1].setmark(5, "new"); + a[2].setmark(6, " "); + } + if (i == 1) { + a[0].setmark(4, "time"); + a[1].setmark(5, "normal"); + a[2].setmark(6, " "); + } + if (i == 2) { + a[0].setmark(4, "time "); + a[1].setmark(5, "hard"); + a[2].setmark(6, " "); + } + } +} \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/start_text.h b/practices/cpp/level1/p11_Fighters/start_text.h new file mode 100644 index 00000000..4eae36ab --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/start_text.h @@ -0,0 +1,12 @@ +#pragma once +#include"Text.h" + +class StartText +{ +public: + StartText(); + sf::Text draw(int i); + void change(int i,int j); +private: + Text a[3]; +}; \ No newline at end of file