1. 用c语言编一个时钟程序实现时分秒计时功能
#include<stdio.h>
#include<time.h>
int main()
{
time_t t1,t2;
struct tm *ptm;
char timestr[128]={'\0'};
time(&t1);
while(1)
{
time(&t2);
if(difftime(t2,t1)>=1)
{
system("cls");
ptm=localtime(&t2);
strftime(timestr,128,"%H:%M:%S",ptm);
printf("%s\n",timestr);
t1=t2;
}
}
system("PAUSE");
return 0;
}
2. 求c语言源代码编写程序要求1输入年月份时分秒2输入增加的分钟数,输出输入的时间及增加分钟数后的时间
#include <stdio.h>
#include <time.h>
int main()
{
int year,month,day,hour,min,sec;
int add;
time_t timep;
struct tm *p, tmt;
printf("请输入日期时间,格式为: 年年年年-月月-日日-时时-分分-秒秒\n");
scanf("%d-%d-%d-%d-%d-%d", &year,&month,&day,&hour,&min,&sec);
//year=2016;month=10;day=30;hour=8;min=20;sec=0; // 测试代码
printf("请输入增加的分钟数:\n");
scanf("%d", &add);
//add=1440+1440; //测试代码
// 打印输入的时间
printf("%4d %2d %2d %2d %2d %2d\n", year, month, day, hour, min, sec);
tmt.tm_year = year-1900;
tmt.tm_mon = month -1;
tmt.tm_mday = day;
tmt.tm_hour = hour;
tmt.tm_min = min;
tmt.tm_sec = sec;
timep = mktime(&tmt);
timep += add*60;
p = localtime(&timep);
// 打印增加分钟后的时间
printf("%4d %2d %2d %2d %2d %2d\n", p->tm_year + 1900, p->tm_mon +1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
getchar();
return 0;
}
3. c语言 怎样编译 时间表 时分秒那种能走的
用time(0)取当前时间,用pritnf(%02d:%02d:%02d\b,hours,min,sec)输出时间,\b回行首不换行,输出新时间覆盖原本的,就是时间在走的感觉,输出循环注意设置刷新间隔和退出条件
4. 用C语言实现:首先获取系统时钟,然后时钟数据格式为:年月日时分秒(YYYYMMDDHHMMSS)
#include <stdio.h>
#include <time.h>
int main()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
int year,month,day,hour,min,sec;
year = 1900+timeinfo->tm_year;
month = 1+timeinfo->tm_mon;
day = timeinfo->tm_mday;
hour = timeinfo->tm_hour;
min = timeinfo->tm_min;
sec = timeinfo->tm_sec;
printf ( "当前时间:%4d-%02d-%02d %02d:%02d:%02d
",year, month,day,hour,min,sec);
printf ( "你需要的格式:%4d%02d%02d%02d%02d%02d
",year, month,day,hour,min,sec);
char hyy[2],lyy[2],MM[2],dd[2],hh[2],mm[2],ss[2];
sprintf(hyy,"%02X",year/100);
sprintf(lyy,"%02X",year%100);
sprintf(MM,"%02X",month);
sprintf(dd,"%02X",day);
sprintf(hh,"%02X",hour);
sprintf(mm,"%02X",min);
sprintf(ss,"%02X",sec);
printf("转化为16进制:%02s%02s%02s%02s%02s%02s%02s
",hyy,lyy,MM,dd,hh,mm,ss);
//exit(0);
return 0;
}
说明:我用VC6.0现写的,运行结果截图如下:
5. c语言编写 输入一个以秒为单位的时间值,将其转化成“时:分:秒”的形式输出,将转换操作定义成函数
#include<stdio.h>
void trans(int sec){
int hour,min;
hour=sec/3600; //计算时 3600进制
min=(sec%3600)/60; //计算分 60进制
sec=(sec%3600)%60; //计算秒 余下的全为秒数
printf("%d时:%d分:%d秒\n",hour,min,sec);
}
int main(){
int sec;
printf("请输入秒数:\n");
scanf("%d",&sec);
trans(sec);
return 0;
}
可以自己试试
6. 如何用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();
}
7. 在c语言程序中如何编辑秒数,让它按小时;分钟,秒的形式输出
您好,是这样的:
1.定义三个变量
分别来存储输入的分钟数、转换的小时数和剩余的分钟数。
2.
从界面获取输入的分钟数。
3.
计算结果。
4.
把结果输出到界面。
//1
定义三个变量
分别来存储输入的分钟数、转换的小时数和剩余的分钟数
//2
从界面获取输入的分钟数
//3
计算结果
//4
把结果输出到界面
int
i,
h,
m;
Console.WriteLine("请输入一个分钟数:");
i
=
int.Parse(Console.ReadLine());
h
=
i/60;
m
=
i%60;
Console.WriteLine("{0}分等于{1}小时{2}分",
i,
h,
m);
Console.ReadKey();
8. C语言如何获取本地时间,然后取时、分、秒的值
#include <stdio.h>
#include <time.h>
int main()
{time_t timep;
struct tm *tp;
time(&timep);
int p;
tp = localtime(&timep); //取得系统时间
printf("Today is %d-%d-%d ", (1900 + tp->tm_year), (1 + tp->tm_mon), tp->tm_mday);
printf("Now is %d:%02d:%02d ", tp->tm_hour, tp->tm_min, tp->tm_sec);
p=tp->tm_sec;
printf("p=%d ",p);
return 0;
}