C++ primer plus 第六版 第二章 编程练习答案

第二章 编程练习答案

补发第二章的代码,比较简单,感觉没什么要注释的。后面没写的几章有空接着补。

1.

#include<iostream>

int main()
{
    using namespace std;
    cout << "Harry";
    cout << endl;
    cout << "183********";
    return 0;
}

2.

#include<iostream>
int longtoyard(int);

int main()
{
    using namespace std;
    cout << "Enter the distance in long: ";
    int n;
    cin >> n;
    int m=longtoyard(n);
    cout << n <<" long = ";
    cout << m <<" yard." << endl;
    return 0;

}

int longtoyard(int t)
{
    int a;

    a=t*220;

    return a;
}

3

#include<iostream>
void A(int);
void B(int);

using namespace std;

 int main()
 {

    A(2);
    B(2);
    return 0;
 }

 void A(int n)
 {
    int i;
    for(i = 0; i < n; i++)
    cout << "Three blind mice" << endl;     

 }

 void B(int n)
 {
    int i;
    for(i = 0; i < n; i++)
    cout << "See how they run" << endl;     

 }

4.

#include<iostream>

int main()
{
    using namespace std;

    int n,m;

    cout << "Enter yout age: ";
    cin >> n;
    m = n * 12;
    cout << "your months: " << m;
    return 0;

}

5.

#include<iostream>

double temperature(double);

int main()
{
    using namespace std;    
    double n,m;
    cout << "Please enter a Celsius value: ";
    cin >> n;
    m=temperature(n);
    cout << n << " degrees Celsius is "
         << m << " degrees Fahrenheit.";

    return 0;

}

double temperature(double n)
{
    double m;
    m = 1.8 * n + 32.0;
    return m;
}

6.

#include<iostream>
double distance(double);

int main()
{
    using namespace std;

    double n, m;
    cout << "Enter the number of light years: ";
    cin >> n;
    m = distance(n);
    cout << n << " light years = "
         << m << " astronomical units.";

    return 0;

}

double distance(double n)
{
    double m;
    m = n * 63240;
    return m;
}

7.

#include<iostream>
void time(int, int);

using namespace std;

int main()
{
    int h,m;
    cout << "Enter the number of hours: ";
    cin >> h;
    cout << "Enter the number of minutes: ";
    cin >> m;
    time(h, m);

    return 0;

}

void time(int h, int m)
{
    cout <<"Time: "<< h <<":"<< m;
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值