您的位置:首页 > 编程语言 > C语言/C++

2018/1/28 C++学习记录(一)

2018-01-28 23:01 381 查看
1.#include stdafx.h 作用

作用是令编译器编译出一个stdafx.obj预编译头文件(pre-compile header,需要设置编译选项),在下次编译时以降低总的编译时间.

2.实例一:摄氏度,华氏度转换

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
//input celsius
double celsius;
cout << "Enter a degree in Celsius: ";
cin >> celsius;

//process
double fahrenheit;
fahrenheit = (9.0 / 5)*celsius + 32;
cout << celsius << " Celsius is " << fahrenheit << " Fahrenheit" << endl;

return 0;
}


3.实例二:计算小费

int main()
{
//input money and tip rate
int subtotal;
int gratuityrate;
cout << "Enter the subtotal and a gratuityrate: ";
cin >> subtotal >> gratuityrate;

//process
double gratuity;
double total;
gratuity = subtotal * gratuityrate / 100.0;//注意是除以100.0
total = gratuity + subtotal;
cout << "The gratuity is $" << gratuity << " and total is $" << total << endl;

return 0;
}


实例三:将一个整数(0-1000)的所有数字相加

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
int num;
cout << "Enter a number between 0 and 1000"<<endl;
cin >> num;
int gewei = num%10;
int remain1 = num / 10;
int shiwei = remain1 % 10;
int remain2 = remain1 / 10;
int baiwei = remain2 % 10;
int remain3 = remain2 / 10;
int qianwei = remain3;
int sum = gewei + shiwei + baiwei + qianwei;
cout << "The sum of the digits is " << sum << endl;

return 0;

四:实例四,显示出当前格林尼治时间,并且输入与格林尼治时间相差的时区,输出特定时区的时间
#include "stdafx.h"
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
int totalSeconds = time(0);
int currentSeconds = totalSeconds % 60;
int totalMinutes = totalSeconds / 60;
int currentMinutes = totalMinutes % 60;
int totalHours = totalMinutes / 60;
int currentHours = totalHours % 24;

int timezoneoffset;
cout << "Current time is " << currentHours << ":" << currentMinutes << ":" << currentSeconds << endl;
cout << "Enter the time zone offset to GMT:";
cin >> timezoneoffset;
int newCurrentHours = currentHours + timezoneoffset;
cout<<"The new current time is "<<newCurrentHours<< ":" << currentMinutes << ":" << currentSeconds << endl;

return 0;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C