① 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()計算兩者的差,即可得到耗費時間。