C++ Primer Plus 第六版 第二章习题

C++ Primer Plus 第六版 第二章习题

2.7.1

#include <iostream>
using namespace std;
int main()
{
    cout<<"姓名:Thomas"<<endl;
    cout<<"地址:重庆市"<<endl;
    return 0;
}

2.7.2

#include <iostream>
using namespace std;
int main()
{
    int dis_long;
    int dis_yard;
    cout<<"please input the int distance value as long:"<<endl;
    cin>>dis_long;
    dis_yard=dis_long*220;
    cout<<"you has input the distance: "<<dis_long<<" long,"<<" equal to "<<dis_yard<<" yard."<<endl;
    return 0;
}

2.7.3

#include <iostream>
using namespace std;
void fun_1();
void fun_2();

int main()
{
    fun_1();
    fun_1();
    fun_2();
    fun_2();
    return 0;
}

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

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

2.7.4

#include <iostream>
using namespace std;

int main()
{
    int age;
    cout<<"Please input your age:"<<endl;
    cin>>age; //据cin>>a 中a的变量类型读取数据,遇到结束符(Space、Tab、Enter)就结束
    //cin.get();//cin.get() 与 cin.get(char ch)用于读取字符,他们的使用是相似的,即:ch=cin.get() 与 cin.get(ch)是等价的
    cout<<"Your age contains "<<age*12<<" months.";
    return 0;
}

2.7.5

#include <iostream>
using namespace std;

double cel_to_Fah(double Celsius)
{
    double Fahrenheit;
    Fahrenheit=1.8*Celsius+32.0;
    return Fahrenheit;
}

int main()
{
    double temp_Celsius;
    cout<<"Please enter a Celsius value:"<<endl;
    cin>>temp_Celsius;
    cout<<temp_Celsius<<" degrees Celsius is "<<cel_to_Fah(temp_Celsius)<<" degrees Fahrenheit"<<endl;
    return 0;
}

2.7.6

#include <iostream>
using namespace std;

double lig_to_ast(double light_years)
{
    double astronomical;
    astronomical=63240*light_years;
    return astronomical;
}

int main()
{
    double light_years;
    cout<<"Please enter the number of light years:"<<endl;
    cin>>light_years;
    cout<<light_years<<" light_years = "<<lig_to_ast(light_years)<<" astronomical units"<<endl;
    return 0;
}

2.7.7

#include <iostream>
using namespace std;

void print_time(int hour, int min)
{
    cout<<"Time "<<hour<<":"<<min<<endl;
}

int main()
{
    int ent_hour;
    int temp_hour;
    int ent_min;
    int temp_min;

    cout<<"Please enter the number of hours:"<<endl;
    cin>>temp_hour;
    while (temp_hour>24||temp_hour<0)
    {
        cout<<"Wrong hours, please enter the number of hours again:"<<endl;
        cin>>temp_hour;
    }
    ent_hour=temp_hour;

    cout<<"Please enter the number of minutes:"<<endl;
    cin>>temp_min;
    while (temp_min>60||temp_min<0)
    {
        cout<<"Wrong minutes, please enter the number of minutes again:"<<endl;
        cin>>temp_min;
    }
    ent_min=temp_min;

    print_time(ent_hour, ent_min);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值