c++计算程序运行时间_程序设计5个基本步骤

c++计算程序运行时间_程序设计5个基本步骤注意:控制台需要加 #include “atltime.h” 头文件#include “stdafx.h” #include “atltime.h”#include <iostream>using namespace std; int _tmain(int argc, _TCHAR* argv[]){ CString strTime; //用于将CTime对象格式…


Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

注意:

控制台需要加 #include “atltime.h” 头文件

#include "stdafx.h"
 
#include "atltime.h"
#include <iostream>
using namespace std;
 
int _tmain(int argc, _TCHAR* argv[])
{ 
   
	CString strTime;		//用于将CTime对象格式化为字符串
	//获取当前的系统时间
	CTime curTime = CTime::GetCurrentTime();
 
	int nYear = curTime.GetYear();
	int nMonth = curTime.GetMonth();
	int nDay = curTime.GetDay();
	int nHour = curTime.GetHour();
	int nMin = curTime.GetMinute();
	int nSec = curTime.GetSecond();
 
	/* 方法一 */
	//输出当前时间
	cout << "当前时间:" << endl;
	cout << nYear << "年" << nMonth << "月" << nDay << "日" << nHour << "时" << nMin << "分" << nSec << "秒" << endl;
 
	/* 方法二 */
	//将当前时间curTime格式化为字符串
	strTime = curTime.Format(_T("%Y-%m-%d %H:%M:%S %A"));
	//出书格式化字符串,由于字符串使用Unicode字符,所以要使用wcout输出
	wcout << (LPCTSTR)strTime << endl;
 
	//为计算时间差,需要设定一个起始时间并输出
	CTime startTime = CTime(2017,10,1,10,20,10);
	cout << "起始时间" << endl;
	cout << startTime.GetYear() << "-" << startTime.GetMonth() << "-" << startTime.GetDay() << " " 
		<< startTime.GetHour() << ":" << startTime.GetMinute() << ":" << startTime.GetSecond() << endl;
 
	//计算时间差
	CTimeSpan timeSpan;
	timeSpan = curTime - startTime;
	cout << "两个时间相差:" << endl;
	cout << timeSpan.GetDays() << "天" << timeSpan.GetHours() << "小时" 
		<< timeSpan.GetMinutes() << "分" << timeSpan.GetSeconds() << "秒"<< endl;
 
	cout << "总小时数:" << timeSpan.GetTotalHours() << "小时" << endl;
	cout << "总分钟数:" << timeSpan.GetTotalMinutes() << "分钟" << endl;
	cout << "总秒数:" << timeSpan.GetTotalSeconds() << "秒" << endl;
 
	getchar();
	return 0;
}

效果图:

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/170955.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • 2.session.setAttribute()和session.getAttribute()区别和联系

    2.session.setAttribute()和session.getAttribute()区别和联系2.session.setAttribute和session.getAttribute()区别和联系在web开发的时候,使用的都是B/S架构,浏览器与服务器直接连接,在服务端就会自动创建一个session对象.。session.setAttribute(“username”,username);》》是将username保存在session中!session的key值为“usern…

    2022年10月17日
  • SVN服务器搭建和使用(一)[通俗易懂]

    SVN服务器搭建和使用(一)[通俗易懂]一、什么是SVNSVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS、CVS,它采用了分支管理系统,它的设计目标就是取代CVS。二、SVN的下载安装下载地址:http

    2022年7月3日
  • ES6数组方法汇总

    ES6数组方法汇总1.forEachforEach会遍历数组,没有返回值,不允许在循环体内写return,不会改变原来数组的内容.constarray=[1,2,3,4];array.forEach((item,index,array)=>{  console.log(item)//顺序打出1234})2.mapmap遍历数组,会返回一个新…

    2022年6月12日
  • APK签名机制之——V2签名机制详解「建议收藏」

    APK签名机制之——V2签名机制详解「建议收藏」通过前一篇《Apk签名机制之——JAR签名机制详解》的分析我们知道,JAR签名需要对apk内所有文件进行hash校验,当资源较多时签名验证速度较慢。为了加快验证速度并加强完整性保证,Andorid在7.0引入一种全文件签名方案V2。下面来看V2方案的具体设计原理。

    2022年5月10日
  • java中文乱码怎么解决_java中文乱码解决总结

    java中文乱码怎么解决_java中文乱码解决总结为什么说乱码是中国程序员无法避免的话题呢?这个首先要从编码机制上说起,大家都是中文和英文的编码格式不是一样,解码也是不一样的!如果中国的程序员不会遇到乱码,那么只有使用汉语编程。汉语编程是怎么回事我也不大清楚,应该是前年吧,我一朋友给我介绍汉语编程,怎么不错不错?当时因为学习忙没去关注这个,等我闲了,那个朋友不弄这个,问他他也不说不大清楚,最后自己对这个学习也不了了之了。今天我写这个不是讲解中英文…

    2022年7月8日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号