diff --git a/level1/p01_running_letter/.idea/.gitignore b/level1/p01_running_letter/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/level1/p01_running_letter/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/level1/p01_running_letter/.idea/modules.xml b/level1/p01_running_letter/.idea/modules.xml new file mode 100644 index 0000000..9aec7d8 --- /dev/null +++ b/level1/p01_running_letter/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/level1/p01_running_letter/.idea/p01_running_letter.iml b/level1/p01_running_letter/.idea/p01_running_letter.iml new file mode 100644 index 0000000..4c94235 --- /dev/null +++ b/level1/p01_running_letter/.idea/p01_running_letter.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/level1/p01_running_letter/.idea/vcs.xml b/level1/p01_running_letter/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/level1/p01_running_letter/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/level1/p01_running_letter/main.c b/level1/p01_running_letter/main.c index f84d224..69755a9 100644 --- a/level1/p01_running_letter/main.c +++ b/level1/p01_running_letter/main.c @@ -1,6 +1,18 @@ -#include - +#include +#include int main() { - printf("hello world!\n"); - return 0; -} \ No newline at end of file + for(int i=1;i<=50;i++){ + for(int j=1;j - +#include +#include +#include +int Prime(){ + int a; + printf("һ:"); + scanf("%d",&a); + int k=sqrt(a); + k++; + if(a==1){ + printf("ⲻһ\n"); + return 0; + } + if(a==2){ + printf("һprime\n"); + return 0; + } + for(int i=2;i<=k;i++){ + if( !(a%i) ){ + printf("ⲻһ\n"); + return 0; + } + } + printf("һprime\n"); +} int main() { - printf("hello world!\n"); - return 0; -} \ No newline at end of file + Prime(); + system("pause"); +} diff --git a/level1/p03_all_primes/main.c b/level1/p03_all_primes/main.c index f84d224..83ad381 100644 --- a/level1/p03_all_primes/main.c +++ b/level1/p03_all_primes/main.c @@ -1,6 +1,27 @@ -#include - +#include +#include +#include +#define MAX 1001 +int prime[500],isP[1020]; +void Prime(){ + for(int i=1;i<=MAX;i++)isP[i]=1; + int num=1; + isP[1]=0,isP[2]=1; + for(int i=2;i<=MAX;i++){ + if(isP[i]){ + prime[num++]=i; + for(int j=2;j*i<=MAX;j++){ + isP[j*i]=0; + } + } + } +} int main() { - printf("hello world!\n"); - return 0; -} \ No newline at end of file + Prime(); + for(int i=1;i<=1000;i++){ + if(isP[i])printf("%d ",i); + } + printf("\n"); + printf("所用时间:%.2lfs\n",(double)clock()/ CLOCKS_PER_SEC); + system("pause"); +} diff --git a/level1/p04_goldbach/main.c b/level1/p04_goldbach/main.c index f84d224..4bc07b5 100644 --- a/level1/p04_goldbach/main.c +++ b/level1/p04_goldbach/main.c @@ -1,6 +1,35 @@ -#include - +#include +#include +#include +#define MAX 1001 +int prime[500],isP[1020]; +void Prime(){ + for(int i=1;i<=MAX;i++)isP[i]=1; + int num=1; + isP[1]=0,isP[2]=1; + for(int i=2;i<=MAX;i++){ + if(isP[i]){ + prime[num++]=i; + for(int j=2;j*i<=MAX;j++){ + isP[j*i]=0; + } + } + } +} int main() { - printf("hello world!\n"); - return 0; -} \ No newline at end of file + Prime(); + int flag=0; + for(int num=6;num<=100;num++){ + flag=0; + for(int j=2;j<=num-4 && !flag;j++){ + for(int k=2;k<=num-4 && !flag;k++){ + int l=num-j-k; + if(isP[j]+isP[k]+isP[l]==3){ + printf("%d=%d+%d+%d\n",num,j,k,l); + flag=1; + } + } + } + } + system("pause"); +} diff --git a/level1/p05_encrypt_decrypt/main.c b/level1/p05_encrypt_decrypt/main.c index f84d224..692d15b 100644 --- a/level1/p05_encrypt_decrypt/main.c +++ b/level1/p05_encrypt_decrypt/main.c @@ -1,6 +1,42 @@ -#include - -int main() { - printf("hello world!\n"); - return 0; -} \ No newline at end of file +#include +#include +#include +char word[800]; +char secret[800]; +char letter[800]; +void encryption(){ + int len=strlen(word); + len--; + for(int i=0;i='a' && word[i]<='z'){ + secret[i]=word[i]+1; + }else if(word[i]>='0' && word[i]<='9'){ + secret[i]=word[i]+1; + }else if(word[i]>='A' && word[i]<='Z'){ + secret[i]=word[i]+1; + }else secret[i]=word[i]; + } + printf("密文为:%s\n",secret); +} +void decryption(){ + int len=strlen(secret); + for(int i=0;i='a' && secret[i]<='z'){ + letter[i]=secret[i]-1; + }else if(secret[i]>='0' && secret[i]<='9'){ + letter[i]=word[i]-1; + }else if(secret[i]>='A' && secret[i]<='Z'){ + letter[i]=secret[i]-1; + }else letter[i]=secret[i]; + } + printf("明文为:%s\n",letter); +} +int main(){ + for(int i=0;;i++){ + word[i]=getchar(); + if(word[i]=='\n')break; + } + encryption(); + decryption(); + system("pause"); +} diff --git a/level1/p06_hanoi/main.c b/level1/p06_hanoi/main.c index f84d224..17c80c2 100644 --- a/level1/p06_hanoi/main.c +++ b/level1/p06_hanoi/main.c @@ -1,6 +1,21 @@ -#include - +#include +#include +void work(char a,char b,int k){ + if(k==1){ + printf("%c -> %c\n",a,b); + return ; + } + char d='A'+'B'+'C'-a-b; + work(a,d,k-1); + work(a,b,1); + work(b,d,k-1); +} int main() { - printf("hello world!\n"); + int num; + printf("请输入汉诺塔圆盘个数:"); + scanf("%d",&num); + work('A','C',num); + system("pause"); return 0; -} \ No newline at end of file + +} diff --git a/level1/p07_maze/main.c b/level1/p07_maze/main.c index f84d224..b9bbcb2 100644 --- a/level1/p07_maze/main.c +++ b/level1/p07_maze/main.c @@ -1,6 +1,112 @@ -#include - -int main() { - printf("hello world!\n"); +#include +#include +#include +#include +HANDLE out; +int maze[40][40],footprint[40][40],game=0; +struct foorward{ + int x,y; +}way[4]; +void setcursor(int x,int y){ + SetConsoleCursorPosition( out , (COORD){x,y} ); +} +void explanation(){ + setcursor(45,5),printf("迷宫游戏"); + setcursor(35,7),printf("请通过方向键移动‘A’,使其到达终点'6'"); + setcursor(0,0); +} +int barrier(int x,int y){ + if( (x*y)<=0 || x>25 || y>25 || footprint[x][y] )return 1; return 0; -} \ No newline at end of file +} +void mazestruct(int x,int y); +void step(int x,int y){ + int a;// 0左 1上 2右 3下 + while(1){ + a=rand()%4; + if( !barrier( x+way[a].x , y+way[a].y) )break; + } + maze[(x+x+way[a].x)/2][(y+y+way[a].y)/2]=0; + mazestruct(x+way[a].x,y+way[a].y); +} +int flag=0; +void mazestruct(int x,int y){ + footprint[x][y]=1; + if( barrier(x-2,y) && barrier(x+2,y) && barrier(x,y+2) && barrier(x,y-2) ){ + flag=1; + return ; + } + step(x,y); + if(flag){ + if( barrier(x-2,y) && barrier(x+2,y) && barrier(x,y+2) && barrier(x,y-2) )return ; + flag--; + step(x,y); + } +} +void mazemake(){ + way[0].x=-2,way[0].y=0,way[1].x=0,way[1].y=-2; + way[2].x=2,way[2].y=0,way[3].x=0,way[3].y=2; + for(int i=0;i<=26;i++) + for(int j=0;j<=26;j++) + maze[i][j]=1,footprint[i][j]=0; + for(int i=1;i<=25;i+=2) + for(int j=1;j<=25;j+=2) + maze[i][j]=0; + maze[25][25]=2; + srand( (unsigned)time(NULL) ); + mazestruct(1,1); + for(int j=0;j<=26;j++){ + for(int i=0;i<=26;i++){ + if(maze[i][j]==1)printf("#"); + if(maze[i][j]==0)printf(" "); + if(maze[i][j]==2)printf("6"); + } + printf("\n"); + } + maze[25][25]=0; +} +int movecheck(COORD k,int key){//左 75 上 72 右 77 下 80 + if(key==75 && maze[k.X-1][k.Y])return 0; + if(key==72 && maze[k.X][k.Y-1])return 0; + if(key==77 && maze[k.X+1][k.Y])return 0; + if(key==80 && maze[k.X][k.Y+1])return 0; + return 1; +} +void move( COORD *k , int key ){ + if(key==75)k->X--; + if(key==72)k->Y--; + if(key==77)k->X++; + if(key==80)k->Y++; + SetConsoleCursorPosition(out,*k); + printf("A"); + if(k->X==25 && k->Y==25)game=1; +} +void gamerun(){ + setcursor(1,1);printf("A"); + COORD A={1,1}; + while(1){ + if(kbhit()){ + int key=getch(); + if( movecheck(A,key) ){ + setcursor(A.X,A.Y);printf(" "); + move(&A,key); + } + } + if(game){ + system("cls"); + printf("你赢啦!\n 按任意键继续游戏"); + char ch=getchar(); + return ; + } + } +} +int main(){ + while(1){ + game=0; + out= GetStdHandle(STD_OUTPUT_HANDLE); + explanation(); + mazemake(); + gamerun(); + } + +} diff --git a/level1/p08_push_boxes/main.c b/level1/p08_push_boxes/main.c index f84d224..1ffca70 100644 --- a/level1/p08_push_boxes/main.c +++ b/level1/p08_push_boxes/main.c @@ -1,6 +1,199 @@ -#include - -int main() { - printf("hello world!\n"); - return 0; -} \ No newline at end of file +#include +#include +#include +char gamemap[100][100]; +int box[100][100]; +HANDLE out; +typedef struct { + COORD A; +}cursor; +cursor role[10]; +int step[10]={0,0,0,0,0,0,0,0}; +int goal[10]={0,4,3,4,4,3}; +int score[10]={0,0,0,0,0,0,0,0}; +int level=1; +COORD markcursor={}; +void cursorset(COORD A){ + SetConsoleCursorPosition(out,A); +} +void explanation(){ + cursorset((COORD){20,10}); + printf("步数=0"); + cursorset( (COORD){20,4} ); + printf("推箱子游戏"); + cursorset( (COORD) {0,0} ); +} +void clear(){ + score[level]=0; + for(int i=0;i<=20;i++){ + for(int j=0;j<=20;j++){ + gamemap[i][j]='a'; + box[i][j]=0; + } + } + system("cls"); +} +void mapin(){ + switch(level){ + case 1 : + freopen("C:\\Users\\scanffer\\CLionProjects\\c2023-a\\level1\\p08_push_boxes\\第一关.txt","r",stdin); + break; + case 2 : + freopen("C:\\Users\\scanffer\\CLionProjects\\c2023-a\\level1\\p08_push_boxes\\第二关.txt","r",stdin); + break; + case 3 : + freopen("C:\\Users\\scanffer\\CLionProjects\\c2023-a\\level1\\p08_push_boxes\\第三关.txt","r",stdin); + break; + case 4 : + freopen("C:\\Users\\scanffer\\CLionProjects\\c2023-a\\level1\\p08_push_boxes\\第四关.txt","r",stdin); + break; + case 5 : + freopen("C:\\Users\\scanffer\\CLionProjects\\c2023-a\\level1\\p08_push_boxes\\第五关.txt","r",stdin); + break; + default: break; + } + char ch; + for(int j=0;;j++){ + for(int i=0;;i++){ + box[i][j]=0; + ch=getchar(); + if(ch=='\n')break; + if(ch=='1')box[i][j]=1; + if(ch=='*')return; + gamemap[i][j]=ch; + } + } +} +void mapout(){ + for(int j=0;j<=10;j++){ + for(int i=0;i<=10;i++){ + if(gamemap[i][j]=='*')return; + if(gamemap[i][j]=='a')printf(" "); + else printf("%c",gamemap[i][j]); + } + printf("\n"); + } +} +void move(COORD *A,int key){//左 75 上 72 右 77 下 80 + int x=A->X,y=A->Y; + cursorset(*A); + printf(" "); + int flag=0; + if(gamemap[A->X][A->Y]=='0')flag=1; + if(key==75){ + A->X--; + cursorset(*A); + printf("A"); + if(box[A->X][A->Y]){ + box[A->X][A->Y]=0; + box[A->X-1][A->Y]=1; + cursorset( (COORD) {A->X-1,A->Y} ); + printf("1"); + if(gamemap[A->X][A->Y]=='0')score[level]--; + if(gamemap[A->X-1][A->Y]=='0')score[level]++; + } + } + if(key==72){ + A->Y--; + cursorset(*A); + printf("A"); + if(box[A->X][A->Y]){ + box[A->X][A->Y]=0; + box[A->X][A->Y-1]=1; + cursorset( (COORD) {A->X,A->Y-1} ); + printf("1"); + if(gamemap[A->X][A->Y]=='0')score[level]--; + if(gamemap[A->X][A->Y-1]=='0')score[level]++; + } + } + if(key==77){ + A->X++; + cursorset(*A); + printf("A"); + if(box[A->X][A->Y]){ + box[A->X][A->Y]=0; + box[A->X+1][A->Y]=1; + cursorset( (COORD) {A->X+1,A->Y} ); + printf("1"); + if(gamemap[A->X][A->Y]=='0')score[level]--; + if(gamemap[A->X+1][A->Y]=='0')score[level]++; + } + } + if(key==80){ + A->Y++; + cursorset(*A); + printf("A"); + if(box[A->X][A->Y]){ + box[A->X][A->Y]=0; + box[A->X][A->Y+1]=1; + cursorset( (COORD) {A->X,A->Y+1} ); + printf("1"); + if(gamemap[A->X][A->Y]=='0')score[level]--; + if(gamemap[A->X][A->Y+1]=='0')score[level]++; + } + } + if(flag){ + cursorset( (COORD){x,y} ); + printf("0"); + } +} +int movecheck(COORD A,int key){//左 75 上 72 右 77 下 80 + int x=A.X,y=A.Y; + if(key==75){ + if(box[x-1][y]) + if(gamemap[x-2][y]=='#' || box[x-2][y])return 0; + if(gamemap[x-1][y]=='#')return 0; + } + else if(key==72){ + if(box[x][y-1]) + if(gamemap[x][y-2]=='#' || box[x][y-2])return 0; + if(gamemap[x][y-1]=='#')return 0; + } + else if(key==77){ + if(box[x+1][y]) + if(gamemap[x+2][y]=='#' || box[x+2][y])return 0; + if(gamemap[x+1][y]=='#')return 0; + } + else if(key==80){ + if(box[x][y+1]) + if(gamemap[x][y+2]=='#' || box[x][y+2])return 0; + if(gamemap[x][y+1]=='#')return 0; + } + if(key>80)return 0; + return 1; +} +void gamerun(){ + clear(); + explanation(); + mapin(); + mapout(); + COORD A=role[level].A; + cursorset(A); + while(1){ + if(kbhit()){ + int key=getch(); + if(key==13){ + gamerun(); + return ; + } + if(movecheck(A,key)){ + move(&A,key); + cursorset((COORD){20,10}); + printf("步数=%d",++step[level]); + cursorset(A); + } + } + if(score[level]==goal[level]){ + level++; + if(level==6)return ; + gamerun(); + return ; + } + } +} +int main(){ + out= GetStdHandle(STD_OUTPUT_HANDLE); + role[1].A=(COORD){4,4},role[2].A=(COORD){1,1},role[3].A=(COORD){2,3}; + role[4].A=(COORD){1,2},role[5].A=(COORD){2,1}; + gamerun(); +} diff --git a/level1/p09_linked_list/main.c b/level1/p09_linked_list/main.c index f84d224..5c2f761 100644 --- a/level1/p09_linked_list/main.c +++ b/level1/p09_linked_list/main.c @@ -1,6 +1,82 @@ -#include +#include +#include +typedef struct node { + int value; + struct node *next; +} LinkedList; + +LinkedList *LinkedListCreate() { + LinkedList *q = (LinkedList *) malloc(sizeof(LinkedList)); + q->next = NULL; + return q; +} + +int LinkedListIsEmpty(LinkedList *q) { + return q->next == NULL; +} + +LinkedList *LinkedListGetEnd(LinkedList *q) { + LinkedList *a = q; + while (a->next != NULL) { + a = a->next; + } + return a; +} + +void LinkedListNodeAdd(int value, LinkedList *q) { + LinkedList *end = LinkedListGetEnd(q); + LinkedList *node = (LinkedList *) malloc(sizeof(LinkedList)); + node->value = value, node->next = NULL; + end->next = node; +} + +void LinkedListPrint(LinkedList *q) { + LinkedList *a = q->next; + while (a->next != NULL) { + printf("%d ", a->value); + a = a->next; + } + printf("%d\n", a->value); +} + +void LinkedListUpDown(LinkedList *q) { + LinkedList *a = q->next; + LinkedList *last = NULL; + while (a->next != NULL) { + LinkedList *next = a->next; + a->next = last; + last = a; + a = next; + } + a->next = last; + q->next = a; +} + +int LinkedListSearch(int value, int index, LinkedList *q) { + LinkedList *a = q->next; + int cnt = -1, count = 0; + while (count < index && a->next != NULL) { + cnt++; + if (a->value == value)count++; + a = a->next; + } + if (a->value == value && a->next==NULL)count++; + if (count == index)return cnt; + return -1; +} + +//链表头不放任何值,只用来访问链表 int main() { - printf("hello world!\n"); - return 0; -} \ No newline at end of file + LinkedList *Q = LinkedListCreate(); + LinkedListNodeAdd(1, Q); + LinkedListNodeAdd(2, Q); + LinkedListNodeAdd(3, Q); + LinkedListNodeAdd(5, Q); + LinkedListNodeAdd(5, Q); + LinkedListPrint(Q); + LinkedListUpDown(Q); + LinkedListPrint(Q); + printf("%d\n", LinkedListSearch(5, 1, Q)); + printf("%d\n", LinkedListSearch(5, 2, Q)); +} diff --git a/level1/p10_warehouse/main.c b/level1/p10_warehouse/main.c index f84d224..66255fe 100644 --- a/level1/p10_warehouse/main.c +++ b/level1/p10_warehouse/main.c @@ -1,6 +1,130 @@ -#include - -int main() { - printf("hello world!\n"); - return 0; -} \ No newline at end of file +#include +#include +#include +#include +typedef struct { + char name[80]; + int num; +}store; +store commodity[4000]; +int sum=0; +HANDLE out; +void store_explanation(){ + out = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleCursorPosition(out, (COORD) {35,3} ); + printf("Inventory Sysmtem"); + SetConsoleCursorPosition(out, (COORD) {30,4} ); + printf("Press '1' to look up the inventory"); + SetConsoleCursorPosition(out, (COORD) {30,5} ); + printf("Press '2' to add goods to the inventory"); + SetConsoleCursorPosition(out, (COORD) {30,6} ); + printf("Press '3' to take out something from the inventory"); + SetConsoleCursorPosition(out, (COORD) {0,0}); +} +void store_read(){ + FILE *fp=NULL; + fp=fopen("C:\\Users\\scanffer\\CLionProjects\\c2023-a\\level1\\p10_warehouse\\仓库信息.txt","r"); + sum=1; + int flag=1; + while(flag){ + flag=fscanf(fp,"Name:%s Amount:%d\n",commodity[sum++].name,&commodity[sum].num); + } + sum-=2; + fclose(fp); + return ; +} +void store_print(){ + store_read(); + system("cls"); + store_explanation(); + for(int i=1;i<=sum;i++){ + printf("Name:%s Amount:%d\n",commodity[i].name,commodity[i].num); + } +} +void store_upload(){ + FILE *fp=fopen("C:\\Users\\scanffer\\CLionProjects\\c2023-a\\level1\\p10_warehouse\\仓库信息.txt","w"); + for(int i=1;i<=sum;i++){ + fprintf(fp,"Name:%s Amount:%d\n",commodity[i].name,commodity[i].num); + } + fprintf(fp,"*\n"); + fclose(fp); +} +char addcom[80],addnum; +void store_add(){ + SetConsoleCursorPosition(out, (COORD) {30,8} ); + printf("complete the list following"); + SetConsoleCursorPosition(out, (COORD) {30,9} ); + printf("Name:"); + SetConsoleCursorPosition(out, (COORD) {30,10} ); + printf("Amount:"); + SetConsoleCursorPosition(out, (COORD) {35,9} ); + scanf("%s",addcom); + SetConsoleCursorPosition(out, (COORD) {37,10} ); + scanf("%d",&addnum); + SetConsoleCursorPosition(out, (COORD) {0,0}); + int flag=0; + for(int i=1;i<=sum;i++){ + if(!strcmp(addcom,commodity[i].name) ){ + flag=1; + commodity[i].num+=addnum; + } + } + if(!flag){ + sum++; + strcpy(commodity[sum].name,addcom); + commodity[sum].num=addnum; + } + store_upload(); +} +void store_out(){ + SetConsoleCursorPosition(out, (COORD) {30,8} ); + printf("complete the list following"); + SetConsoleCursorPosition(out, (COORD) {30,9} ); + printf("Name:"); + SetConsoleCursorPosition(out, (COORD) {30,10} ); + printf("Amount:"); + SetConsoleCursorPosition(out, (COORD) {35,9} ); + scanf("%s",addcom); + SetConsoleCursorPosition(out, (COORD) {37,10} ); + scanf("%d",&addnum); + SetConsoleCursorPosition(out, (COORD) {0,0}); + int flag=0; + for(int i=1;i<=sum;i++){ + if(!strcmp(addcom,commodity[i].name) ){ + flag=1; + if(commodity[i].num>=addnum){ + commodity[i].num-=addnum; + if(commodity[i].num==0)sum--; + }else{ + SetConsoleCursorPosition(out, (COORD) {30,11} ); + printf("We don't have enough commodity"); + SetConsoleCursorPosition(out, (COORD) {0,0} ); + } + } + } + if(!flag){ + SetConsoleCursorPosition(out, (COORD) {30,11} ); + printf("We don't have the commodity you needed"); + SetConsoleCursorPosition(out, (COORD) {0,0} ); + } + store_upload(); +} +void store_control(){ + store_explanation(); + while(1){ + int key=getch(); + if(key==49){ + store_print(); + } + if(key==50){ + store_add(); + } + if(key==51){ + store_out(); + } + if(key==13)return; + } +} +int main(){ + store_control(); +}