zoukankan      html  css  js  c++  java
  • c++ primer plus 第四章 课后题答案

    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
        string first_name;
        string last_name;
        char grade;
        int age;
    
        cout << "What is your first name? ";
        getline(cin,first_name);
        cout << endl << "What is your last name? ";
        getline(cin,last_name);
        cout << endl << "What letter grade do you deserve? ";
        cin >> grade;
        cout << endl << "What is your age? ";
        cin >> age;
        
        cout << "Name: " << last_name << ", " << first_name << endl;
        cout << "Grade: " << char(grade + 1) << endl;
        cout << "Age: " << age << endl;
        cin.get();
        cin.get();
        return 0;
    }

     

    #include <iostream>
    #include<string>
    
    int main()
    {
        using namespace std;
    
        string name;
        string dessert;
    
        cout << "Enter your name:
    ";
        getline(cin,name);
        cout << "Enter your favorite dessert:
    ";
        getline(cin,dessert);
        cout << "I have some delicious " << dessert;
        cout << " for you, " << name << ".
    ";
        cin.get();
        return 0;
    }

     

    #include<iostream>
    #include<cstring>
    
    using namespace std;
    
    int main()
    {
        char first_name[20];
        char last_name[20];
        char name[41];
    
        cout << "Enter your first name: ";
        cin >> first_name;
        cout << endl << "Enter your last name: ";
        cin >> last_name;
    
        strcpy_s(name, last_name);
        strcat_s(name, ",");
        strcat_s(name, first_name);
        cout << endl << "Here’s the information in a single string: " << name;
        
        //cout << endl << "Here’s the information in a single string: " << last_name << " , " << first_name;
    
        cin.get();
        cin.get();
        return 0;
    }

     

    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
        string first_name;
        string last_name;
        string name;
    
        cout << "Enter your first name: ";
        getline(cin,first_name);
        cout << "Enter your last name: ";
        getline(cin,last_name);
    
        name = last_name + "," + first_name;
        cout << "Here's the information in a single string: " << name << endl;
        cin.get();
        //system("pause");
        return 0;
    }

     

    #include<iostream>
    using namespace std;
    
    struct CandyBar
    {
        char bard[20];
        double weight;
        int calories;
    };
    
    int main()
    {
        CandyBar snack =
        {
            "Mocha Munch",
            2.3,
            350
        };
    
        cout << "The bard of this candy is: " << snack.bard << endl;
        cout << "The weight of this candy is: " << snack.weight << endl;
        cout << "The calories of this candy is: " << snack.calories << endl;
    
        cin.get();
        return 0;
    }

     

    #include<iostream>
    using namespace std;
    
    struct Candy {
        char name[20];
        double weight;
        int calories;
    };
    
    int main() {
        //Candy snack[3];
        //snack[0] = { "Mocha Munch1", 2.3, 350 };
        //snack[1] = { "Mocha Munch2", 2.5, 360 };
        //snack[2] = { "Mocha Munch3", 2.7, 390 };
        Candy snack[3] = { { "Mocha Munch1", 2.3, 350 } ,
                           { "Mocha Munch2", 2.5, 360 } ,
                           { "Mocha Munch3", 2.7, 390 } };
        cout << snack[0].name << "'s weight is " << snack[0].weight <<
            ", and it includes " << snack[0].calories << " calories.
    ";
        cout << snack[1].name << "'s weight is " << snack[1].weight <<
            ", and it includes " << snack[1].calories << " calories.
    ";
        cout << snack[2].name << "'s weight is " << snack[2].weight <<
            ", and it includes " << snack[2].calories << " calories.
    ";
    
        system("pause");
        return 0;
    }

     

    #include<iostream>
    using namespace std;
    
    struct Pizza
    {
        char name[20];
        double diameter;
        double weight;
    };
    
    int main()
    {
        Pizza x;
        cout << "Please enter the name of this pizza: ";
        cin >> x.name;
        cout << "Please enter the diameter of this pizza: ";
        cin >> x.diameter;
        cout << "Please enter the weight of this pizza: ";
        cin >> x.weight;
    
        cout << "The name of this pizza is " << x.name << endl;
        cout << "The diameter of this pizza is " << x.diameter << endl;
        cout << "The weight of this pizza is " << x.weight << endl;
    
        system("pause");
        return 0;
    }

     

    #include<iostream>
    using namespace std;
    
    struct Pizza {
        char name[20];
        double diameter;
        double weight;
    };
    
    int main() {
        Pizza *pizza = new Pizza;
        cout << "Please enter the name of this pizza: ";
        cin >> pizza->name;
        cout << "Please enter the diameter of this pizza: ";
        cin >> pizza->diameter;
        cout << "Please enter the weight of this pizza: ";
        cin >> pizza->weight;
    
        cout << "The name of this pizza is " << pizza->name << endl;
        cout << "The diameter of this pizza is " << pizza->diameter << endl;
        cout << "The weight of this pizza is " << pizza->weight << endl;
    
        delete pizza;
    
        system("pause");
        return 0;
    }

     

    #include<iostream>
    using namespace std;
    
    struct Candy {
        char name[20];
        double weight;
        int calories;
    };
    
    int main()
    {
        Candy *snack = new Candy[3];
        snack[0] = { "Mocha Munch1", 2.3, 350 };
        snack[1] = { "Mocha Munch2", 2.5, 360 };
        snack[2] = { "Mocha Munch3", 2.7, 390 };
    
        cout << snack[0].name << "'s weight is " << snack[0].weight <<
            ", and it includes " << snack[0].calories << " calories.
    ";
        cout << snack[1].name << "'s weight is " << snack[1].weight <<
            ", and it includes " << snack[1].calories << " calories.
    ";
        cout << snack[2].name << "'s weight is " << snack[2].weight <<
            ", and it includes " << snack[2].calories << " calories.
    ";
    
        delete [] snack;
    
        system("pause");
        return 0;
    }

    #include<iostream>
    #include<array>
    
    using namespace std;
    const int Times = 3;
    
    int main()
    {
        array<double, Times> grade;
        int i;
        double avg_grade=0.0;
    
        cout << "Please enter your grade: " << endl;
        
        for (i = 0; i <= 2; i++)
        {
            cout << endl << ("%d", i+1) << " time: ";
            cin >> grade[i];
            avg_grade += grade[i];
        }
    
        avg_grade = avg_grade / Times;
        cout << endl << "The grade of average is: " << avg_grade << endl;
    
        system("pause");
        return 0;
    } 
  • 相关阅读:
    Java自学第8期——多线程
    Java自学第7期——异常(Exception)
    Java自学第6期——Collection、Map、迭代器、泛型、可变参数、集合工具类、集合数据结构、Debug
    java自学第5期——Object、Date、Calender、System、StringBuilder、基本类型包装类
    下载com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
    博客园主题SimpleMemory详细配置项
    banner自用图床
    java自学第4期——:Scanner类、匿名对象介绍、Random类、ArrayList集合、标准类格式、String类、static静态、Arrays工具类、Math类(1)
    java自学第3期——继承、多态、接口、抽象类、final关键字、权限修饰符、内部类
    数组,哈希表,字典
  • 原文地址:https://www.cnblogs.com/CJT-blog/p/10230387.html
Copyright © 2011-2022 走看看