c++ - 多重功能

标签 c++ function

@sarnold 好的,这是我的更新尝试遵循你给我的指导希望每个函数都使用 voids 而不是 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?\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;
}

最佳答案

好吧,你已经定义了三次 int main()。你需要给每一个不同的名字。

此外,您不需要重复 #includeusing 语句,只需在开头使用一次即可。

我建议也将您的原型(prototype)移至类(class)开头,以提高可读性。

关于c++ - 多重功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10273921/

相关文章:

c++ - 检测子进程

android - CMake共享项目子目录避免重建

c++ - 我可以声明一个模板只接受具有同质签名的函数吗?

c++ - 如何避免在 C++ 中进行一些繁重的处理时阻塞线程?

c++ - 一台 Linux 机器上的段错误,但另一台使用 C++ 代码的机器上没有

python - 从 bash 将多个参数传递给 python 函数(在变量中)

c++ - 使用 boost 线程和非静态类函数

Mysql LEFT 匹配前 3 个字符

mysql - SQL - 将多个值除以多个值

javascript - 具有 getter 值的函数