
//其实这个并不难,只要你学习一下timeh中的时间函数就能自己做了。
#include <stdioh>
#include <stdlibh>
#include <timeh>
#include <windowsh>
typedef struct
{
int year;
int month;
int day;
int hour;
int minute;
int second;
int week;
}myDate;
int leap(int year)
{
if (year%4 == 0 && year%100 !=0 || year%400 == 0)
{
return 1;
}
return 0;
}
int main()
{
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char weekday[] = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
time_t t = time(NULL);
struct tm cur = localtime(&t);
myDate Date = {cur->tm_year+1900, cur->tm_mon + 1, cur->tm_mday, cur->tm_hour, cur->tm_min, cur->tm_sec, cur->tm_wday};
printf("%4d年%02d月%02d日[%s]%02d时%02d分%02d秒\r", Dateyear, Datemonth, Dateday, weekday[Dateweek], Datehour, Dateminute, Datesecond);
while(1)
{
Sleep(1000);
days[2] = (leap(Dateyear) 28:29);
Datesecond++;
if ( Datesecond == 60)
{
Datesecond = 0;
Dateminute++;
}
if (Dateminute == 60)
{
Dateminute = 0;
Datehour++;
}
if (Datehour == 24)
{
Datehour = 0;
Dateweek = (++Dateweek)%7;
Dateday++;
}
if (Dateday == (days[Datemonth] + 1))
{
Dateday = 1;
Datemonth++;
}
if (Datemonth == 13)
{
Datemonth = 1;
Dateyear++;
}
printf("%4d年%02d月%02d日[%s]%02d时%02d分%02d秒\r", Dateyear, Datemonth, Dateday, weekday[Dateweek], Datehour, Dateminute, Datesecond);
//时间的不换行显示是通过'\r'来实现的
}
return 0;
}
没做完,再等等
#include <iostream>
using namespace std;
class time
{
private:
short int hour;
short int minute;
short int sec;
short int year;
short int month;
short int day;
bool am , pm , _12_24 , AD ;
public:
time()
{
SetYear();
SetMonth();
SetDay();
SetHour();
SetMinute();
SetSec();
cout << "\n\n" ;
_12_24 = true;
}
void SetYear()
{
a:
cout << "请输入年:";
cin >> year;
if (year == 0 )
{
cout << "年份不得为0,请重新输入!" ;
goto a;
}
else if (year > 0 )
{
AD = true;
}
else if (year < 0 )
{
AD = false ;
}
}
void SetMonth()
{
a:
cout << "请输入月:";
cin >> month;
if (month > 12)
{
cout << "输入错误,请重新输入!" << endl;
goto a;
}
}
void SetDay()
{
int i ;
if (year % 4 == 0 )
{
i = 29;
}
else
{
i = 28 ;
}
const int c[] = {31,i,31,30,31,30,31,31,30,31 ,30,31};
for (i = 0 ;i < (month-1) ;i++)
{
;
}
a:
cout << "请输入日:";
cin >> day;
if (day > c[i])
{
cout << "输入错误,请重新输入!" << endl;
goto a;
}
}
void SetHour()
{
a:
cout << "请输入时:";
cin >> hour;
if (hour > 24)
{
cout << "输入错误,请重新输入!" << endl;
goto a;
}
}
void SetMinute()
{
a:
cout << "请输入分:";
cin >> minute;
if (minute > 60)
{
cout << "输入错误,请重新输入!" << endl;
goto a;
}
}
void SetSec()
{
a:
cout << "请输入秒:";
cin >> sec;
if (sec > 60)
{
cout << "输入错误,请重新输入!" << endl;
goto a;
}
}
void display()
{
cout << endl ;
if (year < 0 )
AD = false;
else
AD = true;
if (AD == true)
{
cout << "公元" << year;
}
else if (AD == false)
{
double temp;
temp = (double)year;
temp = abs(temp);
year = (int)temp;
cout << "公元前" << year;
}
cout <<"年" << month << "月" << day << "日" << endl;
if (_12_24 != true)
{
if (am == true)
{
cout << "上午" ;
}
else if (pm == true)
{
cout << "下午" ;
}
}
cout << hour << "时" << minute << "分" << sec << "秒" << endl;
}
void conversion()
{
int flag;
cout <<endl << "\t\t\t1转换为24小时" << endl
<< "\t\t\t2转换为12小时" << endl << "请选择:" ;
cin >> flag;
switch(flag)
{
case 1:
{
if (_12_24 != true )
{
_12_24 = true;
am = pm = false;
hour += 12;
}
else
{
break;
}
}
case 2:
{
if (_12_24 != false)
{
_12_24 = false;
if (hour >= 12 )
{
pm = true;
am = false;
hour -= 12;
break ;
}
if (hour < 12)
{
pm = false;
am = true;
break ;
}
}
else
{
break;
}
}
}
}
bool compare(time a)
{
if (year > aViewYear() )
{
return true;
}
else
{
if (month > aViewMonth() )
{
return true;
}
else
{
if (day > aViewDay() )
{
return true;
}
else
{
if (hour > aViewHour() )
{
return true;
}
else
{
if (minute > aViewMinute())
{
return true;
}
else
{
if (sec > aViewSec())
{
return true;
}
else
{
return false;
}
}
}
}
}
}
}
void operator + (int a )
{
int flag , i;
if (year % 4 == 0 )
{
i = 29;
}
else
{
i = 28 ;
}
const int c[] = {31,i,31,30,31,30,31,31,30,31 ,30,31};
for (i = 0 ;i < (month-1);i++)
{
;
}
cout << "\t\t\t1给年加" << endl << "\t\t\t2给月加" << endl
<< "\t\t\t3给日加" << endl << "\t\t\t4给时加" << endl
<< "\t\t\t5给分加" << endl <<"\t\t\t6给秒加" <<endl
<< "请选择:";
cin >> flag;
switch(flag)
{
case 1:
{
year += a ;
if (AD == false && year >= 0 )
{
AD = true ;
year += 1;
}
break;
}
case 2:
{
month += a;
if (month >= 12 )
{
year += 1 ;
if (AD == false && year >= 0 )
{
year += 1;
AD == true;
}
month = 0 ;
}
break;
}
case 3:
{
int temp;
day += a;
if (day >= c[i])
{
month += 1 ;
day -= c[i];
if (month > 12 )
{
year += 1 ;
if (AD == false && year >= 0 )
{
year += 1;
AD == true;
}
month = 1 ;
}
}
break;
}
case 4:
{
int temp;
hour += a ;
if (hour >= 24)
{
day += 1;
hour = 0;
if (day >= c[i])
{
month += 1 ;
day -= c[i];
if (month > 12 )
{
year += 1 ;
if (AD == false && year >= 0 )
{
year += 1;
AD == true;
}
month = 1 ;
}
}
}
break;
}
case 5:
{
int temp;
minute += a ;
if (minute >= 60)
{
hour += 1 ;
if (hour >= 24)
{
day += 1;
hour = 0;
if (day >= c[i])
{
month += 1 ;
day -= c[i];
if (month > 12 )
{
year += 1 ;
if (AD == false && year >= 0 )
{
year += 1;
AD == true;
}
month = 1 ;
}
}
}
}
break;
}
case 6:
{
int temp;
sec += a;
if (sec >= 60 )
{
minute += 1 ;
sec = 0 ;
if (minute >= 60)
{
hour += 1 ;
if (hour >= 24)
{
day += 1;
hour = 0;
if (day >= c[i])
{
month += 1 ;
day -= c[i];
if (month > 12 )
{
year += 1 ;
if (AD == false && year >= 0 )
{
year += 1;
AD == true;
}
month = 1 ;
}
}
}
}
}
}
break;
}
}
void operator - (int a )
{
int flag;
cout << "\t\t\t1给年减" << endl << "\t\t\t2给月减" << endl
<< "\t\t\t3给日减" << endl << "\t\t\t4给时减" << endl
<< "\t\t\t5给分减" << endl << "\t\t\t6给秒减" <<endl
<<"请选择:";
cin >> flag;
switch(flag)
{
case 1:
{
year -= a;
if (year <= 0 && AD == true)
{
year -= 1;
AD = false ;
}
break;
}
case 2:
{
if (a < month)
month -= a;
else
{
year -= 1 ;
if (year <= 0 && AD == true)
{
year -= 1;
AD = false ;
}
if (a < 12 )
month = 12 - (a - month ) ;
else
cout <<"您输入的数大于或者等于12,请直接用年来减";
}
break;
}
case 3:
{
int i ;
if (year % 4 == 0 )
{
i = 29;
}
else
{
i = 28 ;
}
const int c[] = {31,i,31,30,31,30,31,31,30,31 ,30,31};
for (i = 0 ;i < (month-1) ;i++)
{
;
}
if (day >= c[i])
{
cout << "您输入的数大于或者等于了这一月的天数,请直接用月减";
break;
}
if (a < day)
day -= a;
else
{
month -= 1;
if (month == 0 )
{
month = 12 ;
year -= 1 ;
if (year <= 0 && AD == true )
{
year -= 1;
}
}
day = c[i] - (a - day) - 1 ;
}
break;
}
case 4:
{
if (a <= hour)
hour -= a ;
else
{
}
break;
}
case 5:
{
if (a <= minute)
minute -= a ;
else
{
}
break;
}
case 6:
{
if (a <= sec)
sec -= a;
else
{
}
break;
}
}
}
};
/
2、重载”+”、”-”运算,日期时间加上天数和时间等于新的日期时间,日期时间
减去天数和时间等于新的日期时间;(考虑闰年的情况)
/
用c++的话还是弄成类比较好的,下面是我的解决方法:
#include <ctime>
#include <cstring>
class Timer
{
private:
static const int size = 50;
std::time_t time;
std::tm localTime;
char Ascll[size];
char flag;
public:
Timer()
{
std::time(&time);
flag = 0;
}
~Timer() {}
const char printTime()
{
if(!flag)
{
localTime = std::localtime(&time);
std::strcpy(Ascll, std::asctime(&localTime));
}
flag++;
return Ascll;
}
double timeDiff(Timer time2) const
{
return difftime(time, time2 -> time);
}
} ;
int main()
{
Timer t1;
for(int i = 0; i < 10000; i++ )
{
cout << i;
}
Timer t2;
std::cout << t1printTime() << std::endl;
std::cout << t2printTime() << std::endl;
std::cout << t2timeDiff(&t1) << std::endl;
return 0;
}
以下程序直接显示系统时间
希望对你有帮助
DATAS SEGMENT
BUF1 DB'The time is:$'
BUF2 DB 10
DB 10 DUP()
BUF3 DB ' $'
BUF4 DB ' $'
BUF5 DB ' $'
BUF6 DB ' $'
BUF7 DB ' $'
BUF8 DB ' $'
BUF9 DB ' $'
BUF10 DB '$'
DATAS ENDS
STACKS SEGMENT
DB 100 DUP()
STACKS ENDS
CODES SEGMENT
ASSUME CS:CODES,DS:DATAS,SS:STACKS
CURSOR MACRO ROW,CLM
MOV AH,2
MOV BH,0
MOV DH,ROW
MOV DL,CLM
INT 10H
ENDM
WIN MACRO ROWL,CLML,ROWR,CLMR,COLOR
MOV AH,6
MOV AL,0
MOV CH,ROWL
MOV CL,CLML
MOV DH,ROWR
MOV DL,CLMR
MOV BH,COLOR
INT 10H
ENDM
ASCBCD MACRO REG
MOV al,REG
mov ah,0
mov REG,0
mov bh,10
div bh
mov REG,ah
push cx
MOV CL,4
SHL al,CL
pop cx
add REG,al
ENDM
BCDASC MACRO REG
INC BX
INC BX
MOV AL,REG
push cx
MOV CL,4
SHR AL,CL
pop cx
OR AL,30H
MOV [BX],AL
INC BX
MOV AL,REG
AND AL,0FH
OR AL,30H
MOV [BX],AL
ENDM
INCBCD MACRO REG,COUNT
MOV AL,REG
INC AL
DAA
MOV REG,AL
CMP AL,COUNT
JNZ DISPY
MOV REG,0
ENDM
STRDSPY MACRO ADRS
LEA DX,ADRS
MOV AH,9
INT 21H
ENDM
CLOCK PROC FAR
START: PUSH DS
MOV AX,0
PUSH AX
MOV AX,DATAS
MOV DS,AX
WIN 0,0,24,79,7
WIN 6,9,18,70,01000001B
CURSOR 6,25
STRDSPY BUF5
CURSOR 7,25
STRDSPY BUF6
CURSOR 8,25
STRDSPY BUF7
CURSOR 9,25
STRDSPY BUF8
CURSOR 10,25
STRDSPY BUF9
CURSOR 11,25
STRDSPY BUF10
CURSOR 6,9
STRDSPY BUF3
CURSOR 7,9
STRDSPY BUF4
CURSOR 14,32
STRDSPY BUF1
CURSOR 15,36
MOV AH,2cH
INT 21H
ASCBCD CH
ASCBCD CL
ASCBCD DH
TIMER: CALL DELY
INCBCD DH,60H
INCBCD CL,60H
INCBCD CH,24H
DISPY: LEA BX,BUF2
BCDASC CH
BCDASC CL
BCDASC DH
INC BX
MOV AL,'$'
MOV [BX],AL
PUSH DX
CURSOR 15,36
STRDSPY BUF2+2
POP DX
MOV AH,01H
INT 16H
CMP AL,1BH
JZ X
XOR AL,AL
JMP TIMER
X:MOV AH,4CH
INT 21H
RET
DELY PROC
PUSH CX
PUSH AX
push bx
mov bx,4
x3:MOV AX,9000
X1:MOV CX,0FFFFH
X2:DEC CX
JNE X2
DEC AX
JNE X1
dec bx
jne x3
pop bx
POP AX
POP CX
RET
DELY ENDP
CLOCK ENDP
CODES ENDS
END START
import javautilCalendar;
import javautilDate;
import javautilTimer;
import javautilTimerTask;
public class Test {
public static void main(String[] args) {
//timer1();
timer2();
//timer3();
//timer4();
}
// 第一种方法:设定指定任务task在指定时间time执行 schedule(TimerTask task, Date time)
public static void timer1() {
Timer timer = new Timer();
timerschedule(new TimerTask() {
public void run() {
Systemoutprintln("-------设定要指定任务--------");
}
}, 2000);// 设定指定的时间time,此处为2000毫秒
}
// 第二种方法:设定指定任务task在指定延迟delay后进行固定延迟peroid的执行
// schedule(TimerTask task, long delay, long period)
public static void timer2() {
Timer timer = new Timer();
timerschedule(new TimerTask() {
public void run() {
Systemoutprintln("-------设定要指定任务--------");
}
}, 1000, 1000);
}
// 第三种方法:设定指定任务task在指定延迟delay后进行固定频率peroid的执行。
// scheduleAtFixedRate(TimerTask task, long delay, long period)
public static void timer3() {
Timer timer = new Timer();
timerscheduleAtFixedRate(new TimerTask() {
public void run() {
Systemoutprintln("-------设定要指定任务--------");
}
}, 1000, 2000);
}
// 第四种方法:安排指定的任务task在指定的时间firstTime开始进行重复的固定速率period执行.
// TimerscheduleAtFixedRate(TimerTask task,Date firstTime,long period)
public static void timer4() {
Calendar calendar = CalendargetInstance();
calendarset(CalendarHOUR_OF_DAY, 12); // 控制时
calendarset(CalendarMINUTE, 0); // 控制分
calendarset(CalendarSECOND, 0); // 控制秒
Date time = calendargetTime(); // 得出执行任务的时间,此处为今天的12:00:00
Timer timer = new Timer();
timerscheduleAtFixedRate(new TimerTask() {
public void run() {
Systemoutprintln("-------设定要指定任务--------");
}
}, time, 1000 60 60 24);// 这里设定将延时每天固定执行
}
}
以上就是关于如何让编写一个显示时间的小程序 用C或C++全部的内容,包括:如何让编写一个显示时间的小程序 用C或C++、C++时间计算程序、c++时钟程序代码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)