• c++ primer plus 第七章 课后题答案


    #include <iostream>
    using namespace std;
    double HAR_AVG(double, double);
    void TEST(bool);
    
    int main()
    {
        double x, y;
        cout << "Please enter two values(encountered zero end):
    ";
        cin >> x;
        TEST(cin.fail());
        cin >> y;
        TEST(cin.fail());
        while (x*y != 0)
        {
            cout << "The harmonic mean of the two values is " << HAR_AVG(x, y) << ".
    ";
            cout << "Please enter two values(encountered zero end):
    ";
            cin >> x;
            TEST(cin.fail());
            cin >> y;
            TEST(cin.fail());
        }
    
        system("pause");
    
    }
    
    double HAR_AVG(double x, double y)
    {
        return 2.0*x*y / (x + y);
    }
    
    void TEST(bool a)
    {
        if (a == true)
            cout << "Not value!
    ";
    }

    #include <iostream>
    using namespace std;
    const int Max_Num = 10;
    int In_put(double *, int);
    double Pro_data(double *, int);
    void Display(const double *, int, double);
    
    int main()
    {
        double grade[Max_Num];
        int count;
        double avg_grade;
    
        count=In_put(grade, Max_Num);
        avg_grade=Pro_data(grade, count);
        Display(grade, count, avg_grade);
    
        system("pause");
    }
    
    int In_put(double *grade,int a)
    {
        cout << "Please enter ten golf scores (press any non-numeric button to end): 
    ";
        int count=0;
        for (int i = 0; i < a; i++)
        {
            cin >> grade[i];
            if (cin.fail())
            {
                cout << "End!
    ";
                return count;
            }
            count++;
        }
        return count;
    }
    
    double Pro_data(double *grade, int count)
    {
        double sum=0;
        for (int i = 0; i < count; i++)
        {
            sum += grade[i];
        }
        
        return sum / count;
    }
    
    void Display(const double *grade, int count, double a)
    {
        cout << "The grades you entered are:";
        for (int i = 0; i < count; i++)
        {
            cout << grade[i] << " ";
        }
        
        cout << "
    The average score is " << a << ".
    ";
    }

    #include<iostream>
    using namespace std;
    const int Max_num = 10;
    
    struct box
    {
        char maker[40];
        float height;
        float width;
        float length;
        float volume;
    };
    
    bool In_put(box *,int);
    void Display(box,int);
    void Setting(box *,int);
    
    int main()
    {
        box *abc = new box[Max_num];
        int count=0;
    
        for (int i = 0; i < Max_num; i++)
        {    
            if (In_put(abc, i))
                break;
            count++;
        }
    
        cout << "You have entered " << count << " boxes.
    ";
        for (int j = 0; j < count; j++)
        {
            Display(abc[j], j);
        }
        
        system("pause");
    }
    
    bool In_put(box *abc, int i)
    {
        cout << "Please enter the maker of the box, as well as the length, width, and height:
    ";
        cin >> abc[i].maker;
        if (cin.fail())
        {
            cout << "The maker of the input box has an error.
    ";
            return cin.fail();
        }
        cin >> abc[i].length;
        if (cin.fail())
        {
            cout << "The maker of the input length has an error.
    ";
            return cin.fail();
        }
        cin >> abc[i].width;
        if (cin.fail())
        {
            cout << "The maker of the input width has an error.
    ";
            return cin.fail();
        }
        cin >> abc[i].height;
        if (cin.fail())
        {
            cout << "The maker of the input height has an error.
    ";
            return cin.fail();
        }
        Setting(abc,i);
        return false;
    }
    
    void Setting(box *abc, int i)
    {
        abc[i].volume = abc[i].length*abc[i].width*abc[i].height;
    }
    
    void Display(box abc,int j)
    {
        cout << "The " << j+1 << "th box has the following information:
    ";
        cout << "maker:" << abc.maker << endl;
        cout << "length:" << abc.length << endl;
        cout << "" << abc.width << endl;
        cout << "height:" << abc.height << endl;
        cout << "volume:" << abc.volume << endl;
    }

    #include <iostream>
    long double probability(unsigned, unsigned, unsigned, unsigned);
    int main()
    {
        using namespace std;
        double total1, choices1;
        double total2, choices2;
        cout << "Enter the total number of Domain choices on the game card and
    "
            "the number of picks allowed:
    ";
        cin >> total1 >> choices1;
        cout << "Enter the total number of Special choices on the game card and
    "
            "the number of picks allowed:
    ";
        cin >> total2 >> choices2;
        while (!(cin.fail()) && choices1 <= total1 && choices2 <= total2)
        {
            cout << "You have one chance in ";
            cout << probability(total1, choices1, total2, choices2);
            cout << " of winning.
    ";
            cout << "Next four numbers (Non-numeric to quit): ";
            cin >> total1 >> choices1;
            cin >> total2 >> choices2;
        }
        cout << "bye
    ";
        system("pause");
        return 0;
    }
    
    long double probability(unsigned numbers1, unsigned picks1, unsigned numbers2, unsigned picks2)
    {
        long double result = 1.0;  // here come some local variables
        long double n;
        unsigned p;
    
        for (n = numbers1, p = picks1; p > 0; n--, p--)
            result = result * n / p;
        for (n = numbers2, p = picks2; p > 0; n--, p--)
            result = result * n / p;
        return result;
    }

    #include <iostream>
    using namespace std;
    unsigned long factorial(unsigned int);
    
    int main()
    {
        unsigned int pp;
        unsigned long qq;
        cout << "Please enter a non-negative number:";
        if (cin >> pp)
        {
            qq = factorial(pp);
            cout << "The factorial of this value is:" << qq << endl;
        }
        system("pause");
    }
    
    unsigned long factorial(unsigned int pp)
    {
        unsigned long qq;
        if (pp > 0)
            qq = pp * factorial(pp - 1);
        else
            return 1;
        return qq;
    }

    #include <iostream>
    using namespace std;
    const int Max_num = 10;
    int Fill_array(double *, int);
    void Show_array(const double *, int);
    void Reverse_array(double *, int);
    
    int main()
    {
        double abc[Max_num];
        int num_rel;
        num_rel=Fill_array(abc, Max_num);
        Show_array(abc, num_rel);
        Reverse_array(abc, num_rel);
        Show_array(abc, num_rel);
        Reverse_array(abc+1, num_rel-2);
        Show_array(abc, num_rel);
    
        system("pause");
    }
    
    int Fill_array(double *abc, int t)
    {
        int count_num = 0;
        cout << "Please enter no more than" << t << "digits (experiencing non-numeric exits):
    ";
        for (int i = 0; i < t; i++)
        {
            if (!(cin >> abc[i]))
                break;
            count_num++;
        }
        cout << "Input completed.
    ";
        return count_num;
    }
    
    void Show_array(const double *abc, int t)
    {
        for (int i = 0; i < t; i++)
        {
            cout << abc[i] << endl;
        }
    }
    
    void Reverse_array(double *abc, int t)
    {
        cout << "Reverse!" << endl;
        int i = 0, j = t - 1;
        double qqq;
        while (j > i)
        {
            qqq = abc[i];
            abc[i] = abc[j];
            abc[j] = qqq;
            i++;
            j--;
        }
    }

    #include <iostream>
    const int Max = 5;
    
    double * fill_array(double *, double *);
    void show_array(const double *, const double *); 
    void revalue(double, double *, double *);
    
    int main()
    {
        using namespace std;
        double properties[Max];
    
        double *end = fill_array(properties, properties+Max);
        show_array(properties, end);
        if (end > properties)
        {
            cout << "Enter revaluation factor: ";
            double factor;
            while (!(cin >> factor))  
            {
                cin.clear();
                while (cin.get() != '
    ')
                    continue;
                cout << "Bad input; Please enter a number: ";
            }
            revalue(factor,properties, end);
            show_array(properties, end);
        }
        cout << "Done.
    ";
        
        system("pause");
    }
    
    double * fill_array(double * ar, double * br)
    {
        using namespace std;
        double temp;
        double *i;
        int j = 0;
        for (i = ar; i<br; i++,j++)
        {
            cout << "Enter value #" << (j + 1) << ": ";
            cin >> temp;
            if (!cin)    
            {
                cin.clear();
                while (cin.get() != '
    ')
                    continue;
                cout << "Bad input; input process terminated.
    ";
                break;
            }
            else if (temp < 0)   
                break;
            *i = temp;
        }
        return i;
    }
    
    void show_array(const double *ar, const double *br)
    {
        using namespace std;
        const double *i;
        int j = 0;
        for (i = ar; i < br; i++,j++)
        {
            cout << "Property #" << (j + 1) << ": $";
            cout << *i << endl;
        }
    }
    
    void revalue(double r,double *ar,double *br)
    {
        double *i;
        for (i = ar; i < br; i++)
            (*i)*= r;
    }

    #include <iostream>
    #include <string>
    const int Seasons = 4;
    const char * Snames[Seasons] = { "Spring", "Summer", "Fall", "Winter" };
    
    int main()
    {
        double expenses[Seasons];
        fill(expenses);
        show(expenses);
        system("pause");
    }
    
    void fill(double * pa)
    {
        for (int i = 0; i < Seasons; i++)
        {
            std::cout << "Enter " << Snames[i] << " expenses: ";
            std::cin >> pa[i];
        }
    }
    
    void show(double * da)
    {
        double total = 0.0;
        std::cout << "
    EXPENSES
    ";
        for (int i = 0; i < Seasons; i++)
        {
            std::cout << Snames[i] << ": $" << da[i] << '
    ';
            total += da[i];
        }
        std::cout << "Total: $" << total << '
    ';
    }
    #include <iostream>
    #include <string>
    const int Seasons = 4;
    const char * Snames[Seasons] = { "Spring", "Summer", "Fall", "Winter" };
    struct expenses
    {
        double expenses;
    };
    void fill(expenses *);
    void show(expenses *);
    
    int main()
    {
        expenses *cost = new expenses[Seasons];
        fill(cost);
        show(cost);
        system("pause");
    }
    
    void fill(expenses * pa)
    {
        for (int i = 0; i < Seasons; i++)
        {
            std::cout << "Enter " << Snames[i] << " expenses: ";
            std::cin >> pa[i].expenses;
        }
    }
    
    void show(expenses * da)
    {
        double total = 0.0;
        std::cout << "
    EXPENSES
    ";
        for (int i = 0; i < Seasons; i++)
        {
            std::cout << Snames[i] << ": $" << da[i].expenses << '
    ';
            total += da[i].expenses;
        }
        std::cout << "Total: $" << total << '
    ';
    }

    #include <iostream>
    using namespace std;
    const int SLEN = 30;
    struct student {
        char fullname[SLEN];
        char hobby[SLEN];
        int ooplevel;
    };
    
    int getinfo(student [], int);
    void display1(student);
    void display2(const student *);
    void display3(const student *, int);
    
    int main()
    {
        cout << "Enter class size:";
        int class_size;
        while (!(cin >> class_size))
        {
            cin.clear();
            cin.ignore();
            cout << "Please enter class size:";
        };
        
        student * ptr_stu = new student[class_size];
        int entered = getinfo(ptr_stu, class_size);
        for (int i = 0; i < entered; i++)
        {
            display1(ptr_stu[i]);
            display2(&ptr_stu[i]);
        }
        display3(ptr_stu, entered);
        delete[] ptr_stu;
        cout << "Done
    ";
        system("pause");
    }
    
    int getinfo(student pa[], int n)
    {
        int count = 0;
        for (int i = 0; i < n; i++)
        {
            cin.ignore();
            cout << "Please enter the fullname:";
            cin.getline(pa[i].fullname, SLEN);
            cout << "Please enter the hobby:";
            cin.getline(pa[i].hobby, SLEN);
            cout << "Please enter the ooplevel:";
            cin >> pa[i].ooplevel;
            count++;
        }
        cout << "
    Enter end!";
        return count;
    }
    
    void display1(student pa)
    {
        cout << "
    display1:
    FullName:" << pa.fullname << "
    hobby:" << pa.hobby
            << "
    ooplevel:" << pa.ooplevel << endl;
    }
    void display2(const student * pa)
    {
        cout << "
    display2:
    FullName:" << pa->fullname << "
    hobby:" << pa->hobby
            << "
    ooplevel:" << pa->ooplevel << endl;
    }
    void display3(const student * pa, int n)
    {
        cout << "
    dispaly3:
    " << endl;
        for (int i = 0; i < n; i++)
            cout << i+1 << "::FullName:" << pa[i].fullname << "
    hobby:" << pa[i].hobby
            << "
    ooplevel:" << pa[i].ooplevel << endl;
    }

    #include <iostream>
    using namespace std;
    
    double add(double, double);
    double sub(double, double);
    double calculate(double, double, double (double, double));
    
    int main()
    {
        double a, b, c, d;
        cout << "Please enter two values:
    ";
        while ((cin >> a) && (cin >> b))
        {
            c=calculate(a, b, add);
            d=calculate(a, b, sub);
            cout << "The sum and subtraction of the values are:" << c << "," << d << endl;
            cout << "Please continue to enter:
    ";
        }
        system("pause");
    }
    
    double add(double a, double b)
    {
        double c;
        c = a + b;
        return c;
    }
    
    double sub(double a, double b)
    {
        double d;
        d = a - b;
        return d;
    }
    
    double calculate(double a, double b, double cal(double, double))
    {
        double e;
        e = cal(a, b);
        return e;
    }
  • 相关阅读:
    At least one JAR was scanned for TLDs yet contained no TLDs
    {转} MJPG流媒体在HTML5的呈现方案
    {转}理解HTTP/304响应
    blueimp,预览遮罩范围控制
    快速生成mysql上百万条测试数据
    mysql插入文本文档及读取
    tomcat启动报错:注释指定的bean类.与现有的冲突.相同的名称和类
    csv的文件excel打开长数字后面位变0的解决方法
    UNIX或LINUX时间戳转时间
    Myeclipse更新SVNStatusSubscriber 时报告了错误。1 中的 0 个资源已经同步。
  • 原文地址:https://www.cnblogs.com/CJT-blog/p/10277801.html
Copyright © 2020-2023  润新知