取得计算机名
#include <iostream>
using namespace std;
int main()
{
char name[256]={};
int res;
res = gethostname(name, 256);
if(res < 0)
cout<<"error"<<endl;
else
cout<<name<<endl;
}
显示时间
#include <iostream>
using namespace std;
int main()
{
time_t t1;
time_t t2;
t1 = time(&t2);
cout<<t1<<endl<<t2<<endl;
int h = (t1/3600+8)/12;
int m = t1/60%60;
int s = t1%60;
cout<<h<<":"<<m<<":"<<s<<endl;
}
转换为人可识别的时间
#include <iostream>
using namespace std;
int main()
{
time_t t;
t = time(NULL);
tm* ptime = NULL;
ptime = localtime(&t);
cout<<ptime->tm_year+1900<<"年"
<<ptime->tm_mon+1<<" "
<<ptime->tm_mday<<" "
<<ptime->tm_wday<<" "
<<ptime->tm_hour<<":"
<<ptime->tm_min<<endl;
}
电子钟(记忆:字符串格式的时间)
#include <iostream>
using namespace std;
int main()
{
for(;;)
{
time_t t;
t = time(NULL);
tm* p = NULL;
p = localtime(&t);
char buf[100]={};
strftime(buf, 100, "%F %T");
cout<<"\r"<<buf<<endl;
sleep(1);
}
}
#include <iostream>
using namespace std;
int main()
{
char name[256]={};
int res;
res = gethostname(name, 256);
if(res < 0)
cout<<"error"<<endl;
else
cout<<name<<endl;
}
显示时间
#include <iostream>
using namespace std;
int main()
{
time_t t1;
time_t t2;
t1 = time(&t2);
cout<<t1<<endl<<t2<<endl;
int h = (t1/3600+8)/12;
int m = t1/60%60;
int s = t1%60;
cout<<h<<":"<<m<<":"<<s<<endl;
}
转换为人可识别的时间
#include <iostream>
using namespace std;
int main()
{
time_t t;
t = time(NULL);
tm* ptime = NULL;
ptime = localtime(&t);
cout<<ptime->tm_year+1900<<"年"
<<ptime->tm_mon+1<<" "
<<ptime->tm_mday<<" "
<<ptime->tm_wday<<" "
<<ptime->tm_hour<<":"
<<ptime->tm_min<<endl;
}
电子钟(记忆:字符串格式的时间)
#include <iostream>
using namespace std;
int main()
{
for(;;)
{
time_t t;
t = time(NULL);
tm* p = NULL;
p = localtime(&t);
char buf[100]={};
strftime(buf, 100, "%F %T");
cout<<"\r"<<buf<<endl;
sleep(1);
}
}