using namespace std;
struct Package_Node
{
int bar_code;
float package_weight;
struct Package_Node *next_packaged;
};
struct Key_Node
{
int key;
struct Package_Node *next_package;
};
Key_Node *keyMain=0;
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://entering keys and max values
{
cout << "\nEnter Number of Keys: ";
cin >>X;
cout <<"\nEnter Max Value of the Barcode: ";
cin >>max_value;
break;
}
case 2://enter number of packages.
{
cout << "\nEnter Number of Packages: ";
cin >> N;
break;
}
case 3://create array of keys.
{
//Key_Node *keyMain = new Key_Node[X];
keyMain= new Key_Node[X];
for(int i=0;i<X;i++)
{
keyMain[i].key=i;
cout << keyMain[i].key<<endl;
}
break;
}
case 4:
{
for(int i=0;i<N;i++)
{
barcodeTemp = rand() % max_value + 1;
keyTemp = barcodeTemp % X;
tempWeight = rand() % 500 + 1;
keyMain[keyTemp] = new Package_Node;
Package_Node->bar_code=barcodeTemp;
Package_Node->package_weight=tempWeight;
Package_Node->next_packaged=NULL;
}
for(int i=0;i<N;i++)
{
cout << totalPackages[i].bar_code <<endl;
cout << totalPackages[i].package_weight << endl;
}
cout << keyTemp;
break;
}
case 5:
{
break;
}
case 6:
{
break;
}
default:
{
break;
}
}
}while(optionChosen!=7);
}
我在这里有点困惑。keyMain
是一个静态数组。keyMain[1]
等keyMain[2]
将指向使用Package_Node
. 将添加的索引由keyMain
确定。但这似乎相去甚远。好像我写的代码不起作用。我在这里做错了什么?有任何想法吗?请不要使用向量,因为这是一个课堂项目,我们根本不会谈论向量。new package_node
keyTemp