① c语言编程 关于计算时间的问题 望高手解答!
希望能够我的思路可以帮助你:
①如果password="124567"时,欢迎进入!
②如果password != "124567"时,等待15分钟!
③等待15分钟后返回重新输入密码!
#include <stdio.h>
#include <string.h>
#include<windows.h>
int main()
{
char str[20], password;
int x,i;
//执行4次循环0,1,2,3
for(x=0; x<=3 && strcmp(str,"1234567")!=0; x++)
{
printf("Enter password please:");
scanf("%s",&str);
//当密码错误时提示输入错误!
if(strcmp(str,"1234567")!=0)
{
printf("Input error!\n");
}
//当错误了3次时执行等待,并重置x的初值
if(x==2)
{
printf("Please wait another 15 min.");
for(i=0;i<=(15*60);i++)
Sleep(1000); //停滞一秒
//重置x的初值
x=0;
}
else
//密码输入正确时跳出循环,执行for循环之外的语句
{
if(strcmp(str,"1234567")==0)
printf("Welcome\n");
break;
}
}
//可以插入验证后要执行的代码
return 0;
}
② 用C语言编写一个时间换算
刚才给的那个代码太繁琐了,又重新调整了一下:
#include <stdio.h>
int main()
{
//世界协调时与北京时间换算
int a;
scanf("%d",&a); /*输入北京时间*/
if (a >= 800)
{
printf("%d\n",a-800); /*如果输入的北京时间大于800,直接减去800,就是世界协调时*/
}
else{
int b=a/100; /*换算百位数字*/
int c=a%100; /*换算十位和个位数字*/
printf("%d\n",(24-(8-b))*100+c);
}
return 0;
}
③ 编写一个简单的C语言程序,在屏幕上显示一行时间(包含小时、分钟和秒钟)的信息
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
int main()
{
time_t timep,Tim;
struct tm *p;
time(&timep);
p = localtime(&timep); //此函数获得的tm结构体的时间,是已经进行过时区转化为本地时间
//p = gmtime(&timep); //把日期和时间转换为格林威治(GMT)时间的函数
int Year = 1900 + p->tm_year;
int Month = 1 + p->tm_mon;
int Day = p->tm_mday;
int Hour = p->tm_hour;
int Minute = p->tm_min;
int Second = p->tm_sec;
char year[20];
char month[20];
char day[20];
char hour[20];
char minute[20];
char second[20];
printf("hour=%d\n", Hour);
printf("minute=%d\n", Minute);
printf("second=%d\n", Second);
return 0;
}
每编译一次就会显示这一刻的系统时间;这个程序我也不太懂,我学长讲了半天,数据结构都整出来了,也没把我整明白,我再研究研究;希望能帮到你吧;
④ C语言编程如何设置时间
struct tm *tm; time_t t;
char timebuf[20]
memset(timebuf,0,sizeof(timebuf));
time(&t);
tm = localtime(&t);
strftime(timebuf,15,"%Y%m%d%H%M%S",tm);
printf("current time is [%s]",timebuf);
⑤ c语言编程,输入任意的分钟数,用小时和分钟的方式显示计算旅程时间。
#include <stdio.h>
int main()
{
int minutes,hour;
scanf("&d",&minutes);
hour=minutes/60;
minutes=minutes%60;
printf("旅程时间为%d小时%d分钟\n",hour,minutes);
return 0;
}
⑥ 用C语言编写一个程序输出任意时间下一秒的时间
#include "stdio.h"
#include "conio.h"
int main(void)
{
int s;
int f;
int m;
printf("请输入一个时间: ");
scanf("%i %i %i",&s,&f,&m);
if(m==59){
m=0;
f=f++;
printf("%i %i %i ",s,f,m);
}else {
m=m++;
printf("%i %i %i ",s,f,m);
}
getch();
}
或:
#include <stdio.h>
#include <time.h>
int main(void)
{
int i=123456789;
clock_t start, end;
start = clock();
while(i--);
end = clock();
printf("The time was: %d ", (end - start));//单位是毫秒,注意是%d,不再是%f
printf("The time was: %f ", (double)(end - start) / CLK_TCK); //单位是秒
return 0;
}
(6)c语言编程写时间扩展阅读:
C的数据类型包括:整型(short,int,long,long long)、字符型(char)、实型或浮点型(单精度float和双精度double)、枚举类型(enum)、数组类型、结构体类型(struct)、共用体类型(union)、指针类型和空类型(void)。
变量是以某标识符为名字,其值可以改变的量。标识符是以字母或下划线开头的一串由字母、数字或下划线构成的序列,请注意第一个字符必须为字母或下划线,否则为不合法的变量名。变量在编译时为其分配相应存储单元。
⑦ 如何用C语言编写一个显示时间的函数,要求时间显示精度到毫秒级别。
#include <cstdio>
#include <ctime>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void printTime() {
struct tm t; //tm结构指针
time_t now; //声明time_t类型变量
time(&now); //获取系统日期和时间
localtime_s(&t, &now); //获取当地日期和时间
//格式化输出本地时间
printf("年-月-日-时-分-秒:%d-%d-%d %d:%d:%d ", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
}
int main(int argc, char** argv) {
printTime();
}
⑧ c语言编程,怎么计算时间
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
unsigned char time1[] = {10, 8, 31, 9, 26 };
unsigned char time2[] = { 10, 8, 31, 9, 50 };
struct tm t1 = {0};
struct tm t2 = {0};
time_t _t1;
time_t _t2;
double diff;
t1.tm_year = time1[0] + 100;
t1.tm_mon = time1[1];
t1.tm_mday = time1[2];
t1.tm_hour = time1[3];
t1.tm_min = time1[4];
t2.tm_year = time2[0] + 100;
t2.tm_mon = time2[1];
t2.tm_mday = time2[2];
t2.tm_hour = time2[3];
t2.tm_min = time2[4];
_t1 = _mkgmtime( &t1 );
_t2 = _mkgmtime( &t2 );
diff = difftime(_t2, _t1 );
printf( "相差 %.0f 分钟
", diff / 60 );
}
(8)c语言编程写时间扩展阅读:
C语言中有两个相关的函数用来计算时间差,分别是:
time_t time( time_t *t) 与 clock_t clock(void)
头文件: time.h
计算的时间单位分别为: s , ms
time_t 和 clock_t 是函数库time.h 中定义的用来保存时间的数据结构
返回值:
1、time : 返回从公元1970年1月1号的UTC时间从0时0分0秒算起到现在所经过的秒数。如果参数 t 非空指针的话,返回的时间会保存在 t 所指向的内存。
2、clock:返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数。 1单元 = 1 ms。
所以我们可以根据具体情况需求,判断采用哪一个函数。
具体用法如下例子:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
time_t c_start, t_start, c_end, t_end;
c_start = clock(); //!< 单位为ms
t_start = time(NULL); //!< 单位为s
system("pause");
c_end = clock();
t_end = time(NULL);
//!<difftime(time_t, time_t)返回两个time_t变量间的时间间隔,即时间差
printf("The pause used %f ms by clock() ",difftime(c_end,c_start));
printf("The pause used %f s by time() ",difftime(t_end,t_start));
system("pause");
return 0;
}
因此,要计算某一函数块的占用时间时,只需要在执行该函数块之前和执行完该函数块之后调用同一个时间计算函数。再调用函数difftime()计算两者的差,即可得到耗费时间。