C++ Primer Plus第二章习题

2.1

#include<iostream>
using std::cout;
using std::endl;
int main()
{
    cout<<"My Name is :"<<endl<<"My address is:"<<endl;
    return 0;
}
2.2
#include<iostream>
using std::cout;
using std::endl;
using std::cin;

int main()
{
    double l;
    int yard = 22;
    cin>>l;
    cout<<yard * l << endl;
    return 0;
}

2.3

#include<iostream>
using std::cout;
using std::endl;


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


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


int main()
{
    Print1();
    Print1();
    Print2();
    Print2();
    return 0;
}
2.4
#include<iostream>
using std::cin;
using std::cout;
using std::endl;

int main()
{
    int age, months;
    cout<<"Enter your age: ";
    cin>>age;
    months = age * 12;
    cout<<months<<endl;
    return 0;
}

2.5

#include<iostream>
#include<iomanip>
using std::cout;
using std::cin;
using std::endl;
using std::ios;
using std::setprecision;
double f(double C)
{
    return 1.8 * C + 32.0;
}
int main()
{
    double C, F;
    cout.setf(ios::fixed);
    cout<<"Please Enter a Celsius value: ";
    cin>>C;
    cout<<setprecision(1)<<C<<" degrees Celsius is "<<setprecision(1)<<f(C)<<" degrees  Fahrenheit"<<endl;
    return 0;
}
2.6
#include<iostream>
#include<iomanip>
using std::cout;
using std::cin;
using std::endl;
using std::ios;
using std::setprecision;
double f(double C)
{
    return C * 63240;
}
int main()
{
    double LightYear, F;
    cout.setf(ios::fixed);
    cout<<"Enter the number of light years: ";
    cin>>LightYear;
    cout<<setprecision(1)<<LightYear<<" light years is "<<f(LightYear)<<" astronomical units"<<endl;
    return 0;
}
2.7
#include<iostream>
using std::cout;
using std::cin;
using std::endl;

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


int main()
{
    int h, m;
    cin>>h>>m;
    Time(h, m);
    return 0;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值