编程论坛
400万+工程师在用
华为|鸿蒙开发者日
直播报名
400万+工程师在用
华为|鸿蒙开发者日
直播报名

电子发烧友网工程师

17年用户 16436经验值
擅长:可编程逻辑 MEMS/传感技术 测量仪表 模拟技术 控制/MCU RF/无线
私信 关注

经典c程序100例(91-100)

2008-10-14 13:12

<p><font face="Verdana">经典c程序100例(91-100)</font><br/></p>
<p><span class="STYLE23"><span class="STYLE13">【程序91】</span><br/>题目:时间函数举例1<br/>1.程序分析:<br/>2.程序源代码:<br/>#include "stdio.h"<br/>#include "time.h"<br/>void main()<br/>{ time_t lt; /*define a longint time varible*/<br/>lt=time(NULL);/*system time and date*/<br/>printf(ctime(&lt;)); /*english format output*/<br/>printf(asctime(localtime(&lt;)));/*tranfer to tm*/<br/>printf(asctime(gmtime(&lt;))); /*tranfer to Greenwich time*/<br/>}<br/><span class="STYLE13">==============================================================<br/>【程序92】</span><br/>题目:时间函数举例2<br/>1.程序分析:           <br/>2.程序源代码:<br/>/*calculate time*/<br/>#include "time.h"<br/>#include "stdio.h"<br/>main()<br/>{ time_t start,end;<br/>int i;<br/>start=time(NULL);<br/>for(i=0;i&lt;3000;i++)<br/>{ printf("\1\1\1\1\1\1\1\1\1\1\n");}<br/>end=time(NULL);<br/>printf("\1: The dIFferent is %6.3f\n",difftime(end,start));<br/>}<br/><span class="STYLE13">==============================================================<br/>【程序93】</span><br/>题目:时间函数举例3<br/>1.程序分析:<br/>2.程序源代码:<br/>/*calculate time*/<br/>#include "time.h"<br/>#include "stdio.h"<br/>main()<br/>{ clock_t start,end;<br/>int i;<br/>double var;<br/>start=clock();<br/>for(i=0;i&lt;10000;i++)<br/>{ printf("\1\1\1\1\1\1\1\1\1\1\n");}<br/>end=clock();<br/>printf("\1: The different is %6.3f\n",(double)(end-start));<br/>}<br/><span class="STYLE13">==============================================================<br/>【程序94】</span><br/>题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的)<br/>1.程序分析:<br/>2.程序源代码:<br/>#include "time.h"<br/>#include "stdlib.h"<br/>#include "stdio.h"<br/>main()<br/>{char c;<br/>clock_t start,end;<br/>time_t a,b;<br/>double var;<br/>int i,guess;<br/>srand(time(NULL));<br/>printf("do you want to play it.('y' or 'n') \n");<br/>loop:<br/>while((c=getchar())=='y')<br/>{<br/>i=rand()%100;<br/>printf("\nplease input number you guess:\n");<br/>start=clock();<br/>a=time(NULL);<br/>scanf("%d",&amp;guess);<br/>while(guess!=i)<br/>{if(guess&gt;i)<br/>{printf("please input a little smaller.\n");<br/>scanf("%d",&amp;guess);}<br/>else<br/>{printf("please input a little bigger.\n");<br/>scanf("%d",&amp;guess);}<br/>}<br/>end=clock();<br/>b=time(NULL);<br/>printf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2);<br/>printf("\1: it took you %6.3f seconds\n\n",difftime(b,a));<br/>if(var&lt;15)<br/>printf("\1\1 You are very clever! \1\1\n\n");<br/>else if(var&lt;25)<br/>printf("\1\1 you are normal! \1\1\n\n");<br/>else<br/>printf("\1\1 you are stupid! \1\1\n\n");<br/>printf("\1\1 Congradulations \1\1\n\n");<br/>printf("The number you guess is %d",i);<br/>}<br/>printf("\ndo you want to try it again?(\"yy-:special:1:-.or.-:special:1:-n-:special:1:-)\n");<br/>if((c=getch())=='y')<br/>goto loop;<br/>}<br/><span class="STYLE13">==============================================================<br/>【程序95】</span><br/>题目:家庭财务管理小程序<br/>1.程序分析:<br/>2.程序源代码:<br/>/*money management system*/<br/>#include "stdio.h"<br/>#include "dos.h"<br/>main()<br/>{<br/>FILE *fp;<br/>struct date d;<br/>float sum,chm=0.0;<br/>int len,i,j=0;<br/>int c;<br/>char ch[4]="",ch1[16]="",chtime[12]="",chshop[16],chmoney[8];<br/>pp: clrscr();<br/>sum=0.0;<br/>gotoxy(1,1);printf("|---------------------------------------------------------------------------|");<br/>gotoxy(1,2);printf("| money management system(C1.0) 2000.03 |");<br/>gotoxy(1,3);printf("|---------------------------------------------------------------------------|");<br/>gotoxy(1,4);printf("| -- money records -- | -- today cost list -- |");<br/>gotoxy(1,5);printf("| ------------------------ |-------------------------------------|");<br/>gotoxy(1,6);printf("| date: -------------- | |");<br/>gotoxy(1,7);printf("| | | | |");<br/>gotoxy(1,8);printf("| -------------- | |");<br/>gotoxy(1,9);printf("| thgs: ------------------ | |");<br/>gotoxy(1,10);printf("| | | | |");<br/>gotoxy(1,11);printf("| ------------------ | |");<br/>gotoxy(1,12);printf("| cost: ---------- | |");<br/>gotoxy(1,13);printf("| | | | |");<br/>gotoxy(1,14);printf("| ---------- | |");<br/>gotoxy(1,15);printf("| | |");<br/>gotoxy(1,16);printf("| | |");<br/>gotoxy(1,17);printf("| | |");<br/>gotoxy(1,18);printf("| | |");<br/>gotoxy(1,19);printf("| | |");<br/>gotoxy(1,20);printf("| | |");<br/>gotoxy(1,21);printf("| | |");<br/>gotoxy(1,22);printf("| | |");<br/>gotoxy(1,23);printf("|---------------------------------------------------------------------------|");<br/>i=0;<br/>getdate(&amp;d);<br/>sprintf(chtime,"%4d.%02d.%02d",d.da_year,d.da_mon,d.da_day);<br/>for(;;)<br/>{<br/>gotoxy(3,24);printf(" Tab __browse cost list Esc __quit");<br/>gotoxy(13,10);printf(" ");<br/>gotoxy(13,13);printf(" ");<br/>gotoxy(13,7);printf("%s",chtime);<br/>j=18;<br/>ch[0]=getch();<br/>if(ch[0]==27)<br/>break;<br/>strcpy(chshop,"");<br/>strcpy(chmoney,"");<br/>if(ch[0]==9)<br/>{<br/>mm:i=0;<br/>fp=fopen("home.dat","r+");<br/>gotoxy(3,24);printf(" ");<br/>gotoxy(6,4);printf(" list records ");<br/>gotoxy(1,5);printf("|-------------------------------------|");<br/>gotoxy(41,4);printf(" ");<br/>gotoxy(41,5);printf(" |");<br/>while(fscanf(fp,"%10s%14s%f\n",chtime,chshop,&amp;chm)!=EOF)<br/>{ if(i==36)<br/>{ getch();<br/>i=0;}<br/>if ((i%36)&lt;17)<br/>{ gotoxy(4,6+i);<br/>printf(" ");<br/>gotoxy(4,6+i);}<br/>else<br/>if((i%36)&gt;16)<br/>{ gotoxy(41,4+i-17);<br/>printf(" ");<br/>gotoxy(42,4+i-17);}<br/>i++;<br/>sum=sum+chm;<br/>printf("%10s %-14s %6.1f\n",chtime,chshop,chm);}<br/>gotoxy(1,23);printf("|---------------------------------------------------------------------------|");<br/>gotoxy(1,24);printf("| |");<br/>gotoxy(1,25);printf("|---------------------------------------------------------------------------|");<br/>gotoxy(10,24);printf("total is %8.1f$",sum);<br/>fclose(fp);<br/>gotoxy(49,24);printf("press any key to.....");getch();goto pp;<br/>}<br/>else<br/>{<br/>while(ch[0]!='\r')<br/>{ if(j&lt;10)<br/>{ strncat(chtime,ch,1);<br/>j++;}<br/>if(ch[0]==8)<br/>{<br/>len=strlen(chtime)-1;<br/>if(j&gt;15)<br/>{ len=len+1; j=11;}<br/>strcpy(ch1,"");<br/>j=j-2;<br/>strncat(ch1,chtime,len);<br/>strcpy(chtime,"");<br/>strncat(chtime,ch1,len-1);<br/>gotoxy(13,7);printf(" ");}<br/>gotoxy(13,7);printf("%s",chtime);ch[0]=getch();<br/>if(ch[0]==9)<br/>goto mm;<br/>if(ch[0]==27)<br/>exit(1);<br/>}<br/>gotoxy(3,24);printf(" ");<br/>gotoxy(13,10);<br/>j=0;<br/>ch[0]=getch();<br/>while(ch[0]!='\r')<br/>{ if (j&lt;14)<br/>{ strncat(chshop,ch,1);<br/>j++;}<br/>if(ch[0]==8)<br/>{ len=strlen(chshop)-1;<br/>strcpy(ch1,"");<br/>j=j-2;<br/>strncat(ch1,chshop,len);<br/>strcpy(chshop,"");<br/>strncat(chshop,ch1,len-1);<br/>gotoxy(13,10);printf(" ");}<br/>gotoxy(13,10);printf("%s",chshop);ch[0]=getch();}<br/>gotoxy(13,13);<br/>j=0;<br/>ch[0]=getch();<br/>while(ch[0]!='\r')<br/>{ if (j&lt;6)<br/>{ strncat(chmoney,ch,1);<br/>j++;}<br/>if(ch[0]==8)<br/>{ len=strlen(chmoney)-1;<br/>strcpy(ch1,"");<br/>j=j-2;<br/>strncat(ch1,chmoney,len);<br/>strcpy(chmoney,"");<br/>strncat(chmoney,ch1,len-1);<br/>gotoxy(13,13);printf(" ");}<br/>gotoxy(13,13);printf("%s",chmoney);ch[0]=getch();}<br/>if((strlen(chshop)==0)||(strlen(chmoney)==0))<br/>continue;<br/>if((fp=fopen("home.dat","a+"))!=NULL);<br/>fprintf(fp,"%10s%14s%6s",chtime,chshop,chmoney);<br/>fputc('\n',fp);<br/>fclose(fp);<br/>i++;<br/>gotoxy(41,5+i);<br/>printf("%10s %-14s %-6s",chtime,chshop,chmoney);<br/>}}} </span><br/></p>
<p><span class="STYLE23"><span class="STYLE13">【程序96】</span><br/>题目:计算字符串中子串出现的次数<br/>1.程序分析:<br/>2.程序源代码:<br/>#include "string.h"<br/>#include "stdio.h"<br/>main()<br/>{ char str1[20],str2[20],*p1,*p2;<br/>int sum=0;<br/>printf("please input two strings\n");<br/>scanf("%s%s",str1,str2);<br/>p1=str1;p2=str2;<br/>while(*p1!='\0')<br/>{<br/>if(*p1==*p2)<br/>{while(*p1==*p2&amp;&amp;*p2!='\0')<br/>{p1++;<br/>p2++;}<br/>}<br/>else<br/>p1++;<br/>if(*p2=='\0')<br/>sum++;<br/>p2=str2;<br/>}<br/>printf("%d",sum);<br/>getch();} <br/><span class="STYLE13">==============================================================<br/>【程序97】</span><br/>题目:从键盘输入一些字符,逐个把它们送到磁盘上去,直到输入一个#为止。<br/>1.程序分析:     <br/>2.程序源代码:<br/>#include "stdio.h"<br/>main()<br/>{ FILE *fp;<br/>char ch,filename[10];<br/>scanf("%s",filename);<br/>if((fp=fopen(filename,"w"))==NULL)<br/>{printf("cannot open file\n");<br/>exit(0);}<br/>ch=getchar();<br/>ch=getchar();<br/>while(ch!='#')<br/>{fputc(ch,fp);putchar(ch);<br/>ch=getchar();<br/>}<br/>fclose(fp);<br/>}<br/><span class="STYLE13">==============================================================<br/>【程序98】</span><br/>题目:从键盘输入一个字符串,将小写字母全部转换成大写字母,然后输出到一个磁盘文件“test”中保存。<br/>    输入的字符串以!结束。 <br/>1.程序分析:<br/>2.程序源代码:<br/>#include "stdio.h"<br/>main()<br/>{FILE *fp;<br/>char str[100],filename[10];<br/>int i=0;<br/>if((fp=fopen("test","w"))==NULL)<br/>{ printf("cannot open the file\n");<br/>exit(0);}<br/>printf("please input a string:\n");<br/>gets(str);<br/>while(str!='!')<br/>{ if(str&gt;='a'&amp;&amp;str&lt;='z')<br/>str=str-32;<br/>fputc(str,fp);<br/>i++;}<br/>fclose(fp);<br/>fp=fopen("test","r");<br/>fgets(str,strlen(str)+1,fp);<br/>printf("%s\n",str);<br/>fclose(fp);<br/>}<br/><span class="STYLE13">==============================================================<br/>【程序99】</span><br/>题目:有两个磁盘文件A和B,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列), <br/>    输出到一个新文件C中。<br/>1.程序分析:<br/>2.程序源代码:<br/>#include "stdio.h"<br/>main()<br/>{ FILE *fp;<br/>int i,j,n,ni;<br/>char c[160],t,ch;<br/>if((fp=fopen("A","r"))==NULL)<br/>{printf("file A cannot be opened\n");<br/>exit(0);}<br/>printf("\n A contents are :\n");<br/>for(i=0;(ch=fgetc(fp))!=EOF;i++)<br/>{c=ch;<br/>putchar(c);<br/>}<br/>fclose(fp);<br/>ni=i;<br/>if((fp=fopen("B","r"))==NULL)<br/>{printf("file B cannot be opened\n");<br/>exit(0);}<br/>printf("\n B contents are :\n");<br/>for(i=0;(ch=fgetc(fp))!=EOF;i++)<br/>{c=ch;<br/>putchar(c);<br/>}<br/>fclose(fp);<br/>n=i;<br/>for(i=0;i&lt;n;i++)<br/>for(j=i+1;j&lt;n;j++)<br/>if(c&gt;c[j])<br/>{t=c;c=c[j];c[j]=t;}<br/>printf("\n C file is:\n");<br/>fp=fopen("C","w");<br/>for(i=0;i&lt;n;i++)<br/>{ putc(c,fp);<br/>putchar(c);<br/>}<br/>fclose(fp);<br/>}<br/><span class="STYLE13">==============================================================<br/>【程序100】</span><br/>题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出<br/>    平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件"stud"中。<br/>1.程序分析:<br/>2.程序源代码:<br/>#include "stdio.h"<br/>struct student<br/>{ char num[6];<br/>char name[8];<br/>int score[3];<br/>float avr;<br/>} stu[5];<br/>main()<br/>{int i,j,sum;<br/>FILE *fp;<br/>/*input*/<br/>for(i=0;i&lt;5;i++)<br/>{ printf("\n please input No. %d score:\n",i);<br/>printf("stuNo:");<br/>scanf("%s",stu.num);<br/>printf("name:");<br/>scanf("%s",stu.name);<br/>sum=0;<br/>for(j=0;j&lt;3;j++)<br/>{ printf("score %d.",j+1);<br/>scanf("%d",&amp;stu.score[j]);<br/>sum+=stu.score[j];<br/>}<br/>stu.avr=sum/3.0;<br/>}<br/>fp=fopen("stud","w");<br/>for(i=0;i&lt;5;i++)<br/>if(fwrite(&amp;stu,sizeof(struct student),1,fp)!=1)<br/>printf("file write error\n");<br/>fclose(fp);<br/>}</span></p>

更多回帖

打开APP