C语言源代码
- 格式:doc
- 大小:105.00 KB
- 文档页数:19
源文件#include <stdio.h>#include <malloc.h>#include <string.h>#include <stdlib.h>#define NULL 0#define LEN sizeof(struct student)struct student{ char name[20];long int num;int English[4];int Math;int com[30];int ave;struct student *next;};//定义一个结构题int TOTAL_NUM = 0;//学生总数struct student *head = NULL;void welcome (); //欢迎界面void mainmenu (); //主界面void record (); //记录数据void insert(struct student *stu); //插入数据void display(struct student *stu); //显示一个学生的信息void displayAll (); //显示所有学生的信息void query_by_num (); //按学号查询学生信息void readData (); //读取文件里学生的信息void writeData (); //向文件写入学生信息void freeAll (); //清空链表内容void del (); //删除学生信息void change (); //更改学生信息void devise (struct student *p); //选择更改内容int main (void){ welcome ();//显示主菜单mainmenu ();return 0;}//显示欢迎信息void welcome (){printf ("+----------------------------------------------------+\n");printf ("| |\n");printf ("| 欢迎使用计算机科学与技术2班学生信息管理系统|\n");printf ("| |\n");printf ("+----------------------------------------------------+\n");}//系统主菜单void mainmenu (){ int choice;choice = -1;readData ();printf ("\n温馨提示:为保证您的操作得到保存,请按正常顺序退出系统^_^\n");do{printf ("\n\n\n");printf ("\t\t\t--------------------------------------------\n");printf ("\t\t\t+ 计算机科学与技术2班学生信息管理系统|\n");printf ("\t\t\t--------------------------------------------\n");printf ("\t\t\t+ [1]----录入学生信息|\n"); printf ("\t\t\t+ [2]----浏览学生信息|\n"); printf ("\t\t\t+ [3]----查询学生信息|\n");printf ("\t\t\t+ [4]----删除学生信息|\n"); printf ("\t\t\t+ [5]----修改学生信息|\n"); printf ("\t\t\t+ [0]----退出系统|\n"); printf ("\t\t\t+*·*·*·*·*·*·*·*·*·*·*·*·*·*·*|\n");printf ("\t\t\t--------------------------------------------\n");printf ("请输入您的选择:");scanf ("%d", &choice);switch (choice){case 0:writeData ();freeAll ();exit (0);case 1:record ();break;case 2:displayAll ();break;case 3:query_by_num ();break;case 4:del ();break;case 5:change ();break;default:printf ("\n无效选项!");break;}}while (choice != 0);}//录入学生信息void record (){ struct student *p0;p0 = (struct student *)malloc(LEN);printf ("请输入学生的姓名:");scanf ("%s",p0->name);printf ("请输入学生的学号:");scanf ("%ld",&p0->num);printf ("请输入学生的英语分数:");scanf ("%ld",p0->English);printf ("请输入学生的数学分数:");scanf ("%d",&p0->Math);printf ("请输入学生的计算机分数:");scanf ("%ld",p0->com);printf ("请输入学生的平均分:");scanf ("%ld",&p0->ave);insert (p0);printf ("该学生的信息为:\n");printf("-------------------------------------------------------------------------------\n"); printf ("姓名\t学号\t\t英语分数\t数学分数\t计算机分数\t\t平均分\n");display (p0);}void insert (struct student *stu){struct student *p0, *p1, *p2;p1 = head;p0 = stu;if (head == NULL){head = p0;p0->next = NULL;}else{while ((p0->num > p1->num)&&(p1->next != NULL)) {p2 = p1;p1 = p1->next;}if (p0->num <= p1->num){if (head == p1)head = p0;elsep2->next = p0;p0->next = p1;}else{p1->next = p0;p0->next = NULL;}}TOTAL_NUM++;}void display (struct student *p){printf ("%s\t%ld\t%d\t%ld\t%ld\t\t%ld\n", p->name, p->num, p->English, p->Math, p->com, p->ave);}//浏览学生信息void displayAll(){struct student *p;printf("学生总数:%d\n", TOTAL_NUM);p = head;if (head != NULL){printf("\n姓名\t学号\t\t英语分数\t数学分数\t计算机分数\t\t平均分\n");printf("-------------------------------------------------------------------------------\n");do{display(p);p = p->next;}while(p != NULL);}printf ("\n");}//按学号查询学生信息void query_by_num (){int num;struct student *p1;printf("请输入学生的学号:"); scanf("%ld", &num);if(head==NULL){printf("无学生记录!\n"); return;}p1 = head;while (num!=p1->num && p1->next!=NULL)p1 = p1->next;if (num == p1->num){printf ("姓名\t学号\t\t英语分数\t数学分数\t计算机分数\t\t平均分\n");printf("-------------------------------------------------------------------------------\n"); display (p1);}elseprintf ("没有该学生记录,请核对!");}//写入文件void writeData (){FILE* fp;//文件指针struct student *p;fp = fopen("1.txt", "w");if (!fp){printf("文件打开错误!");return;}fprintf(fp,"%d\n", TOTAL_NUM);for(p = head; p!= NULL; p= p->next){fprintf(fp,"%s\t%ld\t%ld\t%d\t%ld\t%ld\n", p->name, p->num, p->English, p->Math, p->com, p->ave);}fclose (fp);}void freeAll (){struct student *p1, *p2;p1 = p2=head;while(p1){p2=p1->next;free (p1);p1 = p2;}}//读取文件void readData (){FILE* fp;//文件指针struct student *p1, *p2;fp = fopen("1.txt", "r");if (!fp){printf("文件打开错误!");return;}fscanf(fp,"%d\n", &TOTAL_NUM);head = p1 = p2 = (struct student *)malloc(LEN);fscanf(fp,"%s\t%ld\t%ld\t%d\t%ld\t%ld\n", p1->name, &p1->num, p1->English, &p1->Math, p1->com, &p1->ave);while(!feof(fp)){p1 = (struct student *)malloc(LEN);fscanf(fp,"%s\t%ld\t%ld\t%d\t%ld\t%ld\n", p1->name, &p1->num, p1->English, &p1->Math, p1->com, &p1->ave);p2->next = p1;p2 = p1;}p2->next = NULL;fclose(fp);}//删除学生信息void del (){struct student *p1, *p2;long int num;if (head == NULL){printf("无学生记录!\n");return;}printf("请输入您要删除的学生的学号:"); scanf("%ld", &num);p1 = head;while (num != p1->num && p1->next != NULL) {p2 = p1;p1 = p1->next;}if(num == p1->num){if(p1 == head)head = p1->next;else p2->next = p1->next;free(p1);TOTAL_NUM--;}elseprintf("没有该学生记录,请核对!\n");}//修改学生信息void change (){struct student *p1, *p2;long int num;if (head == NULL){printf ("无学生记录!\n");return;}printf ("请输入您要修改的学生的学号:");scanf ("%ld", &num);p1 = head;while (num != p1->num && p1->next != NULL){p2 = p1;p1 = p1->next;}if(num == p1->num)devise (p1);elseprintf("没有该学生记录,请核对!\n");}void devise (struct student *p){int choice;choice = -1;do{printf("请选择您要修改的学生的信息内容:\n");printf("+----------------------+\n");printf("| 姓名请按1 |\n");printf("| 学号请按2 |\n");printf("| 英语分数请按3 |\n"); printf("| 数学分数请按4 |\n");printf("| 计算机分数请按5 |\n"); printf("| 平均分请按6 |\n");printf("| 取消请按0 |\n");printf("+----------------------+\n");printf("请输入您的选择:");scanf("%d", &choice);switch (choice){case 0:return;case 1:printf("请输入新姓名:"); scanf("%s", p->name);break;case 2:printf("请输入新学号:"); scanf("%d", &p->num);break;case 3:printf("请输入新英语分数:");scanf("%s", p->English);break;case 4:printf("请输入新数学分数:");scanf("%s", &p->Math);break;case 5:printf("请输入新计算机分数:");scanf("%s", p->com);break;case 6:printf("请输入新平均分:");scanf("%lf", &p->ave);break;default:printf("\n无效选项!");break;}}while(choice != 0);}。
------------------------------------------------------------------------摘自宋鲁生程序设计大赛乘法口诀表#include <stdio.h>#include <conio.h>void main(void){int i,j,x,y;clrscr();printf("\n\n * * * 乘法口诀表* * * \n\n");x=9;y=5;for(i=1;i<=9;i++){gotoxy(x,y);printf("%2d ",i);x+=3;}x=7;y=6;for(i=1;i<=9;i++){gotoxy(x,y);printf("%2d ",i);y++;}x=9;y= 6;for(i=1;i<=9;i++){for(j=1;j<=9;j++){gotoxy(x,y);printf("%2d ",i*j);y++;}y-=9;x+=3;}printf("\n\n");}用一维数组统计学生成绩#include <stdio.h>void main(){char SelectKey,CreditMoney,DebitMoney;while(1){do{clrscr();puts("=========================");puts("| Please select key: |");puts("| 1. Quary |");puts("| 2. Credit |");puts("| 3. Debit |");puts("| 4. Return |");puts("=========================");SelectKey = getch();}while( SelectKey!='1' && SelectKey!='2' && SelectKey!='3' &&SelectKey!='4' );switch(SelectKey){case '1':clrscr();puts("================================");puts("| Your balance is $1000. |");puts("| Press any key to return... |");puts("================================");getch();break;case '2':do{clrscr();puts("==================================");puts("| Please select Credit money: |");puts("| 1. $50 |");puts("| 2. $100 |");puts("| 3. Return |");puts("==================================");CreditMoney = getch();}while( CreditMoney!='1' && CreditMoney!='2' && CreditMoney!='3' );switch(CreditMoney){case '1':clrscr();puts("=========================================");puts("| Your Credit money is $50,Thank you! |");puts("| Press any key to return... |");puts("=========================================");getch();break;case '2':clrscr();puts("==========================================");puts("| Your Credit money is $100,Thank you! |");puts("| Press any key to return... |");puts("==========================================");getch();break;case '3':break;}break;case '3':do{clrscr();puts("====================================");puts("| Please select Debit money: |");puts("| 1. $50 |");puts("| 2. $100 |");puts("| 3. $500 |");puts("| 4. $1000 |");puts("| 5. Return |");puts("====================================");DebitMoney = getch();}while( DebitMoney!='1' && DebitMoney!='2' && DebitMoney!='3' &&DebitMoney!='4' && DebitMoney!='5' );switch(DebitMoney){case '1':clrscr();puts("===========================================");puts("| Your Debit money is $50,Thank you! |");puts("| Press any key to return... |");puts("===========================================");getch();break;case '2':clrscr();puts("===========================================");puts("| Your Debit money is $100,Thank you! |");puts("| Press any key to return... |");puts("===========================================");getch();break;case '3':clrscr();puts("===========================================");puts("| Your Debit money is $500,Thank you! |");puts("| Press any key to return... |");puts("===========================================");getch();break;case '4':clrscr();puts("===========================================");puts("| Your Debit money is $1000,Thank you! |");puts("| Press any key to return... |");puts("===========================================");getch();break;case '5':break;}break;case '4':clrscr();puts("================================");puts("| Thank you for your using! |");puts("| Good bye! |");puts("================================");return;}}模拟ATM(自动柜员机)界面#include <stdio.h> void main(){int Password=0,Number=0,price=58,i=0;while( Password != 1234 ){if( i >= 3 )return;i++;puts("Please input Password: ");scanf("%d",&Password);}i=0;while( Number!=price ){do{puts("Please input a number between 1 and 100: ");scanf("%d",&Number);printf("Your input number is %d\n",Number);}while( !(Number>=1 && Number<=100) );if( Number >= 90 ){printf("Too Bigger! Press any key to try again!\n");}else if( Number >= 70 && Number < 90 ){printf("Bigger!\n");}else if( Number >= 1 && Number <= 30 ){printf("Too Small! Press any key to try again!\n");}else if( Number > 30 && Number <= 50 ){printf("Small! Press any key to try again!\n");}else{if( Number == price ){printf("OK! You are right! Bye Bye!\n");}else if( Number < price ){printf("Sorry,Only a little smaller! Press any key to try again!\n");}else if( Number > price ){printf(" Sorry, Only a little bigger! Press any key to try again!\n");}getch();}}用二维数组实现矩阵转置/* 用二维数组实现矩阵的转置*/#include <stdio.h>#define ROW 3#define COL 4main(){int matrixA[ROW][COL],matrixB[COL][ROW];int i,j; clrscr();printf("Enter elements of the matrixA,");printf("%d*%d:\n",ROW,COL);for( i=0; i<ROW; i++ ){for( j=0; j<COL; j++ ){scanf("%d",&matrixA[i][j]);}}for( i=0; i<ROW; i++ ){for( j=0; j<COL; j++ ){matrixB[j][i] = matrixA[i][j];}}printf("MatrixB,");printf("%d*%d:\n",COL,ROW);for( i=0; i<COL; i++ ){for( j=0; j<ROW; j++ ){printf("%8d",matrixB[i][j]);}printf("\n");}printf("\n Press Any Key to Quit... \n");getch();}求解二维数组的最大/最小元素#define MAXN 20int a[MAXN][MAXN];main(){int min, /* 存储最小值*/max; /* 存储最大值*/int row,col,n;clrscr();printf("Please input the order of the matrix:\n");/* 输入方阵的阶次*/ scanf("%d",&n);printf("Please input the elements of the matrix,\n from a[0][0] to a[%d][%d]:\n",n-1,n-1);for(row=0;row<n;row++)for(col=0;col<n;col++)scanf("%d",&a[row][col]);for(min=a[0][0],row=0;row<n;row++){/* 从每行选出最大数*/for(max=a[row][0],col=1;col<n;col++)/*从row行选出最大数*/if(max<a[row][col])max=a[row][col];if(min>max)/* 保存至row行的最小数*/min=max;}printf("The minimum of maximum number is %d\n",min);for(max=a[0][0],row=0;row<n;row++){/* 每行选出最小数*/for(min=a[row][0],col=1;col<n;col++)/* 从row行选出最小数*/ if(min>a[row][col])min=a[row][col];if(max<min)/*保存至row行的最大数*/max=min;}printf("The maximum of minimum numbers is %d\n",max);printf("\nPress any key to quit...\n");getch();}利用数组求前n个质数#define N 50main(){int primes[N];int pc,m,k; clrscr();printf("\n The first %d prime numbers are:\n",N);primes[0]=2;/*2是第一个质数*/pc =1;/*已有第一个质数*/m =3;/*被测试的数从3开始*/while(pc<N){/*调整m使它为下一个质数*/k=0;while(primes[k]*primes[k]<=m)if(m%primes[k]==0){/*m是合数*/m+=2;/*让m取下一个奇数*/k=1;/*不必用primes[0]=2去测试m,所以k从一开始*/}elsek++;/*继续用下一个质数去测试*/primes[pc++]=m;m+=2;/*除2外,其余质数均是奇数*/}/*输出primes[0]至primes[pc-1]*/for(k=0;k<pc;k++)printf("%4d",primes[k]);printf("\n\n Press any key to quit...\n ");getch();}编制万年历#include "stdio.h"long int f(int year,int month){/*f(年,月)=年-1,如月<3;否则,f(年,月)=年*/if(month<3) return year-1;else return year;} long int g(int month){/*g(月)=月+13,如月<3;否则,g(月)=月+1*/if(month<3) return month+13;else return month+1;} long int n(int year,int month,int day){/*N=1461*f(年、月)/4+153*g(月)/5+日*/return 1461L*f(year,month)/4+153L*g(month)/5+day;} int w(int year,int month,int day){/*w=(N-621049)%7(0<=w<7)*/return(int)((n(year,month,day)%7-621049L%7+7)%7);} int date[12][6][7];int day_tbl[ ][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};main(){int sw,leap,i,j,k,wd,day;int year;/*年*/char title[]="SUN MON TUE WED THU FRI SAT";clrscr();printf("Please input the year whose calendar you want to know: ");/*输入年*/scanf("%d%*c",&year);/*输入年份值和掠过值后的回车*/sw=w(year,1,1);leap=year%4==0&&year%100||year%400==0;/*判闰年*/for(i=0;i<12;i++)for(j=0;j<6;j++)for(k=0;k<7;k++)date[i][j][k]=0;/*日期表置0*/for(i=0;i<12;i++)/*一年十二个月*/for(wd=0,day=1;day<=day_tbl[leap][i];day++){/*将第i+1月的日期填入日期表*/date[i][wd][sw]=day;sw=++sw%7;/*每星期七天,以0至6计数*/if(sw==0) wd++;/*日期表每七天一行,星期天开始新的一行*/} printf("\n|==================The Calendar of Year %d=====================|\n|",year);for(i=0;i<6;i++){/*先测算第i+1月和第i+7月的最大星期数*/for(wd=0,k=0;k<7;k++)/*日期表的第六行有日期,则wd!=0*/wd+=date[i][5][k]+date[i+6][5][k];wd=wd?6:5;printf("%2d %s %2d %s |\n|",i+1,title,i+7,title);for(j=0;j<wd;j++){printf(" ");/*输出四个空白符*//*左栏为第i+1月,右栏为第i+7月*/for(k=0;k<7;k++)if(date[i][j][k])printf("%4d",date[i][j][k]);else printf(" ");printf(" ");/*输出十个空白符*/for(k=0;k<7;k++)if(date[i+6][j][k])printf("%4d",date[i+6][j][k]);else printf(" ");printf(" |\n|");}/*scanf("%*c");/*键入回车输出下一个月的日历*/}puts("=================================================================|") ;puts("\n Press any key to quit...");getch();}对数组元素排序rest(int a[], int n){int i,low,high,t; for(i=0,low=0,high=n-1;i<=high;) {if(a[i]>0){/*a[i]与a[high]交换,随之high减1*/t=a[i];a[i]=a[high];a[high]=t;high--;}else if(a[i]==0)i++; /* 掠过该元素*/else{/*a[i]与a[low]交换,随之low增1, i增1*/t=a[i];a[i]=a[low];a[low]=t;low++;i++;}}}int s[]={8,4,0,-1,6,0,-5};main(){int i;clrscr();printf("\n The arry before rest is:\n");for(i=0;i<sizeof(s)/sizeof(s[0]);i++)printf("%4d",s[i]);rest(s,sizeof(s)/sizeof(s[0]));printf("\n The arry after rest is:\n");for(i=0;i<sizeof(s)/sizeof(s[0]);i++)printf("%4d",s[i]);printf("\n Press any key to quit...\n");getch();}任意进制数的转换/* 函数trans将无符号整数n翻译成d(2<=d<=16)进制表示的字符串s */ #define M sizeof(unsigned int)*8int trans(unsigned n, int d, char s[]){static char digits[] ="0123456789ABCDEF"; /* 十六进制数字的字符*/char buf[M+1];int j, i = M;if(d<2||d>16){s[0]='\0'; /* 不合理的进制,置s为空字符串*/return 0; /* 不合理的进制,函数返回0 */}buf[i]='\0';do{buf[--i]=digits[n%d]; /*译出最低位,对应字符存入对应工作数组中*/n/=d;}while(n);/* 将译出在工作数组中的字符串复制到s */for(j=0;(s[j]=buf[i])!='\0';j++,i++);/* 其中控制条件可简写成s[j]=buf[i] */return j;}/* 主函数用于测试函数trans() */main(){unsigned int num = 253;int scale[]={2,3,10,16,1};char str[33];int i;clrscr();for(i=0;i<sizeof(scale)/sizeof(scale[0]);i++){if(trans(num,scale[i],str))printf("%5d = %s(%d)\n",num,str,scale[i]);elseprintf("%5d => (%d) Error! \n",num,scale[i]);}printf("\n Press any key to quit...\n");getch();}判断回文数/* 函数circle用于判断正整数n的d进制数表示形式是否是回文数*/ int circle(int n, int d){int s=0,m=n;while(m){s=s*d+m%d;m/=d;}return s==n;}/* main函数用于测试circle函数*/int num[]={232,27,851};int scale[]={2,10,16};main(){int i,j;clrscr();for(i=0;i<sizeof(num)/sizeof(num[0]);i++)for(j=0;j<sizeof(scale)/sizeof(scale[0]);j++)if(circle(num[i],scale[j]))printf("%d -> (%d) is a Circle Number!\n",num[i],scale[j]);elseprintf("%d -> (%d) is not a Circle Number!\n",num[i],scale[j]);printf("\n Press any key to quit...\n");getch();}求解钢材切割的最佳订单#include <stdio.h>#define N 20#define DELTA 2int bestlen;int bestsele[N];int sele[N];int n;int orderlen[N];int total;main(){int i;clrscr();printf("\n Please enter total length of the steel:\n");/* 输入钢材总长*/scanf("%d",&total);printf("\n Please enter number of order:\n"); /* 输入定单数*/ scanf("%d",&n);printf("\n Please enter the orders:\n"); /* 输入各定单*/for(i=0;i<n;i++)scanf("%d",&orderlen[i]);bestlen=0; /*最佳解用料的初值*/for(i=0;i<n;i++)sele[i]=bestsele[i]=0; /*置当前选择和最佳选择初值*/try(); /* 调用函数求解*/for(i=0;i<n;i++) /* 输出结果*/if(bestsele[i])printf("order %d length = %d\n",i+1,orderlen[i]);printf("\n Press any key to quit...");getch();}try(){int i,len;for(len=i=0;i<n;i++) /* 求当前选中的用料量*/if(sele[i])len+=orderlen[i]+DELTA;if(len-DELTA<=total) /* 注意最后一段可能不需要切割*/{if(bestlen < len){/* 找到一个更好的解*/bestlen = len;for(i=0;i<n;i++)bestsele[i]=sele[i];}for(i=0;i<n;i++) /* 对所有未选定单逐一作选中尝试循环*/if(!sele[i]){sele[i]=1; /* 做选中尝试*/try();sele[i]=0;}}}指向数组的指针main(){int x,y,z; /* 定义三个int型变量*/int *xp = &x, /* 定义指针变量xp,并赋值为x的地址,使xp指向x */ *yp = &y, /* 定义指针变量yp,并赋值为y的地址,使yp指向y */*zp = &z; /* 定义指针变量zp,并赋值为z的地址,使zp指向z */int t;printf("\nPlease input x,y,z:\n");scanf("%d%d%d",xp,yp,zp); /* 通过变量的指针,为变量输入值*/ if(*xp>*yp) /* 通过指向变量的指针引用变量的值*/{t=*xp; /* 通过指向变量的指针引用变量的值*/*xp=*yp;/* 通过指向变量x的指针xp,引用变量x的值*/*yp=t; /* 通过指向变量y的指针yp,引用变量y的值*/}if(*xp>*zp) /* 通过指向变量的指针,引用变量的值*/{t=*xp; /* 通过指向变量x的指针xp,引用变量x的值*/*xp=*zp;/* 通过指向变量x的指针xp,引用变量x的值*/*zp=t; /* 通过指向变量z的指针zp,引用变量z的值*/}if(*yp>*zp) /* 通过指向变量的指针,引用变量的值*/{t=*yp; /* 通过指向变量的指针,引用变量的值*/*yp=*zp;/* 通过指向变量y的指针yp,引用变量y的值*/*zp=t;/* 通过指向变量z的指针zp,引用变量z的值*/}printf("x = %d\ty = %d\tz = %d\n",x,y,z);printf("\nPress any key to quit...\n");getch();}阿拉伯数字转换为罗马数字#include <stdio.h>#define ROWS 4#define COLS 4int nums[ROWS][COLS]={{1000,1000,1000,1000},{900,500,400,100},{90,50,40,10},{9,5,4,1}};char *roms[ROWS][COLS]={{"m","m","m","m"}, {"cm","d","cd","c"},{"xc","l","xl","x"},{"ix","v","iv","i"}}; main(int argc,char *argv[ ]){int low,high;char roman[25]; if(argc<2){ printf("Usage:roman decimal_number\n");/*运行程序需带整数参数*/}high=low=atoi(argv[1]);/*将第一个参数转换成整数*/checknum(low);if(argc>2){/*带两个参数*/high=atoi(argv[2]);checknum(high);if(low>high){low=high;high=atoi(argv[1]);}}elselow=1;for(;low<=high;low++){to_roman(low,roman);printf("%d\t%s\n",low,roman);}} checknum(int val)/*检查参数合理性*/{if(val<1||val>9999){printf("The number must be in range 1..9999.\n");exit(0);}}to_roman(int decimal,char roman[ ])/*将整数转换成罗马数字表示*/ {int power,index;roman[0]='\0';for(power=0;power<ROWS;power++)for(index=0;index<COLS;index++)while(decimal>=nums[power][index]){strcat(roman,roms[power][index]);decimal-=nums[power][index];}}通讯录的输入输出#include <stdio.h>#define ZIPLEN 10#define PHONLEN 15/*struct addr类型定义*/ struct addr{char *name;/*姓名*/char *address;/*地址*/char zip[ZIPLEN];/*邮政编码*/char phone[PHONLEN];/*电话号码*/}; main()/*本主函数示意上述输入输出函数的用法*/{struct addr p[100];int i,j;clrscr();for(i=0;readaddr(p+i);i++);for(j=0;j<i;j++) writeaddr(p+j);puts("\n Press any key to quit...");getch();} /* 函数readaddr用于输入一个通信录函数*/int readaddr(struct addr *dpt){int len;char buf[120];/*输入字符串的缓冲区*/ printf("\nPlease input theName:\n");/*输入姓名*/if(scanf("%s",buf)==1){len=strlen(buf);dpt->name=(char *)malloc(len+1);/*申请存贮姓名的空间*/ strcpy(dpt->name,buf);}else return 0;/*Ctrl+Z结束输入*/printf("Please input the Address:\n");/*输入地址*/if(scanf("%s",buf)==1){len=strlen(buf);dpt->address=(char *)malloc(len+1);/*申请存贮地址的空间*/ strcpy(dpt->address,buf);}else{/*Ctrl+Z结束输入*/free(dpt->name);/*释放存贮姓名的空间*/return 0;}printf("Please input the Zip code:\n");/*输入邮编*/if(scanf("%s",buf)==1)strncpy(dpt->zip,buf,ZIPLEN-1);else{free(dpt->name);/*释放存贮姓名的空间*/free(dpt->address);/*释放存贮地址的空间*/return 0;/*Ctrl+Z结束输入*/}printf("Please input the Phone number:\n");/*输入电话号码*/ if(scanf("%s",buf)==1)strncpy(dpt->phone,buf,PHONLEN-1);else{free(dpt->name);free(dpt->address);return 0;/*Ctrl+Z结束输入*/}return 1;} /* 函数writeaddr用于输出通讯录*/int writeaddr(struct addr*dpt){printf("Name : %s\n", dpt->name);/*输出姓名*/printf("Address : %s\n", dpt->address);/*输出地址*/printf("Zip : %s\n", dpt->zip);/*输出邮编*/printf("Phone : %s\n\n", dpt->phone);/*输出电话号码*/}扑克牌的结构表示enum suits{CLUBS,DIAMONDS,HEARTS,SPADES}; struct card{enum suits suit;char value[3];};struct card deck[52];char cardval[][3]= {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};char suitsname[][9]={"CLUBS","DIAMONDS","HEARTS","SPADES"}; main() {int i,j;enum suits s;clrscr();for(i=0;i<=12;i++)for(s=CLUBS;s<=SPADES;s++){j=i*4+s;deck[j].suit=s;strcpy(deck[j].value,cardval[i]);}for(j=0;j<52;j++)printf("(%s%3s)%c",suitsname[deck[j].suit],deck[j].value,j%4==3?'\n':'\t');puts("\nPress any key to quit...");getch();}用“结构”统计学生成绩#include <stdio.h>#define N 200#define SCORES 5#define NUMLEN 10struct std_type{char no[NUMLEN];/*学号*/char *name;/*名字符串指针*/int scores[SCORES];/*五门功课的成绩*/};struct std_type students[N];int order[N];int total[N]; /*[函数]输入一个学生信息函数*/int readastu(struct std_type *spt){int len,j;char buf[120];/*输入字符串的缓冲区*/ printf("\nNumber : ");/*输入学号*/if(scanf("%s",buf)==1)strncpy(spt->no,buf,NUMLEN-1);elsereturn 0;/*Ctrl+Z结束输入*/printf("Name : ");/*输入姓名*/if(scanf("%s",buf)==1){len=strlen(buf);spt->name=(char *)malloc(len+1);/*申请存贮姓名的空间*/ strcpy(spt->name,buf);}else return 0;/*Ctrl+Z结束输入*/printf("Scores : ");/*输入成绩*/for(j=0;j<SCORES;j++)if(scanf("%d",spt->scores+j)!=1)break;if(j==0)/*一个成绩也未输入*/{free(spt->name);/*释放存贮姓名的空间*/return 0;}for(;j<SCORES;j++)/*少数未输入的成绩用0分代之*/ spt->scores[j]=0;return 1;} /*[函数]输出一个学生信息的函数*/int writeastu(struct std_type *spt){int i; printf("Number : %s\n",spt->no);/*输出学号*/printf("Name : %s\n",spt->name);/*输出姓名*/printf("Scores : ");/*输出成绩*/for(i=0;i<SCORES;i++)printf("%4d",spt->scores[i]);printf("\n\n");} main(){int n,i,j,t; clrscr();for(n=0;readastu(students+n);n++);/*采用冒泡法对学生信息数组排序*/for(i=0;i<n;i++){order[i]=i;/*预置第i个输入的学生*/for(t=0,j=0;j<SCORES;j++)/*求第i个学生的总分*/t+=students[i].scores[j];total[i]=t;}/*冒泡排序*/for(i=0;i<n-1;i++)/*共扫视n-1遍*/for(j=0;j<n-1-i;j++)if(total[order[j]]<total[order[j+1]]){/*交换名次*/t=order[j];order[j]=order[j+1];order[j+1]=t;}for(j=0;j<n;j++)/*输出*/writeastu(students+order[j]);printf("\n Press any key to quit...\n");getch();}报数游戏#include <stdio.h>struct ele{int no;struct ele *link;}main(){int n,m,i;struct ele *h,*u,*p;clrscr();printf("Please input n&m:\n");scanf("%d%d",&n,&m);/*输入n和m*/h=u=(struct ele *)malloc(sizeof(struct ele));/*形成首表元*/ h->no=1;for(i=2;i<=n;i++)/*形成其余的n-1个表元*/{u->link=(struct ele *)malloc(sizeof(struct ele));u=u->link;u->no=i;/*第i个表元置编号i*/}u->link=h;/*末表元后继首表元,形成环*/puts("\nThe numbers of who will quit the cycle in turn are:"); while(n){for(i=1;i<m;i++)/*掠过m-1个表元*/u=u->link;p=u->link;/*p指向第m个表元*/u->link=p->link;/*第m个表元从环中脱钩*/printf("%4d",p->no);free(p);/*释放第m个表元占用的空间*/n--;}printf("\n\n Press any key to quit...\n");getch();}学生成绩管理程序/*学生成绩管理程序编制一个统计学生考试分数的管理程序。
c语言课程设计及源代码一、教学目标本课程旨在让学生掌握C语言的基本语法、数据结构、算法和编程思想,培养学生具备基本的程序设计能力。
通过本课程的学习,学生将能够:1.理解C语言的基本语法和编程规则;2.熟练使用C语言进行简单的程序设计;3.掌握常用的数据结构和算法;4.培养良好的编程习惯和问题解决能力。
二、教学内容教学内容将按照教材的章节进行,主要包括:1.C语言的基本语法和编程规则;2.数据类型的定义和运算;3.控制语句和函数的使用;4.常用的数据结构和算法;5.指针和内存管理;6.文件操作和编程实践。
三、教学方法为了激发学生的学习兴趣和主动性,将采用多种教学方法:1.讲授法:通过讲解和演示,让学生掌握C语言的基本语法和编程规则;2.讨论法:通过小组讨论和实践,让学生深入理解和运用数据结构和算法;3.案例分析法:通过分析实际案例,让学生学会解决实际编程问题;4.实验法:通过上机实验,让学生动手实践,巩固所学知识。
四、教学资源为了支持教学内容和教学方法的实施,将准备以下教学资源:1.教材:选用权威、实用的C语言教材;2.参考书:提供相关的参考书籍,供学生自主学习;3.多媒体资料:制作PPT和教学视频,辅助讲解和演示;4.实验设备:提供计算机实验室,让学生进行上机实验。
通过以上教学设计,相信学生能够顺利完成本课程的学习,掌握C语言编程的基本技能。
五、教学评估为了全面、客观、公正地评估学生的学习成果,将采用多种评估方式:1.平时表现:通过课堂参与、提问和讨论,评估学生的学习态度和理解程度;2.作业:布置适量的作业,评估学生的编程能力和实践能力;3.考试:进行期中和期末考试,评估学生对课程知识的掌握程度;4.项目实践:课程项目,让学生综合运用所学知识解决问题,评估学生的综合能力。
六、教学安排教学安排将根据课程内容和学生的实际情况进行设计:1.教学进度:按照教材的章节顺序,合理安排每一节课的教学内容;2.教学时间:根据学生的作息时间,选择合适的时间段进行授课;3.教学地点:选择适宜的教室或实验室,保证教学环境的舒适和设施的齐全;4.教学活动:结合学生的兴趣爱好,安排一些实践活动,提高学生的学习积极性。
经典C语言源代码1、(1)某年某月某日是星期几#include<stdio.h>intmain(){intyear,month,day;{if(month==1||month==2)//判断month是否为1或2{ year--;month+=12;}intc=year/100;inty=year-c*100;intweek=(c/4)-2*c+(y+y/4)+(13*(month+1)/5)+day-1;while(week<0){week+=7;}week%=7;switch(week){}}return0;}1、(2)某年某月某日是第几天(一维数组)voidmain(){inti,flag,year,month,day,dayth;intmonth_day[]={0,31,28,31,30,31,30,31,31,30,31,30,31};请输入年/月/日:dayth=day;flag=(year%400==0)||(year%4==0&&year%100!=0);if(flag)month_day[2]=29;for(i=1;i<month;i++)dayth=dayth+month_day[i];是第%4天}2、30个数中找最小的数及其位置#defineSIZE30voidmain(){inti;floatdata[SIZE];intmin;请输入%d个浮点数:for(i=0;i<SIZE;i++){data[i]=rand()%30+1;、}min=0;for(i=1;i<SIZE;i++){if(data[i]<data[min])min=i;最小值是%5.2f,位置是}3、30个数从小到大排序(1)#defineSIZE30voidmain(){inti,j;floatdata[SIZE],temp;intmin;请输入%d个整型数:for(i=0;i<SIZE;i++){}for(i=0;i<SIZE;i++){min=i;for(j=i+1;j<SIZE;j++)if(data[j]<data[min])temp=data[min];data[min]=data[i];data[i]=temp;}排序后的结果是:for(i=0;i<SIZE;i++)min=j;}(2)模块化程序(数组名作为函数参数)#defineSIZE5voidaccept_array(floata[],intsize);voidsort(floata[],intsize); voidshow_array(floata[],intsize);voidmain(){floatscore[SIZE];accept_array(score,SIZE);排序前:show_array(score,SIZE);sort(score,SIZE);排序后:show_array(score,SIZE);}voidaccept_array(floata[],intsize){inti;请输入%d个分数:for(i=0;i<size;i++)}voidshow_array(floata[],intsize){inti;for(i=0;i<size;i++)}voidsort(floata[],intsize){inti,min,j;floattemp;for(i=0;i<SIZE;i++){min=i;for(j=i+1;j<SIZE;j++)if(a[j]<a[min])min=j;}temp=a[min]; a[min]=a[i]; a[i]=temp;}}4、(1)指针加减:#defineSIZE10voidmain(){inta[SIZE]={1,2,3,4,5,6,7,8,9,10};int*pa,i;pa=&a[0];//pa=a;for(i=0;i<SIZE;i++){pa++;(2)指针比较:#defineSIZE10voidmain(){inta[SIZE]={1,2,3,4,5,6,7,8,9,10};int*pa,i;int*qa;pa=qa=&a[0];请输入%d整型数:for(;pa<qa+SIZE;pa++)for(pa--;qa<=pa;pa--)}5、两字符串相连:voidstr_cat(charstr1[],charstr2[]);voidmain(){inti,j;charstr1[160];charstr2[80];请输入第一个字符串:gets(str1);请输入第二个字符串:gets(str2);str_cat(str1,str2);puts(str1);}voidstr_cat(charstr1[],charstr2[]){ inti,j;i=0;i++;j=0;str1[i]=str2[j];i++;j++;}}6、二维数组(a,b转置)voidmain(){inti,j,b[2][3];inta[3][2]={{1,2},{3,4},{5,6}};for(i=0;i<2;i++){for(j=0;j<3;j++)b[i][j]=a[j][i];}for(i=0;i<3;i++){for(j=0;j<2;j++)}for(i=0;i<2;i++){for(j=0;j<3;j++)}7、输入一个二维数组并输出(指针)voidmain(){intx[2][3];inti,j;for(i=0;i<2;i++)for(j=0;j<3;j++)putchar('');for(i=0;i<2;i++){for(j=0;j<3;j++)putchar('');}}8、冒泡法排序一个数组#definesize10voidmaopao(inta[]);voidmain(){inta[10];inti;请输入10个整数:for(i=0;i<10;i++)maopao(a);}voidmaopao(inta[]){inti,j,temp;for(i=0;i<9;i++){//进行9轮排序for(j=0;j<9-i;j++)//每轮进行9-i次交换{if(a[j]>a[j+1]){temp=a[j];a[j]=a[j+1];//大的沉底,小的上浮a[j+1]=temp;排序结果:for(i=0;i<10;i++)}}9、两数组A,B,要求A<B,如A :4,7,9B :1,3,5,8,9变换后A :1,3, 5B :4,7,8,9,9#include<stdio.h>voidReArranger(int*A,int*B,intm,intn)//A和B是各有m个和n个整数的非降序数组,本算法将B数组元素逐个插入到A中,使A中各元素均不大于B中各元素,且两数组仍保持非降序排列。
剪刀石头布源代码#include<stdio.h〉#include<stdlib。
h〉main(){int d,x;{printf("请输入:1是剪刀,2是石头,3是布”);scanf(”%d”,&d);x=rand()%3;if(d==x)printf("双方平局");else if((d==1&&x==2)||(d==2&&x==3)||(d==3&&x==1)) printf(”你赢了”);elseprintf(”电脑赢了”);}}简单计算器#include〈stdio。
h〉main(){int a,b,d=0;char c;while(d==0){printf(”请开始计算,请输入需要运算的数字和运算法则,数字符号数字:”); scanf(”%d%c%d”,&a,&c,&b);switch(c){case'+':printf(”%d+%d=%d\n",a,b,a+b);break;case’-’:printf(”%d—%d=%d\n",a,b,a-b);break;case'*’:printf("%d*%d=%d\n",a,b,a*b);break;case'/’:if(0==b)printf("除法被除数不能为零!\n”) ;elseprintf(”%d/%d=%d\n",a,b,a/b);break;}}}加油站加油问题#include<stdio。
h>int main(){double a = 3。
25,b = 3.00,c= 2.75;double d = 0。
05, e = 0.10, m;int x,y,z;printf("请输入您要的加油量:”);scanf("%d",&x);printf("请输入您要的汽油种类,1—a型汽油售价3。
#include<stdio.h>main(){float pi=3.14159265,r;printf("enter radius:\n");scanf("%f",&r);printf("r=%.2f,c=%.2f,area=%.2f\n",r,2*pi*r,pi*r*r);system("pause");}#include<stdio.h>main(){int m,n,x,y;printf("inputm,n:\n");scanf("%d%d",&m,&n);if (n%2==1){printf("error!! n bu shi ji shu!\n",n);return;}/*n在这里不能是奇数*/x=(4*m-n)/2;y=(n-2*m)/2;if((x>=0)&&(y>=0))printf("x=%d,y=%d\n",x,y); elseprintf("shu ru cuo wu!\n");getch();}#include<stdio.h>#include<math.h>#include<stdlib.h>main(){float a,b,C;printf("enter 3number(a,b,C):\n"); scanf("%.2f %.2f %.2f",&a,&b,&C); s=0.5*a*b*sinC;printf("s=%.2f\n",s);system("pause");}#include<stdio.h>main(){int ds,nl,yf;char c;printf("shu ru ds;\n");scanf("%d",&ds);if (ds<14||ds>1184){printf("input error!press any end...\n"); scanf("%c",&c);exit(0);}/*输入的数字必须是-14~1184之间*/nl=(ds+115)%100;yf=(ds+115)/100;printf("nl=%d,yf=%d\n",nl,yf);system("pause");}#include<stdio.h>#include<string.h>main(){char s1[100],s2[100],s3[100],s4[100]; printf("input a string:\n");gets(s1);strcpy(s1,s2);strcat(s1,"--------------");strcpy(s3,strcat(s1,"--------------"));strcat(strcpy(s3,strcat(s1,"--------------")),s2);strcpy(s4,strcat(strcpy(s3,strcat(s1,"--------------")),s2)); puts(s4);system("pasue");getch();}#include<stdio.h>#include<math.h>main(){float x1,x2,a,b,c;printf("input 3 number(a,b,c):\n");scanf("%f%f%f",&a,&b,&c);x1=(-b+sqrt(b*b-4*a*c))/(2*a);x2=(-b-sqrt(b*b-4*a*c))/(2*a);printf("x1=%f,X2=%f\n",x1,x2);system("pause");getch();}#include<stdio.h>main(){int a,b,c,t;printf("input 3 number:\n");scanf("%d%d%d",&a,&b,&c);if(a>b){t=a;a=b,b=t;}if(a>c){t=a;a=c;c=t;}if(b>c){t=b;b=c;c=t;}printf("1:%d,2:%d,3:%d\n",a,b,c);system("pause");}#include<stdio.h>#include<stdlib.h>#include<conio.h>main(){float pi=3.14159265,r;textbackground(YELLOW);/* 设置背景色为黄色,注意颜色应该大写,可更改 */ textcolor(RED); /* 设置文件颜色为红色,可更改 */clrscr(); /* 清屏,使设置生效 */printf("enter radius:");scanf("%f",&r);if(r<0)printf("Enter Error!\n");elseprintf("r=%.2f,c=%.2f,area=%.2f\n",r,2*pi*r,pi*r*r); system("pause");/* 暂停,按任一键继续 */#include<stdio.h>#include<math.h>#include<conio.h>main(){float a,b,c,delt,x1,x2,p,q;textcolor(YELLOW);clrscr();printf("Input a b c:\n");scanf("%f%f%f",&a,&b,&c);if(a==0){printf("It's not a quadratic equation!\n");system("pause");return;}delt=b*b-4*a*c;if(delt>=0){x1=(-b+sqrt(delt))/(2*a);x2=(-b-sqrt(delt))/(2*a);printf("x1=%.3f x2=%.4f\n",x1,x2);}else{p=-b/(2*a);q=sqrt(-delt)/(2*a);printf("p=%.4fq=%.4f\n",p,q);}system("pause");getch();}}#include<stdio.h>main(){float pi=3.14159265,r;int k=0;while(k<=3){printf("enter radius:\n");scanf("%f",&r);printf("r=%.2f,c=%.2f,area=%.2f\n",r,2*pi*r,pi*r*r); printf("press any key to continue\npress esc to exit."); k++;}}#include<stdio.h>#include<stdlib.h>#include<time.h>#include<conio.h>main(){int a,b,c,oper;long limit,i=0;char char1;textcolor(GREEN);/*设置字体颜色为绿色*/clrscr(); /*清屏,是设置生效*/while(i<=3){printf("qing xuan ze jia huo jian(1or2,1:+,2:-\n"); scanf("%d",&oper);printf("Enter max (<10000):\n");scanf("%ld",&limit);srand((unsigned long)time(0));a=rand()*limit/RAND_MAX;b=rand()*limit/RAND_MAX;while((a<b)&&(oper==2)){a=rand()*limit/RAND_MAX;b=rand()*limit/RAND_MAX;}char1=(oper==2?'-':'+');printf("%d%c%d\n",a,char1,b);scanf("%d",&c);if((oper==2&&a-b==c)||(oper!=2&&a+b==c)) printf("OK!You are very clever!\n"); elseprintf("The result is not correct!\n"); i++;}getch();}#include<stdio.h>#include<conio.h>main(){int y,i=0;textcolor(YELLOW);/*天下事无难易之分只有做与不做之别*/ textbackground(GREEN);clrscr();/*清屏,是设置生效*/while(i<=3){printf("Input year:\n");scanf("%d",&y);if(y%4==0){if(y%100==0){if(y%400==0)printf("y shi run nian!\n"); elseprintf("y bu shi run nian !\n"); }elseprintf("y shi run nian!\n");}elseprintf("y bu shi run nian!");i++;}getche();}#include<stdio.h>#include<time.h>#include<string.h>main(){int xz,wday1,hour1;struct tm *timeptr;time_t secsnow;char s1[30],s2[30],s3[30];printf("input whom do you say to?:\n"); scanf("%d",&xz);if(xz==1)strcpy(s1,"mother");else if(xz==2)strcpy(s1,"father");elsestrcpy(s1,"");time(&secsnow);timeptr=localtime(&secsnow);wday1=timeptr->tm_wday;if(wday1==6)strcpy(s2,"Happy saturday!");else if(wday1==0)strcpy(s2,"Happy sunday");elsestrcpy(s2,"");hour1=timeptr->tm_hour;if(hour1>=4&&hour1<=10)strcpy(s3,"Good morning!");else if(hour1>=17&&hour1<=22)strcpy(s3,"Good afternoon!");elsestrcpy(s3,"Good evening!");printf("%s%s%s",s1,s2,s3);getch();}#include<stdio.h>#include<conio.h>main(){int day,year,month,i=0;textbackground(BROWN);clrscr();while(i<=3){printf("Input 2 number:\n");scanf("%d%d",&year,&month);switch(month){case 1:case 3:case 5:case 7:case 8:case 10:case 12: day=31;break;case 4:case 6:case 11:day=30;break;case 2:day=28;if((year%4==0&&year%100!=0)||year%400==0)day=29;break;deflault:printf("Invalid month input!\n");return;}printf("There are%d days in %d.%d\n",day,year,month); i++;getch();}}。
学生管理系统c语言源代码学生管理系统c语言源代码#include stdio.h#include dos.h#include string.h#include stdlib.h#include malloc.h#define SIZE 8struct student{char name;char num;int score;float ave;struct student *next;}stu[SIZE],temp,s;void shuru(){int i,j,sum,length,flag=1,a;FILE *fp;while(flag==1){printf(“Define a rangeclass number:");scanf("%d",printf("Input the total number of the class(a):"); scanf("%d",length);if(lengtha)flag=0;}for(i=0;ilength;i++){printf("\n请输入学生的信息:");printf("\n输入姓名:");scanf("%s",stu[i].name);printf("\n输入序号.:");scanf("%s",stu[i].num);printf("\n输入成绩:\n");sum=0;for(j=0;jj++){printf("score %d:",j+1);scanf("%d",stu[i].score[j]);sum+=stu[i].score[j];}stu[i].ave=sum/3.0;}学生管理系统c语言源代码fp=fopen("stu1.txt","w");for(i=0;ilength;i++)if(fwrite(stu[i],sizeof(struct student),1,fp)!=1)printf("File write error\n");fclose(fp);fp=fopen("stu1.txt","r");printf("\name\ NO. score1 score2 score3 sum ave\n");for(i=0;ilength;i++){fread(stu[i],sizeof(struct student),1,fp);printf("%3s%5s%7d%7d%7d%7d%10.2f\n",stu[i].name,stu[i].num,stu[i ].score,stu[i].score,stu[i].score,sum=stu[i].score+stu[i].score+stu[i].score,stu[i].ave);}}void chaxun(){ FILE *fp, *fp1;char n,name;int i,j,k,t,m,flag=1;if((fp=fopen("stu1.txt","r"))==NULL){printf("Can not open the file.");exit(0);}printf("\noriginal data:\n");k=i;printf("\nPlease select the menu(1.number ):"); scanf("%d",switch(m){case 1:printf("\nchaxun number:");scanf("%s",n);for(flag=1,i=0;ii++){if(strcmp(n,stu[i].num)==0){j=i;flag=0;break;}}break;case 2:printf("\nchaxun name:");scanf("%s",name);for(flag=1,i=0;ii++){if(strcmp(name,stu[i].name)==0){j=i;flag=0;break;学生管理系统c语言源代码}}}if(!flag){printf("\nYou can find:\n");fp1=fopen("stu2.txt","w");printf(" name NO. score1 score2 score3ave\n");fwrite(stu[j],sizeof(struct student),1,fp1);printf("%-15s%11s%7d%7d%7d%10.2f",stu[j].name,stu[j].num,stu[j].score,stu[j].score,stu[j].score,stu[j].ave);}else printf("\nNot found!");fclose(fp);fclose(fp1);}xiugai(){ int a;printf("\nplease select the menu(1.CHARU 2.__ ):");scanf("%d",switch(a){case 1:Insert(); break;case 2:Delete(); break;}}Insert(){ FILE *fp;int i,j,t,n;printf("\nNO.:");scanf("%s",s.num);printf("name:");scanf("%s",);printf("score1,score2,score3:");scanf("%d,%d,%d",s.score,s.score,s.score);s.ave=(s.score+s.score+s.score)/3.0;if((fp=fopen("stu1.txt","r"))==NULL){printf("Can not open the file.");exit(0);}printf("\noriginal data:\n");for(i=0;fread(stu[i],sizeof(struct student),1,fp)!=0;i++) {printf("\n%-15s%11s",stu[i].name,stu[i].num);for(j=0;jj++)学生管理系统c语言源代码printf("%7d",stu[i].score[j]);printf("%10.2f",stu[i].ave);}fclose(fp);n=i;for(t=0;stu[t].aves.avett++);printf("\nnow:\n");fp=fopen("stu1.txt","w");for(i=0;ii++){fwrite(stu[i],sizeof(struct student),1,fp);printf("\n%-15s%11s",stu[i].name,stu[i].num);for(j=0;jj++)printf("%7d",stu[i].score[j]);printf("%10.2f",stu[i].ave);}fwrite(s,sizeof(struct student),1,fp);printf("\n%-15s%11s%7d%7d%7d%10.2f",,s.num,s.score,s.score, s.score,s.ave);for(i=t;ii++){fwrite(stu[i],sizeof(struct student),1,fp);printf("\n%-15s%11s",stu[i].name,stu[i].num);for(j=0;jj++)printf("%7d",stu[i].score[j]);printf("%10.2f",stu[i].ave);}fclose(fp);}Delete(){ FILE *fp;int i,j,t,n,flag;char number;if((fp=fopen("stu1.txt","rb"))==NULL){printf("Can not open the file.");exit(0);}printf("\noriginal data:");for(i=0;fread(stu[i],sizeof(struct student),1,fp)!=0;i++) {printf("\n%-15s%11s",stu[i].name,stu[i].num);for(j=0;jj++)printf("%7d",stu[i].score[j]);printf("%10.2f",stu[i].ave);}fclose(fp);n=i;学生管理系统c语言源代码printf("\nInput number deleted:");scanf("%s",number);for(flag=1,i=0;flagii++){if(strcmp(number,stu[i].num)==0){for(t=i;tt++){strcpy(stu[t].num,stu[t+1].num);strcpy(stu[t].name,stu[t+1].name);for(j=0;jj++)stu[t].score[j]=stu[t+1].score[j];stu[t].ave=stu[t+1].ave;}n=n-1;elseprintf("\n Not found!");printf("\nNow,the content of file:\n");fp=fopen("stu1.txt","wb");for(i=0;ii++)fwrite(stu[i],sizeof(struct student),1,fp);fclose(fp);fp=fopen("stu1.txt","r");for(i=0;fread(stu[i],sizeof(struct student),1,fp)!=0;i++)printf("%-15s%11s%7d%7d%7d%10.2f\n",stu[i].name,stu[i].num,stu[i].score, stu[i].score,stu[i].score,stu[i].ave);fclose(fp);}paixu(){FILE *fp;int i,j,n;if((fp=fopen("stu1.txt","r"))==NULL){printf("Can not open the file.");exit(0);}printf("\nfile'stu1.txt':");for(i=0;fread(stu[i],sizeof(struct student),1,fp)!=0;i++) {printf("\n%-15s%11s",stu[i].name,stu[i].num);for(j=0;jj++)printf("%7d",stu[i].score[j]);printf("%10.2f",stu[i].ave);}fclose(fp);n=i;for(i=0;ii++)for(j=i+1;jj++)学生管理系统c语言源代码if(stu[i].avestu[j].ave){temp=stu[i];stu[i]=stu[j];stu[j]=temp;}printf("\nnow:");fp=fopen("stu1.txt","w");for(i=0;ii++){fwrite(stu[i],sizeof(struct student),1,fp);printf("\n%-15s%11s",stu[i].name,stu[i].num);tongji(){ FILE *fp;int i,j,k,labe1,b;int a5=0;int a6=0;int a7=0;int a8=0;int a9=0; int a10=0; float t;if((fp=fopen("stu1.txt","r"))==NULL){printf("Can not open the file.");exit(0);}printf("\nfile'stu1.txt':");for(i=0;fread(stu[i],sizeof(struct student),1,fp)!=0;i++){printf("\n%-15s%11s",stu[i].name,stu[i].num); for(j=0;jj++)printf("%7d",stu[i].score[j]);printf("%10.2f",stu[i].ave);}fclose(fp);k=i;for(i=0;ii++){labe1=0;if(stu[i].ave60){labe1++;t=labe1/(float)k*100;}}printf("\nbujigelv:");printf("%f%",t);printf("\n");for(j=0;jj++){a5=0;a6=0;a7=0;a8=0;a9=0;a10=0;k=i;printf("kemu is %d:\n",j);for(i=0;ii++)学生管理系统c语言源代码{b=stu[i].score[j]/10;if(b6)a5++;elseif(b=6b7)a6++;elseif(b=7b8)a7++;elseif(b=8b9)a8++;if(b=9b10)a9++;elseif(b==10)a10++;}printf(" 不及格is %d\n",a5);printf(" 60--69 is %d\n",a6);printf(" 70--79 is %d\n",a7);printf(" 80--89 is %d\n",a8);printf(" 90--99 is %d\n",a9);printf(" 100 is %d\n",a10);}}main(){int a;printf(" ____\n"); printf(" 欢迎进入学生成绩管理系统\n");printf(" ____\n"); while(1){printf("\n选择菜单:\n");printf("\n");printf(" 1.输入 2.查询 3.排序 4.修改 5.统计 6.退出\n"); scanf("%d",switch(a){case 1: shuru();break;case 2: chaxun(); break;case 3: paixu(); break;case 4: xiugai(); break;学生管理系统c语言源代码case 5: tongji();break; case 6: exit(0); }。
经典C语言源代码1、(1)某年某月某日是星期几#include<>int main(){int year, month, day;while (scanf_s("%d%d%d", &year, &month, &day) != EOF){if (month == 1 || month == 2)d.%d", &start[0], &start[1], &start[2]);printf("请输入结束日期,如:\n");scanf_s("%d.%d.%d", &end[0], &end[1], &end[2]);int sum = 0;for (int mid = start[0]; mid < end[0]; mid++) {if ((mid % 400 == 0) || (mid % 4 == 0 && mid % 100 != 0)) { sum = sum + 366;}elsesum = sum + 365;}sum = sum - indexday(start[0],start[1],start[2]) +indexday(end[0],end[1],end[2]);printf("在%d.%d.%d-%d.%d.%d之间有%d天\n",start[0],start[1],start[2],end[0],end[1],end[2], sum);}int indexday(int year, int month, int day) {int i, flag, dayth;int month_day[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };dayth = day;flag = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);if (flag)month_day[2] = 29;for (i = 1; i < month; i++)dayth = dayth + month_day[i];return dayth;}18、递归求1*1+2*2+3*3+n*n#include ""long Element(int n) {if (n == 1)return 1 * 1;elsereturn Element(n - 1) + n*n; }void main() {int n;printf("请输入n的值:\n");scanf_s("%d", &n);printf("所求值为%d\n", Element(n));}19、最大公约数(辗转相除)#include<>void main() /* 辗转相除法求最大公约数*/{int m, n, a, b, t, c;printf("Input two integer numbers:\n");scanf_s("%d%d", &a, &b);m = a; n = b;while (b != 0) /* 余数不为0,继续相除,直到余数为0 */ {c = a%b; a = b; b = c;}printf("The largest common divisor:%d\n", a);printf("The least common multiple:%d\n", m*n / a); }20、杨辉三角#include<>void main(){int i, j, n, k;printf("Enter n:"); scanf_s("%d", &n);for (i = 1; i <= n; i++){k = 1;for (j = 1; j<i; j++){printf("%3d", k);k = k*(i - j) / j;}//每次要打印的下一个数等于前一个数乘以其所在行数和列数的差再除以其列数printf("%3d", k);printf("\n");}}21、约瑟夫#include <>void main(){int n, m, i, s=0;printf ("Enter n: m: ");scanf("%d%d", &n, &m);for (i=2; i<=n; i++) s=(s+m)%i;printf ("The winner is %d\n", s+1); }22、斐波拉契#include<>void main(){long f, f1, f2; int i, n;printf("Enter n : ");scanf_s("%d", &n);f1 = 1;f2 = 1;printf("%10d%10d", f1, f2);for (i = 1; i <= n; i++){f = f1 + f2;printf("%10d", f);f1 = f2; f2 = f;if (i % 10 == 0)printf("\n");}}23、海滩上有一堆桃子,五只猴子来分。
运行C语言程序的步骤 (2) 运行C语言程序的步骤可以分为以下几个步骤:1.编写源代码首先,需要编写C语言程序的源代码。
源代码是指程序员用某种编程语言编写的程序文本,包括一系列的语句和语法规则。
C语言源代码通常保存为以.c为后缀的文件,例如hello.c。
2.编译源代码编译源代码是将C语言源代码转换为可执行的目标代码的过程。
编译器将源代码转换成机器码,这个过程称为编译。
在编译过程中,编译器会检查源代码中的语法错误和逻辑错误,如果有错误,编译器会报错并提示程序员进行修改。
3.链接目标文件链接器将编译器生成的目标文件连接成一个可执行文件。
链接器负责解决程序中外部函数和全局变量的引用问题,同时将可重用的库函数链接到程序中。
4.运行可执行文件当链接器完成可执行文件的链接后,就可以运行程序了。
在命令行界面输入可执行文件的名称(注意,Linux系统需要使用./可执行文件名来运行),程序会开始执行。
如果一切顺利,程序会输出结果或按预期进行其他操作。
除了以上四个步骤外,还可以使用集成开发环境(IDE)来编译和运行C语言程序。
IDE是一种集成了编辑器、编译器、调试器等工具的软件开发环境。
通过IDE,可以方便地编写、编译、调试和运行程序。
例如,常见的C语言IDE有Visual Studio、Code:Blocks和Dev-C++等。
总结:运行C语言程序的步骤包括编写源代码、编译源代码、链接目标文件和运行可执行文件。
编译器将源代码转换成可执行的目标代码,链接器将目标文件连接成一个可执行文件。
当可执行文件生成后,可以在命令行界面或IDE中运行程序。
c语言概念介绍C语言是一种广泛使用的高级编程语言,它的设计影响了许多其他编程语言,如C++、Java和JavaScript等。
下面是关于C语言的一些基本概念介绍:1.源代码:C语言的源代码是文本文件,其中包含人类可读的代码。
这些代码由程序员编写,然后由编译器转换为机器代码,这是计算机可以理解的。
2.程序结构:C语言程序通常包括函数定义和全局变量声明。
函数是可重复使用的代码块,可以接收输入(称为参数),并且可以返回一个结果(返回值)。
全局变量在整个程序中都可以访问和修改。
3.数据类型:C语言提供了多种数据类型,包括整数(如int和long)、浮点数(如float和double)、字符(char)和布尔类型(bool)等。
4.变量:变量是用于存储数据的容器。
在C语言中,每个变量都有特定的数据类型,这决定了它可以存储的数据的类型和大小。
5.控制结构:C语言提供了几种控制结构,如if-else语句、switch语句和循环(for、while和do-while)。
这些结构用于控制程序执行流程。
6.函数:函数是可重复使用的代码块。
它们可以被其他函数调用,也可以调用其他函数。
函数通常用于将复杂问题分解为更小的、更易于管理的部分。
7.指针:指针是一个变量,它存储了另一个变量的内存地址。
通过使用指针,我们可以间接地访问和修改该变量的值。
8.内存管理:C语言提供了用于分配和释放内存的函数。
这使得程序员可以动态地创建和删除变量。
9.预处理器指令:预处理器指令是特殊的命令,用于告诉编译器在编译之前执行某些操作。
例如,#include指令告诉编译器在编译之前将另一个文件的内容包含在当前文件中。
10.库:库是包含一组函数和/或变量的文件集合。
这些函数和变量被组织在一起以执行特定的任务或提供特定的服务。
例如,标准输入/输出库(stdio.h)包含用于读取和写入数据的函数。
C语言游戏源代码1、简单的开机密码程序#include "conio.h"#include "string.h"#include "stdio.h"void error{window12;10;68;10;textbackground15;textcolor132;clrscr;cprintf"file or system error you can't enter the system";while1; /*若有错误不能通过程序*/}void look{FILE *fauto;*fbak;char *pass="c:\\windows\\password.exe"; /*本程序的位置*/char a25;ch;char *au="autoexec.bat";*bname="hecfback.^^^"; /*bname 是autoexec.bat 的备份*/setdisk2; /*set currently disk c:*/chdir"\\"; /*set currently directory \*/fauto=fopenau;"r+";if fauto==NULL{fauto=fopenau;"w+";if fauto==NULL error;}freada;23;1;fauto; /*读取autoexec.bat前23各字符*/a23='\0';if strcmpa;pass==0 /*若读取的和pass指针一样就关闭文件;不然就添加*/fclosefauto;else{fbak=fopenbname;"w+";if fbak==NULL error;fwritepass;23;1;fbak;fputc'\n';fbak;rewindfauto;whilefeoffauto{ch=fgetcfauto;fputcch;fbak;}rewindfauto;rewindfbak;whilefeoffbak{ch=fgetcfbak;fputcch;fauto;}fclosefauto;fclosefbak;removebname; /*del bname file*/}}void passchar input60;int n;while1{window1;1;80;25;textbackground0;textcolor15;clrscr;n=0;window20;12;60;12;textbackground1;textcolor15;clrscr;cprintf"password:";while1{inputn=getch;if n>58 {putchar7; break;} /*若字符多于58个字符就结束本次输入*/ if inputn==13 break;if inputn>=32 && inputn<=122 /*若字符是数字或字母才算数*/ {putchar'*';n++;}if inputn==8 /*删除键*/if n>0{cprintf"\b \b";inputn='\0';n--;}}inputn='\0';if strcmppassword;input==0break;else{putchar7;window30;14;50;14;textbackground15;textcolor132;clrscr;cprintf"password error";getch;}}}main{look;pass;}2、彩色贪吃蛇#include <graphics.h>#include <stdlib.h>#define N 200#define up 0x4800#define down 0x5000#define left 0x4b00#define right 0x4d00#define esc 0x011b#define Y 0x1579#define n 0x316eint gamespeed; /* 游戏速度 */int i; key; color;int score = 0; /* 游戏分数 */char cai48H ={0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x04; 0x00; 0x18; 0x00; 0x00; 0x00; 0x0E; 0x00; 0x1C; 0x00; 0x00; 0x00; 0x1C; 0x00; 0x1C; 0x00; 0x00; 0x00; 0x20; 0x00; 0x38; 0x00; 0x00; 0x00; 0x40; 0x00; 0x78; 0x00; 0x00; 0x01; 0x80; 0x40; 0x70; 0x00; 0x00; 0x03; 0x80; 0xC0; 0xE0; 0x00; 0x00; 0x07; 0x80; 0x80; 0xC0; 0x00; 0x00; 0x0E; 0x11; 0x81; 0xC0; 0x00; 0x00; 0x08; 0x61; 0x01; 0x80; 0x00; 0x00; 0x00; 0x23; 0x03; 0x04; 0x00; 0x00; 0x02; 0x02; 0x00; 0x06; 0x00; 0x00; 0x1E;0x04; 0x00; 0x0F; 0x00; 0x00; 0x1C; 0x1F; 0x80; 0x1E; 0x00; 0x00; 0x08; 0x3F; 0x80; 0x3C; 0x00; 0x00; 0x00; 0xFF; 0x80; 0x38; 0x00; 0x00; 0x03; 0xFF; 0x80; 0x78; 0x00; 0x00; 0x0F; 0xF8; 0x00; 0xF0; 0x00; 0x00; 0x7F; 0xF0; 0x00; 0xE0; 0x00; 0x03; 0xFF; 0xFC; 0x01; 0x80; 0x00; 0x03; 0xC0; 0xFF; 0x01; 0x03; 0x80; 0x01; 0x01; 0xFF; 0x00; 0x03; 0x80; 0x00; 0x01; 0x3F; 0x00; 0x07; 0x80; 0x00; 0x02; 0x11; 0x00; 0x07; 0x00; 0x00; 0x00; 0x10; 0x00; 0x07; 0x00; 0x00; 0x00; 0x10; 0x00; 0x0E; 0x00; 0x00; 0x08; 0x10; 0x00; 0x1C; 0x00; 0x00; 0x30; 0x10; 0x00; 0x18; 0x00; 0x00; 0x70; 0x10; 0x00; 0x30; 0x00; 0x01; 0xE0; 0x10; 0x00; 0x70; 0x00; 0x03; 0x80; 0x10; 0x00; 0x60; 0x00; 0x00; 0x00; 0x30; 0x00; 0xE0; 0x00; 0x00; 0x00; 0xF0; 0x01; 0xC0; 0x00; 0x00; 0x00; 0x70; 0x03; 0xC0; 0x00; 0x00; 0x00; 0x10; 0x07; 0x80; 0x00; 0x00; 0x00; 0x00; 0x0F; 0x00; 0x00; 0x00; 0x00; 0x00; 0x1E; 0x00; 0x00; 0x00; 0x00; 0x00; 0x3C; 0x00; 0x00; 0x00; 0x00; 0x00; 0x70; 0x00; 0x00; 0x00; 0x00; 0x01; 0xC0; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; };char she48H ={0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x04; 0x00; 0x00; 0x00; 0x00; 0x00; 0x0C; 0x00; 0x00; 0x00; 0x00; 0x00; 0x0E; 0x00; 0x00; 0x00; 0x00; 0x00; 0x0E; 0x00; 0x00; 0x00; 0x03; 0x00; 0x07; 0x00; 0x00; 0x00; 0x02; 0x00; 0x03; 0x00; 0x00; 0x00; 0x02; 0x00; 0x00; 0x00; 0x00; 0x00; 0x02; 0x00; 0x00; 0xF8; 0x00; 0x00; 0x02; 0x00; 0x07; 0x86; 0x00; 0x00; 0x02; 0x00; 0x18; 0x03; 0x00; 0x00; 0x02; 0x00; 0x00; 0x07; 0x80; 0x00; 0x03; 0xF0; 0x00; 0x07; 0x80; 0x00; 0x0F; 0xFC; 0x00; 0x0C; 0x00; 0x00; 0x7E; 0x3F; 0x80; 0x00; 0x00; 0x01; 0xFE; 0x1F; 0x80; 0x00; 0x00; 0x01; 0xE2; 0x39; 0x8C; 0x00; 0x00; 0x00; 0xC2; 0x30; 0x08; 0x00; 0x00; 0x00; 0xC2; 0x60; 0x08; 0x00; 0x00; 0x00; 0xC3; 0xE0; 0x08; 0x60; 0x00; 0x00; 0x7F; 0xE0; 0x01; 0xE0; 0x00; 0x00; 0x3F; 0x80; 0x1F; 0xE0; 0x00; 0x00; 0x1E; 0x00; 0x1F; 0x80; 0x00; 0x00; 0x1E; 0x00; 0x1F; 0x00; 0x00; 0x00; 0x02; 0x38; 0x1E; 0x00; 0x00; 0x00; 0x07; 0xFC; 0x1C;0x00; 0x20; 0x00; 0x07; 0xFC; 0x18; 0x00; 0x20; 0x00; 0x1F; 0x0C; 0x10; 0x00; 0x20; 0x00; 0x7C; 0x04; 0x10; 0x00; 0x60; 0x01; 0xF0; 0x00; 0x10; 0x00; 0x60; 0x01; 0xE0; 0x00; 0x08; 0x00; 0xF0; 0x00; 0x80; 0x00; 0x08; 0x03; 0xF0; 0x00; 0x00; 0x00; 0x07; 0xFF; 0xF0; 0x00; 0x00; 0x00; 0x07; 0xFF; 0xF0; 0x00; 0x00; 0x00; 0x03; 0xFF; 0xE0; 0x00; 0x00; 0x00; 0x01; 0xFF; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; };char tun48H ={0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x0E; 0x00; 0x00; 0x00; 0x00; 0x00; 0x3E; 0x00; 0x00; 0x00; 0x00; 0x00; 0x7F; 0x00; 0x00; 0x00; 0x00; 0x00; 0xE0; 0x00; 0x00; 0x00; 0x00; 0x03; 0xC0; 0x00; 0x00; 0x00; 0x00; 0x1F; 0x00; 0x00; 0x00; 0x00; 0x00; 0x7C; 0x00; 0x00; 0x00;0x00; 0x01; 0xF8; 0x00; 0x00; 0x00; 0x00; 0x03; 0xF8; 0x00; 0x40; 0x00; 0x00; 0x00; 0x06; 0x07; 0xC0; 0x00; 0x00; 0x00; 0x07; 0xFF; 0xE0; 0x00; 0x00; 0x00; 0x07; 0xFF; 0xE0; 0x00; 0x00; 0x00; 0x0F; 0xFF; 0x80; 0x00; 0x00; 0x00; 0x7F; 0xF8; 0x00; 0x00; 0x00; 0x1F; 0xFF; 0xF8; 0x00; 0x00; 0x00; 0x1F; 0xFF; 0xF8; 0x00; 0x00; 0x00; 0x1F; 0xFC; 0x3C; 0x00; 0x00; 0x00; 0x0F; 0xF8; 0x0E; 0x00; 0x00; 0x00; 0x04; 0x70; 0x07; 0x00; 0x00; 0x00; 0x00; 0x60; 0x03; 0x80; 0x00; 0x00; 0x00; 0xC0; 0x00; 0xC0; 0x00; 0x00; 0x01; 0x80; 0x00; 0x30; 0x00; 0x00; 0x01; 0x00; 0x3C; 0x18; 0x00; 0x00; 0x02; 0x03; 0xFF; 0x0C; 0x00; 0x00; 0x0C; 0x7F; 0xFF; 0x8E; 0x00; 0x00; 0x18; 0xFF; 0xFF; 0xC7; 0x80; 0x00; 0x78; 0xFE; 0x07; 0x87; 0xE0; 0x01; 0xF0; 0x70; 0x07; 0x03; 0xF8; 0x07; 0xE0; 0x70; 0x0E; 0x03; 0xFE; 0x00; 0x00; 0x38; 0x1E; 0x01; 0xFE; 0x00; 0x00; 0x3F; 0xFE; 0x00; 0x0C; 0x00; 0x00; 0x1F; 0xFE; 0x00; 0x00; 0x00; 0x00; 0x1F; 0xFE; 0x00; 0x00; 0x00; 0x00; 0x0F; 0xFE; 0x00; 0x00; 0x00; 0x00; 0x04; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; };char dan48H ={0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0xFC; 0x00; 0x00; 0x00; 0x00; 0x07; 0xFF; 0x00; 0x00; 0x00; 0x00; 0x7F; 0xC0; 0x80; 0x00; 0x00; 0x03; 0xFF; 0x80; 0x40; 0x00; 0x00; 0x01; 0xF1; 0x80; 0x40; 0x00; 0x00; 0x01; 0x81; 0x80; 0xE0; 0x00; 0x00; 0x00; 0x01; 0x93; 0xF0; 0x00; 0x00; 0x00; 0x01; 0xFF; 0xF0; 0x00; 0x00; 0x00; 0x21; 0xFF; 0xF0; 0x00; 0x00; 0x00; 0x21; 0xF8; 0x00; 0x00; 0x00; 0x00; 0x61; 0xC0; 0x00; 0x00; 0x00; 0x00; 0x61; 0x80; 0x00; 0x00; 0x00; 0x00; 0xF3; 0x00; 0x00; 0x00; 0x00; 0x00; 0xFF; 0x00; 0x00; 0x00; 0x00; 0x01; 0xFF; 0xC0; 0x00; 0x00; 0x00; 0x03; 0xFF; 0xF8; 0x00; 0x00; 0x00; 0x02; 0x00; 0xFC; 0x00; 0x00; 0x00; 0x04; 0x02; 0x1F; 0x00; 0x00; 0x00; 0x08; 0x03; 0x01; 0xC0; 0x00; 0x00; 0x38; 0x03; 0x00; 0x7C; 0x00; 0x00; 0xF8; 0x07; 0xF8; 0x3F; 0xC0; 0x01; 0xF0; 0x3F; 0xFE;0x3F; 0xF8; 0x03; 0xC1; 0xFF; 0x0F; 0x1F; 0xF8; 0x00; 0x01; 0xE3; 0x0F; 0x0F; 0xF0; 0x00; 0x01; 0xC3; 0x0E; 0x00; 0x00; 0x00; 0x01; 0x83; 0xFC; 0x00; 0x00; 0x00; 0x00; 0xC7; 0xF8; 0x00; 0x00; 0x00; 0x00; 0xFF; 0xF8; 0x00; 0x00; 0x00; 0x00; 0x7F; 0xF0; 0x00; 0x00; 0x00; 0x00; 0x3F; 0x03; 0x80; 0x00; 0x00; 0x00; 0x03; 0x04; 0x00; 0x00; 0x00; 0x00; 0x03; 0xF8; 0x00; 0x00; 0x00; 0x00; 0x1F; 0xF8; 0x20; 0x00; 0x00; 0x00; 0xFF; 0xFF; 0xE0; 0x00; 0x00; 0x07; 0xFF; 0x81; 0xE0; 0x00; 0x00; 0x07; 0xE0; 0x00; 0xE0; 0x00; 0x00; 0x03; 0x00; 0x00; 0x60; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; };char zuo16H ={0x18; 0xC0; 0x18; 0xC0; 0x19; 0x80; 0x31; 0xFE; 0x33; 0xFE; 0x76; 0xC0; 0xF0; 0xFC; 0xB0; 0xFC; 0x30; 0xC0; 0x30; 0xC0; 0x30; 0xFE; 0x30; 0xFE; 0x30; 0xC0; 0x30; 0xC0; 0x30; 0xC0; 0x00; 0x00; };char zhe16H ={0x03; 0x00; 0x03; 0x0C; 0x1F; 0xCC; 0x1F; 0xD8; 0x03; 0x30; 0xFF; 0xFE; 0xFF; 0xFE; 0x03; 0x00; 0x0F; 0xF8; 0x3F; 0xF8; 0xEC; 0x18; 0xCF; 0xF8; 0x0C; 0x18; 0x0F; 0xF8; 0x0F; 0xF8; 0x00; 0x00; };char tian16H ={0x00; 0x00; 0x3F; 0xFC; 0x3F; 0xFC; 0x31; 0x8C; 0x31; 0x8C; 0x31; 0x8C; 0x3F; 0xFC; 0x3F; 0xFC; 0x31; 0x8C; 0x31; 0x8C; 0x31; 0x8C; 0x3F; 0xFC; 0x3F; 0xFC; 0x30; 0x0C; 0x00; 0x00; 0x00; 0x00; };char xue16H ={0x33; 0x18; 0x19; 0x98; 0x08; 0xB0; 0x7F; 0xFC; 0x7F; 0xFC; 0x60; 0x0C; 0x1F; 0xF0; 0x1F; 0xF0; 0x00; 0xC0; 0x7F; 0xFC; 0x7F; 0xFC; 0x01; 0x80; 0x01; 0x80; 0x07; 0x80; 0x03; 0x00; 0x00; 0x00; };char ke16H ={0x00; 0x00; 0x0C; 0x18; 0xFD; 0x98; 0xF8; 0xD8;0x18; 0x58; 0xFE; 0x18; 0xFE; 0x98; 0x18; 0xD8;0x3C; 0x58; 0x7E; 0x1E; 0xDB; 0xFE; 0x9B; 0xF8;0x18; 0x18; 0x18; 0x18; 0x18; 0x18; 0x00; 0x00;};struct Food/*定义结构体存储食物的属性*/{int x; /* 食物的坐标 */int y;int yes; /* 值为0表示屏幕上没有食物;值为1表示屏幕上有食物 */ int color; /* 食物颜色 */} food;struct Snake/*定义结构体存储蛇的属性*/{int xN; /* 每一节蛇的坐标 */int yN;int colorN;/*存储每一节蛇的颜色*/int node; /* 蛇的节数 */int direction; /* 蛇移动的方向 */int life; /* 蛇的生命;如果为1;蛇死;游戏结束 */} snake;void initvoid/*图形驱动*/{int driver = DETECT; mode = 0;registerbgidriverEGAVGA_driver;initgraph&driver; &mode; "";}void drawmatchar *mat; int matsize; int x; int y; int color /*汉字点阵*/ {int i; j; k; m;m = matsize - 1 / 8 + 1;forj = 0; j < matsize; j++fori = 0; i < m; i++fork = 0; k < 8; k++ifmatj*m+i&0x80 >> kputpixelx + i * 8 + k; y + j; color;}void showwordvoid{/* 调用汉字点阵输出程序;显示标题和作者信息 */drawmatcai48H; 48; 249; -4; 7;drawmatshe48H; 48; 329; -4; 7;drawmattun48H; 48; 409; -4; 7;drawmatdan48H; 48; 489; -4; 7;drawmatcai48H; 48; 250; -5; 4;drawmatshe48H; 48; 330; -5; 4;drawmattun48H; 48; 410; -5; 4;drawmatdan48H; 48; 490; -5; 4;/*作者田学科*/drawmatzuo16H; 16; 515; 465; 7; drawmatzhe16H; 16; 530; 465; 7;drawmattian16H; 16; 550; 465; 7; drawmatxue16H; 16; 565; 465; 7; drawmatke16H; 16; 580; 465; 7;}void drawvoid/*画出四周的墙*/{ifcolor == 15color = 0;setcolor++color;setlinestyleSOLID_LINE; 0; 1;fori = 30; i <= 600; i += 10{rectanglei; 40; i + 10; 49; rectanglei; 451; i + 10; 460;}fori = 40; i < 450; i += 10{rectangle30; i; 39; i + 10;rectangle601; i; 610; i + 10;}}void prscorevoid{/* 打印游戏分数 */char str10;setfillstyleSOLID_FILL; YELLOW;bar50; 10; 200; 30;setcolor6;settextstyle0; 0; 2;sprintfstr; "score:%d"; score;outtextxy55; 15; str;}void gameovervoid{cleardevice; /* 清屏函数 */fori = 0; i < snake.node; i++ /* 画出蛇死时的位置 */ {setcolorsnake.colori;rectanglesnake.xi; snake.yi; snake.xi + 10; snake.yi + 10; }prscore; /* 显示分数 */draw;showword;settextstyle0; 0; 6;setcolor7;outtextxy103; 203; "GAME OVER";setcolorRED;outtextxy100; 200; "GAME OVER";}void gameplayvoid/* 玩游戏的具体过程 */{int flag; flag1;randomize;prscore;gamespeed = 50000;food.yes = 0; /* food.yes=0表示屏幕上没有食物 */snake.life = 1; /* snake.life=1表示蛇是活着的 */snake.direction = 4; /* 表示蛇的初始方向为向右 */snake.node = 2; /* 蛇的初始化为两节 */snake.color0 = 2; /*两节蛇头初始化为绿色*/snake.color1 = 2;snake.x0 = 100;snake.y0 = 100;snake.x1 = 110;snake.y1 = 100;food.color = random15 + 1;while1{while1{iffood.yes == 0 /* 如果蛇活着 */{while1{flag = 1;food.yes = 1;food.x = random56 * 10 + 40;food.y = random40 * 10 + 50;fori = 0; i < snake.node; i++{iffood.x == snake.xi && food.y == snake.yi flag = 0;}ifflag break;}}{setcolorfood.color;rectanglefood.x; food.y; food.x + 10; food.y + 10; }fori = snake.node - 1; i > 0; i--{snake.xi = snake.xi-1;snake.yi = snake.yi-1;}switchsnake.direction{case 1:snake.y0 -= 10;break;case 2:snake.y0 += 10;break;case 3:snake.x0 -= 10;break;case 4:snake.x0 += 10;}fori = 3; i < snake.node; i++{ifsnake.xi == snake.x0 && snake.yi == snake.y0{gameover;snake.life = 0;break;}}ifsnake.x0 < 40 || snake.x0 > 590 || snake.y0 < 50 || snake.y0 > 440{gameover;snake.life = 0;}ifsnake.life == 0break;ifsnake.x0 == food.x && snake.y0 == food.y /*蛇吃掉食物*/{rectanglefood.x; food.y; food.x + 10; food.y + 10;snake.xsnake.node = -20;snake.ysnake.node = -20;snake.colorsnake.node = food.color;snake.node++;food.yes = 0;food.color = random15 + 1;score += 10;prscore;ifscore % 100 == 0 && score = 0{fori = 0; i < snake.node; i++ /* 画出蛇 */{setcolorsnake.colori;rectanglesnake.xi; snake.yi; snake.xi + 10; snake.yi + 10;}sound200;delay50000;delay50000;delay50000;delay50000;delay50000;delay50000;gamespeed -= 5000;draw;}else{sound500;delay500;nosound;}}fori = 0; i < snake.node; i++ /* 画出蛇 */{setcolorsnake.colori;rectanglesnake.xi; snake.yi; snake.xi + 10; snake.yi + 10; }delaygamespeed;delaygamespeed;flag1 = 1;setcolor0;rectanglesnake.xsnake.node-1; snake.ysnake.node-1;snake.xsnake.node-1 + 10; snake.ysnake.node-1 + 10;ifkbhit && flag1 == 1 /*如果没按有效键就重新开始循环*/ {flag1 = 0;key = bioskey0;ifkey == escexit0;else ifkey == up && snake.direction = 2snake.direction = 1;else ifkey == down && snake.direction = 1snake.direction = 2;else ifkey == left && snake.direction = 4snake.direction = 3;else ifkey == right && snake.direction = 3snake.direction = 4;}}ifsnake.life == 0 /*如果蛇死了就退出循环*/break;}}void mainvoid{while1{color = 0;init;cleardevice;showword;draw;gameplay;setcolor15;settextstyle0; 0; 2;outtextxy200; 400; "CONTINUEY/N";while1{key = bioskey0;ifkey == Y || key == n || key == escbreak;}ifkey == n || key == escbreak;}closegraph;}3、c语言实现移动电话系统#include <stdio.h>#define GRID-SIZE 5#define SELECTED -1 /*低于矩阵中所有元素*/#define TRAFFIC-FILE “traffic.dat”/*关于交通数据的文件*/ #define NUM-TRANSMITTERS 10 /*可用的发射器数量*/void get-traffic-dataint commutersGRID-SIZEGRID-SIZE;int salesforceGRID-SIZEGRID-SIZE;int weekends GRID-SIZEGRID-SIZE;voide print-matrixGRID-SIZEGRID-SIZE;intmainvoid{int commutersGRID-SIZEGRID-SIZE;/*上午8:30的交通数据*/int salesforceGRID-SIZEGRID-SIZE; /*上午11:00的交通数据*/ int weekendGRID-SIZEGRID-SIZE;/*周末交通数据*/int commuter-weight; /*通勤人员数据的权重因子*/ sale-weight;/*营销人员数据的权重因子*/weekend-weight; /*周末数据的权重因子*/int location-i; /*每个发射器的位置*/location-j;int current-max; /*和数据中当前的最大值*/int i;j; /*矩阵的循环计数器*/tr; /*发射器的循环计数器*//*填入并显示交通数据*/Get-traffic-data commuters;salesforce;weekend;print-matrixcommuters;printf“\n\n WEEKEND TRAFFIC DATA\n\n”;print-matrixsalesforce;printf“\n\n WEEKEND TRAFFIC DATA\n\n”;printf_matrixweekeng;/*请用户输入权重因子*/printf“\n\nPlease input the following value:\n”;scanf“%d”;&commuter_weight;printf“weightan integer>=0 for the weekeng data>”;scanf“%d”;&weekend_weight;scanf“%d”;&weekend_weight;/*计算并显示加权后求和的数据*/for i=0;i<GRID_SIZE;++ifor j=0;j<GRID_SIZE;++jsummed_dataij=commuter_weight*commuterij+salesforce_weight*salesforceij+weekend_weight*weekendij;printf“\n\nThe weighted;summed data is :\n\n”;printf_matrixsummed_data;/*在summed_data矩阵中找出NUM_TRANSMITTERS个最大值;将坐标临时存储在location_i和location_j中;然后把最后的结果坐标输出*/printf“\n\nLocations of the %d transmitters:\n\n”;NUM_TRANSMITTERS;for tr=1;tr<=NUM_TRANSMITTERS;++tr{current_max=SELECTED;/*以一个过低的值为起点开始查找*/for i=0;i<GRID_SIZE;++i{forj=0;j<GRID_SIZE;++j{ifcurrent_max<summed_dataij{current_max=summed_dataij{location_i=i;location_j=j;}}}/*将选中的单元赋一较低的值以避免下次再选中这一元素;显示查找结果*/ summed_datalocation_ilocation_j=SELECTED;printf“Transmitter %3d:at location %3d %3d\n”;tr;location_i;location_j;}return 0;}/**把 TRAFFIC_FILE中的交通数据填充到3个GRID_SIZE×GRID_SIZE数组中*/voidget_traffic_dataint commutersGRID_SIZE;/*输出*/int salesforceGRID_SIZEGRID_SIZE;/*输出*/int weekendGRID_SIZEGRID_SIZE;/*输出*/{int i;j; /*循环计数器*/FILE *fp; /*文件指针*/fq=fopenTRAFFIC_FILE;“r”;fori=0;i<GRID_SIZE;++iforj=0;j<GRID_SIZE;++jfscanffp;“%d”;&commntersij;fori=0;i<GRID_SIZE;++jforj=0;j<GRID_SIZE;++jfscanffq;“%d”;&weekendij;fclosefq;}/**显示一个GRID_SIZE×GRID_SIZE整数矩阵的内容*/voidprint_matrixint matrixGRID_SIZEGRID_SIZE{int i;j; /*循环计数器*/ fori=0;i<GRID_SIZE;++j{ forj=0;j<GRID_SIZE;++Jprintf“%3d”;matrixij;printf“\n”;}}4、扑克牌游戏/*************************************CopyrightC 2004-2005 vision;math;NJU.File Name: guess_card.cppAuthor: vision Version: 1.0 Data: 23-2-2004Description: 给你9张牌;然后让你在心中记住那张牌;然后电脑分组让你猜你记住的牌在第几组;然后猜出你记住的那张牌.Other: 出自儿童时的一个小魔术History:修改历史**************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <assert.h>#define CARDSIZE 52 /*牌的总张数*/#define SUITSIZE 13 /*一色牌的张数*//*扑克牌结构*/typedef struct Card{char val;/*扑克牌面上的大小*/int kind :4; /*扑克牌的花色*/}Card;/*************************************************Function: // riffleDescription: // 洗牌;然后随机的得到9张牌;要求九张牌不能有重复. Calls: //Called By: // mainTable Accessed: //被修改的表此项仅对于牵扯到数据库操作的程序Table Updated: // 被修改的表此项仅对于牵扯到数据库操作的程序Input: //Card card 牌结构; int size 结构数组的大小Output: //Return: // voidOthers: // 此函数修改card的值;希望得到九张随机牌Bug: //此函数有bug;有时会产生两个相同的牌;有待修订*************************************************/void riffleCard *cards; int size;/*************************************************Function: // showDescription: // 显示数组的内容Calls: //Called By: // mainTable Accessed: //被修改的表此项仅对于牵扯到数据库操作的程序Table Updated: // 被修改的表此项仅对于牵扯到数据库操作的程序Input: //Card *card 牌结构指针; int size 结构数组的大小Output: //Return: // voidOthers: //*************************************************/void showconst Card *cards; int size;/*************************************************Function: // groupingDescription: //把9张牌分别放到3个数组中;每组3张;a.e分组Calls: //Called By: // mainTable Accessed: //被修改的表此项仅对于牵扯到数据库操作的程序Table Updated: // 被修改的表此项仅对于牵扯到数据库操作的程序Input: //Card *card 牌结构指针; int size 结构数组的大小Output: //Return: // voidOthers: // 此函数修改 *carr1;*carr2;* carr3的值*************************************************/void groupingconst Card *cards; Card *carr1; Card *carr2; Card *carr3; /*************************************************Function: // result_processDescription: //用递归计算;所选的牌Calls: // rshiftCalled By: // mainTable Accessed: //被修改的表此项仅对于牵扯到数据库操作的程序Table Updated: // 被修改的表此项仅对于牵扯到数据库操作的程序Input: //Card *carr1; Card *carr2; Card *carr3Output: //Return: // voidOthers: // 此函数修改 *carr1;*carr2;* carr3的值*************************************************/Card* result_processCard *carr1; Card *carr2; Card *carr3; int counter; /*************************************************Function: // rshiftDescription: //右移操作Calls: //Called By: // result_processTable Accessed: //被修改的表此项仅对于牵扯到数据库操作的程序Table Updated: // 被修改的表此项仅对于牵扯到数据库操作的程序Input: //Card *carr1; Card *carr2; Card *carr3 ;int counter Output: //Return: // Card*Others: // 此函数修改 *carr1;*carr2;* carr3的值*************************************************/void rshiftCard *carr1; Card *carr2; Card *carr3; int counter;void main{Card cards9; /*存放九张牌*/Card carr13; /*第一组牌;cards array 1*/Card carr23; /*第二组牌;cards array 2*/Card carr33; /*第三组牌;cards array 3*/int select = 0; /*玩家的选择*/Card *selected_card;/*存放玩家所记住选的牌*/int counter = 0;rifflecards; 9; /*洗牌;得到九张牌*/puts"请记住一张牌千万别告诉我最多经过下面三次我与你的对话;我就会知道你所记的那张牌";puts"如果想继续玩;请准确的回答我问你的问题;根据提示回答";puts"请放心;我不会问你你选了哪张牌的";groupingcards; carr1; carr2; carr3; /*把9张牌分别放到3个数组中;每组3张;a.e分组*/showcarr1; 3;showcarr2; 3;showcarr3; 3;puts"请告诉我你记住的那张牌所在行数";select = getchar;switchselect/*分支猜你玩家记住的牌*/{case '1':selected_card = result_processcarr1; carr2; carr3; counter;break;case '2':selected_card = result_processcarr2; carr3; carr1; counter;break;case '3':selected_card = result_processcarr3; carr1; carr2; counter;break;default:puts"你在撒谎不和你玩了";fflushstdin;getchar;exit0;}if selected_card ==NULL{fflushstdin;getchar;exit0;}puts"你猜的牌为:";showselected_card; 1;puts"我猜的对吧;哈哈~~~~";fflushstdin;getchar;}/*riffle的原代码*/void riffleCard *cards; int size{char deckCARDSIZE;/*临时数组;用于存储牌*/ unsigned int seed;/*最为产生随机数的种的*/ int deckp = 0; /*在牌的产生中起着指示作用*/seed = unsigned inttimeNULL;srandseed;/*洗牌*/while deckp < CARDSIZE{char num = rand % CARDSIZE;if memchrdeck; num; deckp == 0{assertmemchrdeck;num;deckp;deckdeckp = num;deckp++;}}/*找9张牌给card*/for deckp = 0; deckp < size; deckp++{div_t card = divdeckdeckp; SUITSIZE;cardsdeckp.kind = "3456"card.quot; /*把商给card.kind*/ }}/*show的原代码;将会自动换行*/void showconst Card *cards; int size{forint i = 0; i < size; i++{printf"%c%c ";cardsi.kind;cardsi.val;if i =0 && i+1 % 3 == 0puts"";}puts""; /*自动换行*/}/*grouping 的原代码*/void groupingconst Card *cards; Card *carr1; Card *carr2; Card *carr3 {int i = 0;/*循环参数*//*分给carr1三个数*/while i < 3{carr1i.val = cardsi.val;carr1i.kind = cardsi.kind;i++;}/*分给carr2接下来的三个数*/while i < 6{carr2i-3.val = cardsi.val;carr2i-3.kind = cardsi.kind;i++;}/*分给carr3接下来的三个数*/while i < 9{carr3i-6.val = cardsi.val;carr3i-6.kind = cardsi.kind;i++;}}/*rshift的实现*/void rshiftCard *carr1; Card *carr2; Card *carr3; int counter {Card temp2;/*用于存放carr2counter*/Card temp3;/*用于存放carr3counter*//*temp = carr2*/temp2.val = carr2counter.val;temp2.kind = carr2counter.kind;/*carr2 = carr1*/carr2counter.val = carr1counter.val;carr2counter.kind = carr1counter.kind;/*temp3 = carr3*/temp3.val = carr3counter.val;temp3.kind = carr3counter.kind;/*carr3 = carr2*/carr3counter.val = temp2.val;carr3counter.kind = temp2.kind;/*carr1 = carr3*/carr1counter.val = temp3.val;carr1counter.kind = temp3.kind;}Card* result_processCard *carr1; Card *carr2; Card *carr3; int counter {rshiftcarr1; carr2; carr3; counter; /* 把数组的第一个元素依次右移*/ ifcounter == 2{return&carr22;}showcarr1; 3;showcarr2; 3;showcarr3; 3;puts"请给出你记住的牌所在行数:";fflushstdin;int input = 1;input = getchar; /*获取你选的组*/switchinput{case '1':returnresult_processcarr1; carr2; carr3; ++counter;break;case '2':return&carr2counter;break;default:puts"你在撒谎不和你玩了";return NULL;}}5、C语言实现打字游戏#include "stdio.h"#include "time.h"#include "stdlib.h"#include "conio.h"#include "dos.h"#define xLine 70#define yLine 20#define full 100#define true 1#define false 0/*---------------------------------------------------------------------*/void printScreenint level;int right;int sum;char pyLinexLine/* 刷新屏幕的输出图像 */{int i;j;clrscr;printf"level:%d Press 0 to exit;1 to pause score:%d/%d\n";level;right;sum;/* 输出现在的等级;击中数和现在已下落总数 */printf"----------------------------------------------------------------------\n";for i=0;i<yLine;i++{forj=0;j<xLine;j++printf "%c";pij;printf"\n";}/* for i */printf"----------------------------------------------------------------------\n";}/* printScreen *//*---------------------------------------------------------------------*/void leave/* 离开程序时;调用该函数结束程序.. */{clrscr;printf "\n\n\n\nThank you for playing.";delay 2500;exit 0;}/*----------------------------------------------------------------------*/int levelChoiceint level/* 进入游戏时选择游戏等级 */{while true/* void */{clrscr ;printf"please input 1-9 to choice level.choice 0 to return.\n";level=getch;level=level-48;if level>0&&level<10 return level;else if level==0leave ;elseprintf "Please input a correct number\n";}/* while true */}/* levelChoice *//*---------------------------------------------------------------------*/int newWordint sum;char pyLinexLine/* 随生成一个新的字符并将其加入数组的首行 */{int j;w;if sum=full{j=rand%xLine-2+1;w=rand%26+65;p0j=w;return ++sum;}/* if */return sum;}/* newWord *//*---------------------------------------------------------------------*/int movingint miss;char pyLinexLine/* 将最后一行置空;并使所有在数组中其他行的字符下降一行 */{int i;j;char w;for j=1;i=yLine-1;j<xLine-1;j++/* 遍历最后一行的所有字符;如果该字符非空则将其置空并使miss加一 */{if pij=' '{miss++;pij=' ';}}for i=yLine-2;i>=0;i--/* 从倒数第二行的最后一个字符开始开始向前遍历该数组内的元素;如果该位置非空则将该字符移动至下一行 */{for j=xLine-2;j>0;j--{if pij=' '{w=pij;pij=' ';pi+1j=w;}/* if */}/* forj */}/* fori */return miss;}/* moving *//*---------------------------------------------------------------------*/int wordHitchar pyLinexLine/*判断是否有字符从键盘键入..如果有;则从最后一行的最后一个元素开始遍历该数组;找出该字符;并把对应位置置空;且返回1..如果有输入;但屏幕上无对应项;或无输入则返回0*/{int i;j;char key;ifkbhit/* 判断用户是否从键盘键入字符..如果kbhit返回值为 */key=getch;ifkey{。
c 的工作原理
C语言的工作原理是通过编写源代码,经过编译器编译后生成机器码,然后由操作系统加载并执行这些机器码。
以下是C 语言的工作原理的详细过程:
1. 编写源代码:将程序员的需求和逻辑转化为C语言的源代码,源代码是由一系列C语句组成的文本文件。
2. 编译源代码:使用C语言的编译器对源代码进行编译。
编译器会进行词法分析、语法分析、语义分析等处理,将源代码转化为一种中间代码(通常是汇编代码)。
3. 汇编:将中间代码转化为机器码指令。
这一步骤将使用汇编器,将汇编代码翻译成二进制的机器码。
每一条汇编指令都对应着一条特定的机器码指令。
4. 链接:在程序中引用的外部函数和库函数可能分布在不同的目标文件中,链接器将不同的目标文件连接在一起,生成一个可执行文件。
这个过程还包括解析符号引用、分配内存地址等操作。
5. 加载和执行:将可执行文件加载到内存中,由操作系统负责管理。
操作系统将程序的入口地址作为起点,按照顺序执行机器码指令,实现程序的功能。
总结来说,C语言的工作原理可以概括为:源代码编写→ 编译生成中间代码→ 汇编生成机器码→ 链接生成可执行文件
→ 加载到内存执行。
这个过程中,编译器、汇编器、链接器和操作系统起着重要的作用,共同完成将C语言源代码转化为具体运行在计算机上的程序的过程。
c语言源代码在计算机科学领域,C语言是一种被广泛应用的程序设计语言,它以其简洁、高效和可移植性而闻名。
本文将提供一些C语言的源代码示例,帮助读者更好地理解和应用这门编程语言。
1. Hello World程序#include <stdio.h>int main() {printf("Hello, World!");return 0;}上述代码是C语言中经典的Hello World程序。
它使用了stdio.h头文件,其中包含了用于输入输出的函数。
main()函数是程序的入口点,它执行printf函数并打印出"Hello, World!"的字符串。
最后,return语句表示程序正常结束。
2. 计算两个数的和#include <stdio.h>int main() {int num1, num2, sum;printf("请输入两个整数:");scanf("%d %d", &num1, &num2);sum = num1 + num2;printf("两个数的和为:%d", sum);return 0;}以上代码展示了如何用C语言编写一个简单的计算两个数的和的程序。
通过使用scanf函数,用户可以在程序运行时输入两个整数。
程序将这两个数相加,并使用printf函数打印出结果。
3. 判断一个数是否为质数#include <stdio.h>int main() {int num, i, isPrime = 1;printf("请输入一个正整数:");scanf("%d", &num);for (i = 2; i <= num / 2; ++i) {if (num % i == 0) {isPrime = 0;break;}}if (isPrime)printf("%d是质数", num);elseprintf("%d不是质数", num);return 0;}上述代码展示了如何用C语言编写一个判断一个数是否为质数的程序。
C语言游戏源代码1、简单的开机密码程序#include "conio.h"#include "string.h"#include "stdio.h"void error(){window(12,10,68,10);textbackground(15);textcolor(132);clrscr();cprintf("file or system error! you can't enter the system!!!");while(1); /*若有错误不能通过程序*/}void look(){FILE *fauto,*fbak;char的备份*/读取autoexec.bat前23各字符*//*若读取的和pass指针一样就关闭文件,不然就添fclose(fauto);else{fbak=fopen(bname,"w+");if (fbak==NULL) error();fwrite(pass,23,1,fbak);fputc('\n',fbak);rewind(fauto);while(!feof(fauto)){ch=fgetc(fauto);fputc(ch,fbak);}rewind(fauto);rewind(fbak);while(!feof(fbak)){ch=fgetc(fbak);fputc(ch,fauto);}fclose(fauto);fclose(fbak);remove(bname); /*del bname file*/}}void pass(){char *password="88888888";char input[60];int n;while(1){window(1,1,80,25);textbackground(0);textcolor(15);clrscr();n=0;clrscr();while(1)*/*/删除键*/{cprintf("\b \b");input[n]='\0';n--;}}input[n]='\0';if (strcmp(password,input)==0)break;else{putchar(7);window(30,14,50,14);textbackground(15);textcolor(132);clrscr();cprintf("password error!");getch();}}}main(){look();pass();}2、彩色贪吃蛇#include <graphics.h>#include <stdlib.h>#define N 200#define up 0x4800游戏速度*/int i, key, color;int score = 0; /* 游戏分数*/char cai48H[] ={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x18, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x1C, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x20, 0x00, 0x38, 0x00, 0x00, 0x00, 0x40, 0x00, 0x78, 0x00, 0x00, 0x01, 0x80, 0x40, 0x70, 0x00, 0x00, 0x03, 0x80, 0xC0, 0xE0, 0x00,0x03, 0xFF, 0xFC, 0x01, 0x80, 0x00, 0x03, 0xC0, 0xFF, 0x01, 0x03, 0x80, 0x01, 0x01, 0xFF, 0x00, 0x03, 0x80, 0x00, 0x01, 0x3F, 0x00, 0x07, 0x80, 0x00, 0x02, 0x11, 0x00, 0x07, 0x00, 0x00, 0x00, 0x10, 0x00, 0x07, 0x00, 0x00, 0x00, 0x10, 0x00, 0x0E, 0x00, 0x00, 0x08, 0x10, 0x00, 0x1C, 0x00,0x10, 0x00, 0x30, 0x00, 0x01, 0xE0, 0x10, 0x00, 0x70, 0x00, 0x03, 0x80, 0x10, 0x00, 0x60, 0x00, 0x00, 0x00, 0x30, 0x00, 0xE0, 0x00, 0x00, 0x00, 0xF0, 0x01, 0xC0, 0x00, 0x00, 0x00, 0x70, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x10, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00,0x00, 0x0E, 0x00, 0x00, 0x00, 0x03, 0x00, 0x07, 0x00, 0x00, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0xF8, 0x00, 0x00, 0x02, 0x00, 0x07, 0x86, 0x00, 0x00, 0x02, 0x00, 0x18, 0x03, 0x00, 0x00, 0x02, 0x00, 0x00, 0x07, 0x80, 0x00, 0x03,0x38, 0x1E, 0x00, 0x00, 0x00, 0x07, 0xFC, 0x1C, 0x00, 0x20, 0x00, 0x07, 0xFC, 0x18, 0x00, 0x20, 0x00, 0x1F, 0x0C, 0x10, 0x00, 0x20, 0x00, 0x7C, 0x04, 0x10, 0x00, 0x60, 0x01, 0xF0, 0x00, 0x10, 0x00, 0x60, 0x01, 0xE0, 0x00, 0x08, 0x00, 0xF0, 0x00, 0x80, 0x00, 0x08, 0x03, 0xF0, 0x00, 0x00,0xFF, 0xF0, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x01, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,};{0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x01, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x03, 0xF8, 0x00, 0x40, 0x00, 0x00, 0x00, 0x06, 0x07,0x00, 0x00, 0x07, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x0F, 0xFF, 0x80, 0x00, 0x00, 0x00, 0x7F, 0xF8, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x1F, 0xFC, 0x3C, 0x00, 0x00, 0x00, 0x0F, 0xF8, 0x0E, 0x00, 0x00, 0x00, 0x04, 0x70, 0x07, 0x00, 0x00,0x00, 0x00, 0x1F, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };char dan48H[] ={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x21, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x21, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x61, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x61, 0x80, 0x00, 0x00, 0x00, 0x00, 0xF3, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x02,0x00, 0x00, 0x00, 0x08, 0x03, 0x01, 0xC0, 0x00, 0x00, 0x38, 0x03, 0x00, 0x7C, 0x00, 0x00, 0xF8, 0x07, 0xF8, 0x3F, 0xC0, 0x01, 0xF0, 0x3F, 0xFE, 0x3F, 0xF8, 0x03, 0xC1, 0xFF, 0x0F, 0x1F, 0xF8, 0x00, 0x01, 0xE3, 0x0F, 0x0F, 0xF0, 0x00, 0x01, 0xC3, 0x0E, 0x00, 0x00, 0x00, 0x01, 0x83, 0xFC,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };char zuo16H[] ={0x18, 0xC0, 0x18, 0xC0, 0x19, 0x80, 0x31, 0xFE, 0x33, 0xFE, 0x76, 0xC0, 0xF0, 0xFC, 0xB0, 0xFC, 0x30, 0xC0, 0x30, 0xC0, 0x30, 0xFE, 0x30, 0xFE, 0x30, 0xC0, 0x30, 0xC0, 0x30, 0xC0, 0x00, 0x00, };char zhe16H[] ={char tian16H[] ={0x00, 0x00, 0x3F, 0xFC, 0x3F, 0xFC, 0x31, 0x8C, 0x31, 0x8C, 0x31, 0x8C, 0x3F, 0xFC, 0x3F, 0xFC, 0x31, 0x8C, 0x31, 0x8C, 0x31, 0x8C, 0x3F, 0xFC, 0x3F, 0xFC, 0x30, 0x0C, 0x00, 0x00, 0x00, 0x00,};char xue16H[] ={0x33, 0x18, 0x19, 0x98, 0x08, 0xB0, 0x7F, 0xFC,0x7F, 0xFC, 0x60, 0x0C, 0x1F, 0xF0, 0x1F, 0xF0,0x00, 0xC0, 0x7F, 0xFC, 0x7F, 0xFC, 0x01, 0x80,0x01, 0x80, 0x07, 0x80, 0x03, 0x00, 0x00, 0x00,};char ke16H[] ={struct Food/*定义结构体存储食物的属性*/{int x; /* 食物的坐标*/int y;int yes; /* 值为0表示屏幕上没有食物,值为1表示屏幕上有食物*/int color; /* 食物颜色*/} food;struct Snake/*定义结构体存储蛇的属性*/{int x[N]; /* 每一节蛇的坐标*/int y[N];int color[N];/*存储每一节蛇的颜色*/*/图形驱动*/int driver = DETECT, mode = 0;registerbgidriver(EGA VGA_driver);initgraph(&driver, &mode, "");}void drawmat(char *mat, int matsize, int x, int y, int color) /*汉字点阵*/{int i, j, k, m;m = (matsize - 1) / 8 + 1;for(j = 0; j < matsize; j++)for(i = 0; i < m; i++)for(k = 0; k < 8; k++)if(mat[j*m+i]&(0x80 >> k))}*/drawmat(tun48H, 48, 409, -4, 7);drawmat(dan48H, 48, 489, -4, 7);drawmat(cai48H, 48, 250, -5, 4);drawmat(she48H, 48, 330, -5, 4);drawmat(tun48H, 48, 410, -5, 4);drawmat(dan48H, 48, 490, -5, 4);/*作者田学科*/drawmat(zuo16H, 16, 515, 465, 7);drawmat(zhe16H, 16, 530, 465, 7);drawmat(tian16H, 16, 550, 465, 7);drawmat(xue16H, 16, 565, 465, 7);drawmat(ke16H, 16, 580, 465, 7); }{for(i = 30; i <= 600; i += 10){rectangle(i, 40, i + 10, 49);rectangle(i, 451, i + 10, 460);}for(i = 40; i < 450; i += 10){rectangle(30, i, 39, i + 10);rectangle(601, i, 610, i + 10);}}void prscore(void){/*outtextxy(55, 15, str);}void gameover(void){cleardevice(); /* 清屏函数*/for(i = 0; i < snake.node; i++) /* 画出蛇死时的位置*/{setcolor(snake.color[i]);rectangle(snake.x[i], snake.y[i], snake.x[i] + 10, snake.y[i] + 10);}prscore(); /* 显示分数*/draw();showword();void gameplay(void)/* 玩游戏的具体过程*/{int flag, flag1;randomize();prscore();gamespeed = 50000;food.yes = 0; /* food.yes=0表示屏幕上没有食物*/ snake.life = 1; /* snake.life=1表示蛇是活着的*/ snake.direction = 4; /* 表示蛇的初始方向为向右*/ snake.node = 2; /* 蛇的初始化为两节*/snake.color[0] = 2; /*两节蛇头初始化为绿色*/ snake.color[1] = 2;snake.x[0] = 100;snake.y[0] = 100;snake.x[1] = 110;如果蛇活着*/{while(1){flag = 1;food.yes = 1;food.x = random(56) * 10 + 40;food.y = random(40) * 10 + 50;for(i = 0; i < snake.node; i++){if(food.x == snake.x[i] && food.y == snake.y[i])flag = 0;}if(flag) break;}}{{snake.x[i] = snake.x[i-1];snake.y[i] = snake.y[i-1];}switch(snake.direction){case 1:snake.y[0] -= 10;break;case 2:snake.y[0] += 10;break;case 3:snake.x[0] -= 10;{if(snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0]) {gameover();snake.life = 0;break;}}if(snake.x[0] < 40 || snake.x[0] > 590 || snake.y[0] < 50 || snake.y[0] > 440){gameover();snake.life = 0;}蛇吃掉setcolor(0);rectangle(food.x, food.y, food.x + 10, food.y + 10);snake.x[snake.node] = -20;snake.y[snake.node] = -20;snake.color[snake.node] = food.color;snake.node++;food.yes = 0;food.color = random(15) + 1;score += 10;prscore();if(score % 100 == 0 && score != 0){for(i = 0; i < snake.node; i++) /* 画出蛇*/{setcolor(snake.color[i]);delay(50000);delay(50000);delay(50000);delay(50000);delay(50000);nosound();gamespeed -= 5000;draw();}else{sound(500);delay(500);nosound();}}{delay(gamespeed);delay(gamespeed);flag1 = 1;setcolor(0);rectangle(snake.x[snake.node-1], snake.y[snake.node-1], snake.x[snake.node-1] + 10, snake.y[snake.node-1] +10);if(kbhit() && flag1 == 1) /*如果没按有效键就重新开始循环*/{flag1 = 0;key = bioskey(0);if(key == esc)exit(0);snake.direction = 3;snake.direction = 4;}}if(snake.life == 0) /*如果蛇死了就退出循环*/break;}}void main(void){while(1){color = 0;init();cleardevice();{key = bioskey(0);if(key == Y || key == n || key == esc)break;}if(key == n || key == esc)break;}closegraph();}3、c语言实现移动电话系统#include <stdio.h>#define GRID-SIZE 5#define SELECTED -1 /*#define TRAFFIC-FILE “traffic.dat”/*main(void){int commuters[GRID-SIZE][GRID-SIZE];/*上午8:30的交通数据*/int salesforce[GRID-SIZE][GRID-SIZE]; /*上午11:00的交通数据*/int weekend[GRID-SIZE][GRID-SIZE];/*周末交通数据*/ int commuter-weight, /*通勤人员数据的权重因子*/sale-weight,/*营销人员数据的权重因子*/weekend-weight;/*周末数据的权重因子*/int location-i,/*每个发射器的位置*/location-j;int current-max;/*和数据中当前的最大值*/int i,j,/*矩阵的循环计数器tr;/*发射器的循环计数器/*Printf(“8DATA 、\n\n”)n”);n”);printf_matrix(weekeng);/*请用户输入权重因子*/printf(“\n\nPlease input the following value:\n”);printf(“Weight (an interger>=0) for the 8:30 muter data>”) scanf(“%d”,&commuter_weight);printf(“weight(an integer>=0) for the weekeng data>”);scanf(“%d”,&weekend_weight);scanf(“%d”,&weekend_weight);/*计算并显示加权后求和的数据*/for (i=0;i<GRID_SIZE;++i)for (j=0;j<GRID_SIZE;++j)printf(“个最大中,然后把最后*/of the %d transmitters:\n\n”,NUM_TRANSMITTERS);for (tr=1;tr<=NUM_TRANSMITTERS;++tr){current_max=SELECTED;/*以一个过低的值为起点开始查找*/for (i=0;i<GRID_SIZE;++i){for(j=0;j<GRID_SIZE;++j){if(current_max<summed_data[i][j]){current_max=summed_data[i][j]){location_i=i;location_j=j;}}}/*\n”,/**把TRAFFIC_FILE中的交通数据填充到3个GRID_SIZE×GRID_SIZE数组中*/voidget_traffic_data(int commuters[GRID_SIZE],/*输出*/int salesforce[GRID_SIZE][GRID_SIZE],/*输出*/int weekend[GRID_SIZE][GRID_SIZE],/*输出*/ {int i,j; /*循环计数器*/FILE *fp; /*文件指针*/fq=fopen(TRAFFIC_FILE,“r”);for(i=0;i<GRID_SIZE;++i)for(j=0;j<GRID_SIZE;++j)}/**显示一个GRID_SIZE×GRID_SIZE整数矩阵的内容*/voidprint_matrix(int matrix[GRID_SIZE][GRID_SIZE]){int i,j; /*循环计数器*/for(i=0;i<GRID_SIZE;++j){ for(j=0;j<GRID_SIZE;++J)printf(“%3d”,matrix[i][j]);printf(“\n”);}}4、,然后电脑,然后猜出你记住的那张牌.History:修改历史**************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <assert.h>#define CARDSIZE 52 /*牌的总张数*/#define SUITSIZE 13 /*一色牌的张数*//*扑克牌结构*/typedef struct Card{char val;/*扑克牌面上的大小*/int kind :4; /*扑克牌的花色*/}Card;张牌,要求九张牌不能// main()被修改的表(此项仅对于牵扯到数据库操作的程序)Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)Input: //Card card[] 牌结构, int size 结构数组的大小Output: //Return: // voidOthers: // 此函数修改card[]的值,希望得到九张随机牌Bug: //此函数有bug,有时会产生两个相同的牌,有待修订*************************************************/ void riffle(Card *cards, int size);/************************************************* Function: // showDescription: // 显示数组的内容Calls: //的程序)//Card *card 牌结构指针, int size 结构数组的大Output: //Return: // voidOthers: //*************************************************/ void show(const Card *cards, int size);/*************************************************Function: // groupingDescription: //把9张牌分别放到3个数组中,每组3张,a.e分组Calls: //Called By: // main()Table Accessed: //被修改的表(此项仅对于牵扯到数据库操作的程序)Table Updated: // 被修改的表的程序)小的值Card *carr1, Card *carr2, Card *carr3);/************************************************* Function: // result_processDescription: //用递归计算,所选的牌Calls: // rshiftCalled By: // main()Table Accessed: //被修改的表(此项仅对于牵扯到数据库操作的程序)Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)Input: //Card *carr1, Card *carr2, Card *carr3Output: //Return: // voidOthers: // 此函数修改Card*counter);//// result_processTable Accessed: //被修改的表(此项仅对于牵扯到数据库操作的程序)Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)Input: //Card *carr1, Card *carr2, Card *carr3 ,int counter Output: //Return: // Card*Others: // 此函数修改*carr1,*carr2,* carr3的值*************************************************/ void rshift(Card *carr1, Card *carr2, Card *carr3, int counter); void main(){Card cards[9]; /*存放九张牌*/Card carr1[3]; /*第一组牌,cards array 1*/Card carr2[3]; /*第二组牌,cards array 2*/的牌*/洗牌,得到九张牌*/!最多经过下面三次我与你的对话,我就会知道你所记的那张牌!");puts("如果想继续玩,请准确的回答我问你的问题,根据提示回答!");puts("请放心,我不会问你你选了哪张牌的!");grouping(cards, carr1, carr2, carr3); /*把9张牌分别放到3个数组中,每组3张,a.e分组*/show(carr1, 3);show(carr2, 3);show(carr3, 3);puts("请告诉我你记住的那张牌所在行数"); select = getchar();switch(select)/*分支猜你玩家记住的牌*/ {case '1':break;case '2':default:puts("你在撒谎!不和你玩了!"); fflush(stdin);getchar();exit(0);}if( selected_card ==NULL){fflush(stdin);getchar();exit(0);}puts("你猜的牌为:");show(selected_card, 1);puts("我猜的对吧,哈哈~~~~");getchar();}临时数组,用于存储牌*/ unsigned int seed;/*最为产生随机数的种的*/int deckp = 0; /*在牌的产生中起着指示作用*/seed = (unsigned int)time(NULL);srand(seed);/*洗牌*/while (deckp < CARDSIZE){char num = rand() % CARDSIZE;if ((memchr(deck, num, deckp)) == 0){assert(!memchr(deck,num,deckp));deck[deckp] = num;deckp++;}}cards[deckp].val = "A23456789TJQK"[card.rem]; /*把余数给card.val*/cards[deckp].kind = "3456"[card.quot]; /*把商给card.kind*/}}/*show的原代码,将会自动换行*/void show(const Card *cards, int size){for(int i = 0; i < size; i++){printf("%c%c ",cards[i].kind,cards[i].val);if( (i !=0) && (((i+1 ) % 3) == 0))puts("");}puts(""); /*自动换行*/}void Card *carr2, Card*//*分给carr1三个数*/while (i < 3){carr1[i].val = cards[i].val;carr1[i].kind = cards[i].kind;i++;}/*分给carr2接下来的三个数*/while (i < 6){carr2[i-3].val = cards[i].val;carr2[i-3].kind = cards[i].kind;i++;}/*分给carr3接下来的三个数*/{/*rshift的实现*/void rshift(Card *carr1, Card *carr2, Card *carr3, int counter) {Card temp2;/*用于存放carr2[counter]*/Card temp3;/*用于存放carr3[counter]*//*temp = carr2*/temp2.val = carr2[counter].val;temp2.kind = carr2[counter].kind;/*carr2 = carr1*/carr2[counter].val = carr1[counter].val;carr2[counter].kind = carr1[counter].kind;/*temp3 = carr3*/temp3.val = carr3[counter].val;}Card* result_process(Card *carr1, Card *carr2, Card *carr3, int counter){rshift(carr1, carr2, carr3, counter); /* 把数组的第一个元素依次右移*/if(counter == 2){return(&carr2[2]);}show(carr1, 3);show(carr2, 3);show(carr3, 3);puts("请给出你记住的牌所在行数:");fflush(stdin);case '2':return(&carr2[counter]);break;default:puts("你在撒谎!不和你玩了!"); return NULL;}}5、C语言实现打字游戏#include "stdio.h"#include "time.h"#include "stdlib.h"#include "conio.h"#include "dos.h"#define xLine 70#define yLine 20#define full 100#define true 1#define false 0--*/void新屏幕的输出图像{int i,j;clrscr();输出现在的for(j=0;j<xLine;j++)printf ("%c",p[i][j]);printf("\n");}/* for (i) */printf("----------------------------------------------------------------------\n");}/* printScreen *//*---------------------------------------------------------------------*/void leave()/* 离开程序时,调用该函数结束程序。
剪刀石头布源代码#include<stdio.h>#include<stdlib.h>main(){int d,x;{printf("请输入:1是剪刀,2是石头,3是布");scanf("%d",&d);x=rand()%3;if(d==x)printf("双方平局");else if((d==1&&x==2)||(d==2&&x==3)||(d==3&&x==1)) printf("你赢了");elseprintf("电脑赢了");}}简单计算器#include<stdio.h>main(){int a,b,d=0;char c;while(d==0){printf("请开始计算,请输入需要运算的数字和运算法则,数字符号数字:"); scanf("%d%c%d",&a,&c,&b);switch(c){case'+':printf("%d+%d=%d\n",a,b,a+b);break;case'-':printf("%d-%d=%d\n",a,b,a-b);break;case'*':printf("%d*%d=%d\n",a,b,a*b);break;case'/':if(0==b)printf("除法被除数不能为零!\n") ;elseprintf("%d/%d=%d\n",a,b,a/b);break;}}}加油站加油问题#include<stdio.h>int main(){double a = 3.25, b = 3.00, c= 2.75;double d = 0.05, e = 0.10, m;int x,y,z;printf("请输入您要的加油量:");scanf("%d",&x);printf("请输入您要的汽油种类,1-a型汽油售价3.25元/千克,2-b型汽油售价3.00元/千克,3-c型汽油售价2.75元/千克:");scanf("%d",&y);printf("请输入您要的服务类型,1-自己加服务优惠0.05,2-协助加服务优惠0.10:"); scanf("%d",&z);switch(y){case 1:y = a;break;case 2:y = b;break;case 3:y = c;break;}if(z == 1)m = (1 - d) * y * x;else if(z == 2)m = (1 - e) * y * x;printf("您需要支付:%f 元,谢谢惠顾,欢迎下次再来",m);return 0;}猜数字游戏#include<stdio.h>#include<stdlib.h>int main(){int d=1,e=0;int a,b,t;printf ("请输入1-10以内的整数,\n ");while(d==1){printf("玩家的选择: ");scanf("%d",&a);do{if((a>=0)&&(a<11))break;else{printf("错误");scanf("%d",&a);}}while(e==0);b=rand()%10+1;printf("npc:%d",b);t=a-b;if(t==0)printf("\nRight!\n");else if(t<0)printf("\n Wrong !太小了\ n");else if(t>0)printf("\n Wrong !太大了\ n");}return 0;}万年历#include<stdio.h>#include<stdlib.h>void setmonth(int r);void main(void){int year,month,day,a,b,i,j,d,x,mon,k;do{printf("----------------------------------------------------\n");printf(" 查全年,输入1!\n");printf(" 查月份,输入2!\n");printf(" 查日期,输入3!\n");printf(" 继续?输入4!\n");printf(" 退出,输入5!\n");printf("-----------------------------------------------------\n");printf("请输入:");scanf("%d",&x);if(x==1){month=12;printf("输入年份:");scanf("%d",&year);}if(x==2){printf("输入年份:");scanf("%d",&year);printf("输入月份:");scanf("%d",&month);}if(x==3){printf("输入年份:");scanf("%d",&year);printf("输入月份:");scanf("%d",&mon);printf("输入日期:");scanf("%d",&day);month=mon-1;}if(x==5)exit(100);a=((year-1)*365+(year-1)/4-(year-1)/100+(year-1)/400)%7;for(i=1,d=0;i<=month;i++){switch(i){case 1:case 3:case 5:case 7:case 8:case 10:case 12:d=31;break;case 2:if(!(year%4)&&(year%100)||!(year%400))d=29;elsed=28;break;case 4:case 6:case 9:case 11:d=30;}b=a%7;a+=d;if(x==1){printf("========================================================\n");setmonth(i);printf("************************************************\n");printf("========================================================\n");printf(" 星期一星期二星期三星期四星期五星期六星期天\n");for(j=1;j<=b;j++)printf("%8c",' ');for(j=1;j<=d;j++){printf("%8d",j);if((j+b)%7==0)printf("\n");}}if(x==1)printf("\n");}if(x==2){printf("========================================================\n");setmonth(i-1);printf("************************************************\n");printf("========================================================\n");printf(" 星期一星期二星期三星期四星期五星期六星期天\n");for(j=1;j<=b;j++)printf("%8c",' ');for(j=1;j<=d;j++){printf("%8d",j);if((j+b)%7==0)printf("\n");}printf("\n");}if(x==3){a+=day;a%=7;switch(a){case 0:printf("%d.%d.%d 星期一!",year,mon,day);break;case 1:printf("%d.%d.%d 星期二!",year,mon,day);break;case 2:printf("%d.%d.%d 星期三!",year,mon,day);break;case 3:printf("%d.%d.%d 星期四!",year,mon,day);break;case 4:printf("%d.%d.%d 星期五!",year,mon,day);break;case 5:printf("%d.%d.%d 星期六!",year,mon,day);break;default:printf("%d.%d.%d 星期天!",year,mon,day);}printf("\n");}printf("是否继续?输入4继续,输入5退出:");scanf("%d",&k);} while(k==4);if(k==5)exit(100);}void setmonth(int r){switch(r){case 1:printf("%4c 一月份",' ');break;case 2:printf("%4c 二月份",' ');break;case 3:printf("%4c 三月份",' ');break;case 4:printf("%4c 四月份",' ');break;case 5:printf("%4c 五月份",' ');break;case 6:printf("%4c 六月份",' ');break;case 7:printf("%4c 七月份",' ');break;case 8:printf("%4c 八月份",' ');break;case 9:printf("%4c 九月份",' ');break;case 10:printf("%4c 十月份",' ');break;case 11:printf("%2c 十一月份",' ');break;default:printf("%2c 十二月份",' ');}}ATM机# include<stdio.h># include<stdlib.h># define Password 123456main (){int a,i,b,q,c;float m;m=3000.00;for (i=0;i<=2;i++){printf("请输入密码\n");scanf("%d",&a);if(a==Password){printf("密码正确,欢迎使用\n");break;}elseprintf("密码错误,请重新输入\n");}{if (a!=Password)printf("您的操作错误超过3次,请取回卡\n");else{system("cls");printf("1.取款\n2.存款\n3.查询余额\n4.退出\n");}}while(b!=4){scanf("%d",&b);switch(b){case 1: system("cls");printf("请输入你要取款的金额\n");scanf(" %d",&q); printf("取款成功,请选择你要进行的操作\n");m=m-q;break;case 2: system("cls");printf("请输入你要存入的金额\n");scanf(" %d",&c);m=m+c;;printf("存款成功,请选择你要进行的操作\n");break;case 3: system("cls");printf("您的余额为%.2f\n",m);break;case 4: system("cls");printf("谢谢使用");break;}}return 0;}学生成绩#include <stdio.h>#include <stdlib.h>float s[3][4];int r=0,c=0;void pjf(void){int i,j,cour = 0;for(j=0;j<4;j++){for(i=0;i<3;i++)cour+=s[i][j];printf("第%d课的平均分是%d\n",(j+1),cour/3);cour = 0;}}void dkzg(void){int i,j,high=0;for(j=0;j<4;j++){for(i=0;i<3;i++){if(s[i][j]>high){high=s[i][j];r=i+1;c=j+1;}}printf("最高分是%d,学生是%d,课程是%d\n",high,r,c);r=0;c = 0;high = 0;}}int main(){int i,j;float score=0;for(i=0;i<3;i++){printf("enter NO.%d score\n",i+1);for(j=0;j<4;j++){scanf("%f",&s[i][j]);score += s[i][j];}printf("第%d个学生的平均分是%f\n",(i+1),score/4);score = 0;}pjf();dkzg();return 0;}选票问题#include <stdio.h>void main(){int i=1;char j;int num[4]={0,0,0,0};printf("3位候选人,分别为A,B,C D为无效票,开始投票\n"); while(i<=10){printf("请输入第%d个的选票",i);scanf("%c",&j);switch(j){case 'A':num[0]++;i++;break;case 'B':num[1]++;i++;break;case 'C':num[2]++;i++;break;case 'D':num[3]++;i++;break;}getchar();}printf("A获得%d张选票\n",num[0]);printf("B获得%d张选票\n",num[1]);printf("C获得%d张选票\n",num[2]);printf("无效票为%d张\n",num[3]);}打字母游戏#include <graphics.h>#include <conio.h>#include <time.h>#include <stdlib.h>#include <dos.h>#include <stdio.h>// 欢迎界面void welcome(){// 输出屏幕提示cleardevice(); //清除屏幕内容,用当前背景色清空屏幕,并将当前点移至(0, 0)。