C++ Primer Plus(第6版) 第2章编程练习

2.1

//2.1

#include<iostream>
using namespace std;
int main()
{
    cout<<"name:Yang Weijie"<<endl<<"address:Beijing\n";
    cin.get();
    return 0;
}

2.2

//2.2

#include<iostream>
using namespace std;
int main()
{
    int n;
    cout<<"请输入一个距离:";
    cin>>n;
    cout<<n<<" long等于"<<n*220<<" 码"; 
    cin.get();
    cin.get();
    return 0;
}

2.3

//2.3

#include<iostream>
using namespace std;
void myFunction1();
void myFunction2();

int main()
{
    myFunction1();
    myFunction1();
    myFunction2();
    myFunction2();
    cin.get();
    return 0;
}

void myFunction1()
{
    cout<<"Three blind mice\n";
}

void myFunction2()
{
    cout<<"See how they run\n";
}

2.4

//2.4

#include<iostream>
using namespace std;
int main()
{
    int n;
    cout<<"Enter your age:";
    cin>>n;
    cout<<n*12<<" months"<<endl;
    cin.get();
    cin.get();
    return 0;
}

2.5

//2.5

#include<iostream>
double convert(double);

int main()
{
    using namespace std;
    double n1; //n1摄氏温度值 
    cout<<"Please enter a Celsius values:";
    cin>>n1;
    double n2=convert(n1); //n2 华氏摄氏度 
    cout<<n1<<" degrees Celsius is "<<n2<<" degrees Fahrenheit.\n";
    cin.get();
    cin.get();
    return 0;

}

double convert(double n1)
{
    return 1.8*n1+32;
}

2.6

//2.6

#include<iostream>
double convert(double);

int main()
{
    using namespace std;
    cout<<"Enter the number of light years:";
    double n1;//n1光年
    cin>>n1;
    double n2=convert(n1);//n2天文单位 
    cout<<n1<<" light years = "<<n2<<" astronomical units.\n";
    cin.get();
    return 0;
} 

double convert(double n1)
{
    return n1*63240;
 } 

2.7

//2.7

#include<iostream>
using namespace std;
void Function(int h, int m);

int main()
{
    cout<<"Enter the number of hours:";
    int h;
    cin>>h;
    cout<<"Enter the number of minutes:";
    int m;
    cin>>m;
    Function(h,m);
    cin.get();
    cin.get();
    return 0;
}

void Function(int h, int m)
{
    cout<<"Time: "<<h<<":"<<m<<endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值