第一篇:简单易懂的C语言源代码(供初学者使用)
#include
#include
#include
#include
#include
#include
#include
#include
textcolor(RED);/* 设置文件颜色为红色,可更改 */ clrscr();/* 清屏,使设置生效 */ printf(“enter radius:”);scanf(“%f”,&r);if(r<0)printf(“Enter Error!n”);else printf(“r=%.2f,c=%.2f,area=%.2fn”,r,2*pi*r,pi*r*r);system(“pause”);/* 暂停,按任一键继续 */
#include
#include
#include #include #include #include #include /* 逆初始置换表IP */ int IP_1_Table[64] = {39,7,47,15,55,23,63,31,38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,34,2,42,10,50,18,58,26,33,1,41,9,49,17,57,25,32,0,40,8,48,16,56,24}; /* 扩充置换表E */ int E_Table[48] = {31, 0, 1, 2, 3, 4,3, 4, 5, 6, 7, 8,7, 8,9,10,11,12,11,12,13,14,15,16,15,16,17,18,19,20,19,20,21,22,23,24,23,24,25,26,27,28,27,28,29,30,31,0}; /* 置换函数P */ int P_Table[32] = {15,6,19,20,28,11,27,16,0,14,22,25,4,17,30,9,1,7,23,13,31,26,2,8,18,12,29,5,21,10,3,24}; /* S盒 */ int S[8][4][16] =/* S1 */ {{{14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7},{0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8},{4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0},{15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13}},/* S2 */ {{15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10},{3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5},{0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15},{13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9}},/* S3 */ {{10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8},{13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1},{13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7},{1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12}},/* S4 */ {{7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15},{13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9},{10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4},{3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14}},/* S5 */ {{2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9},{14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6},{4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14},{11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3}},/* S6 */ {{12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11},{10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8},{9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6},{4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13}},/* S7 */ {{4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1},{13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6}, {1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2},{6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12}},/* S8 */ {{13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7},{1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2},{7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8},{2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11}}}; /* 置换选择1 */ int PC_1[56] = {56,48,40,32,24,16,8,0,57,49,41,33,25,17,9,1,58,50,42,34,26,18,10,2,59,51,43,35,62,54,46,38,30,22,14,6,61,53,45,37,29,21,13,5,60,52,44,36,28,20,12,4,27,19,11,3}; /* 置换选择2 */ int PC_2[48] = {13,16,10,23,0,4,2,27,14,5,20,9,22,18,11,3,25,7,15,6,26,19,12,1,40,51,30,36,46,54,29,39,50,44,32,47,43,48,38,55,33,52,45,41,49,35,28,31};//把8X6的数组变成一个一维数组 void change1(int E[8][6],int r[48]){ int i,j;for(i=0;i<8;i++){ for(j=0;j<6;j++) { r[i*6+j] = E[i][j]; } } } //把一维数组变成8X6的矩阵 void change2(int r[48],int E[8][6]){ int i,j;for(i=0;i<8;i++){ for(j=0;j<6;j++) { E[i][j]= r[i*6+j]; } } } //输入16进制明文 void input(int M[8][8]){ int i,j,k;char c[16];int s[16],d[64],a[16][4]; printf(“请输入16位16进制数:”);for(i=0;i<16;i++){ scanf(“%c”,&c[i]);} for(i=0;i<16;i++) { if(c[i]>=48&&c[i]<=58) s[i]=c[i]-48; else if(c[i]>='A'&&c[i]<='Z') s[i]=c[i]-55; else if(c[i]>='a'&&c[i]<='z') s[i]=c[i]-87; else { puts(“errorn”); return; } } //把16进制数组c[]转化为二进制数: k=0;while(k<16)//每一位分解为四位,注意输出顺序就可以了 { for(j=3;j>=0;j--) { a[k][j]=s[k]%2; s[k]/=2; } k++;} //把a[16][4]转化成a[8][8]类型数组 for(k=0;k<16;k++){ for(j=0;j<4;j++) { d[k*4+j] = a[k][j]; } } for(i=0;i<8;i++){ for(j=0;j<8;j++) { M[i][j] = d[i*8+j]; } } /* //打印密文二进制数组 for(i=0;i<8;i++){ for(j=0;j<8;j++) { printf(“%2d”,M[i][j]); } printf(“n”);} */ } /*初始置换*/ void Startedexchange(int MW[8][8],int temp[8][8]){ int i,j,a[64],b[64];for(i=0;i<8;i++){ for(j=0;j<8;j++) a[i*8+j] = MW[i][j];} for(i=0;i<64;i++){ b[i] = a[IP_Table[i]];} for(i=0;i<8;i++){ for(j=0;j<8;j++){ temp[i][j] = b[i*8+j];} } /*for(i=0;i<8;i++){ for(j=0;j<8;j++){ printf(“%2d”,temp[i][j]);} printf(“n”);} */ } /*生成秘钥*/ void bornkey(int MY[8][8],int n,int key [48]){ int i,j,m;int tem,tem2;int k[64],k1[56];int a[8][7];int c[28],d[28];int list[16]={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};for(i=0;i<8;i++){ for(j=0;j<8;j++){ k[i*8+j] = MY[i][j];} } for(i=0;i<8;i++){ for(j=0;j<7;j++){ a[i][j] = k[PC_1[i*7+j]];} } /* for(i=0;i<8;i++){ for(j=0;j<7;j++){ printf(“%d”,a[i][j]);} } printf(“n”);*/ //c0,d0 for(i=0;i<4;i++){ for(j=0;j<7;j++){ c[i*7+j] = a[i][j];} } for(i=4;i<8;i++){ for(j=0;j<7;j++){ d[i*7+j-28] = a[i][j];} } //确定左移位数 m = 0;for(i=0;i tem=c[0]; for(i=0;i<27;i++) { c[i] = c[i+1]; } c[27] = tem; tem2 = d[0]; for(i=0;i<27;i++) { d[i] = d[i+1]; } d[27] = tem2; m--;} /* for(i=0;i<28;i++){ printf(“%d”,d[i]);} printf(“n”);*/ //合并 for(i=0;i<28;i++){ k1[i] = c[i];} for(i=0;i<28;i++){ k1[28+i] = d[i];} //置换选择2 for(i=0;i<48;i++){ key[i] = k1[PC_2[i]];} } //第一次异或 void firstXOR(int key[48],int R[48],int k2[48]){ int i;for(i=0;i<48;i++){ if(R[i]!=key[i]) { k2[i] = 1; } else k2[i] = 0;} } //S盒压缩变换 void Schange(int k2[48],int k3[32]){ int i,h,l,a,temp,k,m,j; int b[8][6];int num[4]; change2(k2,b); m=0; //printf(“n”); for(i=0;i<8;i++) { h=2*b[i][0]+b[i][5];l=8*b[i][1]+4*b[i][2]+2*b[i][3]+b[i][4];a = S[i][h][l]; //printf(“%d ”,a);//把a装换成二进制输出 k=3;while(a!=0){ temp = a%2; a = a/2; num[k] = temp; k--;} while(k>=0){ num[k]=0; k--;} for(j=0;j<4;j++){ k3[m] = num[j]; m++;} } } //置换运算P void Pchange(int k3[32],int p[32]){ int i;for(i=0;i<32;i++){ p[i] = k3[P_Table[i]];} } //拓展变换 void extension(int a[32],int b[48]){ int i;for(i=0;i<48;i++){ b[i] = a[E_Table[i]]; } } void feistel(int R[32], int key[48],int p[32]){ int b[48],k2[48],k3[32];extension(R,b);firstXOR(key,b,k2);//key后半部分出现问题 Schange(k2,k3);Pchange(k3,p);} //第二次异或 void SecondXOR(int L[32],int p[32],int R1[32]){ int i;for(i=0;i<32;i++){ if(L[i]!=p[i]) { R1[i] = 1; } else R1[i] = 0;} } /*把明文分成两部分32+32*/ void divideLR(int temp[8][8],int L[32],int R[32]){ int i,j;for(i=0;i<4;i++){ for(j=0;j<8;j++) { L[i*8+j] = temp[i][j]; } } for(i=0;i<4;i++){ for(j=0;j<8;j++) { R[i*8+j] = temp[i+4][j]; } } } void exchange(int R[32],int L[32]){ int i; for(i=0;i<32;i++) { L[i] = R[i]; } } //逆初始置换 void restartexchange(int L[32],int R[32],int secret[64]){ int a[64];int i;for(i=0;i<32;i++){ a[i] = L[i];} for(i=0;i<32;i++){ a[32+i] = R[i];} for(i=0;i<64;i++){ secret[i] = a[IP_1_Table[i]];} } // 将二进制 数转化为16进制 void BtoOX(int a[64], char b [16]){ int i,j,k,n,r;n=0; for(i=0;i<16;i++){ k=8; r=0; for(j=1;j<5;j++) { r+=a[n]*k; k=k/2; n++; } if(r<10) b[i]=r+48; else b[i]=r+55; } } void main(){ int i,j,a;int MW[8][8];//密文 int MY[8][8];//秘钥 int temp[8][8];//过渡数组 int L[32];//左边加密部分 int R[32];//右边加密部分 int R1[32];int key[48];//生成的秘钥 int p[32];int secret[64];//密文二进制表示 char miwen[16];printf(“请输入明文:”);input(MW);getchar();Startedexchange(MW,temp);divideLR(temp,L,R); printf(“请输入秘钥:”);input(MY);//打印密文二进制数组 a = 1;while(a<=15){ bornkey(MY,a,key);//true feistel(R,key,p);SecondXOR(L,p,R1);exchange(R,L); exchange(R1,R);a++;} //第16次迭代 bornkey(MY,16,key);feistel(R,key,p);SecondXOR(L,p,R1);exchange(R1,L); restartexchange(L,R,secret);BtoOX(secret,miwen);printf(“n”);for(i=0;i<16;i++){ printf(“%c”,miwen[i]);} printf(“n”);} C语言扫雷源代码.txt如果背叛是一种勇气,那么接受背叛则需要更大的勇气。爱情是块砖,婚姻是座山。砖不在多,有一块就灵;山不在高,守一生就行。#include /*鼠标信息宏定义*/ #define WAITING 0xff00 #define LEFTPRESS 0xff01 #define LEFTCLICK 0xff10 #define LEFTDRAG 0xff19 #define RIGHTPRESS 0xff02 #define RIGHTCLICK 0xff20 #define RIGHTDRAG 0xff2a #define MIDDLEPRESS 0xff04 #define MIDDLECLICK 0xff40 #define MIDDLEDRAG 0xff4c #define MOUSEMOVE 0xff08 struct { int num;/*格子当前处于什么状态,1有雷,0已经显示过数字或者空白格子*/ int roundnum;/*统计格子周围有多少雷*/ int flag;/*右键按下显示红旗的标志,0没有红旗标志,1有红旗标志*/ }Mine[10][10]; int gameAGAIN=0;/*是否重来的变量*/ int gamePLAY=0;/*是否是第一次玩游戏的标志*/ int mineNUM;/*统计处理过的格子数*/ char randmineNUM[3];/*显示数字的字符串*/ int Keystate;int MouseExist;int MouseButton;int MouseX;int MouseY;int up[16][16],down[16][16],mouse_draw[16][16],pixel_save[16][16]; void MouseMath()/*计算鼠标的样子*/ {int i,j,jj,k;long UpNum[16]={ 0x3fff,0x1fff,0x0fff,0x07ff, 0x03ff,0x01ff,0x00ff,0x007f, 0x003f,0x00ff,0x01ff,0x10ff, 0x30ff,0xf87f,0xf87f,0xfc3f };long DownNum[16]={ 0x0000,0x7c00,0x6000,0x7000, 0x7800,0x7c00,0x7e00,0x7f00, 0x7f80,0x7e00,0x7c00,0x4600, 0x0600,0x0300,0x0300,0x0180 };for(i=0;i<16;i++){ j=jj=15;while(UpNum[i]!=0){ up[i][j]=UpNum[i]%2;j--;UpNum[i]/=2;} while(DownNum[i]!=0){ down[i][jj--]=DownNum[i]%2;DownNum[i]/=2;} for(k=j;k>=0;k--)up[i][k]=0;for(k=jj;k>=0;k--)down[i][k]=0;for(k=0;k<16;k++)/*四种组合方式*/ { if(up[i][k]==0&&down[i][k]==0)mouse_draw[i][k]=1;else if(up[i][k]==0&&down[i][k]==1)mouse_draw[i][k]=2;else if(up[i][k]==1&&down[i][k]==0)mouse_draw[i][k]=3;else mouse_draw[i][k]=4;} } mouse_draw[1][2]=4;/*特殊点*/ } void Init(void);/*图形驱动*/ void MouseOn(int,int);/*鼠标光标显示*/ void MouseOff(void);/*鼠标光标隐藏*/ void MouseSetXY(int,int);/*设置当前位置*/ int LeftPress(void);/*左键按下*/ int RightPress(void);/*鼠标右键按下*/ int MiddlePress();void MouseGetXY(void);/*得到当前位置*/ int MouseStatus();void Control(void);/*游戏开始,重新,关闭*/ void GameBegain(void);/*游戏开始画面*/ void DrawSmile(void);/*画笑脸*/ void DrawRedflag(int,int);/*显示红旗*/ void DrawEmpty(int,int,int,int);/*两种空格子的显示*/ void GameOver(void);/*游戏结束*/ void GameWin(void);/*显示胜利*/ int MineStatistics(int,int);/*统计每个格子周围的雷数*/ int ShowWhite(int,int);/*显示无雷区的空白部分*/ void GamePlay(void);/*游戏过程*/ void Close(void);/*图形关闭*/ void main(void){ Init();MouseMath();//MouseOn(MouseX,MouseY);Control();Close();} void Init(void)/*图形开始*/ { int gd=DETECT,gm;registerbgidriver(EGAVGA_driver);initgraph(&gd,&gm,“");} void Close(void)/*图形关闭*/ { closegraph();} /*鼠标光标显示*/ void MouseOn(int x,int y){ int i,j;int color; for(i=0;i<16;i++)/*画鼠标*/ { for(j=0;j<16;j++){ pixel_save[i][j]=getpixel(x+j,y+i);/*保存原来的颜色*/ if(mouse_draw[i][j]==1)putpixel(x+j,y+i,0);else if(mouse_draw[i][j]==2)putpixel(x+j,y+i,15);} } } /*隐藏鼠标*/ void MouseOff(){ int i,j,x,y,color;x=MouseX;y=MouseY;for(i=0;i<16;i++)/*原位置异或消去*/ for(j=0;j<16;j++){ if(mouse_draw[i][j]==3||mouse_draw[i][j]==4)continue;color=getpixel(x+j,y+i);putpixel(x+j,y+i,color^color);putpixel(x+j,y+i,pixel_save[i][j]);} } void MouseSetXY(int x,int y)/*设置当前位置*/ { _CX=x;_DX=y;_AX=0x04;geninterrupt(0x33);} int LeftPress(void)/*鼠标左键按下*/ { _AX=0x03;geninterrupt(0x33);return(_BX&1);} int RightPress(void)/*鼠标右键按下*/ { _AX=0x03;geninterrupt(0x33);return(_BX&2);} /*是否按下中键 返回值同上 */ int MiddlePress(){ _AX=0x03;geninterrupt(0x33);return(_BX&4);} void MouseGetXY(void)/*得到当前位置*/ { _AX=0x03;geninterrupt(0x33);MouseX=_CX;MouseY=_DX;} /*鼠标按键情况,返回0表示只移动,返回1表示左右键同时按下,2表示只按了左键,3表示只按了右键*/ int MouseStatus(){ int x,y;int status;int press=0; int i,j,color;status=0;/*默认鼠标没有移动*/ x=MouseX;y=MouseY; while(x==MouseX&&y==MouseY&&status==0&&press==0){ if(LeftPress()&&RightPress())press=1;else if(LeftPress())press=2;else if(RightPress())press=3;MouseGetXY();if(MouseX!=x||MouseY!=y)status=1;} if(status)/*移动情况才重新显示鼠标*/ { for(i=0;i<16;i++)/*原位置异或消去*/ for(j=0;j<16;j++){ if(mouse_draw[i][j]==3||mouse_draw[i][j]==4)continue;color=getpixel(x+j,y+i);putpixel(x+j,y+i,color^color);putpixel(x+j,y+i,pixel_save[i][j]);} MouseOn(MouseX,MouseY);/*新位置显示*/ } if(press!=0)/*有按键的情况*/ return press;return 0;/*只移动的情况*/ } void Control(void)/*游戏开始,重新,关闭*/ { int gameFLAG=1;/*游戏失败后判断是否重新开始的标志*/ while(1){ MouseStatus();if(gameFLAG)/*游戏失败后没判断出重新开始或者退出游戏的话就继续判断*/ { GameBegain();/*游戏初始画面*/ GamePlay();/*具体游戏*/ if(gameAGAIN==1)/*游戏中重新开始*/ { gameAGAIN=0;continue;} } gameFLAG=0;if(LeftPress())/*判断是否重新开始*/ { if(MouseX>280&&MouseX<300&&MouseY>65&&MouseY<85){ gameFLAG=1;continue;} } if(kbhit())/*判断是否按键退出*/ break;} } void DrawSmile(void)/*画笑脸*/ { MouseOff();setfillstyle(SOLID_FILL,YELLOW);fillellipse(290,75,10,10);setcolor(YELLOW);setfillstyle(SOLID_FILL,BLACK);/*眼睛*/ fillellipse(285,75,2,2);fillellipse(295,75,2,2);setcolor(BLACK);/*嘴巴*/ bar(287,80,293,81);MouseGetXY();MouseOn(MouseX,MouseY);} void DrawRedflag(int i,int j)/*显示红旗*/ { MouseOff();setcolor(7);setfillstyle(SOLID_FILL,RED);bar(198+j*20,95+i*20,198+j*20+5,95+i*20+5);setcolor(BLACK);line(198+j*20,95+i*20,198+j*20,95+i*20+10);MouseGetXY();MouseOn(MouseX,MouseY);} void DrawEmpty(int i,int j,int mode,int color)/*两种空格子的显示*/ { MouseOff();setcolor(color);setfillstyle(SOLID_FILL,color);if(mode==0)/*没有单击过的大格子*/ bar(200+j*20-8,100+i*20-8,200+j*20+8,100+i*20+8);else if(mode==1)/*单击过后显示空白的小格子*/ bar(200+j*20-7,100+i*20-7,200+j*20+7,100+i*20+7);MouseGetXY();MouseOn(MouseX,MouseY);} void GameBegain(void)/*游戏开始画面*/ { int i,j;cleardevice();if(gamePLAY!=1){ MouseSetXY(290,70);/*鼠标一开始的位置,并作为它的初始坐标*/ MouseX=290;MouseY=70;} gamePLAY=1;/*下次按重新开始的话鼠标不重新初始化*/ mineNUM=0;setfillstyle(SOLID_FILL,7);bar(190,60,390,290);for(i=0;i<10;i++)/*画格子*/ for(j=0;j<10;j++)DrawEmpty(i,j,0,8);setcolor(7);DrawSmile();/*画脸*/ randomize();for(i=0;i<10;i++)/*100个格子随机赋值有没有地雷*/ for(j=0;j<10;j++){ Mine[i][j].num=random(8);/*如果随机数的结果是1表示这个格子有地雷*/ if(Mine[i][j].num==1)mineNUM++;/*现有雷数加1*/ else Mine[i][j].num=2;Mine[i][j].flag=0;/*表示没红旗标志*/ } sprintf(randmineNUM,”%d“,mineNUM);/*显示这次总共有多少雷数*/ setcolor(1);settextstyle(0,0,2);outtextxy(210,70,randmineNUM);mineNUM=100-mineNUM;/*变量取空白格数量*/ } void GameOver(void)/*游戏结束画面*/ { int i,j;setcolor(0);for(i=0;i<10;i++)for(j=0;j<10;j++)if(Mine[i][j].num==1)/*显示所有的地雷*/ { DrawEmpty(i,j,0,RED);setfillstyle(SOLID_FILL,BLACK);MouseOff();fillellipse(200+j*20,100+i*20,7,7); MouseGetXY();MouseOn(MouseX,MouseY);} } void GameWin(void)/*显示胜利*/ { setcolor(11);settextstyle(0,0,2);outtextxy(230,30,”YOU WIN!“);} int MineStatistics(int i,int j)/*统计每个格子周围的雷数*/ { int nNUM=0;if(i==0&&j==0)/*左上角格子的统计*/ { if(Mine[0][1].num==1)nNUM++;if(Mine[1][0].num==1)nNUM++;if(Mine[1][1].num==1)nNUM++;} else if(i==0&&j==9)/*右上角格子的统计*/ { if(Mine[0][8].num==1)nNUM++;if(Mine[1][9].num==1)nNUM++;if(Mine[1][8].num==1)nNUM++;} else if(i==9&&j==0)/*左下角格子的统计*/ { if(Mine[8][0].num==1)nNUM++;if(Mine[9][1].num==1)nNUM++;if(Mine[8][1].num==1)nNUM++;} else if(i==9&&j==9)/*右下角格子的统计*/ { if(Mine[9][8].num==1)nNUM++;if(Mine[8][9].num==1)nNUM++;if(Mine[8][8].num==1)nNUM++;} else if(j==0)/*左边第一列格子的统计*/ { if(Mine[i][j+1].num==1)nNUM++;if(Mine[i+1][j].num==1)nNUM++;if(Mine[i-1][j].num==1)nNUM++;if(Mine[i-1][j+1].num==1)nNUM++;if(Mine[i+1][j+1].num==1)nNUM++;} else if(j==9)/*右边第一列格子的统计*/ { if(Mine[i][j-1].num==1)nNUM++;if(Mine[i+1][j].num==1)nNUM++;if(Mine[i-1][j].num==1)nNUM++;if(Mine[i-1][j-1].num==1)nNUM++;if(Mine[i+1][j-1].num==1)nNUM++;} else if(i==0)/*第一行格子的统计*/ { if(Mine[i+1][j].num==1)nNUM++;if(Mine[i][j-1].num==1)nNUM++;if(Mine[i][j+1].num==1)nNUM++;if(Mine[i+1][j-1].num==1)nNUM++;if(Mine[i+1][j+1].num==1)nNUM++;} else if(i==9)/*最后一行格子的统计*/ { if(Mine[i-1][j].num==1)nNUM++;if(Mine[i][j-1].num==1)nNUM++;if(Mine[i][j+1].num==1)nNUM++;if(Mine[i-1][j-1].num==1)nNUM++;if(Mine[i-1][j+1].num==1)nNUM++;} else/*普通格子的统计*/ { if(Mine[i-1][j].num==1)nNUM++;if(Mine[i-1][j+1].num==1)nNUM++;if(Mine[i][j+1].num==1)nNUM++;if(Mine[i+1][j+1].num==1)nNUM++;if(Mine[i+1][j].num==1)nNUM++;if(Mine[i+1][j-1].num==1)nNUM++;if(Mine[i][j-1].num==1)nNUM++;if(Mine[i-1][j-1].num==1)nNUM++;} return(nNUM);/*把格子周围一共有多少雷数的统计结果返回*/ } int ShowWhite(int i,int j)/*显示无雷区的空白部分*/ { if(Mine[i][j].flag==1||Mine[i][j].num==0)/*如果有红旗或该格处理过就不对该格进行任何判断*/ return;mineNUM--;/*显示过数字或者空格的格子就表示多处理了一个格子,当所有格子都处理过了表示胜利*/ if(Mine[i][j].roundnum==0&&Mine[i][j].num!=1)/*显示空格*/ { DrawEmpty(i,j,1,7); Mine[i][j].num=0;} else if(Mine[i][j].roundnum!=0)/*输出雷数*/ { DrawEmpty(i,j,0,8);sprintf(randmineNUM,”%d“,Mine[i][j].roundnum);setcolor(RED);MouseOff();outtextxy(195+j*20,95+i*20,randmineNUM);MouseGetXY();MouseOn(MouseX,MouseY);Mine[i][j].num=0;/*已经输出雷数的格子用0表示已经用过这个格子*/ return;} /*8个方向递归显示所有的空白格子*/ if(i!=0&&Mine[i-1][j].num!=1)ShowWhite(i-1,j);if(i!=0&&j!=9&&Mine[i-1][j+1].num!=1)ShowWhite(i-1,j+1);if(j!=9&&Mine[i][j+1].num!=1)ShowWhite(i,j+1);if(j!=9&&i!=9&&Mine[i+1][j+1].num!=1)ShowWhite(i+1,j+1);if(i!=9&&Mine[i+1][j].num!=1)ShowWhite(i+1,j);if(i!=9&&j!=0&&Mine[i+1][j-1].num!=1)ShowWhite(i+1,j-1);if(j!=0&&Mine[i][j-1].num!=1)ShowWhite(i,j-1);if(i!=0&&j!=0&&Mine[i-1][j-1].num!=1)ShowWhite(i-1,j-1);} void GamePlay(void)/*游戏过程*/ { int i,j,Num;/*Num用来接收统计函数返回一个格子周围有多少地雷*/ for(i=0;i<10;i++)for(j=0;j<10;j++)Mine[i][j].roundnum=MineStatistics(i,j);/*统计每个格子周围有多少地雷*/ while(!kbhit()){ MouseStatus();if(LeftPress())/*鼠标左键盘按下*/ { if(MouseX>280&&MouseX<300&&MouseY>65&&MouseY<85)/*重新来*/ { gameAGAIN=1;break;} if(MouseX>190&&MouseX<390&&MouseY>90&&MouseY<290)/*当前鼠标位置在格子范围内*/ { j=(MouseX-190)/20;/*x坐标*/ i=(MouseY-90)/20;/*y坐标*/ if(Mine[i][j].flag==1)/*如果格子有红旗则左键无效*/ continue;if(Mine[i][j].num!=0)/*如果格子没有处理过*/ { if(Mine[i][j].num==1)/*鼠标按下的格子是地雷*/ { GameOver();/*游戏失败*/ break;} else/*鼠标按下的格子不是地雷*/ { Num=MineStatistics(i,j);if(Num==0)/*周围没地雷就用递归算法来显示空白格子*/ ShowWhite(i,j);else/*按下格子周围有地雷*/ { MouseOff();sprintf(randmineNUM,”%d",Num);/*输出当前格子周围的雷数*/ setcolor(RED);outtextxy(195+j*20,95+i*20,randmineNUM);mineNUM--; MouseGetXY();MouseOn(MouseX,MouseY);} Mine[i][j].num=0;/*点过的格子周围雷数的数字变为0表示这个格子已经用过*/ if(mineNUM<1)/*胜利了*/ { GameWin();break;} } } } } if(RightPress())/*鼠标右键键盘按下*/ { if(MouseX>190&&MouseX<390&&MouseY>90&&MouseY<290)/*当前鼠标位置在格子范围内*/ { j=(MouseX-190)/20;/*x坐标*/ i=(MouseY-90)/20;/*y坐标*/ if(Mine[i][j].flag==0&&Mine[i][j].num!=0)/*本来没红旗现在显示红旗*/ { DrawRedflag(i,j);Mine[i][j].flag=1;} else if(Mine[i][j].flag==1)/*有红旗标志再按右键就红旗消失*/ { DrawEmpty(i,j,0,8);Mine[i][j].flag=0;} } delay(1000000);delay(1000000);delay(1000000);delay(1000000);delay(1000000);} } } /////////////////////////////////////////////////// // 程序名称:一束漂亮的花 // 编译环境:Visual C++ 6.0 / 2010,EasyX 2011惊蛰版 // 作 者:yangw80 #define PI 3.14159265 // 画 花朵 void flower(int x, int y, COLORREF c){ int x1, y1, x2, y2;int d = 15;double e;setcolor(c);for(double a = 0;a < 2 * PI;a += PI / 360){ e = d *(1 + sin(a * 5));x1 = int(x + e * cos(a));y1 = int(y + e * sin(a));x2 = int(x + e * cos(a + PI / 5));y2 = int(y + e * sin(a + PI / 5));line(x1, y1, x2, y2);} // 画 蝴蝶结 void tie(int x, int y, COLORREF c){ int x1, y1, x2, y2;} int d = 80;double e;setcolor(c);for(double a = 0;a < 2 * PI;a += PI / 360){ e = d *(1 + sin(a * 4));x1 = int(x + e * cos(a));y1 = int(y + e * sin(a)/ 2);x2 = int(x + e * cos(a + PI / 9));y2 = int(y + e * sin(a + PI / 9)/ 4.5);line(x1, y1, x2, y2);} // 主函数 void main(){ // 画花朵 flower(320, 160, RED);flower(200, 120, YELLOW);flower(150, 140, LIGHTRED);flower(430, 176, RGB(255, 127, 0));flower(370, 110, RGB(239, 179, 52));flower(250, 72, RGB(235, 95, 186));initgraph(640, 480); // 画枝干 setcolor(GREEN);line(189, 372, 180, 400);line(310, 160, 325, 68);line(310, 160, 187, 374);line(150, 140, 189, 374);line(430, 176, 190, 374);line(370, 110, 187, 374);line(250, 72, 189, 372);line(253, 192, 190, 374);line(189, 372, 187, 400);line(189, 372, 182, 400);line(189, 372, 200, 120);} } flower(325, 68, RGB(228, 119, 98));flower(253, 190, RGB(247, 169, 117));// 画蝴蝶结 tie(195, 354, LIGHTMAGENTA); // 按任意键退出 getch();closegraph(); #include //编号// char name[20]; //起点和终点// char time[5]; //出发时间// int price; //车票价格// int amount; //剩余数量// struct Node *next;}Node;//创建链表并输入数据// struct Node *creat(){ struct Node *head,*r,*s; int i=0; char choice; head=(struct Node *)malloc(sizeof(struct Node));head->next=NULL;r=head;do { s=(struct Node *)malloc(sizeof(struct Node));s->next=NULL;printf(“请输入第%d种火车票的信息:n”,++i);printf(“请输入火车的编号:”); scanf(“%d”,&s->num); printf(“起点和终点:”);scanf(“%s”,s->name);printf(“出发时间:”);scanf(“%s”,s->time);printf(“车票价格:”);scanf(“%d”,&s->price);printf(“剩余数量:”);scanf(“%d”,&s->amount); r->next=s; r=s; printf(“Continue?(Y/N)”);scanf(“%s”,&choice);}while(choice=='Y'||choice=='y'); r->next=NULL;return(head);} //将单链表中的信息保存到文件1.txt中// void save(struct Node *h){ struct Node *s;FILE *fp; char filename[10]=“1.txt”; fp=fopen(“1.txt”,“wt”);if(fp==NULL){ printf(“n写文件出错,按任意键退出!”);getchar();exit(1);} for(s=h->next;s!=NULL;s=s->next) fprintf(fp,“%d %s %s %d %d n”,s->num,s->name,s->time,s->price,s->amount); getchar();fclose(fp);} // 从文件1.txt中读取信息并存入单链表中// struct Node *read(){ struct Node *head,*r,*s;FILE *fp;char filename[10]=“zl.txt”;fp=fopen(“1.txt”,“rt”);if(fp==NULL){ printf(“读文件错误,按任意键退出!”);getchar();exit(1);} head=(struct Node *)malloc(sizeof(struct Node));head->next=NULL;r=head;while(!feof(fp)){ s=(struct Node *)malloc(sizeof(struct Node));fscanf(fp,“%d %s %s %d %d”,&s->num,s->name,s->time,&s->price,&s->amount); r->next=s;r=s; } r->next=NULL;fclose(fp); return head;} //将链表中的数据输出// void print(struct Node *h){ struct Node *s; printf(“n火车票信息如下:n”); printf(“~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~n”);printf(“编号 起点和终点 出发时间 车票价格 剩余票数:n”); for(s=h->next;s->next!=NULL;s=s->next){ printf(“ %d %10s %5s %10d %6dn”,s->num,s->name,s->time,s->price,s->amount);} } //链表查询// struct Node * find(struct Node *h){ int i,j;char s[20];printf(“tt 查询方法有以下几种:n”);printf(“tt 1.火车票编号n”);printf(“tt 2.起点和终点n”);printf(“tt 3.出发时间n”);printf(“tt 4.车票价格n”);printf(“tt 5.剩余票数n”);printf(“请输入您要查询的方法的序号:”);scanf(“%d”,&i);switch(i){ case 1:printf(“请输入你要查询火车票的编号:”);scanf(“%d”,&j); while(h->next!=NULL) { h=h->next; if(h->num==j)return h; } return NULL;break;case 2:printf(“请输入您要查询火车票的起点和终点:”);scanf(“%s”,s);while(h->next!=NULL){ h=h->next;if(strcmp(h->name,s)==0) return h; } return NULL;break;case 3:printf(“请输入您要查询火车票的时间:”); scanf(“%s”,s); while(h->next!=NULL) { h=h->next; if(strcmp(h->time,s)==0) return h; } return NULL; break;case 4:printf(“请输入你要查询火车票的价格 :”);scanf(“%d”,&j); while(h->next!=NULL) { h=h->next; if(h->price==j) return h; } return NULL; break;case 5:printf(“请输入你要查询火车票的剩余票数:”);scanf(“%d”,&j); while(h->next!=NULL) { h=h->next; if(h->amount==j) return h; } return NULL; break;} } //修改信息// change(struct Node *h,int k){ int j;struct Node *p;p=find(h);printf(“------------n”);printf(“t 您要修改哪一项?n”);printf(“t 1.火车编号n”);printf(“t 2.起点和终点n”);printf(“t 3.出发时间n”);printf(“t 4.车票价格n”); printf(“t 5.剩余票数n”);printf(“t 0.退出系统n”); printf(“------------n”);printf(“请输入您要修改项的编号:”);scanf(“%d”,&j);switch(j) { case 1: printf(“修改后的火车编号:”); scanf(“%d”,&p->num); break; case 2: printf(“修改后的起点和终点:”); scanf(“%s”,p->name); break; case 3: printf(“修改后的出发时间:”); scanf(“%s”,p->time); break; case 4: printf(“修改后的车票价格:”); scanf(“%d”,&p->price); break; case 5: printf(“修改后的剩余票数:”); scanf(“%d”,&p->amount); break; case 0:break;} } //删除信息// delete(struct Node *h){ struct Node *p; int j; printf(“请输入您要删除的火车票的编号:”);scanf(“%d”,&j);p=h->next; if(p==NULL) return 0;while(p!=NULL){ if(p->num==j){ h->next=p->next; free(p); return 1;} h=p;p=p->next; } return 0;} //添加信息// void append(){ struct Node *p; FILE *fp; fp=fopen(“1.txt”,“at+”); if(fp==NULL) { printf(“写文件出错,按任意键返回.n”);getchar();exit(1); } printf(“请输入要添加的火车票的信息:火车编号,起点和终点,出发时间,车票价格,剩余票数:n”);scanf(“%d%s%s%d%d”,&p->num,p->name,p->time,&p->price,&p->amount);fprintf(fp,“%d %s %s %d %dn”,p->num,p->name,p->time,p->price,p->amount);getchar();fclose(fp);} //数据的统计// void count(struct Node *h){ struct Node *s;s=h;int i,j,k,n=0;printf(“*****************************************************************************n”); printf(“tt 请选择您要统计项目的序号:n”); printf(“tt 1.车票价格n”); printf(“tt 2.剩余票数n”);printf(“tt 0.退出界面n”); scanf(“%d”,&i);switch(i) { case 1: printf(“请输入您要统计车票的价格的标准:”); scanf(“%d”,&j); printf(“tt 请选择低于或高于标准:n”); printf(“tt 1.价格低于%d的个数n”,j); printf(“tt 2.价格高于%d的个数n”,j); scanf(“%d”,&k); if(k==1) { for(s=h->next;s->next!=NULL;s=s->next) if(s->price n++; printf(“车票价格低于%d的个数有%d个.n”,j,n); } else { for(s=h->next;s->next!=NULL;s=s->next) if(s->price>j) n++; printf(“车票价格低于%d的个数有%d个.n”,j,n); } break; case 2: printf(“请输入您要统计剩余票数的数量:”); scanf(“%d”,&j); printf(“tt 请选择低于或高于所输票数:n”); printf(“tt 1.票数低于%d的个数n”,j); printf(“tt 2.票数高于%d的个数n”,j); scanf(“%d”,&k); if(k==1) { for(s=h->next;s->next!=NULL;s=s->next) if(s->amount n++; printf(“剩余票数低于%d的个数有%d个.n”,j,n); } else { for(s=h->next;s->next!=NULL;s=s->next) if(s->amount>j) n++; printf(“剩余票数高于%d的个数有%d个.n”,j,n); } break; case 0:break; } } //保存用户和密码到文件2.txt中// void save_user(){ char file[10]=“2.txt”;FILE *fp;char name[20];char pwd[10];fp=fopen(“2.txt”,“at+”);if(fp==NULL){ printf(“n写文件出错,按任意键退出.n”); getchar();exit(1);} printf(“请输入用户名:”); scanf(“%s”,name);printf(“请输入密码:”); scanf(“%s”,pwd); fprintf(fp,“%s %sn”,name,pwd); getchar(); fclose(fp); printf(“用户注册成功!n”);} //检验用户和密码是否匹配// int check(char *name,char *pwd){ char name1[20];char pwd1[10];FILE *fp;char file[10]=“2.txt”;if((fp=fopen(“2.txt”,“rt”))==NULL){ printf(“读文件出错,按任意键退出!n”); getchar(); exit(1);} while(!feof(fp)){ fscanf(fp,“%s %s”,name1,pwd1); if(strcmp(name1,name)==0&&strcmp(pwd1,pwd)==0) return 1;} return 0;} //数据排序// void sort(struct Node *h){ struct Node *s,*p,*m,*n;int t,t1,t2,t3;char s1[20];char s2[10]; printf(“车票价格由小到大排序如下:n”);for(s=h->next;s->next!=NULL;s=s->next)for(p=s->next;p->next!=NULL;p=p->next) if(s->price>p->price) { t1=s->num;s->num=p->num;p->num=t1; t2=s->price;s->price=p->price;p->price=t2; t3=s->amount;s->amount=p->amount;p->amount=t3; strcpy(s1,s->name);strcpy(s->name,p->name);strcpy(p->name,s1); strcpy(s2,s->time);strcpy(s->time,p->time);strcpy(p->time,s2); } print(h);printf(“nn剩余车票数量由多到少排序如下:n”);for(s=h->next;s->next!=NULL;s=s->next) for(p=s->next;p->next!=NULL;p=p->next) if(s->amount amount) { t1=s->num;s->num=p->num;p->num=t1; t2=s->price;s->price=p->price;p->price=t2; t3=s->amount;s->amount=p->amount;p->amount=t3; strcpy(s1,s->name);strcpy(s->name,p->name);strcpy(p->name,s1); strcpy(s2,s->time);strcpy(s->time,p->time);strcpy(p->time,s2); } print(h);} void main(){ struct Node *head,*p;int i,j,k;head=(struct Node *)malloc(sizeof(struct Node));head->next=NULL;char name[20];char pwd[10];printf(“n***************欢迎进入火车票管理系统******************n”);printf(“tt 1.用户登录n”);printf(“tt 2.用户注册n”);printf(“tt 0.退出系统 n”);printf(“请输入所选序号:”);scanf(“%d”,&k); switch(k){ case 1: printf(“请输入用户名:”); scanf(“%s”,name); printf(“请输入密码:”); scanf(“%s”,pwd); if(check(name,pwd)) { printf(“密码正确.n”); do { printf(“nntt*********************欢迎进入火车票管理系统***********************n”); printf(“tt 1.录入火车票信息tt 2.添加火车票信息n”); printf(“tt 3.修改火车票信息tt 4.删除火车票信息n”); printf(“tt 5.打印火车票信息tt 6.查询火车票信息n”); printf(“tt 7.统计火车票信息tt 8.火车票销售排行n”); printf(“tt 0.退出系统n”); printf(“请输入您要进入菜单的序号(0-8):”); scanf(“%d”,&i); switch(i) { case 1: printf(“请录入火车票信息nn”); head=creat(); save(head); head=read(); break; case 2: append(); break; case 3: printf(“请输入您要修改的火车票的编号:”); scanf(“%d”,&j); change(head,j); save(head); break; case 4: head=read(); if(delete(head)) { printf(“已正确删除!n”); save(head); } else printf(“要删除的结点不存在!n”); break; case 5: head=read(); print(head); break; case 6: printf(“请输入您要查询火车票的编号(以0结束):”); scanf(“%d”,&j); { p=find(head); printf(“编号 起点和终点 出发时间 车票价格 剩余票数:n”); printf(“%d %10s %5s %10d %6dn”,p->num,p->name,p->time,p->price,p->amount); printf(“请继续输入序号(以0结束):”); scanf(“%d”,&j); } break; case 7: head=read();count(head);break; case 8: sort(head);break; case 0: printf(“************************用!*****************************n”);break; } }while(i!=0); } else printf(“密码错误或用户名不存在.n”); break;case 2:save_user();break;case 0:break;} 谢 谢 使第二篇:DES源代码C语言实现(简洁易懂)
第三篇:C语言扫雷源代码
第四篇:c语言一朵花源代码
第五篇:C语言课程设计火车票系统源代码