zoukankan      html  css  js  c++  java
  • C++Primer Plus习题记录-Chapter7

    IDE : VS2017
    C++
    //7-1
    #include "pch.h"
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    #include <cctype>
    #include <array>
    #include <iomanip>
    
    using namespace std;
    double add(double x, double y);
    double mux(double x, double y);
    double calculate(double x, double y, double (*fun)(double, double));
    int main() {
        double x = 0.0, y = 0.0;
        cin >> x >> y;
        cout << calculate(x, y, add) << "
    " << calculate(x, y, mux);
        return 0;
    }
    double add(double x, double y) {
        return x + y;
    }
    double mux(double x, double y) {
        return x * y;
    }
    double calculate(double x, double y, double (*fun)(double, double)) {
        return (*fun)(x, y);
    }
    //7-2
    #include "pch.h" #include <iostream> #include <fstream> #include <cstdlib> #include <string> #include <cctype> #include <array> #include <iomanip> using namespace std; void ingrade(double *a, unsigned int *number); void display(const double *a, const unsigned int number); void aver(const double *a, const unsigned int number); int main() { double a[10]; unsigned int enter=0; ingrade(a, &enter); display(a, enter); aver(a, enter); return 0; } void ingrade(double *a,unsigned int *number) { while (cin >> a[(*number)++]&&(*number)<11) { if ((a[*number-1] == -1)) break; } (*number)--; } void display(const double *a,const unsigned int number) { for (unsigned int i = 0; i < number; i++) { cout << a[i] << " "; } } void aver(const double *a, const unsigned int number) { double sum = 0.0; for (unsigned int i = 0; i < number; i++) { sum += a[i]; } cout<<sum/number; }

    7-3

    #include "pch.h"
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    #include <cctype>
    #include <array>
    #include <iomanip>
    
    using namespace std;
    
    struct box
    {
        char maker[40];
        float height;
        float width;
        float length;
        float volume;
    };
    void PrintBox(box boxs);
    void PrintAdd(box * boxs);
    int main() {
        box boxs = { "Light monster",1.78,2.2,3.1,8.5 };
        PrintBox(boxs);
        PrintAdd(&boxs);
    
        return 0;
    }
    void PrintBox(box boxs) {
        cout << "name: " << boxs.maker << " ";
        cout << "height: " << boxs.height << " ";
        cout << " " << boxs.width << " ";
        cout << "length: " << boxs.length << " ";
        cout << "volume: " << boxs.volume << endl;
    }
    void PrintAdd(box * boxs) {
        boxs->volume = boxs->height*boxs->length*boxs->width;
        cout << boxs->volume;
    }

    7-4

    #include "pch.h"
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    #include <cctype>
    #include <array>
    #include <iomanip>
    
    using namespace std;
    long double ritio(long double x, double y);
    int main() {
        long double n1, n2, p1, p2;
        cin >> n1 >> p1 >> n2 >> p2;
        cout << ritio(n1, p1)*ritio(n2, p2);
    
        return 0;
    }
    long double ritio(long double x, double y) {
        long double ritios = 1.0;
        long double n = 0.0, p = 0.0;
        for (n = x, p = y; p > 0; n--, p--) {
            ritios *= p / n;
        }
        return ritios;
    }

    7-5

    #include "pch.h"
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    #include <cctype>
    #include <array>
    #include <iomanip>
    
    using namespace std;
    double factorial(int n);
    int main() {
        int n;
        double sum;
        cin >> n;
        sum = factorial(n);
        cout << sum;
        return 0;
    }
    double factorial(int n) {
        if (n == 0 || n == 1)
            return 1;
        else
            return n * factorial(n - 1);
    }

    7-6

    #include "pch.h"
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    #include <cctype>
    #include <array>
    #include <iomanip>
    
    using namespace std;
    int Fill_array(double *ar, int Size);
    void Show_array(double *ar, int Size);
    void Reverse_array(double *ar, int Size);
    int main() {
        const int Size = 10;
        double ar[Size];
        int length=Fill_array(ar, Size);
        Show_array(ar, length);
        Reverse_array(ar, length);
        Show_array(ar, length);
        return 0;
    }
    int Fill_array(double *ar, int Size) {
        cout << "Please in put double type:";
        int i = 0;
        while (i<Size&&cin>>ar[i])
        {
            i++;
        }
        return i;
    }
    void Show_array(double *ar, int Size) {
        for (int i = 0; i < Size; i++)
            cout << ar[i] << " ";
        cout << endl;
    }
    void Reverse_array(double *ar, int Size) {
        for (int i = 0; i < Size/2; i++) {
            double temp = ar[i];
            ar[i] = ar[Size - i - 1];
            ar[Size - i - 1] = temp;
        }
    }

    7-7

    #include "pch.h"
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    #include <cctype>
    #include <array>
    #include <iomanip>
    
    using namespace std;
    double *Fill_array(double *ar, int Size);
    void Show_array(double *ar, double* Size);
    int main() {
        const int Size = 10;
        double ar[Size];
        double *length=Fill_array(ar, Size);
        Show_array(ar, length);
        return 0;
    }
    double *Fill_array(double *ar, int Size) {
        cout << "Please in put double type:";
        int i = 0;
        while (i<Size&&cin>>*ar)
        {
            i++;
            ar = ar + 1;
        }
        return ar;
    }
    void Show_array(double *ar, double* Size) {
        for (;ar<Size; ar++)
            cout << *ar << " ";
        cout << endl;
    }

    7-8

    #include "pch.h"
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    #include <cctype>
    #include <array>
    #include <iomanip>
    
    using namespace std;
    void fill(double *pa);
    void show(double *pa);
    
    const int Size = 4;
    const char *Season[Size] = { "Spring","Summer","Fall","Winter" };
    struct cost
    {
        double expenses[Size];
    };
    void fill(cost *pCost);
    void show(cost *pCost);
    int main() {
        cout << "Situation A:" << endl;
        double pa[Size] = { 0 };
        fill(pa);
        show(pa);
        cout << "Situation B:" << endl;
        cost *pCost=new cost;
        fill(pCost);
        show(pCost);
        delete pCost;
        return 0;
    }
    void fill(double *pa) {
        for (int i = 0; i < Size; i++) {
            cout << Season[i] << ": ";
            cin >> pa[i];
        }
    }
    void show(double *pa) {
        double total=0.0;
        cout << "
    EXPENSES
    ";
        for (int i = 0; i < Size; i++) {
            cout << Season[i] << ": $" << pa[i] << endl;
            total += pa[i];
        }
        cout << "Total expenses: $" << total << endl;
    }
    void fill(struct cost *pCost) {
        for (int i = 0; i < Size; i++) {
            cout << "Enter " << Season[i] << "expenses: ";
            cin >> pCost->expenses[i];
        }
    }
    void show(struct cost *pCost) {
        double total=0.0;
        cout << "
    EXPENSES
    ";
        for (int i = 0; i < Size; i++) {
            cout << Season[i] << ": $" << pCost->expenses[i] << endl;
            total += pCost->expenses[i];
        }
        cout << "Total expenses: $" << total;
    }

    7-9

    #include "pch.h"
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    #include <cctype>
    #include <array>
    #include <iomanip>
    
    using namespace std;
    const int SLEN = 30;
    struct student
    {
        char fullname[SLEN];
        char hobby[SLEN];
        int ooplevel;
    };
    int getinfo(student pa[], int n) {
        int true_in = 0;
        for (int i = 0; i < n; i++) {
            cout << "Enter the info of student #" << i + 1 << endl;
            cout << "Enter full name(blank line to quit): ";
            cin.getline(pa[i].fullname, SLEN);
            cout << "Enter hobby: ";
            cin.getline(pa[i].hobby, SLEN);
            cout << "Enter opplevel: ";
            cin >> pa[i].ooplevel;
            while (cin.get() != '
    ')
                continue;
            true_in++;
        }
        return true_in;
    }
    void display1(student st)
    {
        cout << "Using display1(student st): " << endl;
        cout << "Full name: " << st.fullname << endl;
        cout << "Hobby: " << st.hobby << endl;
        cout << "Ooplevel: " << st.ooplevel << endl;
    }
    void display2(const student *st)
    {
        cout << "Using display2(const student *st)" << endl;
        cout << "Full name: " << st->fullname << endl;
        cout << "Hobby: " << st->hobby << endl;
        cout << "Ooplevel: " << st->ooplevel << endl;
    }
    void display3(const student pa[], int n)
    {
        cout << "Using display3(const student pa[], int n)" << endl;;
        for (int i = 0; i < n; i++)
        {
            cout << "Infomation of student #" << i + 1 << ": " << endl;
            cout << "Full name: " << pa[i].fullname << endl;
            cout << "Hobby: " << pa[i].hobby << endl;
            cout << "Ooplevel: " << pa[i].ooplevel << endl;
        }
    }
    int main() {
        cout << "Enter class size: ";
        int class_size;
        cin >> class_size;
        while (cin.get() != '
    ')
            continue;
        student *ptr_std = new student[class_size];
        int entered = getinfo(ptr_std, class_size);
        for (int i = 0; i < entered; i++) {
            display1(ptr_std[i]);
            display2(&ptr_std[i]);
        }
        display3(ptr_std, entered);
        delete[] ptr_std;
        cout << "Done
    ";
        return 0;
    }

    7-10

    #include "pch.h"
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    #include <cctype>
    #include <array>
    #include <iomanip>
    
    using namespace std;
    double add(double x, double y);
    double mux(double x, double y);
    double calculate(double x, double y, double (*fun)(double, double));
    int main() {
        double x = 0.0, y = 0.0;
        cin >> x >> y;
        cout << calculate(x, y, add) << "
    " << calculate(x, y, mux);
        return 0;
    }
    double add(double x, double y) {
        return x + y;
    }
    double mux(double x, double y) {
        return x * y;
    }
    double calculate(double x, double y, double (*fun)(double, double)) {
        return (*fun)(x, y);
    }
  • 相关阅读:
    Node.js安装及环境配置之Windows篇
    [转]英语论文写作技巧-1
    [转]windows下安装python MySQLdb及问题解决
    SourceTree使用SSH克隆码云项目
    [吴恩达机器学习笔记]16推荐系统5-6协同过滤算法/低秩矩阵分解/均值归一化
    [吴恩达机器学习笔记]16推荐系统3-4协同过滤算法
    [吴恩达机器学习笔记]16推荐系统1-2基于内容的推荐系统
    [吴恩达机器学习笔记]15非监督学习异常检测7-8使用多元高斯分布进行异常检测
    如何求协方差矩阵[转载]
    [吴恩达机器学习笔记]15非监督学习异常检测4-6构建与评价异常检测系统
  • 原文地址:https://www.cnblogs.com/lightmonster/p/10311772.html
Copyright © 2011-2022 走看看