2018/1/28 C++学习记录(一)
2018-01-28 23:01
381 查看
1.#include stdafx.h 作用
作用是令编译器编译出一个stdafx.obj预编译头文件(pre-compile header,需要设置编译选项),在下次编译时以降低总的编译时间.
2.实例一:摄氏度,华氏度转换
3.实例二:计算小费
实例三:将一个整数(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;
作用是令编译器编译出一个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++————学习记录
- C++ dll 学习记录
- 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始
- c++学习记录:operator = 重载
- C++之学习记录1
- 关于c++的学习情况,不再更新了,因为找到了更好的笔记记录工具
- 算法学习记录四(C++)--->通过前序和中序序列重建二叉树
- C++学习、成长记录(2011-2012第一学期第18周)
- C++学习记录
- C++中数组学习要点记录
- C++学习记录5--如何使用math.h中定义的PI
- C++文本文件读写|学习记录
- C++学习记录----C++成员函数指针
- 算法学习记录十二(C++)--->链表题目集合
- C++标准模板库(STL) 学习记录 Part2
- 算法学习记录七(C++)--->二分法找有序旋转数组最小值
- C++学习记录12--#include<iomanip>成员函数
- 算法学习记录九(C++)--->二进制中1的个数
- c++学习记录2
- 孙鑫C++教程学习记录