📄 经典c程序100例==91--100.htm
字号:
guess:\n");<BR>start=clock();<BR>a=time(NULL);<BR>scanf("%d",&guess);<BR>while(guess!=i)<BR>{if(guess>i)<BR>{printf("please
input a little
smaller.\n");<BR>scanf("%d",&guess);}<BR>else<BR>{printf("please input
a little
bigger.\n");<BR>scanf("%d",&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<15)<BR>printf("\1\1 You are very
clever! \1\1\n\n");<BR>else if(var<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\".or.\"n\")\n");<BR>if((c=getch())=='y')<BR>goto
loop;<BR>}<BR>==============================================================<BR><FONT
color=#990000>【程序95】</FONT><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(&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,&chm)!=EOF)<BR>{
if(i==36)<BR>{ getch();<BR>i=0;}<BR>if ((i%36)<17)<BR>{
gotoxy(4,6+i);<BR>printf("
");<BR>gotoxy(4,6+i);}<BR>else<BR>if((i%36)>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<10)<BR>{
strncat(chtime,ch,1);<BR>j++;}<BR>if(ch[0]==8)<BR>{<BR>len=strlen(chtime)-1;<BR>if(j>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<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<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>}}}
<BR>==============================================================<BR><FONT
color=#990000>【程序96】</FONT><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&&*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>==============================================================<BR><FONT
color=#990000>【程序97】</FONT><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>==============================================================<BR><FONT
color=#990000>【程序98】</FONT><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[i]!='!')<BR>{
if(str[i]>='a'&&str[i]<='z')<BR>str[i]=str[i]-32;<BR>fputc(str[i],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>==============================================================<BR><FONT
color=#990000>【程序99】</FONT><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[i]=ch;<BR>putchar(c[i]);<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[i]=ch;<BR>putchar(c[i]);<BR>}<BR>fclose(fp);<BR>n=i;<BR>for(i=0;i<n;i++)<BR>for(j=i+1;j<n;j++)<BR>if(c[i]>c[j])<BR>{t=c[i];c[i]=c[j];c[j]=t;}<BR>printf("\n
C file is:\n");<BR>fp=fopen("C","w");<BR>for(i=0;i<n;i++)<BR>{
putc(c[i],fp);<BR>putchar(c[i]);<BR>}<BR>fclose(fp);<BR>}<BR>==============================================================<BR><FONT
color=#990000>【程序100】</FONT><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<5;i++)<BR>{ printf("\n please input No.
%d
score:\n",i);<BR>printf("stuNo:");<BR>scanf("%s",stu[i].num);<BR>printf("name:");<BR>scanf("%s",stu[i].name);<BR>sum=0;<BR>for(j=0;j<3;j++)<BR>{
printf("score
%d.",j+1);<BR>scanf("%d",&stu[i].score[j]);<BR>sum+=stu[i].score[j];<BR>}<BR>stu[i].avr=sum/3.0;<BR>}<BR>fp=fopen("stud","w");<BR>for(i=0;i<5;i++)<BR>if(fwrite(&stu[i],sizeof(struct
student),1,fp)!=1)<BR>printf("file write
error\n");<BR>fclose(fp);<BR>}</P>
<P> </P></TD></TR></TBODY></TABLE></CENTER></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -