多种功能

Multiple Functions

本文关键字:功能      更新时间:2023-10-16

>@ sarnold 好的,这是我的更新,试图遵循您给我的指南,我尝试删除将原型移动到类型的主电源,并且每个只有一个,我将其缩小到一个包含每个单独函数声明的 int main,并为每个函数使用 void 而不是 int,希望这是正确的举动。

#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?nn";
    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 << "nn";
    }
    while (yn == 'y'  || yn == 'Y');
    system("pause");
    return 0;
}
void input(double & radius)
{
    cout << "---------------------------Re Enter Radius-------------------------nn";
    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-------------------------nn";
    cout << "The volume is: " << total << " nn";
    cout << "---------------------------   Equation    -------------------------nn";
    cout << "The Equation with (" << radius << ") being:nn";
    cout << "4/3 * 3.14 * (" << radius << ")^3 = " << total << "nn";
    cout << "---------------------------     Again?    -------------------------nn";
}
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;
}
好吧,

对于一个你已经定义int main()三次。 您需要给每个人一个不同的名字。

此外,您不需要复制#includeusing语句,只需在开始时使用它们一次即可。

我建议将原型也移到课程的开头,只是为了可读性。

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

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

  • 你的if条件是没有用的:

    if (foo);
    

    仅执行foo测试;如果foo为 true,则分号将要执行的代码末尾括起来:

    if (foo)
        /* then code */ ;
    
  • return a语句会阻止执行其余代码,包括紧随其后的cout行。

如果您使用文本编辑器(例如 vim),其中一些小问题会更直接地显现出来,它可以为您执行语法感知缩进。编辑器并不总是像编译器一样好,但如果您注意它们如何布局您的代码,它们可以帮助您节省一些时间。

此外,至少编译器应该向您报告return后的代码。如果没有,请打开警告。(gcc-Wall -Wextra很好。


更新

要将每个文件放入单个文件中并依次调用它们,您需要稍微重新格式化它们。每个程序目前都是独立的,几乎完全从main()例程中运行。这在玩具程序中很好,但会使您在单个可执行文件中调用它们的努力变得复杂。

目前,在伪代码中,您有一些这样的文件:

方案 1:

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

方案 2:

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

您需要像这样重新排列程序:

程序:

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 */
}

给定这种模板,您几乎可以将内容复制并粘贴在一起;事实上,您可能希望这样做,尽管全局变量会使事情复杂化。(在没有充分理由的情况下阻止全局存储的另一个原因。

如果你

在项目到期前有时间,我强烈建议你退后一步,对代码进行重大重构——你的例程目前混合了输入、计算和输出,如果你从计算中分离出输入和输出,代码通常更容易测试和维护。如果提供一些帮助程序函数,则输入和输出会更容易。快速草图可能如下所示:

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()函数的结果选择要使用的数组。我从《The Elements of Programming Style》(第54页)中学到了这一点——尽管它是为FORTRAN和PL/1编写的,但它仍然是一本优秀的书。无论如何,这个小例程很容易用小程序进行测试,因为它本身不做任何输入和输出。