多功能 [英] Multiple Functions

查看:100
本文介绍了多功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@ sarnold确定这里是我的更新尝试遵循指南,你给我我试图删除电源移动原型到类型,只有每个我缩小到一个int main包含每个单独的函数的声明,

@ sarnold ok here is my update tried to follow the guide that you gave me I tried removing the mains moving prototypes to the type and only having one of each I narrowed it down to one int main containing declarations for each individual functions and used voids instead of int for each function hopefully that was the right move.

#include <iostream>
#include <cmath>
#include <stdio.h>
#include <string>

using namespace std;


void function1()
{
    //Enter Variables
    int number;
    cout << "Enter Variable a: ";
    cin >> number;
    int a = number;


    cout << "Enter Variable b: ";
    cin >> number;
    int b = number;
    cout << "Enter Variable c: ";
    cin >> number;
    int c = number;
    if (a > b && a > c);
    return a;
    cout << "This is your highest number(" << a << ")";
    if (b > a && b > c);
    return b;
    cout << "This is your highest number(" << b << ")";
    if (c > a && c > B);
    return c;
    cout << "This is your highest number(" << c << ")";

}

void function2()
{
    // Prompt User for Integer

    int number;
    cout << "Enter integer 1: ";
    cin >> number;
    int a = number;

    cout << "Enter integer 2: ";
    cin >> number;
    int b = number;

    if (a != B)
        cout << "Continue";

    else
        cout << "Number 1 or 2 is the same!";

    cout << "Enter integer 3: ";
    cin >> number;
    int c = number;

    if ((a != B) != c)
        cout << "Continue";
    else
        cout << "Number 3 is the same as number 1 or 2!";
    cout << "Enter integer 4: ";
    cin >> number;
    int d = number;

    if (((a != B) != c) != d)
        cout << "numbers are all different";
    else
        cout << "Number 3 is the same as number 1, 2, or 3!";
    cout << "Thank you enter another number?\n\n";
    cin >> number;
    return 0;
}

std::string numerals = "VXLCDM";
void function3()
{
    char roman_Numeral;
    int arabic_Numeral = 0;
    cout << "Enter the Roman Numeral in Capital letters (e.g. CCXIX) : ";
    while(cin.get(roman_Numeral))
    {
        if(roman_Numeral == 'M')
            arabic_Numeral = arabic_Numeral + 1000;
        else if(roman_Numeral == 'D')
        {
            roman_Numeral = cin.peek();
            if(numerals.find(roman_Numeral, 5) != std::string::npos)
            {
                arabic_Numeral = arabic_Numeral - 500;
                continue;
            }
            else
            {
                arabic_Numeral = arabic_Numeral + 500;
                continue;
            }
        }
        else if(roman_Numeral == 'C')
        {
            roman_Numeral = cin.peek();
            if(numerals.find(roman_Numeral, 4) != std::string::npos)
            {
                arabic_Numeral = arabic_Numeral - 100;
                continue;
            }
            else
            {
                arabic_Numeral = arabic_Numeral + 100;
                continue;
            }
        }
        else if(roman_Numeral == 'L')
        {
            roman_Numeral = cin.peek();
            if(numerals.find(roman_Numeral, 3) != std::string::npos)
            {
                arabic_Numeral = arabic_Numeral - 50;
                continue;
            }
            else
            {
                arabic_Numeral = arabic_Numeral + 50;
                continue;
            }
        }
        else if(roman_Numeral == 'X')
        {
            roman_Numeral = cin.peek();
            if(numerals.find(roman_Numeral, 2) != std::string::npos)
            {
                arabic_Numeral = arabic_Numeral - 10;
                continue;
            }
            else
            {
                arabic_Numeral = arabic_Numeral + 10;
                continue;
            }
        }
        else if(roman_Numeral == 'V')
        {
            roman_Numeral = cin.peek();
            if(numerals.find(roman_Numeral, 1) != std::string::npos)
            {
                arabic_Numeral = arabic_Numeral - 5;
                continue;
            }
            else
            {
                arabic_Numeral = arabic_Numeral + 5;
                continue;
            }
        }
        else if(roman_Numeral == 'I')
        {
            roman_Numeral = cin.peek();
            if(numerals.find(roman_Numeral) != std::string::npos)
            {
                arabic_Numeral = arabic_Numeral - 1;
                continue;
            }
            else
            {
                arabic_Numeral = arabic_Numeral + 1;
                continue;
            }
        }
        else
            break;
    }
    cout << arabic_Numeral << endl;
    return 0;
}
void input (double &);
void convert (double radius, int & feet, double & total);
void output (int, double, double);
void function4()
{
    double total, radius;
    int feet;
    char yn;
    do
    {
        input (radius);
        convert (radius, feet, total);
        output (feet, total, radius);
        cout << "Enter a Different radius? (y or n)\n";
        cin >> yn;
        cout << "\n\n";
    }
    while (yn == 'y'  || yn == 'Y');
    system("pause");
    return 0;
}
void input(double & radius)
{
    cout << "---------------------------Re Enter Radius-------------------------\n\n";
    cout << "Enter Radius:\n";
    cin >> radius;
    cout << "\n";
}
void convert(double radius, int & feet, double & total)
{
    //=======================================================
    total = 3.14 * radius * radius * radius * 4 / 3;
}
//========================================================
void output(int feet, double total, double radius)
{
    cout << "---------------------------Re Enter Radius-------------------------\n\n";
    cout << "The volume is: " << total << " \n\n";
    cout << "---------------------------   Equation    -------------------------\n\n";
    cout << "The Equation with (" << radius << ") being:\n\n";
    cout << "4/3 * 3.14 * (" << radius << ")^3 = " << total << "\n\n";
    cout << "---------------------------     Again?    -------------------------\n\n";
}
void swap( int &x, int &y )
    int number;

void function5()

{

    cout << "x is ";
    cin >> number;
    int x = number;

    cout << "y is ";
    cin >> number;
    int y = number;
    swap(x, y);
    cout << "Swapped the first number is now(" << x << ")and the second is now (" << y >> ")";

    return 0;
}
int leap (int year);
void function6 (void){
    int month,day,year,dm,dn,leap;

    printf("enter the month:");
    scanf("%d",&month);

    printf("enter the day:");
    scanf("%d",&day);

    printf("enter the year:");
    scanf("%d",&year);

    if (leap==0)
    {   if(month==1)
        dm=0;
        if(month==2)
            dm=31;
        if(month==3)
            dm=59;
        if(month==4)
            dm=90;
        if(month==5)
            dm=120;
        if(month==6)
            dm=151;
        if(month==7)
            dm=181;
        if(month==8)
            dm=212;
        if(month==9)
            dm=243;
        if(month==10)
            dm=273;
        if(month==11)
            dm=304;
        if(month==12)
            dm=334;}

    else
    {    if(month==1)
        dm=0;
        if(month==2)
            dm=31;

        if(month==3)
            dm=60;
        if(month==4)
            dm=91;
        if(month==5)
            dm=121;
        if(month==6)
            dm=152;
        if(month==7)
            dm=182;
        if(month==8)
            dm=213;
        if(month==9)
            dm=244;
        if(month==10)
            dm=274;
        if(month==11)
            dm=304;
        if(month==12)
            dm=335;}

        dn=dm+day;

        printf("the day number is :%d",dn);

        return 0;
}

int leap (int year){
    if((year%100== 0 && year%400==0)||  (year%4==0))

        return 1;
    else
        return 0;}

int main(int argc, char *argv[], int year, int leap, int number, int arabic_Numeral)

{
    function1();
    function2();
    function3();
    function4();
    function5();
    function6();
    return 0;
}


推荐答案

if (a > b && a > c);
return a;
cout << "This is your highest number(" << a << ")";

这三行总结了您的代码中的许多问题:

These three lines summarize many of the problems in your code:


  • 您的如果条件无用:

if (foo);

只执行 foo 如果 foo 为真,则分号括在要执行的代码 end

only executes the foo test; the semicolon brackets the end of the code to execute if the foo is true:

if (foo)
    /* then code */ ;


  • 您的执行剩下的代码,包括紧跟其后的 cout 行。

    如果你使用文本编辑器,例如 vim ,这些小问题会更直接显而易见,可以为你做语法感知的缩进。编辑器并不总是和编译器一样好,但如果你注意它们如何布局你的代码,他们可以帮助你节省一些时间。

    Some of these little problems would be more immediately obvious if you use a text editor, such as vim, that can do syntax-aware indenting for you. The editors aren't always as good as a compiler, but they can help save you some time if you pay attention to how they layout your code.

    此外,至少后面的代码 - return 应该已经由你的编译器报告给你。如果没有,请提高警告。 ( gcc -Wall -Wextra 很不错。)

    Furthermore, at least the code-after-return should have been reported to you by your compiler. If it didn't, turn up the warnings. (On gcc, -Wall -Wextra is nice.)

    更新

    将每个文件放入一个文件,反过来,您需要稍微重新格式化它们。每个程序目前是自包含的,并且几乎完全从 main()例程中运行。这在玩具程序很好,但是复杂的是你的努力,从一个单一的可执行文件中调用它们。

    To put each of these into a single file and call them each in turn, you'll need to re-format them slightly. Each program is currently self-contained and almost entirely run from within the main() routine. This is fine in toy programs but complicates your efforts to call them all from within a single executable.

    目前,在伪代码中,你有一些文件:

    Currently, in pseudo-code you've got a few files like this:

    prog 1:

    main() {
        /* ask for input */
        /* run the routine */
        /* maybe call functions */
        /* output results */
        /* quit */
    }
    

    prog 2:

    main() {
        /* ask for input */
        /* run the routine */
        /* maybe call functions */
        /* output results */
        /* quit */
    }
    

    您需要按以下方式重新安排您的计划:

    You'll need to re-arrange your program like this:

    计划:

    void run_routine_one(); /* I think C++ forbids a (void) prototype here */
    void run_routine_two();
    
    main() {
        run_routine_one();
        run_routine_two();
        /* quit */
    }
    
    void run_routine_one() {
        /* ask for input */
        /* run the routine */
        /* maybe call functions */
        /* output results */
    }
    
    void run_routine_two() {
        /* ask for input */
        /* run the routine */
        /* maybe call functions */
        /* output results */
    }
    

    您可以在这种模板下几乎复制和粘贴东西;事实上,你可能希望这样做,虽然全局变量会使问题复杂化。 (还有一个原因可以阻止全局存储没有很好的理由。)

    You can nearly copy-and-paste things together given this kind of template; in fact, you may wish to do so, though the global variables will complicate matters. (One more reason to discourage global storage without a good reason.)

    如果在项目到期之前有时间,我强烈建议稍后再进行重构和重构代码显着 - 您的例程当前正在混合输入,计算和输出,并且如果将计算的输入和输出拆分,代码通常更容易测试和维护。如果您提供一些帮助功能,您的输入和输出将更容易。这可能是一个快速草图:

    If you have time before the project is due, I strongly recommend taking a little further step back and refactoring the code significantly -- your routines are currently mixing input, calculation, and output, and code is usually far easier to test and maintain if you split apart the input and output from the calculation. Your input and output would be easier if you provide some helper functions. A quick sketch of what this might look like:

    int prompt_for_int(string)
    {
        int ret;
        cout << string;
        cin >> ret;
        return ret;
    }
    
    int days_in_month(int month, int year)
    {
        int leap = is_leap_year(year);
        /*                   J   F   M   A   M   J   J   A   S   O   N   D */
        int days[2][12] = { {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
                            {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
        if (month < 0 || month > 11 || year < 1753)
            return -1;
    
        return days[leap][month];
    }
    
    void program_one()
    {
        int month, year, days;
    
        month = prompt_for_int("Enter month, 1 for January, 2 for February...\n");
        month -= 1; /* zero-indexed month */
    
        year = prompt_for_int("Enter year, not before 1754...\n");
    
        days = days_in_month(month, year);
        cout << "There are " << days << " in " << month + 1
            << " " << year << nl;
        /* I'm no C++ expert -- perhaps >> nl isn't idiomatic or even functional */
    }
    
    int main(int argc, char *argv[])
    {
        program_one();
        program_two();
        /* maybe given them better names */
        return 0;
    }
    



    我希望你能看到从一个程序拆分成更小的片段和具有每个功能尽可能少。你可能会遇到大型函数,确实有意义的保持大的功能,但试图陷入把函数打破成小块,尽可能的习惯。任务越小,你越有可能发现一些公平的简单重写可以导致更简单的代码。 (检查 days_in_month()例程 - 它使用数组存储天数,并根据结果选择要使用的数组 is_leap_year()函数。我从元素的编程风格 page 54 ) - 尽管它是为FORTRAN和PL / 1编写的,仍然是一本好书, em>很容易用小程序测试,因为它不会做任何输入和输出本身。)

    I hope you can see the legibility gains from breaking a program into smaller pieces and having each function do as little as possible. You'll probably come across large functions that really do make sense to keep as large functions but try to get into the habit of breaking functions into as small pieces as you can. The smaller the tasks, the more likely you are to find that some fair simple re-writing can lead to far simpler code. (Check out that days_in_month() routine -- it uses arrays to store the number of days and picks which array to use based on the result of the is_leap_year() function. I learned this from The Elements of Programming Style (page 54) -- which, despite being written for FORTRAN and PL/1, is still an excellent book. Anyway, that little routine is very easy to test with a small program because it doesn't do any input and output itself.)

    这篇关于多功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆