将switch语句更改为两个或多个函数

时间:2012-02-26 19:16:09

标签: c++ function switch-statement

对于3个switch语句,我将用两个或多个函数调用替换每个switch语句。这是为了取代我上一学期开始的C ++课程中的“不完整”,以便我可以获得贷款而且我不知道从哪里开始。

我尝试将switch语句和它们放入自己的函数中,仅仅是switch语句本身,当然,这会产生大量的语法错误(例如counter,random_number)。我不知道该怎么做,如何将正确的值返回main()并让程序与程序的其他部分进行通信(例如,在main中定义/初始化的变量)。可以看出,我只是迷失在这里,并希望得到一些指导来解决这个问题。我不是要求任何人为我做这个,只是一些指导(我对C ++的了解有限,并且有时间限制)。

// random.cpp : Defines entry point for the console application.
//

#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

//random number generator prototypes
void randomize(void);
void randomize(int seed);
int random(void);
int random(int upper_bound);
int random(int upper_bound, int lower_bound);

int main()
{
int upper_bound = 999;
int lower_bound = 100;
int n_random_numbers = 1000;

randomize();

int counter_0 = 0;
int counter_1 = 0;
int counter_2 = 0;
int counter_3 = 0;
int counter_4 = 0;
int counter_5 = 0;
int counter_6 = 0;
int counter_7 = 0;
int counter_8 = 0;
int counter_9 = 0;

for(int counter = 1; counter <= n_random_numbers; counter++)
{
    int random_number = random(upper_bound, lower_bound);
    int digit_1 = random_number % 10; random_number = random_number / 10;
    switch(digit_1)
    {
        case 0:
            counter_0++;
            break;
        case 1:
            counter_1++;
            break;
        case 2:
            counter_2++;
            break;
        case 3:
            counter_3++;
            break;
        case 4:
            counter_4++;
            break;
        case 5:
            counter_5++;
            break;
        case 6:
            counter_6++;
            break;
        case 7:
            counter_7++;
            break;
        case 8:
            counter_8++;
            break;
        case 9:
            counter_9++;
            break;
    }
    int digit_2 = random_number % 10; random_number = random_number / 10;
    switch(digit_2)
    {
        case 0:
            counter_0++;
            break;
        case 1:
            counter_1++;
            break;
        case 2:
            counter_2++;
            break;
        case 3:
            counter_3++;
            break;
        case 4:
            counter_4++;
            break;
        case 5:
            counter_5++;
            break;
        case 6:
            counter_6++;
            break;
        case 7:
            counter_7++;
            break;
        case 8:
            counter_8++;
            break;
        case 9:
            counter_9++;
            break;
    }

    int digit_3 = random_number % 10; random_number = random_number / 10;
    switch(digit_3)
    {
        case 0:
            counter_0++;
            break;
        case 1:
            counter_1++;
            break;
        case 2:
            counter_2++;
            break;
        case 3:
            counter_3++;
            break;
        case 4:
            counter_4++;
            break;
        case 5:
            counter_5++;
            break;
        case 6:
            counter_6++;
            break;
        case 7:
            counter_7++;
            break;
        case 8:
            counter_8++;
            break;
        case 9:
            counter_9++;
            break;
    }

}
cout << "0 occurs " << counter_0 << " times" << endl;
cout << "1 occurs " << counter_1 << " times" << endl;
cout << "2 occurs " << counter_2 << " times" << endl;
cout << "3 occurs " << counter_3 << " times" << endl;
cout << "4 occurs " << counter_4 << " times" << endl;
cout << "5 occurs " << counter_5 << " times" << endl;
cout << "6 occurs " << counter_6 << " times" << endl;
cout << "7 occurs " << counter_7 << " times" << endl;
cout << "8 occurs " << counter_8 << " times" << endl;
cout << "9 occurs " << counter_9 << " times" << endl;

system("pause");

return 0;
}

//random number generators
void randomize(void)
{
srand(unsigned(time(NULL)));
}

void randomize(int seed)
{
srand(unsigned(seed));
}

int random(void)
{
return rand();
}

int random(int upper_bound)
{
return rand() % (upper_bound + 1);
}

int random(int upper_bound, int lower_bound)
{
    if(upper_bound < lower_bound)
        {
    int t = upper_bound;
    upper_bound = lower_bound;
    lower_bound = t;
        }
int range = upper_bound - lower_bound + 1;
int number = rand() % range + lower_bound;
return number;
}

我添加了这些功能,看看它是否可行(图1.1) (我添加了counterupper_boundlower_boundn_random_numbers因为我无法弄清楚如何让函数从main()读取这些变量。我试过使它们成为一个函数,在main中调用它们,并在我创建的函数中调用它们,但这肯定不起作用。这些添加的函数用函数调用替换原始中的开关(参见:图1.2)。它编译,但是输出返回“0发生0次,1发生0次等”

图1.1

int switch1 (int switch_1)
{
int upper_bound = 999;
int lower_bound = 100;
int n_random_numbers = 1000;
int counter = 0;

randomize();

int counter_0 = 0;
int counter_1 = 0;
int counter_2 = 0;
int counter_3 = 0;
int counter_4 = 0;
int counter_5 = 0;
int counter_6 = 0;
int counter_7 = 0;
int counter_8 = 0;
int counter_9 = 0;

int random_number = random(upper_bound, lower_bound);

int digit_1 = random_number % 10; random_number = random_number / 10;
    switch(digit_1)
    {
        case 0:
            counter_0++;
            break;
        case 1:
            counter_1++;
            break;
        case 2:
            counter_2++;
            break;
        case 3:
            counter_3++;
            break;
        case 4:
            counter_4++;
            break;
        case 5:
            counter_5++;
            break;
        case 6:
            counter_6++;
            break;
        case 7:
            counter_7++;
            break;
        case 8:
            counter_8++;
            break;
        case 9:
            counter_9++;
            break;
    }

}

int switch2 (int switch_2)
{
int upper_bound = 999;
int lower_bound = 100;
int n_random_numbers = 1000;
int counter = 0;

randomize();

int counter_0 = 0;
int counter_1 = 0;
int counter_2 = 0;
int counter_3 = 0;
int counter_4 = 0;
int counter_5 = 0;
int counter_6 = 0;
int counter_7 = 0;
int counter_8 = 0;
int counter_9 = 0;

int random_number = random(upper_bound, lower_bound);

int digit_2 = random_number % 10; random_number = random_number / 10;
    switch(digit_2)
    {
        case 0:
            counter_0++;
            break;
        case 1:
            counter_1++;
            break;
        case 2:
            counter_2++;
            break;
        case 3:
            counter_3++;
            break;
        case 4:
            counter_4++;
            break;
        case 5:
            counter_5++;
            break;
        case 6:
            counter_6++;
            break;
        case 7:
            counter_7++;
            break;
        case 8:
            counter_8++;
            break;
        case 9:
            counter_9++;
            break;
    }

}

int switch3 (int switch_3)
{
int upper_bound = 999;
int lower_bound = 100;
int n_random_numbers = 1000;
int counter = 0;

randomize();

int counter_0 = 0;
int counter_1 = 0;
int counter_2 = 0;
int counter_3 = 0;
int counter_4 = 0;
int counter_5 = 0;
int counter_6 = 0;
int counter_7 = 0;
int counter_8 = 0;
int counter_9 = 0;

int random_number = random(upper_bound, lower_bound);

int digit_3 = random_number % 10; random_number = random_number / 10;
    switch(digit_3)
    {
        case 0:
            counter_0++;
            break;
        case 1:
            counter_1++;
            break;
        case 2:
            counter_2++;
            break;
        case 3:
            counter_3++;
            break;
        case 4:
            counter_4++;
            break;
        case 5:
            counter_5++;
            break;
        case 6:
            counter_6++;
            break;
        case 7:
            counter_7++;
            break;
        case 8:
            counter_8++;
            break;
        case 9:
            counter_9++;
            break;
    }

}

图1.2

for(int counter = 1; counter <= n_random_numbers; counter++)
{
    int random_number = random(upper_bound, lower_bound);
    int digit_1 = random_number % 10; random_number = random_number / 10;
    int digit_2 = random_number % 10; random_number = random_number / 10;
    int digit_3 = random_number % 10; random_number = random_number / 10;

switch1 (digit_1);
switch2 (digit_2);
switch3 (digit_3);

}

1 个答案:

答案 0 :(得分:1)

无需开关:

int counters[10] = {};

for(int counter = 1; counter <= n_random_numbers; counter++)
{
    int random_number = random(upper_bound, lower_bound);
    int digit_1 = random_number % 10; random_number = random_number / 10;
    ++counters[digit_1];

    int digit_2 = random_number % 10; random_number = random_number / 10;
    ++counters[digit_2];

    int digit_3 = random_number % 10; random_number = random_number / 10;
    ++counters[digit_3];
}