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