對於3個開關語句,我將用兩個或多個函數調用替換每個switch語句。這是爲了在上個學期開始的C++類中取代「不完整的」,這樣我就可以獲得貸款,並且我不知道從哪裏開始。將開關語句更改爲兩個或更多函數,每個函數
我試着把開關語句放到自己的函數中,只是把開關語句本身放在它們自己的函數中,當然,這也產生了大量的語法錯誤(例如counter,random_number)。我不知道該怎麼做,如何將適當的值返回到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) (我加counter
,upper_bound
,lower_bound
,n_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);
}
你應該讀好C++的書,瞭解數組,向量,和一般的集合。做任何你想做的事情都有更好的方法。 – Mat 2012-02-26 19:18:33
@Mat - 我確定有更好的方法來完成這個任務,但是這個任務專門用來「用兩個或多個函數調用替換每個switch語句」。這就是我以這種方式提出問題的原因。 – Danny 2012-02-26 19:21:35
如果您要爲我們發佈一個任務,您至少可以發佈需求的全文,而不僅僅是重複的「用兩個或多個函數調用替換每個switch語句」的短語。 – 2012-02-26 19:31:37