《C++ Primer Plus(第6版)》编程练习代码 Chapter 5

来源:互联网 发布:ubuntu tftp 命令 编辑:程序博客网 时间:2024/05/17 04:23

Chapter 5

5.1

#include<iostream>int main(){    using namespace std;    int min, max, sum = 0;    cout << "Enter min number: ";    cin >> min;    cout << "Enter max number: ";    cin >> max;    for (int i = min; i <= max; i++)    {        sum += i;    }    cout << "Result: " << sum;    cin.get();    cin.get();}

5.2

#include<array>#include<iostream>int main(){    using namespace std;    const int N = 101;    array<long double, N> factorials;    factorials[0] = factorials[1] = 1LL;    for (int i = 2; i < N; i++)        factorials[i] = i * factorials[i - 1];    for (int i = 0; i < N; i++)        cout << i << "! = " << factorials[i] << endl;    cin.get();    cin.get();}

5.3

#include<iostream>int main(){    using namespace std;    long sum = 0, x;    for (cin >> x; x != 0; cin >> x)    {        sum += x;        cout << "Sum result: " << sum << endl;    }    cout << "Program ends.";    cin.get();    cin.get();}

5.4

#include<iostream>int main(){    using namespace std;    double Daphne, Cleo, SD, SC;    int i = 1;    Daphne = Cleo = 100;    SD = Daphne * 0.1;    SC = Cleo * 0.05;    while (SD > SC)    {        SD += Daphne * 0.1;        SC += (SC + Cleo) * 0.05;        ++i;    }    cout << "After " << i << " years, Cleo > Daphne.\n";    cout << "Daphne's value: " << SD << endl;    cout << "Cleo;s value: " << SC << endl;    cin.get();    cin.get();}

5.5

#include<iostream>#include<string>int main(){    using namespace std;    string month[12] =    {        "Jan",        "Feb",        "Mar",        "Apr",        "May",        "Jun",        "Jul",        "Aug",        "Sep",        "Oct",        "Nov",        "Dec"    };    int sale[12], sum = 0;    for (int i = 0; i < 12; i++)    {        cin >> sale[i];        sum += sale[i];    }    cout << "Sales in this year:\n\n";    for (int i = 0; i < 12; i++)    {        cout << month[i] << ": \t" << sale[i] << endl;    }    cout << "Sum of sales: " << sum;    cin.get();    cin.get();}

5.6

#include<iostream>#include<string>int main(){    using namespace std;    string month[12] =    {        "Jan",        "Feb",        "Mar",        "Apr",        "May",        "Jun",        "Jul",        "Aug",        "Sep",        "Oct",        "Nov",        "Dec"    };    int sale[12][3], sum = 0;    for (int i = 0; i < 12; i++)        for (int j = 0; j < 3; j++)        {            cin >> sale[i][j];            sum += sale[i][j];        }    cout << "Sales in three years:\n\n";    for (int i = 0; i < 12; i++)    {        cout << month[i] << ": \t" ;        for (int j = 0; j < 3; j++)        {            cout << sale[i][j] << "\t";        }        cout << endl;    }    cout << "Sum of sales: " << sum;    cin.get();    cin.get();}

5.7

#include<iostream>#include<string>using namespace std;struct car{    string make;    int year;};int main(){    int numOfCar;    cout << "How many cars do you wish to catalog? ";    cin >> numOfCar;    cin.get();    car * carInfo = new car[numOfCar];    for (int i = 0; i < numOfCar; i++)    {        cout << "Car #" << i+1 << ":\n"            << "Please enter the make: ";        getline(cin, (carInfo + i)->make);        cout << "Please enter the year made: ";        (cin >> carInfo[i].year).get();    }    cout << "Here is your collection:\n";    for (int i = 0; i < numOfCar; i++)    {        cout << (carInfo + i)->year << " " << (carInfo + i)->make << endl;    }    delete [] carInfo;    cin.get();    cin.get();    return 0;}

5.8

#include<iostream>#include<cstring>using namespace std;int main(){    cout << "Enter words (to stop, type the word done):\n";    char word[30];    int n = 0;    cin >> word;    while (strcmp(word, "done"))    {        cin >> word;        n++;    }    cout << "You entered a total of " << n << " words.";    cin.get();    cin.get();}

5.9

#include<iostream>#include<cstring>#include<string>using namespace std;int main(){    cout << "Enter words (to stop, type the word done):\n";    string word;    int n = 0;    cin >> word;    while (word != "done")    {        cin >> word;        n++;    }    cout << "You entered a total of " << n << " words.";    cin.get();    cin.get();}

5.10

#include<iostream>using namespace std;int main(){    cout << "Enter numbet of rows: ";    int row;    cin >> row;    for (int i = 1; i <= row; i++)    {        for (int j = 0; j < (row - i); j++)            cout << ".";                    //print"."        for (int j = 0; j < i; j++)            cout << "*";                    //print"*"        cout << endl;    }    cin.get();    cin.get();}
阅读全文
0 0