我正在制作 C 游戲專案,在游戲中我需要選擇隨機獎金(函式)。
(以下是代碼示例)
void triple_balls(){
...
}
void longer_paddle(){
...
}
void shorter_paddle(){
...
}
void bonus_activator(){
//Here I must choose one of the 3 functions above
//FIXME
}
uj5u.com熱心網友回復:
改用函式指標陣列
using BonusFunc = void (*)();
BonusFunc[3] = { triple_balls, longer_paddle, shorter_paddle };
void bonus_activator(){
BonusFunc[rand() % 3](); // just an example, don't use rand() in real code
}
uj5u.com熱心網友回復:
您可以使用std::function, 將函式存盤在容器中。std::function然后創建一個大小為 的陣列3。
#include <functional>
#include <iostream>
void triple_balls() { /* YOUR CODE */ }
void longer_paddle() { /* YOUR CODE */ }
void shorter_paddle() { /* YOUR CODE */ }
void bonus_activator(){
std::function<void(void)> farr[3] =
{
triple_balls,
longer_paddle,
shorter_paddle
};
farr[rand() % 3]();
}
uj5u.com熱心網友回復:
Lorem Ipsum 只是印刷和排版行業的虛擬文本。自 1500 年代以來,Lorem Ipsum 一直是行業的標準虛擬文本,當時一位不知名的印刷商采用了一種型別的廚房并將其加擾以制作型別樣本書。它不僅經歷了五個世紀,而且經歷了電子排版的飛躍,基本保持不變。它在 1960 年代隨著包含 Lorem Ipsum 段落的 Letraset 表的發布而流行起來,最近還隨著 Aldus PageMaker 等桌面出版軟體(包括 Lorem Ipsum 的版本)而普及。
#include <iostream>
using namespace std;
int main()
{
int n1, n2, max;
cout << "Enter two numbers: ";
cin >> n1 >> n2;
// maximum value between n1 and n2 is stored in max
max = (n1 > n2) ? n1 : n2;
do
{
if (max % n1 == 0 && max % n2 == 0)
{
cout << "LCM = " << max;
break;
}
else
max;
} while (true);
return 0;
}
嘿嘿,我是jack暖氣
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/463683.html
標籤:C
上一篇:如何使每個腳本與每個啟蒙分開?
下一篇:通過參考傳遞給接受通用參考的函式
