91、时间函数举例1
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "当前本地时间为: %s", asctime (timeinfo) );
return 0;
}
92、时间函数举例2
#include <stdio.h>
#include <time.h>
int main()
{
time_t start,end;
int i;
start=time(NULL);
for(i=0;i<300000;i++)
{
printf("\n"); // 返回两个time_t型变量之间的时间间隔
}
end=time(NULL);
// 输出执行时间
printf("时间间隔为 %6.3f\n",difftime(end,start));
}
93、时间函数举例2
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
long i=10000000L;
clock_t start,finish;
double TheTimes;
printf("做%ld次空循环需要的时间为",i);
start=clock();
while(i--);
finish=clock();
TheTimes=(double)(finish-start)/CLOCKS_PER_SEC;
printf("%f秒。\n",TheTimes);
return 0;
}
94、猜谜游戏。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void caizi(void)
{
int n;
char begin;
int count = 1;
srand((int)time(NULL));
int m = (rand() % 100) + 1;
puts("游戏开始,请输入数字:");
while (1)
{
scanf("%d", &n);
if (n == m)
{
printf("猜中了,使用了 %d 次!\n", count);
if (count == 1)
{
printf("你是神级人物了!膜拜\n");
getchar();
printf("你已经达到最高级别,还需要玩吗?Y/N \n");
scanf("%c", &begin);
if (begin == 'Y' || begin == 'y') //重复玩的一个嵌套循环
{
caizi();
}
else
{
printf("谢谢,再见!\n");
}
}
else if (count <= 5)
{
printf("你是王级人物了!非常赞\n");
getchar();
printf("需要挑战最高级别不?Y/N \n");
scanf("%c", &begin);
if (begin == 'Y' || begin == 'y')
{
caizi();
}
else
{
printf("谢谢,再见!\n");
}
}
else if (count <= 10)
{
printf("你是大师级人物了!狂赞\n");
getchar();
printf("需要挑战最高级别不?Y/N \n");
scanf("%c", &begin);
if (begin == 'Y' || begin == 'y')
{
caizi();
}
else
{
printf("谢谢,再见!\n");
}
}
else if (count <= 15)
{
printf("你是钻石级人物了!怒赞\n");
getchar();
printf("需要挑战最高级别不?Y/N \n");
scanf("%c", &begin);
if (begin == 'Y' || begin == 'y')
{
caizi();
}
else
{
printf("谢谢,再见!\n");
}
}
else
{
getchar();
printf("你的技术还有待提高哦!重玩? Y/N\n");
scanf("%c",&begin);
if (begin == 'Y' || begin == 'y')
{
caizi();
}
else
{
printf("谢谢,再见!\n");
}
}
break;
}
else if (n < m)
{
puts("太小了!");
puts("重新输入:");
}
else
{
puts("太大了!");
puts("重新输入:");
}
count++;//计数器
}
}
int main(void)
{
caizi();
system("pause");
return 0;
}
95、简单的结构体应用实例。
#include <stdio.h>
struct programming
{
float constant;
char *pointer;
};
int main()
{
struct programming variable;
char string[] = "菜鸟教程:http://www.runoob.com";
variable.constant = 1.23;
variable.pointer = string;
printf("%f\n", variable.constant);
printf("%s\n", variable.pointer);
return 0;
}