C++ primer plus 第二章 习题
来源:互联网 发布:新疆矩阵农产品交易 编辑:程序博客网 时间:2024/06/05 20:36
helloworld.cpp
#include <iostream>
//include 是iostream 而不是c中的stdio.h
int main(void){
using namespace std;//使用命名空间 using 的用法cout << "hello,world!";//cout 使用,<< 与>> 重定向符
cout << endl;
return 0;
}
carrots.cpp
#include <iostream>
int main(void)
{
using namespace std;
int carrots;//变量声明与使用与c一样
carrots=25;
cout << "i have " << carrots << " carrots."<<endl<<"Crunch,Crunch,now i have "<<carrots-1<<" carrots"<<endl;
return 0;
}
getinfo.cpp
#include <iostream>
int main(void)
{
using namespace std;
int carrots;
cout <<"how many carrots do you have?"<<endl;
cin >> carrots;//获取输入,赋值变量
cout <<"here are two more"<<endl;
carrots+=2;
cout <<"Now you have "
<<carrots
<<" carrots"
<<endl;//多行拼接,格式更好.
return 0;
}
sqrt.cpp
#include <iostream>
#include <cmath>//or match.h
int main(void)
{
using namespace std;
double area;
cout << "Enter the floor area ,in square feet,of your home "<<endl;
cin >> area;
double side;
side = sqrt(area);
cout <<"That's the equivalent of a square "
<<side
<<" feet to the side"
<<endl
<<"How fascinating!"
<<endl;
return 0;
}
//ourfunc.cpp --define our own function
#include <iostream>
void simon(int);//注意函数声明需要加;include不需要加;
int main(){
using namespace std;
int count;
cout <<"pick an integer\n";
cin >> count;
simon(count);
cout<<"done";
return 0;
}
void simon(int n){
using namespace std;//可以将这个语句放在include后面,本文件中的所有函数使用std,不需要每个函数都重复输入
cout << "Simon says touch your toes "<<n<<" times"<<endl;
}
//convert.cpp
#include <iostream>
double stoneToLb(double);
int main(){
using namespace std;
double stone;
cout<<"Enter the weight in stone"<<endl;
cin >> stone;
double pounds;
pounds=stoneToLb(stone);
cout << stone << " stone = " <<pounds<<" pounds"<<endl;
return 0;
}
double stoneToLb(double sts)
{
return sts*14;
}
习题:
复习题:
1,c++程序的模块叫做什么?
函数
2,下面的预处理器编译指令是做什么用的?#include <iostream>
在编译前使用iostream 文件替换该指令位置.
3,下面的语句是做什么用的?using namespace std;
程序可以使用std中的定义的常量,方法等.
//program_exercise1.cpp
//1,编写一个c++程序,它显示您的姓名和地址
#include <iostream>
using namespace std;
int main(void){
cout <<"my name is hujiawei"
<<endl
<<"my address is nanjing"
<<endl;
return 0;
}
//2.编写一个c++程序,它要求用户输入一个以long为单位的距离,然后将它转换为码(1 long= 220 ma);
//program_exercise2.cpp
#include <iostream>
using namespace std;
int main(void){
double along,ma;
cout <<"Enter the distance in long:";
cin >> along;
ma=220*along;
cout <<"the distance is equivalent to "<<ma<<" ma"<<endl;
return 0;
}
/*3,编写一个c++程序,它使用三个用户定义的函数,(包括main()),并生辰下面的输出:
Three blind mice
Three blind mice
See how they run
See how they run
*/
//program_exercise3.cpp
#include <iostream>
using namespace std;
void blind(void);
void run(void);
int main(void){
blind();
run();
return 0;
}
void blind(void){
cout <<"Three blind mice"
<<endl
<<"Three blind mice"
<<endl;
}
void run(void){
cout <<"See how they run"
<< endl
<<"See how they run"
<<endl;
}
/*4,编写一个c++程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下所示:
Enter your age:29
*/
//program_exercise4.cpp
#include <iostream>
using namespace std;
int main(void){
int age;
cout << "Enter your age:";
cin >> age;
cout << age<<"age = "<< age*12 << " monthes"<<endl;
return 0;
}
/*5,编写一个c++程序,其中的main()调用一个用户定义的函数(以摄氏温度值为参数,并返回相应的华氏温度值).
该程序按下面的格式要求用户输入摄氏温度值.并显示结果:
Please enter a Celsius value:20
20 degrees Celsius is 68 degrees Fahrenheit.
下面是转换公式:
Fahrenheit=1.8*Celsius+32.0
*/
//program_exercise5.cpp
#include <iostream>
using namespace std;
double celToFah(double);
int main(void){
cout <<"Please enter a Celsius value:";
double cel,fah;
cin >>cel;
fah=celToFah(cel);
cout << cel
<< " degrees Celsius is "
<<fah
<<" degrees Fahrenheit."
<<endl;
return 0;
}
double celToFah(double cel){
return 1.8*cel+32.0;
}
/*6,编写一个c++程序,其main()调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值).
该程序按下面的格式要求用户输入光年值,并显示结果:
Enter the number of light years:4.2
4.2 light years = 265608 astronomical units.
天文单位是从地球到太阳的平均距
1光年=63240 天文单位
*/
//program_exercise6.cpp
#include <iostream>
using namespace std;
double lyToAu(double);
int main(void){
double ly;
cout <<"Enter the number of light years:";
cin >>ly;
cout << ly<<" light years = "<<lyToAu(ly)<<" astronomical units."<<endl;
return 0;
}
double lyToAu(double ly){
return 63240.0*ly;
}
/*7,编写一个c++程序,要求用户输入小时数和分钟数.在main()函数中,将这两个值给一个void函数,后者以下面这样的格式
显示这两个值:
Enter the number of hours:9
Enter the number of minutes:28
Time: 9:28
*/
//program_exercise7.cpp
#include <iostream>
using namespace std;
void showTime(int ,int);
int main(void){
cout <<"Enter the number of hours:";
int h,m;
cin>>h;
cout <<"Enter the number of minutes:";
cin>>m;
showTime(h,m);
return 0;
}
void showTime(int h,int m){
cout<<"Time: "<<h<<":"<<m<<endl;
}
- c primer plus 第二章习题摘录
- 《C Primer Plus》第二章 C语言概述 课后习题
- 【C Primer Plus】【课后习题】第二章C语言概述
- 《C Primer Plus 》第六版 习题 第二章
- C++ Primer Plus 课后习题 第二章
- C++ primer plus 第二章 习题
- C++ Primer Plus第二章习题
- C primer plus 第二章
- C Primer Plus 第二章
- c++primer第二章习题
- C++primer 第二章习题
- C++primer第二章习题
- c primer plus第14章习题
- C Primer Plus 第五章习题
- C primer plus第七章习题
- c primer plus 11章习题答案
- c primer plus第十一章课后习题
- C++primer plus 第五章习题!
- 链表:链表中环的入口结点
- 基于raspbian+motion的家庭监控网络
- Axon Framework配置
- Axon Framework命令模型
- 字符格式转换
- C++ primer plus 第二章 习题
- 记录昨天20170607
- 论文排版中的问题及对策
- 常用的加密方式学习笔记
- java垃圾回收策略及对象管理机制
- 欢迎使用CSDN-markdown编辑器
- ATL--创建简单的ATL之dll工程,添加类和类的接口并在MFC中调用
- Python Tkinter界面应用开发-05 拉出
- hdu 1426