栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

《C++》Primer Plus 第二章(含所有习题详解)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

《C++》Primer Plus 第二章(含所有习题详解)

目录

前言

第二章示例程序

第二章习题答案

第二章编程题答案


前言

Hi,有趣的灵魂。

我是一名自学编程的初中生,在此记录下我的学习内容及心得。

本系列主要用于记录 C++ 相关知识,希望使您有所收获~

注:所有程序均在 Win 11 最新版 VScode(C++11)环境中测试通过。

话不多说,直入主题。

今天我们主要来聊一聊《C++ Primer Plus》这本书第二章的内容。


以下是第二章的示例程序:

(示例程序中的部分注释是我做的笔记)

【2.1】

// hello_world.cpp -- displays a message
# include  

// 预处理器:处理名称以#开头的编译指令,将在程序编译时自动运行(在源代码被编译前,替换或添加文本)
// 预处理编译指令,让预处理器将iostream文件(包含文件/头文件)的内容添加到程序中    

int main()    // 函数头
// 函数体,用 {} 括起
{  
    using namespace std;    // 编译指令,使用名称空间
    
    cout << "Hello World!" << endl;    // cout工具输出(流),endl 表示换行
    cout << "Hello C++ World!" << endl;
     
    system("pause");
    return 0;    // 结束函数,返回值(main 函数可省略该语句),返回给操作系统
} 

【2.2】

// carrots.cpp -- uses and displays a variable
#include 

int main()
{
    using namespace std;

    int carrots;    // 声明一个整型变量

    carrots = 25;    // 给变量赋值,不赋值则默认为0  
    cout << "I have ";
    cout << carrots;    // 显示变量的值
    cout << " carrots.";
    cout << endl;
    carrots = carrots - 1;    // 修改变量的值,将其减 1
    cout << "Crunch, crunch. Now I have " << carrots << " carrots." << endl;
    
    system("pause");
    return 0;   
}

【2.3】

// getinfo.cpp -- input and output
#include 

int main()
{
    using namespace std;

    int carrots;

    cout << "How many carrots do you have?" << endl;
    cin >> carrots;    // C++ 输入语句,输入流
    cout << "Here are two more. ";
    carrots = carrots + 2;
    cout << "Now you have " << carrots << " carrots." << endl;    // 连结输出 
    
    system("pause");
    return 0;
}

【2.4】

// sqrt.cpp -- using the sqrt() function
#include 
#include     

// 导入头文件 cmath,其中包含 sqrt 函数的原型。
// 函数原型:type_name func_name(argument_list);

int main()
{
    using namespace std;

    double num;

    cout << "Please input a number:" << endl;
    cin >> num;
    double root = sqrt(num);    // 编译器编译程序时会自动在库文件中搜索使用的函数
    cout << "The square root of " << num << " is " << root << endl;
    
    system("pause");
    return 0;
}

【2.5】

// ourfunc.cpp -- defining your own function
#include 

// 创建函数的基本格式 —— type funcname(parameterlist)
// type 表示返回值,parameterlist 表示接受的参数列表
// (函数原型)

using namespace std;    // 将该编译指令放在所有函数前,使得所有函数均可访问名称空间
void ella(int);    // 将定义的函数的原型放到 main 之前,void代表无返回值,int代表接受一个整数参数

int main()
{
    // using namespace std;
    ella(1102);    // 调用函数
    cout << "Pick an interger: ";    // 无换行符,输入提示与输入将出现在同一行中
    int count;    // 声明变量 count
    cin >> count;
    ella(count);
    cout << "Done!" << endl;
    
    system("pause");
    return 0;
}

void ella(int n)    // n 是一个新的变量,函数调用时传递的值将被赋给它
{
    // using namespace std;
    cout << "Ella says that she touch your toes " << n << " times." << endl;

}

【2.6】

// convert.cpp -- converts pounds to kilograms 
#include 

double lbtokg(double);    // 函数原型

int main()
{
    using namespace std;
    double lb;
    cout << "Enter your weight in pounds: ";
    cin >> lb;    // 通过用户输入来给变量赋值
    double kilogram = lbtokg(lb);    // 调用函数,lb 将作为参数被传递给 lbtokg 函数
    cout << lb << " pounds = " << kilogram << " kilograms." << endl;
    
    system("pause");
    return 0;
}

double lbtokg(double ltk)
{
    // return 2.2 / ltk; 
    double kg = ltk / 2.2;
    return kg;    // 将结果返回给 main
}

以下是第二章的习题答案:


 以下是第二章的编程题答案:

【2_p1】

// name.cpp -- dispalys your name and address

#include 

int main()
{
    using namespace std;
    cout << "My name is Li Tianxing, I'm from Beijing." << endl;
    
    system("pause");
    return 0;
}

【2_p2】

// long.cpp -- converts metres to kilometres

#include 

int main()
{
    using namespace std;

    double metres;
    cout << "Please enter how many meters you have walked: ";
    cin >> metres;
    double km = metres / 1000;
    cout << metres << " metres =  " << km << " kilometres." << endl;

    system("pause");
    return 0;
}

【2_p3】

// funcs.cpp -- using 3 functions

#include 

using namespace std;    // 将名称空间放在所有函数之前(之外),使得所有函数均可使用该编译指令
void tbm(void);
void shtr(void);    // 没有返回值,不接收任何参数的函数

int main()
{
    tbm();    // 不接收任何信息的函数括号为空
    shtr();
    tbm();    // 不返回任何信息的函数不能使用赋值语句,直接调用即可
    shtr();
    system("pause");
    return 0;
}

void tbm()    // 在 C++ 中空括号与 void 等价
{
    cout << "Three blind mice," << endl;
}

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

【2_p4】

// month.cpp -- how many months your age include?

#include 

int main()
{
    using namespace std;

    double age;
    cout << "How old are you? ";
    cin >> age;
    double months = age * 12;
    cout << "Your age include " << months << " months." << endl;
    
    system("pause");
    return 0;
}

【2_p5】

// degrees.cpp -- converts °C to °F
#include 

double c_to_f(double);

int main()
{
    using namespace std;

    double c;
    cout << "Degree centigrade: ";
    cin >> c;
    double f = c_to_f(c);
    cout << c << " C = " << f << " F.";
    
    system("pause");
    return 0;
}

double c_to_f(double degrees)
{
    return degrees * 1.8 + 32;
}

【2_p6】

// astronomical_units.cpp -- converts light years to astronomical units
#include 

double au_to_ly(double);

int main()
{
    using namespace std;

    double ly; 
    cout << "Enter the number of light years: ";
    cin >> ly;
    double au = au_to_ly(ly);
    cout << ly << " light years = " << au << " astronomical units." << endl;
    
    system("pause");
    return 0;
}

double au_to_ly(double light_years)
{
    return 63240 * light_years;
}

【2_p7】

// time.cpp -- what time is it now?
#include 

using namespace std;
void now_time(int, int);    // 括号内表明该函数接收两个参数,参数之间用逗号分开

int main()
{
    int h;
    cout << "Enter the number of hours: ";
    cin >> h;
    int m;
    cout << "Enter the number of minutes: ";
    cin >> m;
    
    now_time(h, m);    // 向函数传递两个参数
    system("pause");
    return 0;
}

void now_time(int hours, int minutes)     // 分别命名
{
    cout << "Time: " << hours << ":" << minutes << endl;
}
Hey!谢谢你一直看到这里!我们下一篇再见! 2020-6-19.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/983561.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号