编程练习题

  1. 编写一个C++程序,它显示你的姓名和地址。
/********************************************************************
 * 
 * Date : 2020 - 02 - 25
 * Author : Soler HO
 * 
 * Book : C++ Primer Plus 
 * 
 * description : 编写一个程序,显示你的姓名和地址
 * 
 ********************************************************************/

#include<iostream>

using namespace std;

int main(void)
{
    string name = "Soler HO";
    string myAddress = "Shanghai";
    cout<<"我的名字是:"<<name<<","<<"我现在居住在"<<myAddress<<endl;

    return 0;
}
  1. 写一个C++程序,要求用户输入一个以long为单位的距离,然后转换为码(一long = 220码)。
/********************************************************************
 * 
 * Date : 2020 - 02 - 25
 * Author : Soler HO
 * 
 * Book : C++ Primer Plus
 * 
 * description : 
 * 写一个C++程序,要求用户输入一个以long为单位的距离,然后转换为码(一long = 220码)。
 * 
 ********************************************************************/

#include<iostream>

using namespace std;

int main(void)
{
    int length;
    cout<<"请输入以long为单位的距离:"<<endl;
    cin >> length;

    int stack = 220 * length; // 转换为码
    cout<<"转换为码后的值为:"<<stack<<endl;

    return 0;
}
  1. 编写C++程序,使用三个函数(包括了main()函数),输出以下内容:

    Three blind mice

    Three blind mice

    See how they run

    See how they run

    其中一个函数调用两次,该函数生成前两行,另一个函数也调用两次,输出剩下的内容。

/********************************************************************
 * 
 * Date : 2020 - 02 - 25
 * Author : Soler HO
 * 
 * Book : C++ Primer Plus
 * 
 * description : 
 * 
 * 编写C++程序,使用三个函数(包括了main()函数),输出以下内容:
 * Three blind mice
 * Three blind mice
 * See how they run
 * See how they run
 * 其中一个函数调用两次,该函数生成前两行,另一个函数也调用两次,输出剩下的内容。
 * 
 ********************************************************************/

#include<iostream>

using namespace std;

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

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

int main(void)
{
    func_A();
    func_A();

    func_B();
    func_B();

    return 0;
}
  1. 写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月。
/********************************************************************
 * 
 * Date : 2020 - 02 - 25
 * Author : Soler HO
 * 
 * Book : C++ Primer Plus 
 * 
 * description :  
 * 
 * 写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月。
 * 
 ********************************************************************/


#include<iostream>

using namespace std;

int main(void)
{
    int age;
    cout<<"请输入你的年龄:"<<endl;
    cin>>age;
    int months = age*12;
    int days = age*365;

    cout<<"你年龄里面包含了 "<<months<<" 个月"<<endl;
    cout<<"你已经活了大概 "<<days<<" 天"<<endl;

    return 0;
}
  1. 写一个程序,其中的main()调用一个用户自定义的函数(以摄氏温度作为参数,并返回相应的华氏温度值),按照下面的格式进行输入输出。

    请输入你要转换的摄氏温度:23

    23 摄氏度 = 华氏温度

    转换公式:

    华氏温度 = 1.8 * 摄氏温度 + 32.0

/********************************************************************
 * 
 * Date : 2020 - 02 - 25
 * Author : Soler HO
 * 
 * Book : C++ Primer Plus 
 * 
 * description :  
 * 
 * 写一个程序,其中的main()调用一个用户自定义的函数(以摄氏温度作为参数,并返回相应的华氏温度值),按照下面的格式进行输入输出。
 * 
 * 请输入你要转换的摄氏温度:23
 * 23 摄氏度 =   华氏温度
 * 
 * 转换公式:
 * 
 * 华氏温度 = 1.8 * 摄氏温度 + 32.0
 * 
 ********************************************************************/

#include<iostream>

using namespace std;

double func(double celsius)
{
    double Fahrenheit = 1.8 * celsius + 32.0;
    return Fahrenheit;
}

int main(void)
{
    double celsius,Fahrenheit;
    cout<<"请输入要转换的摄氏温度:"<<endl;
    cin>>celsius;
    Fahrenheit = func(celsius);
    cout<<celsius<<" 摄氏度"<<" = "<<Fahrenheit<<"华氏温度"<<endl;

    return 0;
}
  1. 编写一个程序,其main()调用一个用户自定义的函数(以光年值为参数,并返回对应天文单位的值)

    根据用户输入光年值,并显示结果。

    请使用double类型,1光年 = 63240 天文单位

/********************************************************************
 * 
 * Date : 2020 - 02 - 25
 * Author : Soler HO
 * 
 * Book : C++ Primer Plus
 * 
 * description :  
 * 
 * 编写一个程序,其main()调用一个用户自定义的函数(以光年值为参数,并返回对应天文单位的值)
 * 根据用户输入光年值,并显示结果。
 * 
 * 请使用double类型,1光年 = 63240 天文单位
 * 
 * 
 ********************************************************************/

#include<iostream>

using namespace std;

int func(double lightYears)
{
    double AU = lightYears * 63240;
    return AU; // 天文单位的缩写
}

int main(void)
{
    double lightYears_value; // 光年
    cout<<"请输入你的值:"<<endl;
    cin>>lightYears_value;

    double AU = func(lightYears_value); // 调用func函数
    cout<<lightYears_value<<" 光年"<<" = "<<AU<<" 天文单位"<<endl;

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

    请输入你的小时数:

    请输入你的分钟数:

    你设置的时间为:小时数:分钟数

/********************************************************************
 * 
 * Date : 2020 - 02 - 25
 * Author : Soler HO
 * 
 * Book : C++ Primer Plus
 * 
 * description :  
 * 
 * 编写一个程序,要求用户输入小时数和分钟数。在main()函数中,将这两个值传递给一个void函数,后者以下面的格式显示两个值:
 * 请输入你的小时数:
 * 请输入你的分钟数:
 * 你设置的时间为:小时数:分钟数
 * 
 * 
 ********************************************************************/


#include<iostream>

using namespace std;

void time_func(int hour,int munites)
{
    cout<<"你设置的时间是:"<< hour << ":" << munites << endl;
}

int main(void)
{
    int hours;
    cout<<"请输入你的小时数字:";
    cin>>hours;

    int minutes;
    cout<<"请输入你的分钟数:";
    cin>>minutes;
    time_func(hours, minutes);

    return 0;
}