From a969ef5a964138f59a835019616597f3bb813cf2 Mon Sep 17 00:00:00 2001 From: Anthem <837704683@qq.com> Date: Sun, 11 Mar 2018 21:17:33 +0800 Subject: [PATCH 01/17] =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- level1/p01_runningLetter/main.c | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 level1/p01_runningLetter/main.c diff --git a/level1/p01_runningLetter/main.c b/level1/p01_runningLetter/main.c new file mode 100644 index 00000000..9df6682e --- /dev/null +++ b/level1/p01_runningLetter/main.c @@ -0,0 +1,42 @@ +#include +#include +#include +/* run this program using the console pauser or add your own getch, system("pause") or input loop */ + +int main(int argc, char *argv[]) { + int a,b; + a = 0; + while(1){ + //从左向右 + while (a<=79) + { + system ("cls"); + b = 1; + while (b<=a) + { + printf(" "); + ++b; + } + printf("H"); + Sleep(100); + ++a; + } + a--; + //从右向左 + while (a> 0) //如果使用大于等于,将会于开头处输出两个0空格的H,导致停顿时间变长 + { + system ("cls"); + b = 1; + while (b<=a) + { + printf(" "); + ++b; + } + printf("H"); + Sleep(100); + --a; + } + } + system("pause"); + return 0; +} From accc12a41870d9dc746186abfb01abe3ec306489 Mon Sep 17 00:00:00 2001 From: Anthem <32233926+Anthem9@users.noreply.github.com> Date: Tue, 13 Mar 2018 09:55:02 +0800 Subject: [PATCH 02/17] =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- level1/p02_isPrime/main.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 level1/p02_isPrime/main.c diff --git a/level1/p02_isPrime/main.c b/level1/p02_isPrime/main.c new file mode 100644 index 00000000..c8173408 --- /dev/null +++ b/level1/p02_isPrime/main.c @@ -0,0 +1,34 @@ +#include +#include +#define MIN_PRIME 2 +/* run this program using the console pauser or add your own getch, system("pause") or input loop */ + +int main(int argc, char *argv[]) { + + /*input*/ + printf("Please input a number\n"); + int number; + scanf("%d",&number); + + /*judgement*/ + int i; + for(i=MIN_PRIME;i Date: Sat, 17 Mar 2018 21:48:46 +0800 Subject: [PATCH 03/17] =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- level1/p08_hanoi/hanoi.c | 59 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 level1/p08_hanoi/hanoi.c diff --git a/level1/p08_hanoi/hanoi.c b/level1/p08_hanoi/hanoi.c new file mode 100644 index 00000000..553c4a41 --- /dev/null +++ b/level1/p08_hanoi/hanoi.c @@ -0,0 +1,59 @@ +#include +#include + +/* run this program using the console pauser or add your own getch, system("pause") or input loop */ +int change(n,position1,position2) +{ + //得到position3 + char position3; + if(position1 == 'A') + { + if(position2 == 'B') + { + position3 = 'C'; + } + else + { + position3 = 'B'; + } + } + else if(position1 == 'B') + { + if(position2 == 'A') + { + position3 = 'C'; + } + else + { + position3 = 'A'; + } + } + else + { + if(position2 == 'A') + { + position3 = 'B'; + } + else + { + position3 = 'A'; + } + } + + if(n==1) + { + printf("%c->%c\n",position1,position2); + return 0; + } + + change(n-1,position1,position3); + change(1,position1,position2); + change(n-1,position3,position2); +} +int main(int argc, char *argv[]) { + int n; + printf("输入塔的层数\n"); + scanf("%d",&n); + change(n,'A','B'); + return 0; +} From ae7798e4f9e94e8ced2a4f89408242fb8ef9ca96 Mon Sep 17 00:00:00 2001 From: Anthem <32233926+Anthem9@users.noreply.github.com> Date: Tue, 20 Mar 2018 09:41:46 +0800 Subject: [PATCH 04/17] Create p04_ narcissus.c --- level1/p04_ narcissus/p04_ narcissus.c | 27 ++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 level1/p04_ narcissus/p04_ narcissus.c diff --git a/level1/p04_ narcissus/p04_ narcissus.c b/level1/p04_ narcissus/p04_ narcissus.c new file mode 100644 index 00000000..07b47d3f --- /dev/null +++ b/level1/p04_ narcissus/p04_ narcissus.c @@ -0,0 +1,27 @@ +#include +#include +#include +/* run this program using the console pauser or add your own getch, system("pause") or input loop */ + +int main(int argc, char *argv[]) { + int number; + int hundred; + int ten; + int one; + int target; + + number = 100; + while(number<=999) + { + hundred = number/100; + ten = (number-hundred*100)/10; + one = number%10; + target = pow(hundred,3)+pow(ten,3)+pow(one,3); + if(target==number) + { + printf("%d\n",number); + } + number++; + } + return 0; +} From d5b96358a2a5c6b828f7e48ae8fbdb47f73f126d Mon Sep 17 00:00:00 2001 From: Anthem <32233926+Anthem9@users.noreply.github.com> Date: Tue, 20 Mar 2018 10:05:01 +0800 Subject: [PATCH 05/17] Create p05_allPrimes.c --- level1/p05_allPrimes/p05_allPrimes.c | 42 ++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 level1/p05_allPrimes/p05_allPrimes.c diff --git a/level1/p05_allPrimes/p05_allPrimes.c b/level1/p05_allPrimes/p05_allPrimes.c new file mode 100644 index 00000000..d1063f9b --- /dev/null +++ b/level1/p05_allPrimes/p05_allPrimes.c @@ -0,0 +1,42 @@ +#include +#include +#include +#define MIN_PRIME 2 +/* run this program using the console pauser or add your own getch, system("pause") or input loop */ + +int main(int argc, char *argv[]) { + int number; + clock_t t1,t2; + t1 = clock(); + number = MIN_PRIME; + while(number<=1000) + { + if(is_prime(number)) + { + printf("%d\n",number); + } + number++; + } + t2 = clock(); + printf("%d\n",t2-t1); + return 0; +} +int is_prime(number){ + int i; + for (i=MIN_PRIME;i Date: Tue, 10 Apr 2018 08:33:20 +0800 Subject: [PATCH 06/17] Add files via upload --- level1/p09_maze/main.c | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 level1/p09_maze/main.c diff --git a/level1/p09_maze/main.c b/level1/p09_maze/main.c new file mode 100644 index 00000000..401ba832 --- /dev/null +++ b/level1/p09_maze/main.c @@ -0,0 +1,10 @@ +#include + +int main() { + for(float FatherAge=1;FatherAge<200;FatherAge++){ + if(FatherAge/6+FatherAge/12+FatherAge/7+5+FatherAge/2+4==FatherAge){ + printf("%f\n",FatherAge-4); + } + } + return 0; +} \ No newline at end of file From 81bb50214329232902f5251e43b23ccb7f2f66fa Mon Sep 17 00:00:00 2001 From: Anthem <32233926+Anthem9@users.noreply.github.com> Date: Tue, 10 Apr 2018 08:34:11 +0800 Subject: [PATCH 07/17] Delete main.c --- level1/p09_maze/main.c | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 level1/p09_maze/main.c diff --git a/level1/p09_maze/main.c b/level1/p09_maze/main.c deleted file mode 100644 index 401ba832..00000000 --- a/level1/p09_maze/main.c +++ /dev/null @@ -1,10 +0,0 @@ -#include - -int main() { - for(float FatherAge=1;FatherAge<200;FatherAge++){ - if(FatherAge/6+FatherAge/12+FatherAge/7+5+FatherAge/2+4==FatherAge){ - printf("%f\n",FatherAge-4); - } - } - return 0; -} \ No newline at end of file From 5afc16389c4a4f805d627ef53924a0199b092094 Mon Sep 17 00:00:00 2001 From: Anthem <32233926+Anthem9@users.noreply.github.com> Date: Tue, 10 Apr 2018 08:35:07 +0800 Subject: [PATCH 08/17] Add files via upload --- level1/p03_Diophantus/main.c | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 level1/p03_Diophantus/main.c diff --git a/level1/p03_Diophantus/main.c b/level1/p03_Diophantus/main.c new file mode 100644 index 00000000..401ba832 --- /dev/null +++ b/level1/p03_Diophantus/main.c @@ -0,0 +1,10 @@ +#include + +int main() { + for(float FatherAge=1;FatherAge<200;FatherAge++){ + if(FatherAge/6+FatherAge/12+FatherAge/7+5+FatherAge/2+4==FatherAge){ + printf("%f\n",FatherAge-4); + } + } + return 0; +} \ No newline at end of file From 87c478c38ad4d12d78aff176ff1cf9e412736530 Mon Sep 17 00:00:00 2001 From: Anthem <32233926+Anthem9@users.noreply.github.com> Date: Tue, 10 Apr 2018 08:47:19 +0800 Subject: [PATCH 09/17] Add files via upload --- level1/p06_Goldbach/main.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 level1/p06_Goldbach/main.c diff --git a/level1/p06_Goldbach/main.c b/level1/p06_Goldbach/main.c new file mode 100644 index 00000000..99f64c50 --- /dev/null +++ b/level1/p06_Goldbach/main.c @@ -0,0 +1,18 @@ +#include + +int main() { + int Primes[25] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; + for (int i = 2; i < 99; i += 2) { + for (int j = 0; j < 25; j++) { + if (Primes[j] >= i)break; + for (int k = j; k < 25; k++) { + if (Primes[j] + Primes[k] == i) + printf("%d=%d+%d\n", i, Primes[j], Primes[k]); + else if (Primes[j] + Primes[k] > i) { + break; + } + } + } + } + return 0; +} \ No newline at end of file From a766cac3639653db0ac5275ba3bfcccd19775765 Mon Sep 17 00:00:00 2001 From: Anthem <32233926+Anthem9@users.noreply.github.com> Date: Tue, 10 Apr 2018 09:32:59 +0800 Subject: [PATCH 10/17] Add files via upload --- level1/p07_encrypt_decrypt/main.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 level1/p07_encrypt_decrypt/main.c diff --git a/level1/p07_encrypt_decrypt/main.c b/level1/p07_encrypt_decrypt/main.c new file mode 100644 index 00000000..b62c704c --- /dev/null +++ b/level1/p07_encrypt_decrypt/main.c @@ -0,0 +1,20 @@ +#include +#include + +/* run this program using the console pauser or add your own getch, system("pause") or input loop */ +/*reverse encrypt*/ +int main(int argc, char *argv[]) { + char c[1000]; + fgets(c,(sizeof c / sizeof c[0]),stdin); + int d = strlen(c); + + char a[1000]; + int j = 0; + for (int i=d-2;i>=0;i--) + { + a[i] = c [j]; + j++; + } + puts(a); + return 0; +} From ca6f765a92c062b93500be7c05827dba1a714bbe Mon Sep 17 00:00:00 2001 From: Anthem <32233926+Anthem9@users.noreply.github.com> Date: Sat, 23 Jun 2018 08:42:52 +0800 Subject: [PATCH 11/17] Add files via upload --- level1/p09_maze/maze.cpp | 136 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 level1/p09_maze/maze.cpp diff --git a/level1/p09_maze/maze.cpp b/level1/p09_maze/maze.cpp new file mode 100644 index 00000000..61e5cbe5 --- /dev/null +++ b/level1/p09_maze/maze.cpp @@ -0,0 +1,136 @@ +#include +#include +#include +#include +#include +#define ROW 10 +#define COL 10 + + + +int main(){ + void output(int (*p)[COL],int x,int y,char ch); //函数声明 + void move(int (*p)[COL],char ch,int *px,int *py); + void print(int *p); + void goto_xy(int,int); + + int maze[ROW][COL]={ //迷宫数组 + 35,43,35,35,35,35,35,35,35,35, + 35,43,43,43,35,35,35,43,35,35, + 35,35,35,43,35,35,35,43,35,35, + 35,35,35,43,43,43,43,35,35,35, + 35,43,43,43,43,35,43,35,35,35, + 35,43,35,35,43,35,43,35,35,35, + 35,43,43,35,35,35,43,35,43,35, + 35,43,43,43,43,35,43,43,43,35, + 35,43,35,35,35,35,35,35,43,43, + 35,35,35,35,35,35,35,35,35,35 + }; + + int x = 0,y = 1; //定义变量 + int *px = &x,*py = &y; + int (*p)[COL] = maze; + char m; + + output(p,x,y,'w'); //迷宫实现 + + m = getch(); + while (1){ + move(p,m,px,py); + if (maze[ROW-2][COL-1] == 64){ + break; + } + m = getch(); + } + printf("Congratulations!\n"); + system("pause"); + return 0; +} + +void print(int *p){ //输出迷宫数组 + printf("\n"); + for (int i = 0; i Date: Sat, 23 Jun 2018 08:43:38 +0800 Subject: [PATCH 12/17] Add files via upload --- level1/p10_pushBoxes/map1.dat | 10 + level1/p10_pushBoxes/map2.dat | 10 + level1/p10_pushBoxes/pushBoxes.c | 513 +++++++++++++++++++++++++++++++ 3 files changed, 533 insertions(+) create mode 100644 level1/p10_pushBoxes/map1.dat create mode 100644 level1/p10_pushBoxes/map2.dat create mode 100644 level1/p10_pushBoxes/pushBoxes.c diff --git a/level1/p10_pushBoxes/map1.dat b/level1/p10_pushBoxes/map1.dat new file mode 100644 index 00000000..464962ce --- /dev/null +++ b/level1/p10_pushBoxes/map1.dat @@ -0,0 +1,10 @@ +9,9,4, +35,35,35,35,35,35,35,35,35, +35,32,32,32,79,32,32,32,35, +35,32,32,32,32,32,32,32,35, +35,32,32,32,88,32,32,32,35, +35,79,32,88,64,88,32,79,35, +35,32,32,32,88,32,32,32,35, +35,32,32,32,32,32,32,32,35, +35,32,32,32,79,32,32,32,35, +35,35,35,35,35,35,35,35,35 \ No newline at end of file diff --git a/level1/p10_pushBoxes/map2.dat b/level1/p10_pushBoxes/map2.dat new file mode 100644 index 00000000..934695f7 --- /dev/null +++ b/level1/p10_pushBoxes/map2.dat @@ -0,0 +1,10 @@ +9,9,3, +35,35,35,35,35,32,32,32,32, +35,32,32,32,35,32,32,32,32, +35,32,88,32,35,32,35,35,35, +35,32,88,64,35,32,35,79,35, +35,35,35,88,35,35,35,79,35, +32,35,35,32,32,32,32,79,35, +32,35,32,32,32,35,32,32,35, +32,35,32,32,32,35,35,35,35, +32,35,35,35,35,35,32,32,32. \ No newline at end of file diff --git a/level1/p10_pushBoxes/pushBoxes.c b/level1/p10_pushBoxes/pushBoxes.c new file mode 100644 index 00000000..8a1ac7b8 --- /dev/null +++ b/level1/p10_pushBoxes/pushBoxes.c @@ -0,0 +1,513 @@ +#include +#include +#include +#include + +int main(){ + void chooselevel(int *plevel); + void showscore(); + void getrowcolaim(FILE *fin,int *prow,int *pcol,int *paim); + void creatmap(FILE *fin,int *p,int row,int col); + void output(int *p,int x,int y,int n,int COL); //函数声明 + int getmove(int *p,char ch,int *px,int *py,int ROW,int COL); + void print(int *p,int ROW,int COL); + void findplayer(int *p,int *px,int *py,int ROW,int COL); + void goto_xy(int,int); + int gameover(int *p,int ROW,int COL,int AIM); + void savescores(FILE *fout,int steps); + + int x = 0,y = 0; //定义变量 + int *px = &x,*py = &y; + + char m; + + int row, col, aim; + int *prow = &row, *pcol = &col, *paim = &aim; + + FILE *fin,*fout; + + int level; + int *plevel = &level; + + chooselevel(plevel); //调用选关函数 + while (1){ + switch (level){ + case 0:{ //调用函数查看纪录 + showscore(); + system("pause"); + system("cls"); + chooselevel(plevel); + } + case 1:{ //打开对应关卡文件 + if ((fin = fopen("map1.dat","r+")) == NULL) { + printf("Can't open this map!\n"); + exit(0); + } + if ((fout = fopen("score1.dat","a+")) == NULL) { + printf("Can't read the scores!\n"); + exit(0); + } + break; + } + case 2:{ + if ((fin = fopen("map2.dat","r+")) == NULL) { + printf("Can't open this map!\n"); + exit(0); + } + if ((fout = fopen("score2.dat","a+")) == NULL) { + printf("Can't read the scores!\n"); + exit(0); + } + break; + } + default:; + } + if (level != 0){ + break; + } + } + + + getrowcolaim(fin,prow,pcol,paim); //获取地图行数,列数和箱子数 + + const int ROW = row, COL = col, AIM = aim; //创建对应的大小数组 并 读入地图数组 + int map[ROW][COL]; + int *p = &map[0][0]; + creatmap(fin,&map[0][0],ROW,COL); + + + print(&map[0][0],ROW,COL); //输出地图 + findplayer(&map[0][0],px,py,ROW,COL); //定位玩家初始坐标 + goto_xy(0,ROW+9); + + int steps = 0; //此变量记录玩家步数 + + m = getch(); + while (1){ + + if (m == 'q') { //输入 q 退出游戏 + printf("Game Over!\n"); + system("pause"); + exit(0); + } + + int temp = getmove(p,m,px,py,ROW,COL); //玩家每推着箱子走一步,计数器+1 + if (temp == 1){ + steps++; + } + goto_xy(0,ROW+9); + printf("已走步数:%d\n",steps); + + if (gameover(&map[0][0],ROW,COL,AIM) == 0){ //箱子全部到达目的地,游戏结束 + break; + } + + m = getch(); + } + printf("Congratulations!\n"); //收尾工作 + savescores(fout,steps); //保存成绩 + fclose(fin); //关闭文件 + fclose(fout); + system("pause"); + return 0; +} + +void chooselevel(int *plevel) { //选关函数 + printf("推箱子小游戏\n"); + printf("输入 0 查看纪录 1,2... 选关\n"); + + scanf("%d",plevel); + system("cls"); + +} + +void showscore(){ //从对应文件在读入纪录 + printf("请输入你想查看纪录的关卡:\n"); + int n; + scanf("%d",&n); + FILE *fscore; + switch(n){ + case 1:{ + if ((fscore = fopen("score1.dat","r")) == NULL) { + printf("Can't read your score!\n"); + exit(0); + } + break; + } + case 2:{ + if ((fscore = fopen("score2.dat","r")) == NULL) { + printf("Can't read your score!\n"); + exit(0); + } + break; + } + default:; + } + rewind(fscore); + + while(!feof(fscore)){ + putchar(getc(fscore)); + } + putchar(10); //输出一个空格 +} + +void getrowcolaim(FILE *fin,int *prow,int *pcol,int *paim){ //从文件中读入地图行数,列数和箱子数 + int row,col,aim; + fscanf(fin,"%d,%d,%d,",&row,&col,&aim); + *prow = row; *pcol = col; *paim = aim; +} + +void creatmap(FILE *fin,int *p,int row,int col){ //从文件中读入地图数组 + + for (int i = 0; i < row; i++){ + for (int j = 0; j < col; j++){ + fscanf(fin,"%d,",(p+(i*col)+j)); + } + } +} + +void print(int *p,int ROW,int COL){ //输出地图数组 + for (int i = 0; i Date: Sat, 23 Jun 2018 08:45:29 +0800 Subject: [PATCH 13/17] Add files via upload --- linkedlist.c | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 linkedlist.c diff --git a/linkedlist.c b/linkedlist.c new file mode 100644 index 00000000..c0c52773 --- /dev/null +++ b/linkedlist.c @@ -0,0 +1,111 @@ +#include +#include +#define LEN sizeof(Stu) + +typedef struct Student{ + long num; + float score; + struct Student *next; +}Stu; + +int n; +Stu * creat(); +void print(Stu *head); +Stu * reverse(Stu *head); +int search(Stu *head,int t); + +int main(){ + int t=0; + Stu *pt = creat();//创建链表 + printf("\n"); + print(pt); + pt = reverse(pt);//反转链表 + printf("\nreverse:\n"); + print(pt); + printf("\n"); + t = search(pt,t);//查找第一个满足要求的节点 + printf("%d ",t); + t = search(pt,t);//查找下一个满足要求的节点 + printf("%d ",t); + return 0; +} + +Stu * creat() { + Stu *head = NULL, *p1 = NULL, *p2 = NULL; + p1 = p2 = (Stu *) malloc(LEN); + scanf("%ld %f",&p1 -> num,&p1 -> score); + while (p1 -> num != 0){ + n++; + if (n == 1){ + head = p1; + } + else{ + p2 -> next = p1; + } + p2 = p1; + p1 = (Stu *) malloc(LEN); + scanf("%ld %f",&p1 -> num,&p1 -> score); + } + p2 -> next = NULL; + return head; +} + +void print(Stu *head){ + Stu *p = head; + while (head != NULL){ + printf("%ld %.1f\n",p -> num,p -> score); + if (p -> next == NULL) { + break; + } + p = p -> next; + } +} + +Stu * reverse(Stu *head) { + /*递归法 + if (head == NULL || head -> next == NULL) { + return head; + } + Stu *p = head -> next; + Stu *newhead = reverse(p); + p -> next = head; + head -> next = NULL; + return newhead; */ + + Stu *cur = head, *temp = head; + if ((cur -> next) != NULL){ + cur = cur -> next; + temp -> next = NULL; + } + while (1){ + temp = cur; + cur = cur -> next; + temp -> next = head; + head = temp; + if (cur -> next == NULL){ + cur -> next = temp; + break; + } + } + head = cur; + return head; +} + +int search(Stu *head,int t){ + int m=0; + Stu *p = head; + while(head != NULL){ + m++; + if((p->score < 60) || (p->next == NULL)){ + if(m>t){ + break; + } + } + p = p -> next; + } + if((m>=n)&&(p->score >= 60)){ + return -1; + } + return m; +} + From 9b8f86d3514d2deeae4a4769ef565b8d2b380437 Mon Sep 17 00:00:00 2001 From: Anthem <32233926+Anthem9@users.noreply.github.com> Date: Sat, 23 Jun 2018 08:45:55 +0800 Subject: [PATCH 14/17] Add files via upload --- level1/p11_linkedList/linkedlist.c | 111 +++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 level1/p11_linkedList/linkedlist.c diff --git a/level1/p11_linkedList/linkedlist.c b/level1/p11_linkedList/linkedlist.c new file mode 100644 index 00000000..c0c52773 --- /dev/null +++ b/level1/p11_linkedList/linkedlist.c @@ -0,0 +1,111 @@ +#include +#include +#define LEN sizeof(Stu) + +typedef struct Student{ + long num; + float score; + struct Student *next; +}Stu; + +int n; +Stu * creat(); +void print(Stu *head); +Stu * reverse(Stu *head); +int search(Stu *head,int t); + +int main(){ + int t=0; + Stu *pt = creat();//创建链表 + printf("\n"); + print(pt); + pt = reverse(pt);//反转链表 + printf("\nreverse:\n"); + print(pt); + printf("\n"); + t = search(pt,t);//查找第一个满足要求的节点 + printf("%d ",t); + t = search(pt,t);//查找下一个满足要求的节点 + printf("%d ",t); + return 0; +} + +Stu * creat() { + Stu *head = NULL, *p1 = NULL, *p2 = NULL; + p1 = p2 = (Stu *) malloc(LEN); + scanf("%ld %f",&p1 -> num,&p1 -> score); + while (p1 -> num != 0){ + n++; + if (n == 1){ + head = p1; + } + else{ + p2 -> next = p1; + } + p2 = p1; + p1 = (Stu *) malloc(LEN); + scanf("%ld %f",&p1 -> num,&p1 -> score); + } + p2 -> next = NULL; + return head; +} + +void print(Stu *head){ + Stu *p = head; + while (head != NULL){ + printf("%ld %.1f\n",p -> num,p -> score); + if (p -> next == NULL) { + break; + } + p = p -> next; + } +} + +Stu * reverse(Stu *head) { + /*递归法 + if (head == NULL || head -> next == NULL) { + return head; + } + Stu *p = head -> next; + Stu *newhead = reverse(p); + p -> next = head; + head -> next = NULL; + return newhead; */ + + Stu *cur = head, *temp = head; + if ((cur -> next) != NULL){ + cur = cur -> next; + temp -> next = NULL; + } + while (1){ + temp = cur; + cur = cur -> next; + temp -> next = head; + head = temp; + if (cur -> next == NULL){ + cur -> next = temp; + break; + } + } + head = cur; + return head; +} + +int search(Stu *head,int t){ + int m=0; + Stu *p = head; + while(head != NULL){ + m++; + if((p->score < 60) || (p->next == NULL)){ + if(m>t){ + break; + } + } + p = p -> next; + } + if((m>=n)&&(p->score >= 60)){ + return -1; + } + return m; +} + From b1d67b113f51f62fd952288e77db0f1e7f257f55 Mon Sep 17 00:00:00 2001 From: Anthem <32233926+Anthem9@users.noreply.github.com> Date: Sat, 23 Jun 2018 08:46:29 +0800 Subject: [PATCH 15/17] Add files via upload --- warehouse.c | 165 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 warehouse.c diff --git a/warehouse.c b/warehouse.c new file mode 100644 index 00000000..e5fb914c --- /dev/null +++ b/warehouse.c @@ -0,0 +1,165 @@ +#include +#include + +//货号为4位,数量为2位 + +void print(); +void outList(); +void add(); +void move(); + +int main() { + print(); + int m; + m=getch(); + while(1){ + system("cls"); + switch(m){ + case 49:{ + printf("货号 数量\n"); + outList(); + break; + } + case 50:{ + add(); + break; + } + case 51:{ + move(); + break; + } + case 52:{ + printf("感谢使用!\n"); + system("pause"); + exit(0); + break; + } + default:; + } + system("cls"); + print(); + m=getch(); + } + + + return 0; +} + +void print(){ + printf("1.显示存货列表\n"); + printf("2.入库\n"); + printf("3.出库\n"); + printf("4.退出程序\n"); +} + +void outList(){ + FILE *fin; + if((fin=fopen("list.dat","r"))==NULL){ + printf("读取存货列表失败!\n"); + system("pause"); + system("cls"); + return; + } + rewind(fin); + while(!feof(fin)){ + putchar(getc(fin)); + } + putchar(10); + fclose(fin); + + system("pause"); + system("cls"); +} + +void add(){ + FILE *fout; + if((fout=fopen("list.dat","a+"))==NULL){ + printf("打开文件失败!"); + system("pause"); + system("cls"); + return; + } + char ch='y'; + while(ch=='y'){ + char goods[20]; + int i = 0; + printf("请输入货物型号(以 # 结束):\n"); + goods[0] = getchar(); + + while (goods[i] != '#'){ + fprintf(fout,"%c",goods[i]); + i++; + goods[i] = getchar(); + } + fprintf(fout," : "); + + int num; + printf("请输入货物数量:\n"); + getchar(); + scanf("%d",&num); + getchar(); + if(num<10){ + fprintf(fout,"%d",0); + fprintf(fout,"%d\n",num); + } + else{ + fprintf(fout,"%d\n",num); + } + + printf("是否继续?(y/n)\n"); + ch = getch(); + } + fclose(fout); +} + +void move(){ + FILE *fout; + if((fout=fopen("list.dat","r+"))==NULL){ + printf("打开文件失败!"); + system("pause"); + system("cls"); + return; + } + + int goods=0,num1=0,num2=0,num3=0; + char ch='y'; + while(ch=='y'){ + long tag=7; + printf("请输入货号:\n"); + scanf("%d",&goods); + getchar(); + tag=tag+1+(goods-1)*10; + fseek(fout,tag,SEEK_SET); + + printf("请输入出库数量:\n"); + scanf("%d",&num1); + getchar(); + fscanf(fout,"%d\n",&num2); + rewind(fout); + fseek(fout,tag,SEEK_SET); + num3=num2-num1; + if(num3<10){ + if(num3>0){ + fprintf(fout,"%d",0); + fprintf(fout,"%d",num3); + } + else{ + printf("出库数量大于库存数量,请重新输入!\n"); + continue; + } + } + else{ + fprintf(fout,"%d",num3); + } + + fseek(fout,tag,SEEK_SET); + printf("操作成功!此货物目前数量:"); + fscanf(fout,"%d\n",&num2); + printf("%d\n",num2); + + printf("是否继续?(y/n)\n"); + ch=getch(); + } + fclose(fout); +} + From d78129778798039ded240d7aef150ba00738fa94 Mon Sep 17 00:00:00 2001 From: Anthem <32233926+Anthem9@users.noreply.github.com> Date: Sat, 23 Jun 2018 08:46:50 +0800 Subject: [PATCH 16/17] Delete warehouse.c --- warehouse.c | 165 ---------------------------------------------------- 1 file changed, 165 deletions(-) delete mode 100644 warehouse.c diff --git a/warehouse.c b/warehouse.c deleted file mode 100644 index e5fb914c..00000000 --- a/warehouse.c +++ /dev/null @@ -1,165 +0,0 @@ -#include -#include - -//货号为4位,数量为2位 - -void print(); -void outList(); -void add(); -void move(); - -int main() { - print(); - int m; - m=getch(); - while(1){ - system("cls"); - switch(m){ - case 49:{ - printf("货号 数量\n"); - outList(); - break; - } - case 50:{ - add(); - break; - } - case 51:{ - move(); - break; - } - case 52:{ - printf("感谢使用!\n"); - system("pause"); - exit(0); - break; - } - default:; - } - system("cls"); - print(); - m=getch(); - } - - - return 0; -} - -void print(){ - printf("1.显示存货列表\n"); - printf("2.入库\n"); - printf("3.出库\n"); - printf("4.退出程序\n"); -} - -void outList(){ - FILE *fin; - if((fin=fopen("list.dat","r"))==NULL){ - printf("读取存货列表失败!\n"); - system("pause"); - system("cls"); - return; - } - rewind(fin); - while(!feof(fin)){ - putchar(getc(fin)); - } - putchar(10); - fclose(fin); - - system("pause"); - system("cls"); -} - -void add(){ - FILE *fout; - if((fout=fopen("list.dat","a+"))==NULL){ - printf("打开文件失败!"); - system("pause"); - system("cls"); - return; - } - char ch='y'; - while(ch=='y'){ - char goods[20]; - int i = 0; - printf("请输入货物型号(以 # 结束):\n"); - goods[0] = getchar(); - - while (goods[i] != '#'){ - fprintf(fout,"%c",goods[i]); - i++; - goods[i] = getchar(); - } - fprintf(fout," : "); - - int num; - printf("请输入货物数量:\n"); - getchar(); - scanf("%d",&num); - getchar(); - if(num<10){ - fprintf(fout,"%d",0); - fprintf(fout,"%d\n",num); - } - else{ - fprintf(fout,"%d\n",num); - } - - printf("是否继续?(y/n)\n"); - ch = getch(); - } - fclose(fout); -} - -void move(){ - FILE *fout; - if((fout=fopen("list.dat","r+"))==NULL){ - printf("打开文件失败!"); - system("pause"); - system("cls"); - return; - } - - int goods=0,num1=0,num2=0,num3=0; - char ch='y'; - while(ch=='y'){ - long tag=7; - printf("请输入货号:\n"); - scanf("%d",&goods); - getchar(); - tag=tag+1+(goods-1)*10; - fseek(fout,tag,SEEK_SET); - - printf("请输入出库数量:\n"); - scanf("%d",&num1); - getchar(); - fscanf(fout,"%d\n",&num2); - rewind(fout); - fseek(fout,tag,SEEK_SET); - num3=num2-num1; - if(num3<10){ - if(num3>0){ - fprintf(fout,"%d",0); - fprintf(fout,"%d",num3); - } - else{ - printf("出库数量大于库存数量,请重新输入!\n"); - continue; - } - } - else{ - fprintf(fout,"%d",num3); - } - - fseek(fout,tag,SEEK_SET); - printf("操作成功!此货物目前数量:"); - fscanf(fout,"%d\n",&num2); - printf("%d\n",num2); - - printf("是否继续?(y/n)\n"); - ch=getch(); - } - fclose(fout); -} - From 7886132c1727f8a9ab6000c6c13d93512fa43bf8 Mon Sep 17 00:00:00 2001 From: Anthem <32233926+Anthem9@users.noreply.github.com> Date: Sat, 23 Jun 2018 08:47:01 +0800 Subject: [PATCH 17/17] Delete linkedlist.c --- linkedlist.c | 111 --------------------------------------------------- 1 file changed, 111 deletions(-) delete mode 100644 linkedlist.c diff --git a/linkedlist.c b/linkedlist.c deleted file mode 100644 index c0c52773..00000000 --- a/linkedlist.c +++ /dev/null @@ -1,111 +0,0 @@ -#include -#include -#define LEN sizeof(Stu) - -typedef struct Student{ - long num; - float score; - struct Student *next; -}Stu; - -int n; -Stu * creat(); -void print(Stu *head); -Stu * reverse(Stu *head); -int search(Stu *head,int t); - -int main(){ - int t=0; - Stu *pt = creat();//创建链表 - printf("\n"); - print(pt); - pt = reverse(pt);//反转链表 - printf("\nreverse:\n"); - print(pt); - printf("\n"); - t = search(pt,t);//查找第一个满足要求的节点 - printf("%d ",t); - t = search(pt,t);//查找下一个满足要求的节点 - printf("%d ",t); - return 0; -} - -Stu * creat() { - Stu *head = NULL, *p1 = NULL, *p2 = NULL; - p1 = p2 = (Stu *) malloc(LEN); - scanf("%ld %f",&p1 -> num,&p1 -> score); - while (p1 -> num != 0){ - n++; - if (n == 1){ - head = p1; - } - else{ - p2 -> next = p1; - } - p2 = p1; - p1 = (Stu *) malloc(LEN); - scanf("%ld %f",&p1 -> num,&p1 -> score); - } - p2 -> next = NULL; - return head; -} - -void print(Stu *head){ - Stu *p = head; - while (head != NULL){ - printf("%ld %.1f\n",p -> num,p -> score); - if (p -> next == NULL) { - break; - } - p = p -> next; - } -} - -Stu * reverse(Stu *head) { - /*递归法 - if (head == NULL || head -> next == NULL) { - return head; - } - Stu *p = head -> next; - Stu *newhead = reverse(p); - p -> next = head; - head -> next = NULL; - return newhead; */ - - Stu *cur = head, *temp = head; - if ((cur -> next) != NULL){ - cur = cur -> next; - temp -> next = NULL; - } - while (1){ - temp = cur; - cur = cur -> next; - temp -> next = head; - head = temp; - if (cur -> next == NULL){ - cur -> next = temp; - break; - } - } - head = cur; - return head; -} - -int search(Stu *head,int t){ - int m=0; - Stu *p = head; - while(head != NULL){ - m++; - if((p->score < 60) || (p->next == NULL)){ - if(m>t){ - break; - } - } - p = p -> next; - } - if((m>=n)&&(p->score >= 60)){ - return -1; - } - return m; -} -