diff --git a/level1/p01_runningLetter/running letter.cpp b/level1/p01_runningLetter/running letter.cpp new file mode 100644 index 00000000..b5280b26 --- /dev/null +++ b/level1/p01_runningLetter/running letter.cpp @@ -0,0 +1,38 @@ +#include +#include +int main() +{ + int m = 10; + while (1) + { + for (int n = 0; n < m; n++) + { + for (int i = 0; i <= n; ++i) + { + printf("\n"); + } + for (int i = 0; i <= n; ++i) + { + printf(" "); + } + printf("H"); + Sleep(100); + system("cls"); + } + for (int n = m; n > 0; n--) + { + + for (int i = 0; i <= n; ++i) + { + printf("\n"); + }for (int i = 0;i<=n; ++i) + { + printf(" "); + } + printf("H"); + Sleep(100); + system("cls"); + } + } + return 0; +} \ No newline at end of file diff --git a/level1/p02_isPrime/isPrime.cpp b/level1/p02_isPrime/isPrime.cpp new file mode 100644 index 00000000..9e5ca85a --- /dev/null +++ b/level1/p02_isPrime/isPrime.cpp @@ -0,0 +1,27 @@ +#include +#include +int main() +{ + int n,flag=0; + scanf("%d", &n); + if (n == 2||n==1) + printf("yes\n"); + else + { + for (int i=2; i <=sqrt(n); i++) + { + if (n % i == 0) + { + flag = 1; + break; + } + + } + if (flag == 1) + printf("no\n"); + else + printf("yes\n"); + } + + return 0; +} diff --git a/level1/p03_Diophantus/Diophantus.cpp b/level1/p03_Diophantus/Diophantus.cpp new file mode 100644 index 00000000..50b8eae6 --- /dev/null +++ b/level1/p03_Diophantus/Diophantus.cpp @@ -0,0 +1,14 @@ +#include + +int main() +{ + for(int i=1;i<100;++i) + { + if(i/2==i-(1.0/6*i+1.0/12*i+1.0/7*i+5+4)) + { + printf("%d",i); + break; + } + } + return 0; +} \ No newline at end of file diff --git a/level1/p04_ narcissus/narcissus.cpp b/level1/p04_ narcissus/narcissus.cpp new file mode 100644 index 00000000..6b162bc5 --- /dev/null +++ b/level1/p04_ narcissus/narcissus.cpp @@ -0,0 +1,14 @@ +#include +int main() +{ + for (int i = 100; i <= 999; ++i) + { + int a = i / 100; + int b = i / 10 % 10; + int c = i % 10; + if (i ==a*a*a+b*b*b+c*c*c) + printf("%d\t", i); + + } + return 0; +} \ No newline at end of file diff --git a/level1/p05_allPrimes/prime.cpp b/level1/p05_allPrimes/prime.cpp new file mode 100644 index 00000000..dea96a70 --- /dev/null +++ b/level1/p05_allPrimes/prime.cpp @@ -0,0 +1,19 @@ +#include +#include +int main() +{ + printf("2\t"); + for (int i = 3; i <= 1000; ++i) + { + int k = 0; + for (int j = 2; j <= sqrt(i); ++j) + { + if (i % j == 0) + k = 1; + + } + if (k == 0) + printf("%d\t", i); + + } +} \ No newline at end of file diff --git a/level1/p06_Goldbach/Goldbach.cpp b/level1/p06_Goldbach/Goldbach.cpp new file mode 100644 index 00000000..31b52a30 --- /dev/null +++ b/level1/p06_Goldbach/Goldbach.cpp @@ -0,0 +1,38 @@ +#include +#include +int isPrime(int x) +{ + int flag=1; + if(x>2) + { + for(int i=2;i +#include +#include +void crypt(int m) +{ + char text[256] = { '\0' }, text1[256] = { '\0' }; + if (m == 0) + { + printf("enter the words you want to encrypt:\n"); + scanf("%s", text); + for (int i = 0; i <= int(sizeof(text)); i++) + { + if ((text[i] >= 'a' && text[i] <= 'z' )||( text[i] >= 'A' && text[i] <= 'Z'))//uvwxyz + { + if ((text[i] >= 'a' && text[i] <= 'u' )||( text[i] >= 'A' && text[i] <= 'U')) + { + text1[i] = text[i] + 5; + } + else + { + text1[i] = text[i] + 5 - 26; + } + } + + } + printf("%s-->%s", text, text1); + } + else if (m == 1) + { + printf("enter the words you want to decrypt:\n"); + scanf("%s", text); + for (int i = 0; i <= int(sizeof(text)); i++) + { + if ((text[i] >= 'a' && text[i] <= 'z' )||( text[i] >= 'A' && text[i] <= 'Z'))//uvwxyz + { + if ((text[i] >= 'e' && text[i] <= 'z' )||( text[i] >= 'E' && text[i] <= 'Z')) + { + text1[i] = text[i] - 5; + } + else + { + text1[i] = text[i] -5+26; + } + } + + } + printf("%s<--%s", text, text1); + } + else + { + printf("error"); + } +} + +int main() +{ + int m; + printf("press 0 to encrypt , press 1 to decrypt, press 2 to end the exe\n"); + scanf("%d", &m); + while(m!=2) + { + crypt(m); + //system("cls"); + printf("\npress 0 to encrypt , press 1 to decrypt, press 2 to end the exe\n"); + scanf("%d", &m); + } + return 0; +} diff --git a/level1/p08_hanoi/hanio of function pointer.cpp b/level1/p08_hanoi/hanio of function pointer.cpp new file mode 100644 index 00000000..b83d482e --- /dev/null +++ b/level1/p08_hanoi/hanio of function pointer.cpp @@ -0,0 +1,36 @@ +#include +int main() +{ + int n; + char A='A',B='B',C='C'; + printf("enter the number of plate you want to move:\n"); + void hanio(int n,char a,char b,char c); + scanf("%d",&n); + hanio(n,A,B,C); + return 0; +} + + +void hanio(int n,char a,char b,char c) +{ + void move(char a,char b); + void(*p)(char,char); + void(*q)(int,char,char,char); + q=hanio; + p=move; + if(n==1) + { + (*p)(a,c); + } + else + { + (*q)(n-1,a,c,b); + (*p)(a,c); + (*q)(n-1,b,c,a); + } +} + +void move(char a,char b) +{ + printf("%c -> %c\n",a,b); +} diff --git a/level1/p08_hanoi/hanio.cpp b/level1/p08_hanoi/hanio.cpp new file mode 100644 index 00000000..c05e42a4 --- /dev/null +++ b/level1/p08_hanoi/hanio.cpp @@ -0,0 +1,32 @@ +#include +int main() +{ + int n; + char A='A',B='B',C='C'; + printf("enter the number of plate you want to move:\n"); + void hanio(int n,char a,char b,char c); + scanf("%d",&n); + hanio(n,A,B,C); + return 0; +} + + +void hanio(int n,char a,char b,char c) +{ + void move(char a,char b); + if(n==1) + { + move(a,c); + } + else + { + hanio(n-1,a,c,b); + move(a,c); + hanio(n-1,b,c,a); + } +} + +void move(char a,char b) +{ + printf("%c -> %c\n",a,b); +} diff --git a/level1/p09_maze/try.cpp b/level1/p09_maze/try.cpp new file mode 100644 index 00000000..362ca9dc --- /dev/null +++ b/level1/p09_maze/try.cpp @@ -0,0 +1,205 @@ +#include +#include +#include +#include +#include + +#define L 20 + +//墙和路径的标识 +#define WALL 0 +#define ROUTE 1 + +//控制迷宫的复杂度,数值越大复杂度越低,最小值为0 +static int Rank = 0; + +//生成迷宫 +void CreateMaze(int **maze, int x, int y); +void print(int **Maze); +void move(int** Maze, char t, int *x, int *y); +int init(int** Maze); +void start(int** Maze); + +int main() { + + int **Maze = (int**)malloc(L * sizeof(int *)); + for (int i = 0; i < L; i++) + { + Maze[i] = (int*)calloc(L, sizeof(int)); + } + + start(Maze); + + for (int i = 0; i < L; i++) free(Maze[i]); + free(Maze); + return 0; +} + +void start(int** Maze) +{ + char t; + int x=2,y=1,out=0; + int *p=&x,*q=&y; + out=init(Maze); + system("cls"); + printf("输入WASD进行移动,输入ESC结束游戏\n\n\n"); + print(Maze); + while(1) + { + t=getch(); + if(t==27) + { + break; + printf("游戏结束"); + } + system("cls"); + move(Maze,t,p,q); + print(Maze); + if(x==out&&y==L-2) + { + system("cls"); + printf("游戏胜利!\n\n"); + Sleep(1500); + break; + } + } +} + +int init(int** Maze) +{ + srand((unsigned)time(NULL)); + + + + //最外围层设为路径的原因,为了防止挖路时挖出边界,同时为了保护迷宫主体外的一圈墙体被挖穿 + for (int i = 0; i < L; i++){ + Maze[i][0] = ROUTE; + Maze[0][i] = ROUTE; + Maze[i][L - 1] = ROUTE; + Maze[L - 1][i] = ROUTE; + } + + //创造迷宫,(2,2)为起点 + CreateMaze(Maze, 2, 2); + + //画迷宫的入口和出口 + Maze[2][1] = ROUTE; + + int i; + //由于算法随机性,出口有一定概率不在(L-3,L-2)处,此时需要寻找出口 + for ( i = L - 3; i >= 0; i--) { + if (Maze[i][L - 3] == ROUTE) { + Maze[i][L - 2] = ROUTE; + break; + } + } + return i; + +} + +void move(int** Maze, char t, int *x, int *y) { + int i = *x, j = *y;//记录原始位置 + switch(t) { + case 'w': //向上移动 + *x -= 1; + break; + case 's': //向下移动 + *x += 1; + break; + case 'a': //向左移动 + *y -= 1; + break; + case 'd': //向右移动 + *y += 1; + break; + default: + break; + } + if(*x>=0 && *x=0 && *y0) { + dx += direction[i][0]; + dy += direction[i][1]; + + //排除掉回头路 + if (maze[dx][dy] == ROUTE) { + break; + } + + //判断是否挖穿路径 + int count = 0; + for (int j = dx - 1; j < dx + 2; j++) { + for (int k = dy - 1; k < dy + 2; k++) { + //abs(j - dx) + abs(k - dy) == 1 确保只判断九宫格的四个特定位置 + if (abs(j - dx) + abs(k - dy) == 1 && maze[j][k] == ROUTE) { + count++; + } + } + } + + if (count > 1) { + break; + } + + //确保不会挖穿时,前进 + --range; + maze[dx][dy] = ROUTE; + } + + //没有挖穿危险,以此为节点递归 + if (range <= 0) { + CreateMaze(maze, dx, dy); + } + } +} diff --git a/level1/p10_pushBoxes/func.cpp b/level1/p10_pushBoxes/func.cpp new file mode 100644 index 00000000..3fe92950 --- /dev/null +++ b/level1/p10_pushBoxes/func.cpp @@ -0,0 +1,240 @@ +// +// Created by chovy on 2021/5/6. +// +#include +#include +#include +#include "func.h" + +void menu(void) +{ + int k=1; + int (*map)[L]; + while(k) + { + system("cls"); + printf("\nwelcome to the game\n"); + printf("1.\t\tlevel1\n"); + printf("2.\t\tlevel2\n"); + printf("3.\t\tlevel3\n"); + printf("4.\t\texit\n"); + int c; + scanf("%d",&c); + switch(c) + { + case 1: + { + map=fileReader(1); + start(map,1); + break; + } + case 2: + { + map=fileReader(2); + start(map,2); + break; + } + case 3: + { + map=fileReader(3); + start(map,3); + break; + } + case 4: + { + printf("thank you for your playing......\n"); + k=0; + Sleep(1500); + break; + } + default: + break; + } + } +} + +void start(int map[][L],int nmap) { + int num,goal = 0; + for(int i=0;i0;num--) { + for (int i = 0; i < L; i++) { + fscanf(fp, "%d %d %d %d %d %d %d %d", &map[i][0], &map[i][1], &map[i][2], &map[i][3], &map[i][4], + &map[i][5], &map[i][6], &map[i][7]); + } + fscanf(fp,"%d",&map[L][0]); + } + fclose(fp); + return map; +} diff --git a/level1/p10_pushBoxes/func.h b/level1/p10_pushBoxes/func.h new file mode 100644 index 00000000..07f02110 --- /dev/null +++ b/level1/p10_pushBoxes/func.h @@ -0,0 +1,24 @@ +// +// Created by chovy on 2021/5/6. +// + +#ifndef PUSHTHEBOX_FUNC_H +#define PUSHTHEBOX_FUNC_H + +const int route=0; +const int wall=1; +const int player=2; +const int box=3; +const int destination=4; +const int finish=5; +const int pfinish=6; +const int L=8; + +void menu(void); +int (*fileReader(int num))[L]; +int (*Push(int map[][L]))[L]; +int check(int map[][L]); +void print(int map[][L]); +void start(int map[][L],int nmap); + +#endif //PUSHTHEBOX_FUNC_H diff --git a/level1/p10_pushBoxes/main.cpp b/level1/p10_pushBoxes/main.cpp new file mode 100644 index 00000000..e381b2e3 --- /dev/null +++ b/level1/p10_pushBoxes/main.cpp @@ -0,0 +1,10 @@ +#include +#include +#include +#include "func.h" + +int main() +{ + menu(); + return 0; +} \ No newline at end of file diff --git a/level1/p10_pushBoxes/map.txt b/level1/p10_pushBoxes/map.txt new file mode 100644 index 00000000..8b24cc73 --- /dev/null +++ b/level1/p10_pushBoxes/map.txt @@ -0,0 +1,27 @@ +0 0 1 1 1 0 0 0 +0 0 1 4 1 0 0 0 +0 0 1 0 1 1 1 1 +1 1 1 3 0 3 4 1 +1 4 0 3 2 1 1 1 +1 1 1 1 3 1 0 0 +0 0 0 1 4 1 0 0 +0 0 0 1 1 1 0 0 +100 +1 1 1 1 1 1 1 0 +1 4 4 3 4 4 1 0 +1 4 4 1 4 4 1 0 +1 0 3 3 3 0 1 0 +1 0 0 3 0 0 1 0 +1 0 3 3 3 0 1 0 +1 0 0 1 2 0 1 0 +1 1 1 1 1 1 1 0 +100 +1 1 1 1 1 1 1 0 +1 4 0 4 0 4 1 0 +1 0 3 3 3 0 1 0 +1 4 3 2 3 4 1 0 +1 0 3 3 3 0 1 0 +1 4 0 4 0 4 1 0 +1 1 1 1 1 1 1 0 +0 0 0 0 0 0 0 0 +100 \ No newline at end of file diff --git a/level1/p11_linkedList/link.cpp b/level1/p11_linkedList/link.cpp new file mode 100644 index 00000000..46f8eb1d --- /dev/null +++ b/level1/p11_linkedList/link.cpp @@ -0,0 +1,208 @@ +#include +#include + +struct Link* init(void); +struct Link* inverse3(struct Link*head); +void print(struct Link * head); +void search(struct Link*head); +struct Link* append(struct Link*head,int location,int data); +void menu(struct Link* head); + +struct Link +{ + int data; + struct Link*next; +}; + +struct Link* init(void) +{ + struct Link* head=NULL,*p1,*p2; + p1=p2=(Link*)malloc(sizeof(struct Link)); + int n=0,temp=1; + while(1) + { + n=n+1; + scanf("%d",&temp); + if(temp!=0) + { + p1->data=temp; + } + else + break; + if(n==1) + { + head=p1; + } + else + { + p2->next=p1; + } + p2=p1; + p1=(Link*)malloc(sizeof(struct Link)); + } + p2->next=NULL; + return(head); +} + +void print(struct Link * head) +{ + struct Link* p=head; + int i=1; + while(p!=NULL) + { + + printf("the %d num is %d\n",i,p->data); + p=p->next; + i++; + } + return; +} + +struct Link* append(struct Link*head,int location,int data) +{ + if(location>1) + { + struct Link* p1=head; + for(int i=1;inext; + } + struct Link* new_node=(struct Link*)malloc(sizeof(struct Link)); + struct Link* temp=p1->next; + new_node->data=data; + new_node->next=temp; + p1->next=new_node; + return head; + } + else if(location==1) + { + struct Link* temp=head; + struct Link* p1=head; + temp->data=data; + temp->next=p1->next; + return temp; + } + else + { + printf("the wrong location\n"); + return NULL; + } +} + +struct Link* inverse1(struct Link*head) +{ + struct Link* former=NULL,*mid=head,*latter=head->next; + while(latter!=NULL) + { + mid->next=former; + former=mid; + mid=latter; + latter=latter->next; + } + mid->next=former; + head=mid; + return head; +} + +struct Link* inverse2(struct Link*head) +{ + struct Link* new_head=NULL,*temp=NULL; + if(head==NULL||head->next==NULL) + return head; + while(head!=NULL) + { + temp=head; + head=head->next; + temp->next=new_head; + new_head=temp; + } + return new_head; +} + +struct Link* inverse3(struct Link*head) +{ + struct Link*beg=NULL,*end=NULL; + if(head==NULL||head->next==NULL) + return head; + beg=head; + end=beg->next; + while(end!=NULL) + { + beg->next=end->next; + end->next=head; + head=end; + end=beg->next; + } + return head; +} + +void search(struct Link*head) +{ + struct Link* p=head; + int flag=1,i=1; + while(p!=NULL) + { + if(p->data==5) + { + printf("the %d th num of 5 is on %d\n",flag,i); + flag++; + } + i++; + + p=p->next; + } + if(flag==1) + printf("there is no 5 in this list"); + return; +} + +void menu(struct Link* head) +{ + printf("\n"); + printf("choose the function you want to do:\n"); + printf("press 1 to append a number in a certain place\n"); + printf("press 2 to inverse the list\n"); + printf("press 3 to search all '5' in the list\n"); + printf("\n"); + int m; + scanf("%d",&m); + switch(m) + { + case 1: + { + printf("enter the appending number\n"); + int apdata,location; + scanf("%d",&apdata); + printf("enter the its location:\n"); + scanf("%d",&location); + head=append(head,location,apdata); + print(head); + printf("\n"); + system("pause"); + break; + } + case 2: + { + head=inverse1(head); + printf("the inversed list is:\n"); + print(head); + break; + } + case 3: + { + search(head); + break; + } + default: + printf("error!\t enter the right number\n"); + break; + } +} +int main() +{ + printf("enter the number you want to init in the list(enter 0 as the last number to quit)\n"); + struct Link* head=init(); + print(head); + menu(head); + return 0; +} diff --git a/level1/p12_warehouse/warehouse.cpp b/level1/p12_warehouse/warehouse.cpp new file mode 100644 index 00000000..35d5710d --- /dev/null +++ b/level1/p12_warehouse/warehouse.cpp @@ -0,0 +1,247 @@ +#include +#include +#include +#include +//就差将数据存入文件 + +struct Ware +{ + int num;//产品序号 + int stock;//产品库存 + char name[20];//产品名字(创建时可以用静态数组) + char store[20];//产品所在仓库 + struct Ware *next;//下一个指针节点 +}; +typedef struct Ware ware ; + +void print(ware* p); +void menu(void); +ware* putin(ware* p); +ware* putout(ware* p); +ware* init(ware*p); +void append(ware* temp); +void deleteNode(ware* p,int numm); +ware* readFile(); +void writeFile(ware* temp1); + + +int main() +{ + menu(); + return 0; +} + +void menu(void) +{ + int k=1; + ware* p=(ware*)malloc(sizeof(ware)); + p=init(p); + while(k) + { + system("cls"); + int t; + printf("\n\t1.\tshow the list\t\n"); + printf("\n\t2.\tenter warehouse\t\n"); + printf("\n\t3.\tputout warehouse\t\n"); + printf("\n\t4.\tsave the file\t\n"); + printf("\n\t5.\tread the file\t\n"); + printf("\n\t6.\texit\t\n"); + scanf("%d",&t); + switch(t) + { + case 1: + print(p); + break; + case 2: + p=putin(p); + break; + case 3: + p=putout(p); + break; + case 4: + writeFile(p); + Sleep(500); + break; + case 5: + p=readFile(); + break; + case 6: + printf("\n\tthanks for using!\n"); + k=0; + break; + default: + printf("\n\terror!\n"); + break; + } + } + +} + +void print(ware* p) +{ + system("cls"); + ware* temp=p;//头节点是否有数据 + printf("\nnum\tstore\tname\tcommodity stock\n"); + printf("------------------------------------------\n"); + while(temp->next!=NULL){ + printf("%d\t%s\t%s\t%d\n",temp->num,temp->store,temp->name,temp->stock); + printf("------------------------------------------\n"); + temp=temp->next; + } + printf("\t enter anykey to back to menu\n"); + while(!getch()) + { + //press anykey + } +} + +ware* putin(ware* p)//入库,加入物品 +{ + system("cls"); + printf("\tthe ware is under putin mode\n"); + printf("-----------------------------------------------\n"); + ware* temp=p;//从头节点开始,头节点有数据 + int i=1; + while(temp->next!=NULL) + { + temp=temp->next; + ++i; + } + //将指针移动到表尾 + //创建新链表接上 + append(temp);//接上表尾 + //获取参数函数 + printf("\n\n"); + printf("the ware is successfully putin....\n\n\n"); + printf("\t enter anykey to back to menu\n");//一次加一个,插入表尾 + while(!getch()) + { + //press anykey to continue + } + return p;//返回头节点 +} + +void append(ware* temp) +{ + ware* newnode; + newnode=(ware*)malloc(sizeof(ware)); + temp->next=newnode; + temp=newnode; + printf("\ninput the name of the store:\n"); + char tempChar[20]; + scanf("%s",tempChar); + strcpy(temp->store,tempChar); + printf("\ninput the name of the commodity:\n"); + scanf("%s",tempChar); + strcpy(temp->name,tempChar); + printf("\ninput the stock of the ware:\n"); + scanf("%d",&temp->stock); + temp->num=5; + temp->next=NULL; +} + +ware* putout(ware* p)//出库,删除物品 +{ + + system("cls"); + ware* temp=p; + printf("\nthe repository is below:\n"); + printf("------------------------------------------\n"); + printf("\nnum\tstore\tname\tcommodity stock\n"); + printf("------------------------------------------\n"); + while(temp->next!=NULL)//指针 + { + printf("%d\t%s\t%s\t%d\n",temp->num,temp->store,temp->name,temp->stock); + printf("------------------------------------------\n"); + temp=temp->next; + } + printf("\nenter the number of the commodity you want to out\n"); + int numm; + scanf("%d",&numm); + deleteNode(p,numm); + //删除链表 + //Sleep(1500); + return p; +} + +void deleteNode(ware* p,int numm) +{ + ware* temp1=p;//用于移动 + ware* temp2;//用于插入 + while(temp1->num!=numm&&temp1->next!=NULL) + { + temp2=temp1; + temp1=temp1->next; + } + if(temp1->num==numm) + { + temp2->next=temp1->next; + } else + { + printf("\nthere is no such a node to delete...\n"); + } +} + +ware* init(ware*p) +{ + ware* temp1=p; + ware* temp2;//移动temp2 + /*temp1->next=(ware*)malloc(sizeof(ware)); + temp1=temp1->next;*/ + for(int i=0;i<5;i++) + { + strcpy(temp1->name,"123"); + strcpy(temp1->store,"321");//用字符串函数赋值 + temp1->num=i+1; + temp1->stock=50; + temp2=(ware*)malloc(sizeof(ware)); + temp1->next=temp2; + temp1=temp2; + } + temp1->next=NULL; + return p; +} + +void writeFile(ware* temp1)//文件光标 +{ + FILE* fp; + fp=fopen("data.txt","w"); + if(fp==NULL) + { + printf("File fail to open!\n"); + exit(0); + } + fseek(fp,1,SEEK_END);//全写在一行了 + while(temp1->next!=NULL) + { + fprintf(fp,"\n%d\t%s\t%s\t%d\n",temp1->num,temp1->name,temp1->store,temp1->stock); + temp1=temp1->next; + } + fclose(fp); + printf("the file is successfully saved!\n"); +} + +ware* readFile() +{ + FILE* fp; + fp=fopen("data.txt","r"); + if(fp==NULL) + { + printf("File fail to open!\n"); + exit(0); + } + ware data[100]; + ware* temp=(ware*)malloc(sizeof(ware)); + int i=0; + while(fscanf(fp,"%d\t%s\t%s\t%d\n",&data[i].num,data[i].name,data[i].store,&data[i].stock)!=EOF) + { + temp->next=&data[i]; + temp=temp->next; + temp->next=NULL; + i++; + } + ware* p=data; + printf("the file is successfully read!\n"); + return p; + +}