zoukankan      html  css  js  c++  java
  • 7.13编程练习

    1. 第一题
    #include <iostream>
    using namespace std;
    double average(int, int);
    
    
    int main() {
    
        int a, b;
        double s;
        while (cin >> a >> b && a != 0 && b != 0)
        {
            s = average(a, b);
            cout << "调和平均数为:" << s << endl;
        }
    
        return 0;
    }
    
    double average(int a, int b)
    {
        return 2.0*a*b/(a+b);
    }
    

    运行结果:

    2 4
    调和平均数为:2.66667
    2 2
    调和平均数为:2
    2 0
    
    1. 第二题
    #include <iostream>
    using namespace std;
    int in(double [], int);
    void show(const double [], int);
    double average(const double [], int);
    const int Max_grade = 10;
    
    
    int main() {
    
        double grade[Max_grade];
        int size = in(grade, Max_grade);
        show(grade, size);
        if (size > 0)
        {
            double av = average(grade, size);
            cout << "平均成绩为:" << av << endl;
        }
    
        return 0;
    }
    
    int in(double grade [], int n)
    {
        double x;
        int i = 0;
        while (cin >> x && x != -1 && i < Max_grade)
        {
            grade[i++] = x;
        }
        return i;
    }
    
    void show(const double arr[], int n)
    {
        for (int i=0;i<n;i++)
        {
            cout << "高尔夫成绩为 #" << (i+1) << ": ";
            cout << arr[i];
        }
        cout << endl;
    }
    
    double average(const double arr[], int n)
    {
        double sum=0;
        for (int i=0;i<n;i++)
        {
            sum += arr[i];
        }
    
        return sum / n;
    }
    

    运行结果:

    99
    87
    87
    -1
    高尔夫成绩为 #1: 99高尔夫成绩为 #2: 87高尔夫成绩为 #3: 87
    平均成绩为:91
    
    1. 第三题
    #include <iostream>
    using namespace std;
    struct box
    {
        char maker[40];
        float height;
        float width;
        float length;
        float volume;
    };
    
    void a(box x);
    void b(box * x);
    
    
    int main() {
    
        box fox = {"crazy", 8, 8, 8, 500};
        a(fox);
        b(&fox);
    
        return 0;
    }
    
    void a(box x)
    {
        cout << x.maker << endl;
        cout << x.height << endl;
        cout << x.width << endl;
        cout << x.volume << endl;
    }
    
    void b(box * x)
    {
        x->volume = x->length * x->width * x->height;
        cout << "容量变为:" << x->volume << endl;
    }
    

    运行结果:

    crazy
    8
    8
    500
    容量变为:512
    
    1. 第四题
    #include <iostream>
    using namespace std;
    long double probability(unsigned numbers1, unsigned picks1, unsigned numbers2, unsigned picks2);
    
    
    int main() {
    
        double total_1, choices_1, total_2, choices_2;
        cout << "Enter the total number of choices on the game card and
    "
                "the number of picks allowed:
    ";
        while ((cin >> total_1 >> choices_1 >> total_2 >> choices_2) && choices_1 <= total_1 && choices_2 <= total_2)
        {
            cout << "You have one chance in ";
            cout << probability(total_1, choices_1, total_2, choices_2);
            cout << " of winning.
    ";
            cout << "Next two numbers (q to quit):";
        }
        cout << "bye
    ";
    
        return 0;
    }
    
    long double probability(unsigned numbers1, unsigned picks1, unsigned numbers2, unsigned picks2)
    {
        long double result1 = 1.0, result2 = 1.0;
        long double n1, n2;
        unsigned p1, p2;
    
        for (n1=numbers1, p1=picks1;p1 > 0;n1--, p1--)
        {
            result1 = result1 * n1 / p1;
        }
        for (n2=numbers2, p2=picks2;p2 > 0;n2--, p2--)
        {
            result2 = result2 * n2 / p2;
        }
        return result1 * result2;
    }
    

    运行结果:

    Enter the total number of choices on the game card and
    the number of picks allowed:
    47 5 27 1
    You have one chance in 4.14164e+07 of winning.
    Next two numbers (q to quit):q
    bye
    
    1. 第五题
    #include <iostream>
    using namespace std;
    long jc(int n);
    
    
    int main() {
    
        int n;
        long sum=0;
        cout << "请输入一个整数计算阶乘:";
        while (cin >> n)
        {
            sum = jc(n);
            cout << n << "的阶乘为:" << sum << endl;
            cout << "请再次输入数值进行求解:(q to quit)";
        }
        cout << "bye!
    ";
    
        return 0;
    }
    
    long jc(int n)
    {
        if (n == 0 || n == 1)
            return 1;
        if (n > 1)
            return n * jc(n-1);
    }
    

    运行结果:

    请输入一个整数计算阶乘:4
    4的阶乘为:24
    请再次输入数值进行求解:(q to quit)5
    5的阶乘为:120
    请再次输入数值进行求解:(q to quit)q
    bye!
    
    1. 第六题
    #include <iostream>
    using namespace std;
    const int Max = 20;
    int Fill_array(double arr[], int n);
    void Show_array(const double arr[], int n);
    void Reverse_array(double * arr, int n);
    void Reverse_array_2(double * arr, int n);
    
    
    int main() {
    
        double x[Max];
        int size = Fill_array(x, Max);
        Show_array(x, size);
        if (size > 1)
        {
            cout << "完全反转数组:
    ";
            Reverse_array(x, size);
            cout << "除第一个和最后一个之外反转数组:
    ";
            Reverse_array_2(x, size);
        }
        else if (size == 1)
        {
            cout << "数组大小为1,反转结果相同
    ";
        }
        else
        {
            cout << "数组大小为0
    ";
        }
    
        return 0;
    }
    
    int Fill_array(double arr[], int n)
    {
        double x;
        int i = 0;
        while (cin >> x && i < Max)
        {
            arr[i++] = x;
        }
        return i;
    }
    
    void Show_array(const double arr[], int n)
    {
        for (int i=0;i<n;i++)
        {
            cout << (i+1) << ": ";
            cout << arr[i] << endl;
        }
    }
    
    void Reverse_array(double * arr, int n)
    {
        double x;
        for (int i=0, j=n-1;i<j;i++, j--)
        {
            x = arr[i];
            arr[i] = arr[j];
            arr[j] = x;
        }
        Show_array(arr, n);
    }
    
    void Reverse_array_2(double * arr, int n)
    {
        double x;
        for (int i=1, j=n-2;i<j;i++, j--)
        {
            x = arr[i];
            arr[i] = arr[j];
            arr[j] = x;
        }
        Show_array(arr, n);
    }
    

    运行结果:

    1 2 3 4 5 q
    1: 1
    2: 2
    3: 3
    4: 4
    5: 5
    完全反转数组:
    1: 5
    2: 4
    3: 3
    4: 2
    5: 1
    除第一个和最后一个之外反转数组:
    1: 5
    2: 2
    3: 3
    4: 4
    5: 1
    
    1. 第七题
    #include <iostream>
    using namespace std;
    const int Max = 5;
    double * fill_array(double begin[], int limit);
    void show_array(const double * begin, const double * end);
    void revalue(double r, double * begin, const double * end);
    
    
    int main() {
    
        double properties[Max];
    
        double * end = fill_array(properties, Max);
        show_array(properties, end);
        if (properties != end)
        {
            cout << "Enter revaluation factor:";
            double factor;
            while (!(cin >> factor))
            {
                cin.clear();
                while (cin.get() != '
    ')
                    continue;
                cout << "Bad input; input process terminated.
    ";
            }
            revalue(factor, properties, end);
            show_array(properties, end);
        }
        cout << "Done.
    ";
        cin.get();
        cin.get();
    
        return 0;
    }
    
    double * fill_array(double begin[], int limit)
    {
        double temp;
        int i;
        for (i = 0; i < limit; ++i)
        {
            cout << "Enter value #" << (i+1) << ":";
            cin >> temp;
            if (!cin)
            {
                cin.clear();
                while (cin.get() != '
    ')
                    continue;
                cout << "Bad input; input process terminated.
    ";
                break;
            }
            else if (temp < 0)
                break;
            begin[i] = temp;
        }
        return &begin[i];
    }
    
    void show_array(const double * begin, const double * end)
    {
        const double * pt;
        int i = 0;
        for (pt = begin;pt != end; pt ++)
        {
            cout << "Property #" << (i+1) << ": $";
            cout << *pt << endl;
            i += 1;
        }
    }
    
    void revalue(double r, double * begin, const double * end)
    {
        double * pt;
        for (pt = begin;pt != end; pt ++)
        {
            *pt *= r;
        }
    }
    

    运行结果:(列举了两种情况)

    Enter value #1:100000
    Enter value #2:80000
    Enter value #3:222000
    Enter value #4:240000
    Enter value #5:118000
    Property #1: $100000
    Property #2: $80000
    Property #3: $222000
    Property #4: $240000
    Property #5: $118000
    Enter revaluation factor:0.8
    Property #1: $80000
    Property #2: $64000
    Property #3: $177600
    Property #4: $192000
    Property #5: $94400
    Done.
    Enter value #1:200000
    Enter value #2:84000
    Enter value #3:160000
    Enter value #4:-2
    Property #1: $200000
    Property #2: $84000
    Property #3: $160000
    Enter revaluation factor:1.20
    Property #1: $240000
    Property #2: $100800
    Property #3: $192000
    Done.
    
    1. 第八题
      a
    #include <iostream>
    using namespace std;
    const int Seasons = 4;
    const char * Snames[Seasons] = {
            "Spring", "Summer", "Fall", "Winter"
    };
    
    void fill(double * pa);
    void show(double * da);
    
    
    int main() {
    
        double expenses[Seasons];
        fill(&expenses[0]);
        show(expenses);
    
        return 0;
    }
    
    void fill(double * pa)
    {
        for (int i = 0; i < Seasons; ++i)
        {
            cout << "Enter " << Snames[i] << " expenses:";
            cin >> pa[i];
        }
    }
    
    void show(double * da)
    {
        double total = 0.0;
        cout << "
    EXPENSES
    ";
        for (int i = 0; i < Seasons; ++i)
        {
            cout << Snames[i] << ": $" << da[i] << endl;
            total += da[i];
        }
        cout << "Total Expenses: $" << total << endl;
    }
    

    运行结果:

    Enter Spring expenses:212
    Enter Summer expenses:256
    Enter Fall expenses:208
    Enter Winter expenses:244
    
    EXPENSES
    Spring: $212
    Summer: $256
    Fall: $208
    Winter: $244
    Total Expenses: $920
    

    b

    #include <iostream>
    using namespace std;
    const int Seasons = 4;
    const char * Snames[Seasons] = {
            "Spring", "Summer", "Fall", "Winter"
    };
    struct money
    {
        double expenses[Seasons];
    };
    
    void fill(money * pa);
    void show(money da);
    
    
    int main() {
    
        money out{};
        fill(&out);
        show(out);
    
        return 0;
    }
    
    void fill(money * pa)
    {
        for (int i = 0; i < Seasons; ++i)
        {
            cout << "Enter " << Snames[i] << " expenses:";
            cin >> pa->expenses[i];
        }
    }
    
    void show(money da)
    {
        double total = 0.0;
        cout << "
    EXPENSES
    ";
        for (int i = 0; i < Seasons; ++i)
        {
            cout << Snames[i] << ": $" << da.expenses[i] << endl;
            total += da.expenses[i];
        }
        cout << "Total Expenses: $" << total << endl;
    }
    

    运行结果:

    Enter Spring expenses:212
    Enter Summer expenses:256
    Enter Fall expenses:208
    Enter Winter expenses:244
    
    EXPENSES
    Spring: $212
    Summer: $256
    Fall: $208
    Winter: $244
    Total Expenses: $920
    
    1. 第九题
    #include <iostream>
    #include <cstring>
    using namespace std;
    const int SLEN = 30;
    struct student
    {
        char fullname[SLEN];
        char hobby[SLEN];
        int ooplevel;
    };
    
    int getinfo(student pa[], int n);
    void display1(student st);
    void display2(const student * ps);
    void display3(const student pa[], int n);
    
    int main() {
    
        cout << "Enter class size:";
        int class_size;
        cin >> class_size;
        while (cin.get() != '
    ')
            continue;
    
        auto * 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
    ";
    
        return 0;
    }
    
    int getinfo(student pa[], int n)
    {
        char name[SLEN], bo[SLEN];
        int oop;
        int i = 0;
        while (i < n && cin >> name >> bo >> oop && strcmp(name, "") != 0)
        {
            strcpy(pa[i].fullname, name);
            strcpy(pa[i].hobby, bo);
            pa[i].ooplevel = oop;
            i += 1;
        }
        return i;
    }
    
    void display1(student st)
    {
        cout << st.fullname << " " << st.hobby << " " << st.ooplevel << endl;
    }
    
    void display2(const student * ps)
    {
        cout << ps->fullname << " " << ps->hobby << " " << ps->ooplevel << endl;
    }
    
    void display3(const student pa[], int n)
    {
        for (int i = 0; i < n; ++i)
        {
            cout << pa[i].fullname << " " << pa[i].hobby << " " << pa[i].ooplevel << endl;
        }
    }
    

    运行结果:

    Enter class size:4
    zxc 123 1
    asd 456 2
    qwe 789 3
    rty 741 4
    zxc 123 1
    zxc 123 1
    asd 456 2
    asd 456 2
    qwe 789 3
    qwe 789 3
    rty 741 4
    rty 741 4
    zxc 123 1
    asd 456 2
    qwe 789 3
    rty 741 4
    Done
    
    1. 第十题
    #include <iostream>
    using namespace std;
    
    double calculate(double x, double y, double (*pf)(double x, double y));
    double add(double x, double y);
    double subtract(double x, double y);
    double multiply(double x, double y);
    double divide(double x, double y);
    
    
    int main() {
    
        int x, y;
        double number[4];
        double (*pf[4])(double x, double y) = {add, subtract, multiply, divide};
        while (cin >> x >> y)
        {
            for (int i = 0;i<4;i++)
            {
                number[i] = calculate(x, y, pf[i]);
            }
            cout << "加减乘除结果分别为:
    ";
            for (double i : number)
                cout << i << endl;
        }
    
        return 0;
    }
    
    double calculate(double x, double y, double (*pf)(double x, double y))
    {
        return pf(x, y);
    }
    
    double add(double x, double y)
    {
        return x + y;
    }
    
    double subtract(double x, double y)
    {
        return x - y;
    }
    
    double multiply(double x, double y)
    {
        return x * y;
    }
    
    double divide(double x, double y)
    {
        return x / y;
    }
    

    运行结果:

    2 4
    加减乘除结果分别为:
    6
    -2
    8
    0.5
    
    1 1
    加减乘除结果分别为:
    2
    0
    1
    1
    q
    

    上面只是提到了指向函数指针的指针,下面我增加了指向该数组指针的指针,看代码:

    #include <iostream>
    using namespace std;
    
    double calculate(double x, double y, double (*pf)(double x, double y));
    double add(double x, double y);
    double subtract(double x, double y);
    double multiply(double x, double y);
    double divide(double x, double y);
    
    
    int main() {
    
        int x, y;
        double number[4];
        double (*pf[4])(double x, double y) = {add, subtract, multiply, divide};
    
        // 1.使用指向函数指针的指针
        while (cin >> x >> y)
        {
            for (int i = 0;i<4;i++)
            {
                number[i] = calculate(x, y, pf[i]);
            }
            cout << "加减乘除结果分别为:
    ";
            for (double i : number)
                cout << i << endl;
            cout << endl;
        }
    
        // 上面使用非double型数字跳出后会导致输入流关闭,需要重新打开才可以输入
        if (!cin)
        {
            cin.clear();
            while (cin.get() != '
    ')
                continue;
        }
        cout << "扩展:
    ";
        // 2.应用指向数组指针的指针,两种表示方法,pc和pd都是指向同一个数组指针的指针
        auto pc = &pf;
        double (*(*pd)[4])(double x, double y) = &pf;
    
        while (cin >> x >> y)
        {
            for (int i = 0;i<4;i++)
            {
                //number[i] = calculate(x, y, (*pc)[i]); 
                // 使用pc和pd结果相同,很简单,有定义pc = &pf,那么两边都加上*,*pc = *&pf = pf,和上面一样调用
                // 至于*(*pd)其实和(*pd)效果是一样的,这涉及到了两种学派的使用方法。
                number[i] = calculate(x, y, *(*pd)[i]);
            }
            cout << "加减乘除结果分别为:
    ";
            for (double i : number)
                cout << i << endl;
            cout << endl;
        }
    
        return 0;
    }
    
    double calculate(double x, double y, double (*pf)(double x, double y))
    {
        return pf(x, y);
    }
    
    double add(double x, double y)
    {
        return x + y;
    }
    
    double subtract(double x, double y)
    {
        return x - y;
    }
    
    double multiply(double x, double y)
    {
        return x * y;
    }
    
    double divide(double x, double y)
    {
        return x / y;
    }
    

    运行结果:

    2 4
    加减乘除结果分别为:
    6
    -2
    8
    0.5
    
    q
    扩展:
    2 4
    加减乘除结果分别为:
    6
    -2
    8
    0.5
    
    q
    
  • 相关阅读:
    centos yum安装报错could not resolve host: mirrorlist.centos.org
    feign拦截器和解码器
    git error:invalid path
    Codeforces Round #689 (Div. 2)
    RSA加解密,Java和C#互通
    C# WinForm遍历窗体控件的3种方法
    vs2019如何自动生成有下划线前缀的字段名?
    C# EF Core 后端代码已定义的用户实体,如何扩展字段?
    SQL Server 查询分析及优化方法
    SQL Server 新增函数大全(各版本)
  • 原文地址:https://www.cnblogs.com/ycycn/p/14456836.html
Copyright © 2011-2022 走看看