C++ Primer Plus编程作业

本文展示了几个C++程序实例,涵盖了用户输入处理,如获取姓名、字母等级和年龄,以及使用结构体存储和显示数据,如糖果品牌、披萨信息。此外,还涉及动态内存分配,如使用new创建结构体实例和数组。程序还包括计算平均值、求和以及比较投资收益等数学运算的应用。
摘要由CSDN通过智能技术生成

编写一个C++程序,如下述输出示例所示的那样请求并显示信息:
What is your first name? Betty Sue
What is your last name? Yewe
What letter grade do you deserve? B
What is your age? 22
Name: Yewe, Betty Sue
Grade:
Age: 22

#include <iostream>
using namespace std;
int main() {
//    string first;
    char first[20];
//    string last;
    char last[20];
    int age;
    char grade;
    cout << "what is your first name?" << endl;
//    getline(cin,first);
    cin.getline(first, 20);
    cout << "what is your last name?" << endl;
//    getline(cin,last);
    cin.getline(last, 20);
    cout << "what letter grade do you deserve?" << endl;
    cin >> grade;
    cout << "what is your age?" << endl;
    cin >> age;
    cout << "Name: " << first << ", " << last << endl;
    cout << "Grade: " << grade << endl;
    cout << "Age: " << age << endl;
    return 0;
}

CandyBar结构包含3个成员,分别为品牌名称、重量和热量。请编写一个函数,参数包含CandyBar的引用、char指针、double和int作为参数,并用最后3个参数作为结构成员的值,其默认值分别为“Millennium Munch”、2.85和350,函数需要显示结构。并创建一个包含3个元素的CandyBar数组…

#include <iostream>
using namespace std;
int main() {
    struct CandyBar{
        string name;
        float weight;
        int calorie;
    };
    CandyBar snack[3] = {
            {"Mocha Munch",2.3,350},
            {"Mocha1 Munch",2.4,450},
            {"Mocha2 Munch",2.5,550},
    };
    cout << "The snack name is " << snack[0].name << " weight is " << snack[0].weight << " calories is " << snack[0].calorie << endl;
    cout << "The snack1 name is " << snack[1].name << " weight is " << snack[1].weight << " calories is " << snack[1].calorie << endl;
    cout << "The snack2 name is " << snack[2].name << " weight is " << snack[2].weight << " calories is " << snack[2].calorie << endl;
    return 0;
}

William Wingate 从事披萨饼分析服务。对于每个披萨饼,他都需要记录下列信息:
(1)披萨饼公司的名称,可以有多个单词组成
(2)披萨饼的直径
(3)披萨饼的重量
请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。
程序将请求用户输入上述信息,然后显示这些信息。请使用cin(或它的方法)和cout。

#include <iostream>
using namespace std;
int main() {
    struct Pizza{
        string name;
        float diameter;
        float weight;
    };
    Pizza pizza;
    cout << "Please enter pizza name!" << endl;
    getline(cin, pizza.name);
    cout << "Please enter pizza diameter!" << endl;
    cin >> pizza.diameter;
    cout << "Please enter pizza weight!" << endl;
    cin >> pizza.weight;
    cout << "The pizza name is " << pizza.name << " diameter is " << pizza.diameter << " weight is " << pizza.weight;
    return 0;
}

完成编程练习7,但使用new来为结构分配内存,而不是声明一个结构变量。
另外,让程序在请求输入比萨饼公司名称之前输入披萨饼的直径。

#include <iostream>
using namespace std;
int main() {
    struct Pizza{
        string name;
        float diameter;
        float weight;
    };
    Pizza * pizza = new Pizza;
    cout << "Please enter pizza name!" << endl;
    getline(cin, pizza->name);
    cout << "Please enter pizza diameter!" << endl;
    cin >> pizza->diameter;
    cout << "Please enter pizza weight!" << endl;
    cin >> pizza->weight;
    cout << "The pizza name is " << pizza->name << " diameter is " << pizza->diameter << " weight is " << pizza->weight;
    return 0;
}

完成编程练习6,但使用new来动态分配数组,而不是声明一个包含3个元素的CandyBar数组。

#include <iostream>
using namespace std;
int main() {
    struct CandyBar{
        string name;
        float weight;
        int calorie;
    };
    CandyBar * snack = new CandyBar[3]{
            {"Mocha Munch",2.3,350},
            {"Mocha1 Munch",2.4,450},
            {"Mocha2 Munch",2.5,550},
    };
    cout << "The snack name is " << snack[0].name << " weight is " << snack[0].weight << " calories is " << snack[0].calorie << endl;
    cout << "The snack1 name is " << snack[1].name << " weight is " << snack[1].weight << " calories is " << snack[1].calorie << endl;
    cout << "The snack2 name is " << snack[2].name << " weight is " << snack[2].weight << " calories is " << snack[2].calorie << endl;
    return 0;
}

编写一个程序,让用户输入三次40码跑的成绩(如果您愿意,也可以让用户输入40米跑的成绩),
并显示次数和平均成绩,请使用一个array对象来存储数据。

#include <iostream>
#include "array"
using namespace std;
int main() {
    array<double , 4> grade;
    cout << "Enter 4 garden!(Enter key to enter the next one)" << endl;
    cin >> grade[0];
    cin >> grade[1];
    cin >> grade[2];
    cin >> grade[3];
    cout << "First garde is: " << grade[0] << endl;
    cout << "Second garde is: " << grade[1] << endl;
    cout << "Third garde is: " << grade[2] << endl;
    cout << "Fourth garde is: " << grade[3] << endl;
    return 0;
}

编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间(包括这两个整数)所有整数的和。这里假设先输入较小的整数。例如,如果用户输入的是2和9,则程序将指出2~9之间所有整数和为44.

#include <iostream>

using namespace std;
int main() {
    int min, max;
    int sum = 0;
    cout << "Enter a min number!" << endl;
    cin >> min;
    cout << "Enter a max number!" << endl;
    cin >> max;
    for(min; min <= max; min++){
        sum = sum + min;
    };
    cout << "Sum = " << sum << endl;
    return 0;
}

使用array对象(而不是数组)和long double(而不是long long)重新编写程序清单5.4,并计算100!的值。

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

编写一个要求用户输入数字的程序。每次输入后,程序都将报告到目前为止,所有输入的累计和。当用户输入0时,程序结束。

#include <iostream>
using namespace std;
int main() {
    double number, sum;
    do{
        cout << "Enter 0 to end!" << endl;
        cin >> number;
        sum += number;
        cout << "Sum = " << sum << endl;
    } while (number != 0);
    cout << "END!!!!!" << endl;
    return 0;
}

Daphe以10%的单利投资了100美元。也就是说,每一年的利润都是投资额的10%,即每年10美元:
利息 = 0.10x原始存款
而Cleo以5%的复利投资了100美元。也就是说,;利息是当前存款(包括获得利息)的5%;
利息 = 0.05x当前存款
Cleo在第一年投资100美元的盈利是5%,计算多少年后,Cleo的投资价值才能超过Daphne的投资价值,并显示此时两人的投资价值。

#include <iostream>
using namespace std;
int main() {
    double Daphne = 100, valueD = 0,Cleo = 100, valueC = 0;
    int year;
    for(int i = 1, j = 1; valueD >= valueC; i++, j++){
        valueD += Daphne * 0.1;
        valueC = Cleo * 0.05;
        Cleo += Cleo + valueC;
        year = i;
    }
    cout << year << " years Cleo value > Daphne value " << endl;
    cout << "Daphne value is : " << valueD << endl;
    cout << "ClEO value is : " << valueC << endl;
    return 0;
}

*假设要销售《C++ For Fools》一书。请编写一个程序,输入全年中每个月的销售量(图书数量,而不是销售额)。程序通过循环,使用初始化为月份字符串的char 数组(或string对象数组)逐月进行提示,并将输入的数据储存在一个int数组中。然后程序计算数组中各元素的总数,并报告这一年的销售情况。
完成上述编程练习,使用一个二维数组来存储输入——3年中每个月的销售量。程序将报告每年销售量以及三年的总销售量。

#include <iostream>
#include "string"
using namespace std;
const int arrSizeM = 12, arrSizeY = 3;
const string month[] = {
        "Jan","Feb","Mar","Apr","May","Jun",
        "July","Aug","Sqp","Oct","Nov","Dec"
};
int main() {
    int num[arrSizeY][arrSizeM];
    int years[3] = {1, 2, 3};
    for(int i = 0; i < arrSizeY; i++){
        for(int j = 0; j < arrSizeM; j++ ){
            cout << "Enter books sales for " << month[j] << " in " << years[i] << endl;
            cin >> num[i][j];
        }
    };

    cout << " ";
    for(int i = 0; i < arrSizeM; i++){
        cout << month[i] << " ";
    };
    cout << " sum " << endl;

    int sum[3] = {0, 0, 0};
    for(int i = 0; i < arrSizeY; i++){
        cout << years[i] << " : ";
        for (int j = 0; j < arrSizeM; j++) {
            cout << num[i][j] << " ";
            sum[i] += num[i][j];
        }
        cout << sum[i] << endl;
    }
    cout << "Sum = " << sum[0] + sum[1] + sum[2];
    return 0;
}

设计一个名为car的结构,用它存储下述有关汽车的信息: 生产商(存储在字符数组或string对象中的字符串)、 生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。随后,程序使用new来创建一个由相应数量的car结构组成的动态数组。 接下来,程序提示用户输入每辆车的生产商(可能由多个单词组成)和年份信息。请注意,这需要特别小心,因为它将交替读取 数值和字符串。

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

编写一个程序,它使用一个char数组和循环来每次读取一个单词,直到用户输入done为止。随后该程序指出用户输入了多少个单词(不包括done在内)

#include <iostream>
#include <cstring>
using namespace std;
const int arrSize = 20;
int main() {
    char * words[arrSize];
    int i = 0, sum = 0;
    cout << "Enter words (to stop , type the word done)" << endl;
    cin >> words[i];
    while (strcmp(words[i], "done") != 0){
        ++i;
        sum++;
        char * n;
        cin.get();
        cin >> n;
        words[i] = n;
    }
    cout << "You entered a total of " << sum << " words." << endl;
    return 0;
}

编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应行数的星号,其中第一行包括一个星号,第二行包括两个星号,依此类推。每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句点。该程序的运行情况如

#include <iostream>
using namespace std;
int main() {
    int rows;
    cout << "Enter number of rows: " << endl;
    cin >> rows;
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < rows - i ; ++j) {
            cout << "@ ";
        }
        for (int k = 0; k < i + 1; ++k) {
            cout << "* ";
        }
        cout << endl;
    }
    return 0;
}

编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符转换为小写,将小写字符转换为大写(别忘了cctype函数系列)。

#include <iostream>
#include <cctype>
using namespace std;
int main() {
    int i = 0;
    char num;
    cout << "Enter a number! " << endl;
    cin >> num;
    while (num != '@'){
        if (!isdigit(num)){
            if (islower(num)){
                num = toupper(num);
                cout << "---------" << num << "---------" << endl;
            } else{
                num = tolower(num);
                cout << "---------" << num << "---------" << endl;
            }
        };
        cin >> num;
    }
    return 0;
}

编写一个程序,最多将10个donation值读入到一个double数组中(如果您愿意,也可使用模板类array)。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。

#include <iostream>
using namespace std;
const int arrySize = 10;
int main() {
    double num[arrySize];
    double sum = 0, ave = 0;
    int i = 0;
    while (cin >> num[i]){
        ++i;
        if(i > 9){
            break;
        }
    }
    cout << "Have " << i << " number" << endl;
    for (int j = 0; j < i; ++j) {
        sum += num[j];
    }
    ave = sum / i;
    cout << "The number average is: " << ave << endl;
    int s = 0;
    for (int j = 0; j < i; ++j) {
        if(num[j] > ave){
            ++s;
        }
    }
    cout << "Have " << s << " number > " << "average"<< endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值