本文件包含了一系列精心设计的C语言编程示例,共计约200行代码,旨在帮助初学者掌握基础语法与高级特性。
时间函数举例程序分析
【程序92】题目:时间函数举例2
1. 程序源代码:
```c
/*calculate time*/
#include time.h
#include stdio.h
int main(){
time_t 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);
printf(The different is %6.3f,difftime(end,start));
}
```
【程序93】题目:时间函数举例3
2. 程序源代码:
```c
/*calculate time*/
#include time.h
#include stdio.h
int main(){
clock_t start,end;
int i;
double var;
start=clock();
for(i=0;i<10000;i++)
printf(\1\1\1\1\1\1\1\1\1\1\n);
end=clock();
printf(The different is %6.3f,(double)(end-start));
}
```
【程序94】题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。
2. 程序源代码:
```c
#include time.h
#include stdlib.h
#include stdio.h
int main(){
char c;
clock_t start,end;
time_t a,b;
double var;
srand(time(NULL));
printf(do you want to play it.(y or n) \n);
loop:
while((c=getchar())==y){
int i,guess=rand()%100;
printf(\nplease input number you guess:\n);
start=clock();
a=time(NULL);
scanf(%d,&guess);
while(guess!=i){
if(guess>i) {
printf(please input a little smaller.\n);
scanf(%d,&guess);
} else {
printf(please input a little bigger.\n);
scanf(%d,&guess);
}
}
end=clock();
b=time(NULL);
var=(double)(end-start)/18.2;
if(var<15)
printf(\1\1 You are very clever! \1\1\n\n);
else if(var<25)
printf(\1\1 you are normal! \1\1\n\n);
else
printf(\1\1 you are stupid! \1\1\n\n);
printf(The number you guess is %d,i);
}
printf(\ndo you want to try it again?(\yy\.or.\n\)\n);
if((c=getchar())==y)
goto loop;
}
```
【程序95】题目:家庭财务管理小程序
2. 程序源代码:
```c
/*money management system*/
#include stdio.h
int main(){
FILE *fp;
struct date d;
float sum=0,chm=0.0;
int len,i,j=0,c;
char ch[4]=,ch1[16]=,chtime[12]=;
for(;;){
clrscr();
printf(|----------------------------------------------------|\n);
printf(| money management system(C1.0) 2000.03 |\n);
printf(|----------------------------------------------------|\n);
i=0;
getdate(&d);
sprintf(chtime,%4d.%02d.%02d,d.da_year,d.da_mon,d.da_day);
for(;;){
ch[0]=getch();
if(ch[0]==27)
break;
else{
while(*p1!=\0) {
if(*p1==*p2) {
while(*p1==*p2&&*p2!=\0){ p1++; p2++;}
}
else
p1++;
if(*p2==\0)
sum++;
}
printf(%d,sum);
}
getch();
}
```
【程序97】题目:从键盘输入一些字符,逐个把它们送到磁盘上去,直到输入一个#为止。
2. 程序源代码:
```c
#include stdio.h
int main(){
FILE *fp;
char ch,filename[10];
scanf(%s,filename);
if((fp=fopen(filename,w))==NULL) {
printf(cannot open file\n);
exit(0);
}
while(ch!=#){
fputc(ch,fp);
putchar(ch);
ch=getchar();
}
fclose(fp