C++ primer plus(第六版)学习笔记、习题答案(1)

快速入门

书是前几天才到,异常兴奋,看了两章,感觉写的很细,条理也很清楚,特别是课后习题的量把握的很合理,比较适合入门的人。

第一部分:课本中重要的笔记

1.泛型

泛型编程需要对语言进行扩展,以便可以只编写一个泛型函数,并用于各种实际类型,例如cout函数就可以针对不同类型的输入。

2.编程步骤

源代码到目标代码到可执行代码

3.提示

改正错误是,应首先修改第一个错误。如果在标示为有错误的那一行找不到错误,检查前一行

4. c++

对大小写敏感

5.名称空间

    using namespace std;
叫做using编译指令,可以解决命名污染问题,上面语句可以让函数使用名称空间std中所有元素,如cout,cin

6.C++源代码风格

①每条语句占一行

②函数的开始结束花括号各占一行

③函数中的语句都相对于花括号进行缩进

④与函数名称相关的圆括号周围没有空白

⑤循环结构有空白

7.原型和函数

库文件中包含了函数的编译代码,而头文件中只是包含了原型。


第二部分:课后习题


P35

2.1.

// test1.cpp -- implement show your name and address
//2014/12/4

#include <iostream>
using namespace std;

int main()
{
	cout << "my name is XXX and I live in beijing";
	cin.get();
	return 0;
}

2.2

// test2.cpp
//2014/12/4

#include <iostream>
using namespace std;

int main()
{
	int d;
	cout << "Enter the distance";
	cin >> d;
	int d2 = d * 220;
	cout << "转换后的距离:"<<endl<<d2;

	cin.get();
	cin.get();

	return 0;
}
2.3

// test3.cpp
//2014/12/4

#include <iostream>
using namespace std;

void mice();
void run();
int main()
{
	mice();
	mice();
	run();
	run();

	cin.get();
	return 0;
}

void mice()
{
	cout<<"Three blind mice"<<endl;
}

void run()
{
	cout<<"See how they run"<<endl;
}

2.4

// test4.cpp 

#include <iostream>
using namespace std;

int main()
{
	int age;
	cout<<"你的年龄"<<endl;
	cin >> age;
	cout<<"你年龄有"<<age*12<<"个月"<<endl;

	cin.get();
	cin.get();
	return 0;
}

2.5
// test5.cpp
//2014/12/4

#include <iostream>
using namespace std;

double transfer(double);

int main()
{
	double cel;

	cout<<"Please enter a Celsius value:";
	cin>>cel;
	cout<<cel<<"degrees Celsius is "<<transfer(cel)<<"degrees Fahrenheit.";

	cin.get();
	cin.get();
	return 0;
}

double transfer(double value)
{
	return value * 1.8 + 32.0;
}
2.6

// test6.cpp
//2014/12/4

#include <iostream>
using namespace std;

double transfer2(double);

int main()
{
	double light;

	cout<<"Enter the number of light years:"<<endl;
	cin>>light;
	cout<<light<<" light years =  "<<transfer2(light)<<"astronomical units";

	cin.get();
	cin.get();
	return 0;
}

double transfer2(double value)
{
	return value * 63240;
}
2.7

// test7.cpp
//2014/12/4

#include <iostream>
using namespace std;

void my_time(int,int);

int main()
{
	int h,m;

	cout<<"Enter the number of hours:"<<endl;
	cin >> h;

	cout<<"Enter the number of minutes:"<<endl;
	cin >> m;

	my_time(h,m);

	cin.get();
	cin.get();
	return 0;
}

void my_time(int h,int m)
{
	cout<<"Time:"<<h<<":"<<m;
}

继续努力。。。




















  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值