我的代码是这样的,还少了什么?

#include <iostream>
using namespace std;
#pragma comment(lib, "winmm.lib")
#include<stdio.h>
#include <windows.h>
#include <string>
#include <stdlib.h> 
#include <time.h>
void dsptime(const struct tm* ptm)
{
cout<<ptm->tm_hour<<":"<<ptm->tm_min<<":"<<endl;
}
void main()
{
time_t nowtime;
nowtime=time(NULL);
struct *local;
local=localtime(time(NULL));
srand(time(NULL));
dsptime(local);
int n;
n=rand()%20;

}
我就是要表示下时间,然后用rand函数求下随机数,要怎么做啊?

吃鸡游戏
浏览 247回答 2
2回答

萧十郎

void main(){time_t nowtime;nowtime=time(NULL);//获取时间1970年1月1日00:00:00到当前时刻的秒数struct tm *local;//注意类型是tm struct 只是表示是结构体,tm才是结构体名称local=localtime(&nowtime);//将秒数转化成本地时间,并转成struct tm类型,该类型的各数据成员分别表示年月日时分秒。dsptime(local);//显示时间srand(time(NULL));//产生时间种子int n;n=rand()%20;//产生随机数cout<<n<<endl;}

湖上湖

时间:#include <time.h>#include <stdio.h>#include <sys/types.h>#include <sys/timeb.h>#include <string.h>void main(){char tmpbuf[128], ampm[] = "AM";time_t ltime;struct _timeb tstruct;struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };/* Set time zone from TZ environment variable. If TZ is not set,* the operating system is queried to obtain the default value&nbsp;* for the variable.&nbsp;*/_tzset();/* Display operating system-style date and time. */_strtime( tmpbuf );printf( "OS time:\t\t\t\t%s\n", tmpbuf );_strdate( tmpbuf );printf( "OS date:\t\t\t\t%s\n", tmpbuf );/* Get UNIX-style time and display as number and string. */time( <ime );printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );printf( "UNIX time and date:\t\t\t%s", ctime( <ime ) );/* Display UTC. */gmt = gmtime( <ime );printf( "Coordinated universal time:\t\t%s", asctime( gmt ) );/* Convert to time structure and adjust for PM if necessary. */today = localtime( <ime );if( today->tm_hour > 12 ){strcpy( ampm, "PM" );today->tm_hour -= 12;}if( today->tm_hour == 0 ) /* Adjust if midnight hour. */today->tm_hour = 12;/* Note how pointer addition is used to skip the first 11&nbsp;* characters and printf is used to trim off terminating&nbsp;* characters.*/printf( "12-hour time:\t\t\t\t%.8s %s\n",asctime( today ) + 11, ampm );/* Print additional time information. */_ftime( &tstruct );printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm );printf( "Zone difference in seconds from UTC:\t%u\n",&nbsp;tstruct.timezone );printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] );printf( "Daylight savings:\t\t\t%s\n",&nbsp;tstruct.dstflag ? "YES" : "NO" );/* Make time for noon on Christmas, 1993. */if( mktime( &xmas ) != (time_t)-1 )printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) );/* Use time structure to build a customized time string. */today = localtime( <ime );/* Use strftime to build a customized time string. */strftime( tmpbuf, 128,"Today is %A, day %d of %B in the year %Y.\n", today );printf( tmpbuf );}OutputOS time: 21:51:03OS date: 05/03/94Time in seconds since UTC 1/1/70: 768027063UNIX time and date: Tue May 03 21:51:03 1994Coordinated universal time: Wed May 04 04:51:03 199412-hour time: 09:51:03 PMPlus milliseconds: 279Zone difference in seconds from UTC: 480Time zone name:&nbsp;&nbsp;Daylight savings: YESChristmas Sat Dec 25 12:00:00 1993Today is Tuesday, day 03 of May in the year 1994.随机数:#include <stdlib.h>#include <stdio.h>#include <time.h>void main( void ){int i;/* Seed the random-number generator with current time so that* the numbers will be different every time we run.*/srand( (unsigned)time( NULL ) );/* Display 10 numbers. */for( i = 0; i < 10;i++ )printf( " %6d\n", rand() );}
打开App,查看更多内容
随时随地看视频慕课网APP