當前位置:首頁 » 編程語言 » 時分秒用c語言編輯
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

時分秒用c語言編輯

發布時間: 2022-05-10 11:15:59

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;

}