diff --git a/README.md b/README.md index 34c10400..2938d987 100755 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ -# c2019 +# 大一c语言作业 +#c2019 ## 基本操作流程 diff --git a/level1/p01_runningLetter/main.c b/level1/p01_runningLetter/main.c new file mode 100644 index 00000000..55aa6157 --- /dev/null +++ b/level1/p01_runningLetter/main.c @@ -0,0 +1,32 @@ +#include +#include +#define N 78 +unsigned int sleep(unsigned int seconds); +int main() +{ + int i,j; + while(1) + { + for(i=0;i0;i--) + { + for(j = 0;j <= i;j++) + { + printf(" "); + } + printf("s"); + Sleep(100); + system("cls"); + } + } + return 0; +} diff --git a/level1/p02_isPrime/sushu.c b/level1/p02_isPrime/sushu.c new file mode 100644 index 00000000..3765635e --- /dev/null +++ b/level1/p02_isPrime/sushu.c @@ -0,0 +1,38 @@ +#include +int main() +{ + int num,i,count = 0; + printf("input a number:"); + scanf("%d",&num); + if(num <= 0) + { + printf("error!input again:");//һ + scanf("%d",&num); + } + else if(num == 1) + { + printf("N"); //1 + } + else if(num == 2) + { + printf("Y"); //2 + } + else + { + for(i = 2;i < num;i++) + { + if(num%i == 0) + { + printf("N"); // + break; + } + else + { + count++; + } + } + if(count == (num - 2)) + printf("Y"); // + } + return 0; +} diff --git a/level1/p03_Diophantus/diufantu.c b/level1/p03_Diophantus/diufantu.c new file mode 100644 index 00000000..466c0ff6 --- /dev/null +++ b/level1/p03_Diophantus/diufantu.c @@ -0,0 +1,14 @@ +#include +//1/6x+1/12x+1/7x+5+1/2x+4=x +//תΪ14x+7x+12x+5*84+42x+4*84=84x +int main() +{ + int i; + for(i = 0;i < 200;i++) //0-150ÿͶԣܻꡣ + { + if(14*i + 7*i + 12*i + 5*84 + 42*i + 4*84 == 84*i) + break; + } + printf("year = %d",i); + return 0; +} diff --git a/level1/p04_ narcissus/shuixian.c b/level1/p04_ narcissus/shuixian.c new file mode 100644 index 00000000..5315f1f3 --- /dev/null +++ b/level1/p04_ narcissus/shuixian.c @@ -0,0 +1,22 @@ +#include +#include +#include +int main() +{ + int a,b,c,i,num; + + for(i=100;i<1000;i++) + { + num = i; + a=num%10; + num /= 10; + b=num%10; + num /= 10; + c = num; + if(i == (pow(a,3)+pow(b,3)+pow(c,3))) + { + printf("%d\n",i); + } + } + return 0; +} diff --git a/level1/p05_allPrimes/allprimes.c b/level1/p05_allPrimes/allprimes.c new file mode 100644 index 00000000..0f8c651e --- /dev/null +++ b/level1/p05_allPrimes/allprimes.c @@ -0,0 +1,29 @@ +#include +#include +#include +int main() +{ + clock_t start, finish; + double cost; + int i,j; + start=clock(); + printf("allPrimes:\n\n2 3 5 7 "); + for(i=5;i<1000;i += 2) + { + int count = 3; + for(j=3;j +int main() +{ + int i,j,x,k; + int num ; + 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(i = 4;i < 100;i += 2) + { + + for(j = 0;j < 25;j++) + { + num = 0; + x = i - primes[j]; + for(k = 0;k<25;k++) + { + if(x==primes[k]) + num = 1; + } + if(!(x<2||num == 0||primes[j]>x)) + printf("%-3d= %-3d+ %-3d\n",i,primes[j],x); + } + } + return 0; +} + diff --git a/level1/p07_encrypt_decrypt/jiamitijiao.c b/level1/p07_encrypt_decrypt/jiamitijiao.c new file mode 100644 index 00000000..06226475 --- /dev/null +++ b/level1/p07_encrypt_decrypt/jiamitijiao.c @@ -0,0 +1,26 @@ +#include +#include +#include +#define N 5 //Կ +int main() +{ + char password[50]; + int i,count; + printf("Ҫܵ룺"); + gets(password); + count = strlen(password); + for(i = 0;i < count;i++) + { + password[i] = password[i] + i + N; + } + system("cls"); + printf("ܺΪ%s\n",password); + printf("\n"); + system("pause"); + for(i = 0;i < count;i++) + { + password[i] = password[i] - i - N; + } + printf("ܺǣ%s",password); + return 0; +} diff --git a/level1/p08_hanoi/hanoi.c b/level1/p08_hanoi/hanoi.c new file mode 100644 index 00000000..46f52aa0 --- /dev/null +++ b/level1/p08_hanoi/hanoi.c @@ -0,0 +1,24 @@ +#include +#include +void hanoi(int,char,char,char); +int main() +{ + int n; + printf("Բ̸"); + scanf("%d",&n); + hanoi(n,'A','B','C'); + int count = pow(2,n) - 1; + printf("һҪ%d",count); + return 0; +} +void hanoi(int n,char a,char b,char c) +{ + if(n == 1) + printf("%c-->%c\n",a,c); + else + { + hanoi(n - 1,a,c,b); + printf("%c-->%c\n",a,c); + hanoi(n - 1,b,a,c); + } +} diff --git a/level1/p09_maze/main.c b/level1/p09_maze/main.c new file mode 100644 index 00000000..56375462 --- /dev/null +++ b/level1/p09_maze/main.c @@ -0,0 +1,170 @@ +#include +#include +#include +#include +#define Height 25 //Թĸ߶ȣΪ +#define Width 25 //ԹĿȣΪ +#define Wall 1 +#define Road 0 +#define Start 2 +#define End 3 +#define Esc 5 +#define Up 1 +#define Down 2 +#define Left 3 +#define Right 4 +int map[Height+2][Width+2]; +void gotoxy(int x,int y) //ƶ +{ + COORD coord; + coord.X=x; + coord.Y=y; + SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord ); +} +void hidden()//ع +{ + HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); + CONSOLE_CURSOR_INFO cci; + GetConsoleCursorInfo(hOut,&cci); + cci.bVisible=0;//1Ϊʾ0Ϊ + SetConsoleCursorInfo(hOut,&cci); +} +void create(int x,int y) //Թ +{ + int c[4][2]={0,1,1,0,0,-1,-1,0}; //ĸ + int i,j,t; + // + for(i=0;i<4;i++) + { + j=rand()%4; + t=c[i][0];c[i][0]=c[j][0];c[j][0]=t; + t=c[i][1];c[i][1]=c[j][1];c[j][1]=t; + } + map[x][y]=Road; + for(i=0;i<4;i++) + if(map[x+2*c[i][0]][y+2*c[i][1]]==Wall) + { + map[x+c[i][0]][y+c[i][1]]=Road; + create(x+2*c[i][0],y+2*c[i][1]); + } +} +int get_key() //հ +{ + char c; + while(c=getch()) + { + if(c==27) return Esc; //Esc + if(c!=-32)continue; + c=getch(); + if(c==72) return Up; // + if(c==80) return Down; // + if(c==75) return Left; // + if(c==77) return Right; // + } + return 0; +} +void paint(int x,int y) //Թ +{ + gotoxy(2*y-2,x-1); + switch(map[x][y]) + { + case Start: + printf("");break; // + case End: + printf("");break; // + case Wall: + printf("~");break; //ǽ + case Road: + printf(" ");break; //· + } +} +void game() +{ + int x=2,y=1; //ҵǰλãտʼڴ + int c; //հ + while(1) + { + gotoxy(2*y-2,x-1); + printf(""); //ҵǰλ + if(map[x][y]==End) //жǷ񵽴 + { + gotoxy(30,24); + printf("յ㣬"); + getch(); + break; + } + c=get_key(); + if(c==Esc) + { + gotoxy(0,24); + break; + } + switch(c) + { + case Up: // + if(map[x-1][y]!=Wall) + { + paint(x,y); + x--; + } + break; + case Down: // + if(map[x+1][y]!=Wall) + { + paint(x,y); + x++; + } + break; + case Left: // + if(map[x][y-1]!=Wall) + { + paint(x,y); + y--; + } + break; + case Right: // + if(map[x][y+1]!=Wall) + { + paint(x,y); + y++; + } + break; + } + } +} +int main() +{ + system("title yourname"); + int i,j; + srand((unsigned)time(NULL)); //ʼ漴 + hidden(); //ع + for(i=0;i<=Height+1;i++) + for(j=0;j<=Width+1;j++) + if(i==0||i==Height+1||j==0||j==Width+1) //ʼԹ + map[i][j]=Road; + else map[i][j]=Wall; + + create(2*(rand()%(Height/2)+1),2*(rand()%(Width/2)+1)); //һ㿪ʼԹõжΪż + for(i=0;i<=Height+1;i++) //߽紦 + { + map[i][0]=Wall; + map[i][Width+1]=Wall; + } + + for(j=0;j<=Width+1;j++) //߽紦 + { + map[0][j]=Wall; + map[Height+1][j]=Wall; + } + map[2][1]=Start; // + map[Height-1][Width]=End; // + for(i=1;i<=Height;i++) + { + for(j=1;j<=Width;j++) //Թ + paint(i,j); + } + game(); //ʼϷ + getch(); + return 0; +} + diff --git a/level1/p10_pushBoxes/README.md b/level1/p10_pushBoxes/README.md deleted file mode 100755 index 5895c0d2..00000000 --- a/level1/p10_pushBoxes/README.md +++ /dev/null @@ -1,9 +0,0 @@ -### 题目:推箱子小游戏(基于console) - -### 功能要求: - -1. 将p09迷宫游戏改造为“推箱子”游戏; -1. 在地图中增加箱子、箱子目标位置等图形; -1. 当玩家将所有箱子归位,则显示玩家赢得了游戏; -1. 按玩家走动步数记分; -1. 设计多个关卡,每一关的地图从文件中读取,玩家每关的分数记录到文件中; \ No newline at end of file diff --git a/level1/p10_pushBoxes/pushbox.c b/level1/p10_pushBoxes/pushbox.c new file mode 100644 index 00000000..f659d29c --- /dev/null +++ b/level1/p10_pushBoxes/pushbox.c @@ -0,0 +1,367 @@ +#include +#include +#include +int i,j; +void draw_map(int map[10][12]); +int main() +{ + char input; + int count=0; + int map[10][12] = { + {2,2,2,2,2,1,1,1,1,1,2,2}, + {1,1,1,1,2,1,0,0,0,1,1,2}, + {1,0,0,1,1,1,0,1,0,0,1,2}, + {1,0,4,3,3,3,3,3,1,0,1,1}, + {1,0,0,1,1,3,3,3,4,0,0,1}, + {1,0,0,0,0,4,1,1,4,1,0,1}, + {1,0,4,1,4,0,0,0,4,0,0,1}, + {1,1,0,6,0,1,1,1,4,1,0,1}, + {2,1,1,1,1,1,2,1,0,0,0,1}, + {2,2,2,2,2,2,2,1,1,1,1,1} + }; + while (1) + { + system("CLS"); + printf("\n"); + printf("\n"); + draw_map(map); + printf("ǰ÷֣%d\n",count); + for (i=0;i<10;i++) + { + for (j=0;j<12;j++) + { + if (map[i][j]==6||map[i][j]==9) + break; + } + if (map[i][j]==6||map[i][j]==9) + break; + } + printf("ĵǰ꣨%d%d",i,j); + input = getch(); + switch (input) + { + case 'w': + if(map[i-1][j]==0) + { + map[i-1][j]=6+0; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + else if((map[i-1][j]==3)||(map[i-1][j]==9)) + { + map[i-1][j]=6+3; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + else if(map[i-1][j]==4) + { + if (map[i-2][j]==0) + { + map[i-2][j]=4; + if(map[i-1][j]==7) + map[i-1][j]=9; + else + map[i-1][j]=6; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + else if (map[i-2][j]==3) + { + map[i-2][j]=7; + count++; + if(map[i-1][j]==7) + map[i-1][j]=9; + else + map[i-1][j]=6; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + } + else if(map[i-1][j]==7) + { + if(map[i-2][j]==0) + { + count--; + map[i-2][j]=4; + map[i-1][j]=9; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + if(map[i-2][j]==3) + { + map[i-2][j]=7; + map[i-1][j]=9; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + } + break; + case 's': + if(map[i+1][j]==0) + { + map[i+1][j]=6+0; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + else if(map[i+1][j]==3) + { + map[i+1][j]=6+3; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + else if(map[i+1][j]==4) + { + if (map[i+2][j]==0) + { + map[i+2][j]=4; + if(map[i+1][j]==7) + map[i+1][j]=9; + else + map[i+1][j]=6; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + else if (map[i+2][j]==3) + { + map[i-2][j]=7; + count++; + if(map[i+1][j]==7) + map[i+1][j]=9; + else + map[i+1][j]=6; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + } + else if(map[i+1][j]==7) + { + if(map[i+2][j]==0) + { + count--; + map[i+2][j]=4; + map[i+1][j]=9; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + if(map[i+2][j]==3) + { + map[i+2][j]=7; + map[i+1][j]=9; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + } + break; + case 'a': + if(map[i][j-1]==0) + { + map[i][j-1]=6+0; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + else if(map[i][j-1]==3) + { + map[i][j-1]=6+3; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + else if(map[i][j-1]==4) + { + if (map[i][j-2]==0) + { + map[i][j-2]=4; + + if(map[i][j-1]==7) + map[i][j-1]=9; + else + map[i][j-1]=6; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + else if (map[i][j-2]==3) + { + count++; + map[i][j-2]=7; + if(map[i][j-1]==7) + map[i][j-1]=9; + else + map[i][j-1]=6; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + } + else if(map[i][j-1]==7) + { + if(map[i][j-2]==0) + { + count--; + map[i][j-2]=4; + map[i][j-1]=9; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + if(map[i][j-2]==3) + { + map[i][j-2]=7; + map[i][j-1]=9; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + } + break; + case 'd': + if(map[i][j+1]==0) + { + map[i][j+1]=6+0; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + + else if(map[i][j+1]==3) + { + map[i][j+1]=6+3; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + else if(map[i][j+1]==4) + { + if (map[i][j+2]==0) + { + map[i][j+2]=4; + if(map[i][j+1]==7) + map[i][j+1]=9; + else + map[i][j+1]=6; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + else if (map[i][j+2]==3) + { + count++; + map[i][j+2]=7; + if(map[i][j+1]==7) + map[i][j+1]=9; + else + map[i][j+1]=6; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + } + else if(map[i][j+1]==7) + { + if(map[i][j+2]==0) + { + count--; + map[i][j+2]=4; + map[i][j+1]=9; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + if(map[i][j+2]==3) + { + map[i][j+2]=7; + map[i][j+1]=9; + if(map[i][j]==9) + map[i][j]=3; + else + map[i][j]=0; + } + } + break; + } + if(count==8) + { + system("CLS"); + draw_map(map); + break; + } + } + printf("\nϲ㣬ˣ\n"); + return 0; +} +void draw_map(int map[10][12]) +{ + + for(i=0;i<10;i++) + { + for(j=0;j<12;j++) + { + switch(map[i][j]) + { + case 0: + printf(" "); + break; + case 1: + printf("#"); + break; + case 2: + printf(" "); + break; + case 3: + printf("!"); + break; + case 4: + printf("*"); + break; + case 7: + printf("$"); + break; + case 6: + printf("@"); + break; + case 9: + printf("@"); + break; + } + } + printf("\n"); + } +} + + diff --git a/level1/p11_linkedList/main.c b/level1/p11_linkedList/main.c new file mode 100644 index 00000000..8d967596 --- /dev/null +++ b/level1/p11_linkedList/main.c @@ -0,0 +1,119 @@ +#include +#include +struct Node +{ + int data; + struct Node * pNext; +}; +struct Node * CreateList(int len); +void TraverseList(struct Node * pHead); +void conTraverseList(struct Node * pHead,int len); +int find5(struct Node * pHead,int len); +int main(void) +{ + struct Node * pHead=NULL;//ͷָ, + int len; + printf("Ҫɵӽڵĸlen="); + scanf("%d",&len); + pHead=CreateList(len); + TraverseList(pHead); + printf("\n:\n"); + conTraverseList(pHead,len); + find5(pHead,len); + return 0; +} +struct Node * CreateList(int len) +{ + int i; + int val; + struct Node * pHead=(struct Node *)malloc(sizeof(struct Node)); + if(NULL==pHead) + { + printf("ʧܣֹ!\n"); + exit(-1); + } + struct Node * pTail=pHead; + pTail->pNext=NULL; + + + for(i=0;idata=val; + pTail->pNext=pNew; + pNew->pNext=NULL; + pTail=pNew; + } + return pHead; +} + +void TraverseList(struct Node * pHead) +{ + struct Node * p=pHead->pNext; + if(pHead->pNext==NULL) + { + printf("Ϊգ"); + } + else + { + while(p!=NULL) + { + printf("%d ",p->data); + p=p->pNext; + } + } +} +void conTraverseList(struct Node * pHead,int len) +{ + struct Node * p=pHead->pNext; + int j,i=0;; + if(pHead->pNext==NULL) + { + printf("Ϊգ"); + } + else + { + for(i=0;ipNext; + if(j=len-i-1) + { + printf("%d ",p->data); + break; + } + + + } + } + } +} +int find5(struct Node * pHead,int len) +{ + int index=1; + struct Node * p=pHead->pNext; + for(int i=0;idata==5) + { + printf("ֵΪ5Ľڵǣ%d ",index); + break; + } + index++; + } +} + + + + + diff --git a/level1/p12_warehouse/amount.txt b/level1/p12_warehouse/amount.txt new file mode 100644 index 00000000..ab7dea26 --- /dev/null +++ b/level1/p12_warehouse/amount.txt @@ -0,0 +1,20 @@ +1 A1 456 0 0 456 400 +2 A2 23 0 0 23 100 +3 A3 67 0 0 67 100 +4 A4 104 0 0 104 100 +5 A5 78 0 0 78 100 +6 A6 630 0 0 630 500 +7 A7 92 0 0 92 100 +8 A8 162 0 0 162 100 +9 A9 401 0 0 401 200 +10 A10 276 0 0 276 200 +11 B1 400 0 0 400 400 +12 B2 989 0 0 989 400 +13 B3 194 0 0 194 100 +14 C1 495 0 0 495 200 +15 C2 287 0 0 287 200 +16 C3 88 0 0 88 100 +17 C4 933 0 0 933 500 +18 C5 56 0 0 56 100 +19 D1 786 0 0 786 600 +20 D2 237 0 0 237 100 diff --git a/level1/p12_warehouse/goods.txt b/level1/p12_warehouse/goods.txt new file mode 100644 index 00000000..b248b274 --- /dev/null +++ b/level1/p12_warehouse/goods.txt @@ -0,0 +1,20 @@ +1 A1 456 0 0 456 400 +2 A2 23 0 0 23 100 +3 A3 67 0 0 67 100 +4 A4 104 0 0 104 100 +5 A5 78 0 0 78 100 +6 A6 630 0 0 630 500 +7 A7 92 0 0 92 100 +8 A8 162 0 0 162 100 +9 A9 401 0 0 401 200 +10 A10 276 0 0 276 200 +11 B1 400 0 0 400 400 +12 B2 989 0 0 989 400 +13 B3 194 0 0 194 100 +14 C1 495 0 0 495 200 +15 C2 287 0 0 287 200 +16 C3 88 0 0 88 100 +17 C4 933 0 0 933 500 +18 C5 56 0 0 56 100 +19 D1 786 0 0 786 600 +20 D2 237 0 0 237 100 \ No newline at end of file diff --git a/level1/p12_warehouse/main.c b/level1/p12_warehouse/main.c new file mode 100644 index 00000000..e48c46ca --- /dev/null +++ b/level1/p12_warehouse/main.c @@ -0,0 +1,190 @@ +#include +#include +#include +#include +#define M 50 +/*ʵµIJ˵ѡ˵ܣ +ʾб + + +˳ +ʵֲ˵Ӧܣ¼ͺšϢ +ʱļжȡǰݣ˳ʱݣ +*/ +typedef struct +{ + int num; //Ʒ + char name[20]; //Ʒ + int stock; //ԭʼ + int in; //Ŀ + int out; //Ŀ + int amount; //տ + int warning_value; + int state; //״̬ +}goods; +goods s[M]; //ڴŻƷϢ +goods r[M]; //ڴƷϢ +goods t[M]; //ڴųƷϢ + +void Re_file(); +void Stock_in(); +void Stock_out(); +void Display(); +void Printf_back(); +void Modify(); +void Statistics(); +int Wr_file(); + +int N; +int P; + +int main() +{ + int sele; + Re_file(); //ȡƷϢ + sele=1; + while(sele) + { + system("cls"); + printf("\n\n"); + printf("*********************************************\n"); + printf("* *\n"); + printf("* 1. 2. *\n"); + printf("* *\n"); + printf("* *\n"); + printf("* 3. 4.˳ *\n"); + printf("* *\n"); + printf("*********************************************\n"); + printf("ѡ:"); + scanf("%d",&sele); + switch(sele) + { + case 1:Stock_in();Display();break; + case 2:Stock_out();Display();break; + case 3:Statistics();break; + case 4:exit(0);sele=0;break; + } + printf("\n\n...\n"); + getch(); + } + Wr_file(); + return 0; +} + +void Re_file() //ԭʼļ +{ + FILE*fp; + N=0; + fp=fopen("goods.txt","r"); + while(fscanf(fp,"%d%s%d%d%d%d%d",&s[N].num,&s[N].name,&s[N].stock,&s[N].in,&s[N].out,&s[N].amount,&s[N].warning_value)!=EOF)N++; + fclose(fp); + P=N; +} + +void Stock_in() //ļ +{ + FILE*fp; + int i,j; + N=0; + fp=fopen("stockin.txt","r"); + while(fscanf(fp,"%d%d",&r[N].num,&r[N].in)!=EOF)N++; + fclose(fp); + for(i=0;i