Q1.使用书籍,编程环境。
使用书籍C++ Primer Plus 第六版,编程环境mba+XCode。
Q2.为什么要使用using namespace std。
namespace表示不用封装内容的同名代码,使用命名空间加以区分。可以使用sts::cout等替代。类,函数和变量是C++的标准组件,他们都被存放在std中,所以一般都需要using。
Q3.为什么要在main函数末尾return 0。
mian函数被操作系统调用,所以要给操作系统返回一个返回值,通常的约定是返回0表示程序运行后成功,返回非0则表示运行不成功。
课后练习
//
// main.cpp
// exam2
//
// Created by L7ynn on 2017/2/23.
// Copyright © 2017年 L7ynn. All rights reserved.
//
#include <iostream>
#include <cmath>
using namespace std;
void proc1();
void proc2();
int main(int argc, const char * argv[]) {
//e1 编写一个C++程序,它显示您的姓名和地址
cout << "张三,广西大学" << endl;
//e2 编写一个C++程序,他要求用户输入一个以long为单位的距离,然后将它转换为码(/220)
cout << "Input long:";
double t = 0;
cin >> t;
cout << "=" << t * 220 << "码" << endl;
//e3 编写一个程序,使用3个用户定义的函数,生成指定输出
//其中一个函数要调用两次,该函数生成前两行;另一个函数也表调用两次,并生成其余的输出
proc1();proc1();
proc2();proc2();
//e4 编写一个程序,让用户输入年龄,然后显示该年龄包含多少个月
cout << "Enter your age";
cin >> t;
cout << endl << t * 12<< "month" << endl;
//e5 编写一个程序,讲摄氏度转化为华氏度
cout << "please enter a celsius value:";
cin >> t;
cout << endl << t << " degrees celsius is " << t * 1.8 + 32 << "dehrees fahrenheit" << endl;
//e6 编写一个程序,其main调用一个用户定义的函数将光年转化为天文单位
cout << "enter the number of light years:";
cin >> t;
cout << endl << t << "light years = " << t * 63240 << " astronomical units" << endl;
//e7 编写一个程序,要求用户输入小时数和分钟数,在main函数中,将这两个值传递给一个void函数
cout << "enter the number of hours:";
int h;int m;
cin >> h;
cout << endl << "enter the number of minuters:";
cin >> m;
cout << endl << "time:" << h << ":" << m << endl;
return 0;
}
void proc1()
{
cout << "three blind mice"<< endl;
}
void proc2()
{
cout << "see how they run" << endl;
}