考虑:
struct Package_Node
{
int bar_code;
float package_weight;
struct Package_Node *next_packaged;
};
struct Key_Node
{
int key;
struct Package_Node *next_package;
};
int weightTemp = 0, barcodeTemp = 0, keyTemp = 0, N = 0, X = 0, max_value = 0, optionChosen = 0, optionChosenTwo = 0;
float tempWeight;
int main()
{
srand(time(NULL));
do
{
cout << "1 - Enter Number of keys and the Max Value\n2 - Enter Number of Packages (N)\n";
cout << "3 - Create Array of Keys\n4 - Organize Packages\n5 - Table Stats\n6 - Clean Table\n7 - Exit\n\n";
cin >> optionChosen;
switch(optionChosen)
{
case 1:
{
cout << "\nEnter Number of Keys:";
cin >>X;
cout <<"\nEnter Max Value of the Barcode: ";
cin >>max_value;
break;
}
case 2:
{
cout << "\nEnter Number of Packages: ";
cin >> N;
//keyTemp=
break;
}
case 3:
{
Key_Node keyMain[X];
for(int i=0;i<X;i++)
{
keyMain[i].key=i;
//cout << keyMain[i].key; //to see key values.
}
break;
}
case 4:
{
Package_Node totalPackages[N];
for(int i=0;i<N;i++)
{
barcodeTemp = rand() % max_value + 1;
keyTemp = barcodeTemp % X;
tempWeight = rand() % 500 + 1;
totalPackages[i].bar_code=barcodeTemp;
totalPackages[i].package_weight=tempWeight;
}
for(int i=0;i<N;i++)
{
cout << totalPackages[i].bar_code <<endl;
cout << totalPackages[i].package_weight << endl;
}
这是我的代码。我正在尝试将总包 [i] (动态数组)的信息链接到 keyMain (Package_Node 数据类型和静态数组)。但我完全迷失在这里如何做到这一点。
另外,我对动态数组“totalPackages”的声明是否正确?还是我不应该声明它的大小,因为它会根据用户输入的包裹总数而增长?totalPackages
应该是一个随着程序需要而增长的链表。