栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ primer plus 课后题答案 第三章

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ primer plus 课后题答案 第三章

第一题:

#include 
using namespace std;

int main()
{
    const int INCH_PER_FEET = 12;
    cout << "please enter your height:___bbb";
    //b的含义是,将光标从当前位置向前(左)移动一个字符(遇到n或r则停止移动)
    int height;
    cin >> height;
    cout << "your height is " << height / INCH_PER_FEET << " feet and "
        << height % INCH_PER_FEET << " inches";

    return 0;
}

第三题

#include 
using namespace std;


int main()
{
    float degrees, minutes, seconds;
    cout << "Enter a latitude in degrees,minutes and seconds:"<< endl;
    cout << "First,enter the degreees: ";
    cin >> degrees;
    cout << "Next,enter the minutes of arc:";
    cin >> minutes;
    cout << "Finally,enter the seconds of arc:";
    cin >> seconds;
    float result=degrees + (minutes / 60.0) + (seconds / (60.0 * 60.0));
    cout << degrees << " degrees," << minutes << " minutes," << seconds << " seconds= " << result<<" degrees";

    return 0;
}

第四题:

#include 
using namespace std;

int main()
{
    const int HOUR_PER_DAY = 24;      //1天有24小时;
    const int MINUTE_PER_HOUR = 60;   //1小时有60分钟;
    const int SECOND_PER_MINUTE = 60; //1分钟有60秒;
    long long second;

    cout << "Enter the number of seconds: ";
    cin >> second;

    cout << second << " seconds = ";
    cout << second / (HOUR_PER_DAY * MINUTE_PER_HOUR * SECOND_PER_MINUTE);
    cout << " days, ";
    cout << second / (SECOND_PER_MINUTE * MINUTE_PER_HOUR) % HOUR_PER_DAY;
    cout << " hours, ";
    cout << second % (SECOND_PER_MINUTE * MINUTE_PER_HOUR) / SECOND_PER_MINUTE;
    cout << " minutes, ";
    cout << second % SECOND_PER_MINUTE;
    cout << " seconds" << endl;

    return 0;
}

第五题:

#include 
using namespace std;

int main()
{
    long long world, us;
    cout << "Enter the world's population:";
    cin >> world;
    cout << "Enter the population of the US:";
    cin >> us;
    
    cout << "The population of the US is " << double(us) / world * 100LL<< " % of the world population";

    return 0;
}

第三题

#include 
#include 
using namespace std;

int main()
{
    const int ArSize = 20;
    char fname[ArSize];
    char lname[ArSize];

    cout << "Enter your first name: ";
    cin.getline(fname, ArSize);
    cout << "Enter your last name: ";
    cin.getline(lname, ArSize);

    int len = strlen(fname) + strlen(lname) + 10;
    char name[10];
    strcpy(name, lname);
    strcat(name, ", ");
    strcat(name, fname);

    cout << "Here's the information in a single string: ";
    cout << name << endl;

    return 0;
}

第八题:

#include 
#include 
using namespace std;

struct piza
{
    string band;
    float radius;
    float weight;
};

int main()
{
   
    piza *p=new piza;
    getline(cin, p->band);
    cin >> p->radius;
    cin >> p->weight;
    cout << "披萨的品牌:" << p->band << endl;
    cout << "披萨的直径:" << p->radius << endl;
    cout << "披萨的重量:" << p->weight << endl;

    return 0;
}


 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/629759.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号