C++ Primer Plus 第六版 编程练习题及详细答案

答案分两种,第一种为蓝桥官方答案,第二种为我个人学习编写。

第二章

2.1

编写一个C++程序,它显示您的姓名和地址。

cpp 复制代码
#include<iostream> //蓝桥官方
int main()
{
    char myname[] = "myname";
    char myaddr[] = "中国";
    std::cout<<myname<<" "<<myaddr<<std::endl;
    return 0;

}
cpp 复制代码
#include <iostream> //个人
int main()
{
    std::cout<<"姓名"<<std::endl;
    std::cout<<"好长好长的地址呀"<<std::endl;
    return 0;
}

2.2

编写一个C++程序,它要求用户输入一个以long为单位的距离,然后将它转换为码(一long等于220码)。

cpp 复制代码
#include<iostream> //蓝桥官方
int main()
{
    long long int  length_long,length_yard;
    std::cin >> length_long;
    length_yard = length_long * 220;
    std::cout<<length_yard<<std::endl;
    return 0;

}
cpp 复制代码
#include <iostream> //个人
int main()
{
	int l, m;
	std::cin >> l;
	m = 220 * l;
	std::cout << m << std::endl;
	return 0;
}

2.3

编写一个C++程序,它使用3个用户定义的函数(包括main()),并生成下面的输出:

Three blind mice

Three blind mice

See how they run

See how they run

cpp 复制代码
#include<iostream> //蓝桥官方
void print1();
void print2();

int main()
{
    print1();
    print1();
    print2();
    print2();
    return 0;

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

void print2()
{
    std::cout<<"See how they run"<<std::endl;
}
cpp 复制代码
#include <iostream> //个人

void mice(void);
void run(void);

int main()
{
    mice();
    mice();
    run();
    run();
    return 0;
}


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

void run(void)
{
    std::cout<<"See how they run"<<std::endl;
}
cpp 复制代码
#include <iostream> //个人

void mice(void);
void run(void);

int main()
{
    mice();
    mice();
    run();
    run();
    return 0;
}


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

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

其中一个函数要调用两次,该函数生成前两行;另一个函数也被调用两次,并生成其余的输出。

2.4

编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下所示:

Enter your age: 29

Your age in months is 348.

cpp 复制代码
#include<iostream> //蓝桥官方
int main()
{
    int  year,month;
    std::cout<<"Enter your age: ";
    std::cin >> year;

    month = year * 12;

    std::cout<<month<<std::endl;;
    return 0;

}
cpp 复制代码
#include <iostream> //个人
int main()
{
    std::cout<<"请输入你的年龄:";
    int age;
    std::cin>>age;
    std::cout<<"您度过了"<<age*12<<"个月"<<std::endl;
    return 0;
}

2.5

编写一个程序,其中的main()调用一个用户定义的函数(以摄氏温度值为参数,并返回相应的华氏温度值)。该程序按下面的格式要求用户输入摄氏温度值,并显示结果:

Please enter a Celsius value: 20

20 degrees Celsius is 68 degrees Fahrenheit.

下面是转换公式: 华氏温度 = 1.8×摄氏温度 + 32.0

cpp 复制代码
#include<iostream> //蓝桥官方

double C2F(double C)
{
    return C * 1.8 + 32.0;
}

int main()
{
    double  C,F;
    std::cout<<"Please enter a Celsius value: ";
    std::cin >> C;

    F = C2F(C);

    std::cout<<C<<" degrees Celsius is "<<F<<" degrees Fahrenheit."<<std::endl;;
    return 0;

}
cpp 复制代码
#include <iostream> //个人
int main()
{
    double celsius,fahrenheit;
    std::cout<<"请输入摄氏度值:";
    std::cin>>celsius;
    fahrenheit=1.8*celsius+32.0;
    std::cout<<celsius<<"摄氏度="<<fahrenheit<<"华氏度"<<std::endl;
    return 0;
}

2.6

编写一个程序,其main()调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值)。 该程序按下面的格式要求用户输入光年值,并显示结果:

Enter the number of light years: 4.2

4.2 light years = 265608 astronomical units.

天文单位是从地球到太阳的平均距离(约150000000公里或93000000英里),光年是光一年走的距离 (约10万亿公里或6万亿英里)(除太阳外,最近的恒星大约离地球4.2光年)。请使用double类型(参见程 序清单2.4),转换公式为: 1光年=63240天文单位

cpp 复制代码
#include<iostream> //蓝桥官方
long long int light_years2astronomical_units(double  light_years)
{
    return light_years * 63240;
}
int main()
{
    double  light_years;
    std::cout<<"Enter the number of light years: ";
    std::cin >> light_years;

    long long int astronomical_units = light_years2astronomical_units(light_years);

    std::cout<<light_years<<" light years = "<<astronomical_units<<" astronomical units."<<std::endl;
    return 0;

}
cpp 复制代码
#include <iostream> //个人

long long light_years_to_astronomical_unit(double);

int main()
{
    std::cout<<"请输入光年值:";
    double light_years,astronomical_unit;
    std::cin>>light_years;
    astronomical_unit=light_years_to_astronomical_unit(light_years);
    std::cout<<light_years<<"光年="<<astronomical_unit<<"天文单位"<<std::endl;
    return 0;
}

long long light_years_to_astronomical_unit(double light_years)
{
    return light_years*63240;
}

2.7

编写一个程序,要求用户输入小时数和分钟数。在main()函数中,将这两个值传递给一个void函数, 后者以下面这样的格式显示这两个值:

Enter the number of hours: 9

Enter the number of minutes: 28

Time: 9:28

cpp 复制代码
#include<iostream> //蓝桥官方
void format_time(int hours,int minutes)
{
    std::cout<<"Time: "<<hours<<":"<<minutes<<std::endl;
}
int main()
{
    int hours,minutes;
    std::cout<<"Enter the number of hours: ";
    std::cin >> hours;
    std::cout<<"Enter the number of minutes: ";
    std::cin >> minutes;

    format_time(hours,minutes);
    return 0;

}
cpp 复制代码
#include <iostream> //个人

void print_time(int,int);

int main()
{
    int hour,minute;
    std::cout<<"请入小时数:";
    std::cin>>hour;
    std::cout<<"请输入分钟数:";
    std::cin>>minute;
    print_time(hour,minute);
    return 0;
}

void print_time(int hour,int minute)
{
    if(minute<10)
        std::cout<<"您输入的时间为:"<<hour<<":"<<"0"<<minute<<std::endl;
    else
        std::cout<<"您输入的时间为:"<<hour<<":"<<minute<<std::endl;
}
相关推荐
_wyt00113 小时前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解
c++·gesp
LDR00615 小时前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术15 小时前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
通信小呆呆15 小时前
当算法有了“五感”:多模态数据融合如何向人体感官协同学习?
人工智能·学习·算法·机器学习·机器人
码云数智-园园15 小时前
C++20 Modules 模块详解
java·开发语言·spring
H__Rick15 小时前
自动对焦学习-3
人工智能·学习·计算机视觉
Daisy Lee15 小时前
量化学习-第1章-什么是量化金融
学习·金融·datawhale
swordbob16 小时前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
源分享16 小时前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm
Luminous.16 小时前
C语言--day30
c语言·开发语言