使用功能创建菜单

时间:2016-07-09 18:20:58

标签: c++ function menu

原始作业是编写一个程序,要求用户输入正整数值。程序应使用循环来获取从1到输入数字的所有整数之和。例如,如果用户输入50,则循环将找到1-50的sume。输入验证:不接受负起始编号。我在下面写了这段代码。

除上述之外:请修改上述代码以反映以下内容:

向用户显示算术函数菜单,允许他们选择Summation,Factorial或Exponential,如下所示:

  1. 求和
  2. 阶乘
  3. 指数
  4. Akkkkkkkkkkkk,请不要再有数学函数了。 ;)
  5. 请输入您选择的数学函数(1,2,3或4退出):

    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
        //Constants for menu choices
        const int SUM = 1,
            FACTORIAL = 2,
            EXPONENTIAL = 3,
            QUIT = 4;
    
        //Variables
        int choice, input;
        long double num = 1, value = 1, sum = 0;
    
        do
        {
            //Display menu of arithmetic functions
            cout << "\n\t\tArithmetic Functions\n\n"
                << "1. Summation\n"
                << "2. Factorial\n"
                << "3. Exponential\n"
                << "4. Akkkkkkk No more math functions please ;)\n\n"
                << "Enter your choice: ";
            cin >> choice;
    
            //validate the menu selection
            while (choice < SUM || choice > QUIT)
            {
                cout << "Please enter a valid menu choice: ";
                cin >> choice;
    
            }
    
            //process the user's choice
            if (choice != QUIT)
            {
                //ask user to input a positive integer
                cout << "Please enter any positive integer: ";
                cin >> input;
    
                while (input < 1) //input validation of positive integer
                {
                    cout << "\n" << input << " is not a positive integer.\n";
                    cout << "Please enter a positive integer: ";
                    cin >> input;
                }
    
                //Respond to the user's menu selection
                switch (choice)
                {
                case SUM:
                    for (int counter = 1; counter <= input; counter++)
                    {
                        sum += counter;
                    }
    
                    //Display result
                    cout << "\nThe sum of numbers 1 - " << input
                        << " " << "is: " << sum << endl;
    
                    sum = 0; // resets variable
                    break;
    
    
                case FACTORIAL:
                    for (int counter = 1; counter <= input; counter++)
                    {
                        value *= counter;
                    }
    
                    //Display result
                    cout << "\nThe value of " << input << " factorial is: ";
                    cout << value << endl;
    
                    value = 1; //resets variable
                    break;
    
                case EXPONENTIAL:
                    for (int counter = num; counter <= input; counter++)
                    {
                        num *= 2;
                    }
    
                    //Display result
                    cout << "\nThe result is base 2 raised to the power of " << input;
                    cout << ": ";
                    cout << num << endl;
    
                    num = 1; //resets variable
                    break;
                }
            }
    
            else
            {
                cout << "\nThe program has ended. Goodbye.\n";
            }
    
        } while (choice != QUIT);
        return 0;
    }
    

    现在新程序有这些说明: 您将需要至少4个功能:菜单,求和,阶乘和指数。

    确保标题栏显示您具有功能以及如何在它们之间传递信息。每个函数都应该有自己的标题块。标题块由每个函数顶部的几行注释组成,用于解释函数的作用。

    我被困住了,不知道从哪里开始。如何做任何建议将不胜感激。

0 个答案:

没有答案