C++primer plus第六版第四章编程题代码

1、

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{   
    char name1[20];
    string name2;
    char grade;
    int age;
    cout << "What is your first name?";
    cin.getline(name1,20);
    cout << "What is your last name?";
    cin >> name2;
    cout << "What letter grade do you deserve?";
    cin >> grade;
    cout << "What is your age?";
    cin >> age;
    cout << "Name: " << name2 << ", "<< name1 << endl;
    cout << "Grade: " << char(grade + 1) << endl;
    cout << "Age: " << age << endl;

}

2、

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{   
    string name;
    string dessert;

    cout << "Enter your name:\n";
    getline(cin, name);
    cout << "Enter your favorite dessert:\n";
    getline(cin, dessert);
    cout << "I have some delicious " << dessert;
    cout << " for you, " << name << ".\n";
    return 0;

}

3、

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{   
    char name1[256];
    char name2[256];

    cout << "Enter your first name: ";
    cin.getline(name1, 256);
    cout << "Enter your last name: ";
    cin.getline(name2, 256);
    cout << "Here's the information in a single string: " << name2 << ", " << name1 << endl;
    return 0;

}

4、

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{   
    string name1;
    string name2;

    cout << "Enter your first name: ";
    cin >> name1;
    cout << "Enter your last name: ";
    cin >> name2;
    cout << "Here's the information in a single string: " << name2 << ", " << name1 << endl;
    return 0;

}

5、

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct CandyBar {
    char name[30];
    double weight;
    int kaluli;
}snack;
int main()
{   

    CandyBar snack = {
        "Mocha Munch",
        2.3,
        250
    };
    cout << snack.name <<endl<< snack.weight <<endl<< snack.kaluli << endl;


}

6、

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct CandyBar {
    char name[30];
    double weight;
    int kaluli;
}snack;
int main()
{   

    CandyBar snack[3] = {
        {"Mocha Munch",2.3,250},
        { "Bana Chu",3.4,360 },
        { "Kelvin Jay",4.7,630}
    };
    for(int i=0;i<3;i++)
    cout << snack[i].name <<endl<< snack[i].weight <<endl<< snack[i].kaluli << endl;


}

7、

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct Pizza {
    char name[100];
    float length;
    float weight;
}temp;
int main()
{   

    Pizza temp;
    cout << "Enter the pizza company name: ";
    cin.getline(temp.name,100);
    cout << "Enter the pizza length: ";
    cin>>temp.length;
    cout << "Enter the pizza weight: ";
    cin>>temp.weight;
    cout << "The company name of pizza is " << temp.name << ", the size of the pizza is " << temp.length << ", the weight of the pizza is " << temp.weight << endl;
    return 0;
}

8、

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct Pizza {
    char name[100];
    float length;
    float weight;
}temp;
int main()
{   

    Pizza *temp=new Pizza;
    cout << "Enter the pizza length: ";
    cin>>temp->length;
    cin.get();
    cout << "Enter the pizza company name: ";
    cin >> temp->name;
    cout << "Enter the pizza weight: ";
    cin >> temp->weight;
    cout << "The company name of pizza is " << temp->name << ", the size of the pizza is " << temp->length << ", the weight of the pizza is " << temp->weight << endl;
    delete temp;
    return 0;
}

9、

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct CandyBar {
    char name[30];
    double weight;
    int kaluli;
}snack;
int main()
{

    CandyBar *a1 = new CandyBar[3]{
        { "Mocha Munch", 2.3, 250 },
        { "Bana Chu",3.4,360 },
        { "Kelvin Jay",4.7,630 }
    };
    for (int i = 0; i<3; i++)
        cout << "the infomation of CandyBar, Name: " << a1[i].name << ", Weight: " << a1[i].weight << ", " << " Calories: " << a1[i].kaluli << "." << endl;
    delete[]a1;

}

10、

#include "stdafx.h"
#include <iostream>
#include <array>
using namespace std;


int main()
{
    array<double, 3>a1;
    double avg = 0.0;
    cout << "Enter the three scores: ";
    cin >> a1[0];
    cin >> a1[1];
    cin >> a1[2];
    avg = (a1[0] + a1[1] + a1[2]) / 3;
    cout << "Result: " << a1[0]<<", " << a1[1] <<", "<< a1[2] << endl;
    cout << "Average Result: " << avg << endl;
    return 0;


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值