首页 > 编程语言 >C++PrimerPlus中文第六版第5章编程练习答案

C++PrimerPlus中文第六版第5章编程练习答案

时间:2022-11-12 21:55:28浏览次数:33  
标签:main PrimerPlus cout int namespace C++ 第六版 using include

1、

#include<iostream>
using namespace std;

int main()
{
    int i, j;
    cout << "Enter two numbers: ";
    cin >> i >> j;
    //cout << "Sum between " << i << " and " << j << " is " << (i + j) * (j - i + 1) / 2 << endl;
    int sum = 0;
    while (i++ <= j)
        sum += i;
    cout << "sum: " << sum << endl;

}

2、

#include<iostream>
#include<array>
using namespace std;

int main()
{
    const int ArSize = 100;
    array<long double, ArSize> factorials;
    factorials[1] = factorials[0] = 1;
    for (int i = 2; i < ArSize; i++)
        factorials[i] = factorials[i - 1] * i;
    for (int i = 0; i < ArSize; i++)
        cout << "factorials[" << i << "]=" << factorials[i] << endl;

}

3、

#include<iostream>
#include<array>
using namespace std;

int main()
{
    int i, sum = 0;
    cout << "Enter a series of integer; quit when input 0\n";
    cin >> i;
    while (i != 0)
    {
        sum += i;
        cout << "You entered " << i << ", and the sum is: " << sum << endl;
        cin >> i;
    }
    cout << "QUIT\n";
}

4、

#include<iostream>
using namespace std;

int main()
{
    double Daphe = 100.0, Celo = 100.0;
    double totalD = Daphe, totalC = Celo;
    int years = 0;
    while (totalC <= totalD)
    {
        totalD += 0.1 * Daphe;
        totalC *= 1.05;
        years++;
    }
    cout << "After " << years << " years, Celo has $" << totalC << " assets, more than Daphe with $" << totalD << endl;
}

5、

#include<iostream>
using namespace std;

int main()
{
    int sales[12];
    const char* months[12] =
    {
        "January", "February", "March",
        "April", "May", "June",
        "July", "August", "September",
        "October", "November", "December"
    };
    int sum = 0;
    cout << "Enter the books sales each month\n";
    for (int i = 0; i < 12; i++)
    {
        cout << months[i] << ": \t";
        if (strlen(months[i]) < 6)
            cout << "\t";
        cin >> sales[i];
        sum += sales[i];
    }
    cout << "The total sales is: " << sum << endl;
}

6、

#include<iostream>
using namespace std;

int main()
{
    int sales[3][12];
    const char* Months[12] =
    {
        "January", "February", "March",
        "April", "May", "June",
        "July", "August", "September",
        "October", "November", "December"
    };
    int sumEachYear, sumTotal = 0;
    for (int year = 0; year < 3; year++)
    {
        sumEachYear = 0;
        cout << "Enter the books sales each month in the " << year+1<< "th year\n";
        for (int month = 0; month < 12; month++)
        {
            cout << Months[month] << ": \t";
            if (strlen(Months[month]) < 6)
                cout << "\t";
            cin >> sales[year][month];
            sumEachYear += sales[year][month];
        }
        cout << "In the "<<year+1 << "th year, the total sales is: " << sumEachYear << endl;
        sumTotal += sumEachYear;
    }
    cout << "Total sales is " << sumTotal << " for three years\n";
        
}

7、

#include<iostream>
#include<string>
using namespace std;

//const int CharSize = 20;
struct car
{
    string make;//char make[CharSize];
    int year;
};

int main()
{
    int n;
    cout << "How many cars do you wish to catalog? ";
    (cin >> n).get();
    car* cars = new car[n];
    for (int i = 0; i < n; i++)
    {
        cout << "Car #" << i + 1 << endl;
        cout << "Please enter the make: ";
        getline(cin, cars[i].make);//cin.getline(cars[i].make, CharSize);
        cout << "Please enter the year made: ";
        (cin >> cars[i].year).get();
    }
    cout << "Here is your collection:\n";
    for (int i = 0; i < n; i++)
        cout << cars[i].year << " " << cars[i].make << endl;
}

8、

#include<iostream>
using namespace std;


const int Size = 20;
int main()
{
    const char* obj = "done";
    char word[Size];
    cout << "Enter words (to stop, type the word done)\n";
    cin >> word;
    int count = 0;
    while (strcmp(word, obj))
    {
        count++;
        cin >> word;
    };
    cout << "You entered " << count << " words\n";
}

9、

#include<iostream>
using namespace std;


const int Size = 20;
int main()
{
    string obj = "done";
    string word;
    cout << "Enter words (to stop, type the word done)\n";
    cin >> word;
    int count = 0;
    while (word != obj)
    {
        count++;
        cin >> word;
    };
    cout << "You entered " << count << " words\n";
}

10、

#include<iostream>
using namespace std;

int main()
{
    int n;
    cout << "Enter number of rows:";
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        string s1(n - i - 1, '.'), s2(i + 1, '*');
        cout << s1 << s2 << endl;
    }
}

 

标签:main,PrimerPlus,cout,int,namespace,C++,第六版,using,include
From: https://www.cnblogs.com/xinmind/p/16884774.html

相关文章

  • C++学习笔记1:类的使用
    [基本数据结构]()[类的使用]()[类的继承和派生]()[面向对象特征之一——多态]()[操作符重载]()[const关键字的使用]()1.类的定义与使用1.1类的定义最简单的类的申明:class......
  • [C++] - GCC和LLVM对方法 warning: non-void function does not return a value [-Wre
    最近做一个C++开源项目发现一个奇怪问题,通过clang编译链接执行程序每到有一个就崩溃了,gcc下则没有此问题。后来通过调试,发现原因是bool返回的方法是没有return语句!问......
  • C++预定义指令
    C++预定义指令1.预定义器以#开头的命令,称之为预定义器指令。预定义器指令不在编译器中执行,而是在预定义器中运行。常见的预定义器指令为//文件包含指令#include//宏......
  • C++学习------cerrno头文件的作用与源码学习
    引言cerrno是C++对errno.h头文件的封装,里面实现了一个errno宏,返回上一次的错误码。我们来看看这个宏的具体实现以及其背后的原理。cerrno头文件代码位置:​​www.aospxref.......
  • 理解C++中 const 在指针中的用法
    intmain(){ int*constarray; constint*array; inta=10; array=&a;//Youcan'texchangearrayself,arrayjustisaintegar// *array=13;//Thisiserror......
  • C++飞机票订票系统
    C++飞机票订票系统题目7飞机票订票系统问题描述:某公司每天有10航班(航班号、价格),每个航班的飞机,共有80个座位,分20排,每排4个位子。编号为A,B,C,D。如座位号:10D表示1......
  • C++一元多项式计算器的设计与实现
    C++一元多项式计算器的设计与实现七、一元多项式计算器的设计与实现1.基于动态数组或者链表实现--元多项式的计算,可以使用STL的vector或者list。2.在系统中需要提供必......
  • 周六900C++班级2022-11-12-搜索练习
    KnightMoves#include<bits/stdc++.h>usingnamespacestd;intnex[8][2]={{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2}};//方向数组intvis[310][3......
  • 关于Redhat-Linux中-compat-sap-c++软件包的说明
    本文OS版本:RedHatEnterpriseLinuxrelease8.6(Ootpa)还是先说一下compat-sap-c++软件包的作用:InordertorunSAPapplicationscompiledwithcertainnewerGCC......
  • C++获取毫秒时间戳的方法
    c++std::chrono::millisecondsTimeUtils::CurrentTimeMillis(){returnstd::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock:......