c++primer 第四章编程练习答案

4.13.1

#include<iostream>

struct students {
    char firstname[20];
    char lastname[20];
    char grade;
    int age;
};
int main() {
    using namespace std;
    students student1;
    cout << "What is your fistname? ";
    cin.get(student1.firstname, 20).get();
    cout << "What is your lastname? ";
    cin.getline(student1.lastname, 20);
    cout << "What latter grade do you deserve? ";
    cin >> student1.grade;
    cout << "What is your age? ";
    cin >> student1.age;
    cin.get();
    cout << endl;
    cout << "name: " << student1.firstname << " , " << student1.lastname << endl;
    cout << "grade: " << char (student1.grade + 1) << endl;
    cout << "age: " << student1.age;
    cin.get();
}

4.13.2

#include<iostream>

struct students {
    char firstname[20];
    char lastname[20];
    char grade;
    int age;
};
int main() {
    using namespace std;
    students student1;
    cout << "What is your fistname? ";
    cin.get(student1.firstname, 20).get();
    cout << "What is your lastname? ";
    cin.getline(student1.lastname, 20);
    cout << "What latter grade do you deserve? ";
    cin >> student1.grade;
    cout << "What is your age? ";
    cin >> student1.age;
    cin.get();
    cout << endl;
    cout << "name: " << student1.firstname << " , " << student1.lastname << endl;
    cout << "grade: " << char (student1.grade + 1) << endl;
    cout << "age: " << student1.age;
    cin.get();
}

4.13.3

#include <iostream>
#include <cstring>

const int Strlen = 20;

int main()
{
using namespace std;

char first_name[Strlen];
char last_name[Strlen];
char full_name[2 * Strlen];

cout << "Enter your first name: ";
cin.get(first_name, Strlen).get();
cout << "Enter your last name: ";
cin.get(last_name, Strlen).get();

strcpy_s(full_name, last_name);//strcpy经常发生缓存区溢出,被视为是不安全的
strcat_s(full_name, ", ");
strcat_s(full_name, first_name);

cout << "Here‘s the information in a single string: " << full_name;
cin.get();
}

4.13.4

#include<iostream>
#include<string>

int main() {
    using namespace std;
    string firstname;
    string lastname, fullname;
    cout << "Enter your first name: ";
    getline(cin, firstname);
    cout << "Enter your last name: ";
    cin >> lastname;
    cin.get();
    fullname = firstname + ‘,‘ + lastname;
    cout << "Here‘s the information in a single string: " << fullname;
    cin.get();
}

4.13.5

#include<iostream>
#include<string>

struct CandyBar
{
    std::string brand;
    double weight;
    int calories;
};
S
int main() {
    using namespace std;
    CandyBar snack = {
        "Mocha Munch",
        2.3,
        350
    };
    cout << snack.brand << endl << snack.weight << endl << snack.calories;
    cin.get();
}

4.13.6

#include<iostream>
#include<string>

struct CandyBar
{
    std::string brand;
    double weight;
    int calories;
};

int main() {
    using namespace std;
    int i;
    CandyBar candy[3]{
        {"haha",12.3,24},
        {"jude",11.2,25},
        {"zc",25.2,132}
    };
    for (i = 0; i < 3; i++) {
        cout << "candybar " << i << endl
            << candy[i].brand << endl
            << candy[i].weight << endl
            << candy[i].calories << endl;
    }
    cin.get();
}

4.13.7

#include<iostream>
#include<string>

struct PizzaBar
{
    std::string name;
    float diameter;
    float weight;
};
int main() {
    using namespace std;
    PizzaBar pizza;
    cout << "Please Enter your pizza name: ";
    cin >> pizza.name;
    cout << "Please Enter your pizza diameter: ";
    cin >> pizza.diameter;
    cout << "Please Enter your pizza weight: ";
    cin >> pizza.weight;
    cin.get();
    cout << "your pizza well be ordered ,please confirm!" << endl;
    cout << pizza.name << endl << pizza.diameter << endl << pizza.weight;
    cin.get();
}

4.13.8

#include <iostream>
#include<string>

int main() {
    using namespace std;
    char *name = new char[20];
    double *diameter = new double;
    double *weight = new double;
    cout << "pizza name: ";
    cin >> name;
    cout << "pizza weight: ";
    cin >> *weight;
    cout << "pizza diameter: ";
    cin >> *diameter;
    cin.get();
    cout << "name: " << name << endl;
    cout << "weight:" << *weight << endl;
    cout << "diameter: " << *diameter << endl;
    cin.get();
}

4.13.9

#include<iostream>
#include<string>

using namespace std;
struct CandyBar
{
    string brand;
    double weight;
    int calories;
};

int main() {
    CandyBar *candy = new CandyBar[3];
    int i;
    candy->brand = "dad";
    candy->weight = 12.16;
    candy->calories = 123;
    (candy + 1)->brand = "wqe";
    (candy + 1)->weight = 45.6;
    (candy + 1)->calories = 456;
    (candy + 2)->brand = "zxc";
    (candy + 2)->weight = 78.9;
    (candy + 2)->calories = 789;
    for (i = 0; i < 3; i++) {
        cout << "display " << i+1 << " CandyBar\n";
        cout << "brand: " << candy[i].brand << endl;
        cout << "weight: " << candy[i].weight << endl;
        cout << "calories: " << candy[i].calories << endl;
        cout << endl;
    }
    delete [] candy;
    cin.get();
}

4.13.10

#include<iostream>
#include<array>

int main() {
    using namespace std;
    array<double, 3> score;
    double sum = 0;
    int i;
    for (i = 0; i < 3; i++) {
        cout << "input " << i+1 << " score: ";
        cin >> score[i];
        sum += score[i];
        cout << "display " << i + 1 << " average score is " << sum / (i + 1) << endl;
        cin.get();
    }
    cin.get();
}
时间: 2024-10-10 23:17:10

c++primer 第四章编程练习答案的相关文章

c++primer 第五章编程练习答案

5.9.1 #include<iostream> int main() { using namespace std; int one, two, temp, sum = 0; cout << "input first interger: "; cin >> one; cout << "input second interger: "; cin >> two; for (temp = one; temp &l

c++primer 第三章编程练习答案

3.7.1 #include<iostream> int main() { using namespace std; const int unit = 12; int height,inch,foot; cout << "please input your height __ inch\b\b\b\b\b\b\b"; cin >> height; inch = height % unit; foot = height / unit; cin.get(

c++ primer plus(第6版)中文版 第九章编程练习答案

首先,说明下环境: linux:fedora14: IDE:eclipse: python:python2.7 python框架:django web服务器:apache web服务器的python模块:mod_wsgi 写在前面: 之前用的windows下面的xampp,写的php后台,现在想转向linux下面的python,跟以前一样,选择apache和eclipse作为自己的开发工具. eclipse的python配置, 参见之前的博客:http://blog.csdn.net/zy416

c++ primer plus(第6版)中文版 第十三章编程练习答案

第十三章编程练习答案 13.1根据Cd基类,完成派生出一个Classic类,并测试 //13.1根据Cd基类,完成派生出一个Classic类,并测试 #include <iostream> #include <cstring> using namespace std; // base class class Cd { char performers[50]; char label[20]; int selections; // number of selections double

c++ primer plus(第6版)中文版 第十二章编程练习答案

第十二章编程练习答案 12.1根据以下类声明,完成类,并编小程序使用它 //12.1根据以下类声明,完成类,并编小程序使用它 #include <iostream> #include <cstring> using namespace std; class Cow{ char name[20]; char * hobby; double weight; public: Cow(); Cow(const char * nm, const char * ho, double wt);

c++ primer plus(第6版)中文版 第七章编程练习答案

第七章编程练习答案 7.1编写一个程序,用户不停输入两数,直到有0出现为止,计算调和平均数 //7.1编写一个程序,用户不停输入两数,直到有0出现为止,计算调和平均数 #include <iostream> using namespace std; double average (unsigned x, unsigned y) { return (2.0 * x * y / (x + y)); } int main () { while (true) { unsigned x, y; cout

java第四章编程题(初学篇)

代码: 1 /* 2 test.java 3 */ 4 package test; 5 public class test { 6 public static void main(String args[] ) 7 { 8 CPU ccp= new CPU(); 9 HardDisk hhd=new HardDisk(); 10 PC pc =new PC(); 11 ccp.setSpeed(2200); 12 hhd.setAmount(200); 13 pc.setCPU(ccp); 14

第四章编程练习

#include<iostream> #include<climits> #include<string> #include<cstring> #include<array> using namespace std; struct CandyBar { string brand; double weight; int carl; }; struct Pizza { string companyName; float diameter; float

C Primer Plus (第五版) 第四章 编程练习

第四章    字符串和格式化输入/输出 编程练习 编写一个程序,要求输入名字和姓氏,然后以"名字,姓氏"的格式打印. #include <stdio.h> #define LEN 21 int main(void) { char last_name[LEN]; char first_name[LEN]; printf("请输入你的名字和姓氏:\n"); scanf("%s%s", &first_name, &last_