时表c语言
⑴ c语言顺序表
你输入的括号是汉字括号,应该改为英语括号。比较:
汉字:()
英语:()
⑵ 怎么用c语言 编一个时间表 源代码是什么_
用到的数据结构:
time_t是一个long类型 代表机器时间,可由time( )函数获得。
日历时间用一个(char *) 类型的字符串表示。格式为:星期 月 日 小时:分:秒 年\n\0
可由函数ctime( ) asctime( ) 得到。
以tm结构表达的时间,结构tm定义如下:
struct tm { 可由函数localtime( ), gmtime( )得到
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst; };
//系统日期
struct date {
int da_year; /* Year - 1980 */
char da_day; /* Day of the month */
char da_mon; /* Month (1 = Jan) */ };
//系统时间
struct time {
unsigned char ti_min; /* Minutes */
unsigned char ti_hour; /* Hours */
unsigned char ti_hund; /* Hundredths of seconds */
unsigned char ti_sec; /* Seconds */ };
用到的函数:
char * asctime(struct tm * ptr) 将tm结构的时间转化为日历时间。
char *ctime(long time) 将机器时间转化为日历时间。
struct tm *gmtime(time_t *time) 将机器时间转化为tm时间
当ptr为空时,得到机器时间;非空时,设置机器时间。
time_t time(time_t *ptr) 得到或设置机器时间
double difftime(time_t time2, time_t time1) 得到两次机器时间差,单位为秒
long dostounix(struct data *d, struct time *t) 将DOS的日期和时间格式转换成UNIX标准
(机器时间(用到dos.h库).void unixtodos(long utime, struct date *d, struct time *t)
将UNIX格式的时间和日期转换成DOS格式(由time 和date两部分组成,只能由机器时间得到,并且用到dos.h库)
void getdate(struct date *d) 得到系统日期,d 存放得到的日期信息
void setdate(struct date *d)
void gettime(struct date *t) 得到系统时间 d 存放得到的时间信息
void settime(struct date *t)
C语言的标准库函数包括一系列日期和时间处理函数,它们都在头文件中说明。下面列出了这些函数。
在头文件中定义了三种类型:time_t,struct tm和clock_t。
在中说明的C语言时间函数
time_t time(time_t *timer);
double difftime(time_t time1,time_t time2);
struct tm *gmtime(const time_t *timer);
struct tm *localtime(const time_t *timer);
char *asctime(const struct tm *timeptr);
char *ctime(const time_t *timer);
size_t strftime(char *s,size_t maxsize,const char *format,const struct tm *timeptr);
time_t mktime(struct tm *timeptr);
clock_t clock(void);
下面是我从网上收集到的时间函数集
asctime(将时间和日期以字符串格式表示)
相关函数
time,ctime,gmtime,localtime
表头文件
#i nclude
定义函数
char * asctime(const struct tm * timeptr);
函数说明
asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。
此函数已经由时区转换成当地时间,字符串格式为:"Wed Jun 30 21:49:08 1993\n"
返回值
若再调用相关的时间日期函数,此字符串可能会被破坏。此函数与ctime不同处在于传入的参数是不同的结构。
附加说明
返回一字符串表示目前当地的时间日期。
范例
#i nclude
main()
{
time_t timep;
time (&timep);
printf("%s",asctime(gmtime(&timep)));
}
执行
Sat Oct 28 02:10:06 2000
ctime(将时间和日期以字符串格式表示)
相关函数
time,asctime,gmtime,localtime
表头文件
#i nclude
定义函数
char *ctime(const time_t *timep);
函数说明
ctime ()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。
此函数已经由时区转换成当地时间,字符串格式为"Wed Jun 30 21 :49 :08 1993\n"。若再调用相关的时间日期函数,此字符串可能会被破坏。
返回值
返回一字符串表示目前当地的时间日期。
范例
#i nclude
main()
{
time_t timep;
time (&timep);
printf("%s",ctime(&timep));
}
执行
Sat Oct 28 10 : 12 : 05 2000
gettimeofday(取得目前的时间)
相关函数
time,ctime,ftime,settimeofday
表头文件
#i nclude
#i nclude
定义函数
int gettimeofday ( struct timeval * tv , struct timezone * tz )
函数说明
gettimeofday()会把目前的时间有tv所指的结构返回,当地时区的信息则放到tz所指的结构中。
timeval结构定义为:
struct timeval{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
timezone 结构定义为:
struct timezone{
int tz_minuteswest; /*和Greenwich 时间差了多少分钟*/
int tz_dsttime; /*日光节约时间的状态*/
};
上述两个结构都定义在/usr/include/sys/time.h。tz_dsttime 所代表的状态如下
DST_NONE /*不使用*/
DST_USA /*美国*/
DST_AUST /*澳洲*/
DST_WET /*西欧*/
DST_MET /*中欧*/
DST_EET /*东欧*/
DST_CAN /*加拿大*/
DST_GB /*大不列颠*/
DST_RUM /*罗马尼亚*/
DST_TUR /*土耳其*/
DST_AUSTALT /*澳洲(1986年以后)*/
返回值
成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存取权限。
范例
#i nclude
#i nclude
main(){
struct timeval tv;
struct timezone tz;
gettimeofday (&tv , &tz);
printf("tv_sec; %d\n", tv,.tv_sec) ;
printf("tv_usec; %d\n",tv.tv_usec);
printf("tz_minuteswest; %d\n", tz.tz_minuteswest);
printf("tz_dsttime, %d\n",tz.tz_dsttime);
}
执行
tv_sec: 974857339
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0
gmtime(取得目前时间和日期)
相关函数
time,asctime,ctime,localtime
表头文件
#i nclude
定义函数
struct tm*gmtime(const time_t*timep);
函数说明
gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。
结构tm的定义为
struct tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
int tm_sec 代表目前秒数,正常范围为0-59,但允许至61秒
int tm_min 代表目前分数,范围0-59
int tm_hour 从午夜算起的时数,范围为0-23
int tm_mday 目前月份的日数,范围01-31
int tm_mon 代表目前月份,从一月算起,范围从0-11
int tm_year 从1900 年算起至今的年数
int tm_wday 一星期的日数,从星期一算起,范围为0-6
int tm_yday 从今年1月1日算起至今的天数,范围为0-365
int tm_isdst 日光节约时间的旗标
此函数返回的时间日期未经时区转换,而是UTC时间。
返回值
返回结构tm代表目前UTC 时间
范例
#i nclude
main(){
char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
struct tm *p;
time(&timep);
p=gmtime(&timep);
printf("%d%d%d",(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
printf("%s%d;%d;%d\n", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
}
执行
2000/10/28 Sat 8:15:38
localtime(取得当地目前时间和日期)
相关函数
time, asctime, ctime, gmtime
表头文件
#i nclude
定义函数
struct tm *localtime(const time_t * timep);
函数说明
localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。
结构tm的定义请参考gmtime()。此函
数返回的时间日期已经转换成当地时区。
返回值
返回结构tm代表目前的当地时间。
范例
#i nclude
main(){
char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
struct tm *p;
time(&timep);
p=localtime(&timep); /*取得当地时间*/
printf ("%d%d%d ", (1900+p->tm_year),( l+p->tm_mon), p->tm_mday);
printf("%s%d:%d:%d\n", wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec);
}
执行
2000/10/28 Sat 11:12:22
mktime(将时间结构数据转换成经过的秒数)
相关函数
time,asctime,gmtime,localtime
表头文件
#i nclude
定义函数
time_t mktime(strcut tm * timeptr);
函数说明
mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。
返回值
返回经过的秒数。
范例
/* 用time()取得时间(秒数),利用localtime()
转换成struct tm 再利用mktine()将struct tm转换成原来的秒数*/
#i nclude
main()
{
time_t timep;
strcut tm *p;
time(&timep);
printf("time() : %d \n",timep);
p=localtime(&timep);
timep = mktime(p);
printf("time()->localtime()->mktime():%d\n",timep);
}
执行
time():974943297
time()->localtime()->mktime():974943297
settimeofday(设置目前时间)
相关函数
time,ctime,ftime,gettimeofday
表头文件
#i nclude
#i nclude
定义函数
int settimeofday ( const struct timeval *tv,const struct timezone *tz);
函数说明
settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。详细的说明请参考gettimeofday()。
注意,只有root权限才能使用此函数修改时间。
返回值
成功则返回0,失败返回-1,错误代码存于errno。
错误代码
EPERM 并非由root权限调用settimeofday(),权限不够。
EINVAL 时区或某个数据是不正确的,无法正确设置时间。
time(取得目前的时间)
相关函数
ctime,ftime,gettimeofday
表头文件
#i nclude
定义函数
time_t time(time_t *t);
函数说明
此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,
此函数也会将返回值存到t指针所指的内存。
返回值
成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中。
⑶ c语言中参数表是什么意思
多个参数,安顺序排列 叫 参数表。
参数1,参数2,参数3,参数4,参数5,。。。参数n
共 1 到 n 个。
printf("*****"\n");-- 没有输出参数,没有 输出参数表。
printf("%d %d"\n",a,b);-- a,b 两个参数,参数表就是 a,b.
printf("%d %d %d"\n",a,b,c);-- a,b,c 3个参数,参数表就是 a,b,c
⑷ C语言查表
表就是自己做的.
举个简单的例子,根据公式计算后,0-100对应100度,那就直接根据AD值去对应温度
⑸ c语言编写钟表的问题
#include<graphics.h>
#include<math.h>
#include<dos.h>
#define PI 3.1415926
#define x0 320 /*定义钟表中心坐标*/
#define y0 240
void DrawClock(int x,int y,int color) /*画表盘*/
{ int r=150; /*表盘的半径*/
float th;
setcolor(color);
circle(x,y,r);
circle(x,y,2);
}
void DrawHand(int x,int y,float th,int l,int color)
{
int x1,y1;
x1=x l*sin(th);
y1=y-l*cos(th);
setcolor(color);
line(x,y,x1,y1);
}
void main()
{int gdriver=DETECT,gmode;
struct time curtime;
float th_hour,th_min,th_sec;
initgraph(&gdriver,&gmode,"");
setbkcolor(0);
while(! kbhit())
{
DrawClock(x0,y0,14);
gettime(&curtime); /*得到当前系统时间*/
gotoxy(35,20); /*定位输出位置*/
if((float)curtime.ti_hour<=12) /*午前的处理*/
{printf("AM ");
if((float)curtime.ti_hour<10) printf("0"); /*十点之前在小时数前加零*/
printf("%.0f:",(float)curtime.ti_hour);
}
else /*午后的处理*/
{printf("PM ");
if((float)curtime.ti_hour-12<10) printf("0");
printf("%.0f:",(float)curtime.ti_hour-12);
}
if((float)curtime.ti_min<10) printf("0");
printf("%.0f:",(float)curtime.ti_min);
if((float)curtime.ti_sec<10) printf("0");
printf("%.0f",(float)curtime.ti_sec);
/*以下三行计算表针转动角度,以竖直向上为起点,顺时针为正*/
th_sec=(float)curtime.ti_sec*0.1047197551; /*2π/60=0.1047197551*/
th_min=(float)curtime.ti_min*0.1047197551 th_sec/60.0;
th_hour=(float)curtime.ti_hour*0.523598775 th_min/12.0; /* 2π/12=0.5235987755 */
DrawHand(x0,y0,th_hour,70,2); /*画时针*/
DrawHand(x0,y0,th_min,110,3); /*分针*/
DrawHand(x0,y0,th_sec,140,12); /*秒针*/
sleep(1); /*延时一秒后刷新*/
cleardevice();
}
closegraph();
}
能正常运行,我测试过
来自网络转载
⑹ 用C语言程序运行出一个钟表 要求切身时间过得
你要给哥加分啊。。。这可视个高难度的 花了哥一个小时 首先我的声明 1.这是用windows api写的程序。所以要求是纯c的话就没有办法了 2.其中定时用了两种方法。一种是用取消息。另一种是延时队列。这里只使用了取消息的方法。延时队列由于我机器上是vc6.0,CreateTimerQueue在本人机器上无法使用,需要新的sdk,所以没有加以验证,但取消息的方式是可行的。 3.稍稍验证了下,基本满足要求。 ------------------------------------------- 程序如下: // DigitalClock.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <windows.h> #include <winbase.h> typedef struct _st_time{ int hour; int min; int sec; }ST_TIME; ST_TIME g_Time; // The struct contain the hour,min and sec. HANDLE g_hStdout; // WORD g_cxCenter, g_cyCenter; // Center of the screen. HANDLE g_DoneEvent; // The program could be over. BOOL g_ThreadTerminated; // The Thread should be terminated. #define SECOND_CIRCLE 60 #define MINUTE_CIRCLE 60 #define HOUR_CIRCLE 24 void TimeIncreaseSecond(ST_TIME & st) { st.sec ++; if (st.sec >= SECOND_CIRCLE) { st.sec -= SECOND_CIRCLE; st.min++; if (st.min >= MINUTE_CIRCLE) { st.min -= MINUTE_CIRCLE; st.hour++; if (st.hour >= HOUR_CIRCLE) { st.hour -= HOUR_CIRCLE; } } } } void PrintTimeToScreen(HANDLE hStdout, short cxCenter, short cyCenter, ST_TIME st) { char buf[64] = {0}; COORD crdPos; // make it format to output. sprintf (buf, "%02d:%02d:%02d", st.hour, st.min, st.sec); crdPos.X = cxCenter - 4; crdPos.Y = cyCenter; SetConsoleCursorPosition(hStdout, crdPos); printf(buf); } #ifdef USE_TIMERQUEUE // if we use the timer queue function. // Its procre is in this. void CALLBACK TimerRoutine (LPVOID lpParam, BOOL TimerOrWaitFired) { if (lpParam == NULL) { printf ("NULL parameters.\n"); } else { ST_TIME *st = (ST_TIME *)lpParam; TimeIncreaseSecond(st); PrintTimeToScreen(g_hStdout, g_cxCenter, g_cyCenter, *st); } } #else DWORD WINAPI TimerThreadProc(LPVOID lpParam) { #define ID_TIMER_SECOND 1 MSG msg; BOOL ret; ST_TIME *st = (ST_TIME *)lpParam; SetTimer(NULL, ID_TIMER_SECOND, 1000, NULL); PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE); while (!g_ThreadTerminated && (ret = GetMessage (&msg, NULL, 0, 0)) != 0) { if (ret == -1) { //process fatal event. } else if (msg.message == WM_TIMER) { TimeIncreaseSecond(*st); PrintTimeToScreen(g_hStdout, g_cxCenter, g_cyCenter, *st); } else { TranslateMessage (&msg); DispatchMessage (&msg); } } return 1; } #endif // If the ctrl+break combined key pressed. call this function. // It set the g_DoneEvent. this terminate the program. BOOL WINAPI CtrlHandler(DWORD fdwCtrlType) { switch (fdwCtrlType) { case CTRL_BREAK_EVENT: // Terminate the program. printf ("Terminate.\n"); SetEvent(g_DoneEvent); return TRUE; default: return FALSE; } } BOOL InitApplication() { // Get the stdin and stdout handle. HANDLE hStdIn; hStdIn = GetStdHandle(STD_INPUT_HANDLE); if (hStdIn == INVALID_HANDLE_VALUE) return FALSE; g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE); // Set the mode, make the input echo. DWORD fOldMode; GetConsoleMode(hStdIn, &fOldMode); fOldMode |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT; SetConsoleMode(hStdIn, fOldMode); // Set the window buffer. // make a line 40 columns. CONSOLE_SCREEN_BUFFER_INFO csbiInfo; GetConsoleScreenBufferInfo(g_hStdout, &csbiInfo); csbiInfo.srWindow.Right = 40; // get the center point. g_cxCenter = csbiInfo.srWindow.Right / 2; g_cyCenter = csbiInfo.srWindow.Bottom / 2; // Set the window. SetConsoleWindowInfo(g_hStdout, TRUE, &csbiInfo.srWindow); return TRUE; } BOOL (HANDLE hStdout, WORD cxCenter, WORD cyCenter, ST_TIME & time) { #define GAPS_LEFT_COLON (-2) #define GAPS_RIGHT_COLON (1) #define GAPS_LEFT_UNDERLINE_START (-4) #define GAPS_MIDDLE_UNDERLINE_START (-1) #define GAPS_RIGHT_UNDERLINE_START (2) // __:__:__ // So the left ":" center -2 // so the right ":" center + 1 // so the left "_" center - 4; // so the lfet "_" center - 1; // so the right "_" center + 2; COORD crdPos; crdPos.X = cxCenter + GAPS_LEFT_COLON; crdPos.Y = cyCenter; SetConsoleCursorPosition(hStdout, crdPos); printf (":"); crdPos.X = cxCenter + GAPS_RIGHT_COLON; SetConsoleCursorPosition(hStdout, crdPos); printf (":"); crdPos.X = cxCenter + GAPS_LEFT_UNDERLINE_START; SetConsoleCursorPosition(hStdout, crdPos); scanf ("%d", &time.hour); crdPos.X = cxCenter + GAPS_MIDDLE_UNDERLINE_START; SetConsoleCursorPosition(hStdout, crdPos); scanf ("%d", &time.min); crdPos.X = cxCenter + GAPS_RIGHT_UNDERLINE_START; SetConsoleCursorPosition(hStdout, crdPos); scanf ("%d", &time.sec); if (time.hour < 0 || time.hour > HOUR_CIRCLE || time.min < 0 || time.min > MINUTE_CIRCLE || time.sec < 0 || time.sec > SECOND_CIRCLE) return FALSE; return TRUE; } int main(int argc, char* argv[]) { InitApplication(); (g_hStdout, g_cxCenter, g_cyCenter, g_Time); // create a event to tell the program to terminate. g_DoneEvent = CreateEvent(NULL, TRUE, FALSE, NULL); #ifdef USE_TIMERQUEUE HANDLE hTimerQueue, hTimer; hTimerQueue = CreateTimerQueue(); if (!CreateTimerQueueTimer(&hTimer, hTimerQueue, TimerRoutine, &g_Time, 1000, 0, 0)) { printf("CreateTimerQueueTimer failed (%d)\\n", GetLastError()); return 3; } #else // create the thread. HANDLE hThreadTimer; DWORD dwThreadId; g_ThreadTerminated = FALSE; hThreadTimer = CreateThread(NULL, 0, TimerThreadProc, &g_Time, 0, &dwThreadId); if (hThreadTimer == NULL) { } #endif SetConsoleCtrlHandler(CtrlHandler, TRUE); if (WaitForSingleObject(g_DoneEvent, INFINITE) != WAIT_OBJECT_0) printf("WaitForSingleObject failed (%d)\\n", GetLastError()); #ifdef USE_TIMERQUEUE if (!DeleteTimerQueue(hTimerQueue)) printf("DeleteTimerQueue failed(%d) \\n", GetLastError()); #else g_ThreadTerminated = TRUE; if (WaitForSingleObject(hThreadTimer, INFINITE) != WAIT_OBJECT_0) printf("WaitForSingleObject failed (%d)\\n", GetLastError()); #endif return 0; } -------------------------------------------- 下面是纯c的。 有几个问题: 1.textmode函数在turboc中没有办法使用,不知道是什么问题,而borland c就可以。 2.无论怎么设置,自己的ctrlbreak函数在上述两个环境中都不能被调用,非常遗憾。所以不能够优雅的退出。只能按两次ctrlbreak。 下面是程序。 ------------------------------------------ #include <stdio.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <dos.h> #define ABORT 0 int jump_out_loop = -1; int jump_out(void) { jump_out_loop = 1; printf("Abort ..\n"); return ABORT; } int main(void) { struct text_info ti; int center_x, center_y; int hour, min, sec; char str_out[64] = {0}; clrscr(); /*textmode(BW40);*/ /*textmode在turbo c下设置会出问题*/ gettextinfo(&ti); center_x = ti.winright / 2; center_y = ti.winbottom / 2; gotoxy(center_x - 4, center_y); cprintf(" : : "); gotoxy(center_x - 4, center_y); cscanf("%d", &hour); gotoxy(center_x - 1, center_y); cscanf("%d", &min); gotoxy(center_x + 2, center_y); cscanf("%d", &sec); /* check input valid or not */ {} setcbrk(1); ctrlbrk(jump_out); /*jump_out没有起到作用,实在不好意思.*/ /* if (getcbrk()) printf("crtl break is on\n"); else printf("is off\n"); */ while (1) { delay(1000); sec++; if (sec >= 60) { sec -= 60; min++; if (min >= 60) { min -= 60; hour++; if (hour >= 24) { hour -= 24; } } } sprintf(str_out, "%02d:%02d:%02d", hour, min, sec); gotoxy(center_x - 4, center_y); cprintf(str_out); } /* getch();*/ return 0; }
⑺ c语言电子毫秒表计时程序
使用time()函数。它在头文件time.h中
具体使用方法如下:
time_t
a,b;//time_t是表示时间的结构体,你可以在time.h中找到它的原型。
a=time(null);//表示获取当前的机器时间。
代码段
b=time(null);//表示获取当前的机器时间。
a是代码段执行前的时间,b是代码段执行后的时间(单位是秒),那么b-a当然是代码段的执行时间了。输出时,以长整型输出时间。
希望这个解答可以帮到你。
⑻ c语言怎么查表
查表是数据结构中的一个概念。查表的前提是先建表。
在C语言实现中,建表也就是将一系版列的数据,或者权有原始数据中提取出的特征值,存储到一定的数据结构中,如数组或链表中。
查表的时候,就是对数组或链表查询的过程。常用的方式有如下几种:
1 对于有序数组,可以采用折半查找的方式快速查询。
2 对于链表,可以根据链表的构建方式,进行针对性查询算法的编写。
3 大多数情况,可以通过遍历的方式进行查表。即从第一个元素开始,一直顺序查询到最后一个元素,逐一对比。
⑼ c语言钟表(这样为什么60秒后继续输出了)
#include<stdio.h>
#include<windows.h>
intmain()
{inth,m,s;
printf("请输入当前时间");
scanf("%d:%d:%d",&h,&m,&s);
if(h>24||h<0)return0;
if(m>60||m<0)return0;
system("cls");
for(;;)
{for(;s<60;s++)
{printf("%.2d:%.2d:%.2d",h,m,s);//有修改
Sleep(1000);
system("cls");
}
s=0;//新增
m++;
if(m==60)
{h++;
m=0;
}
if(h==24)
{h=0;
}
}
return0;
}