我的问题是我无法从函数内部更改数组结构的大小,但我可以从int main()
int main()
{
srand(time(NULL));
InfoFriendBook *IFB = new InfoFriendBook [TableSize];
for ( int i=0; i < TableSize; i++ )
{
IFB[i] = NEWBOOK(); /// Gives a name for fast debuging
IFB[i].Last_Talked = Rand ( TableSize , 0 ); /// Gives a nr 0-10 for fast debuging
}
cout << " "; /// have no need right now
while ( ChooseMenu !=0 )
{
HeadMenu ( IFB , &TableSize );
//IFB = ResizeArray ( IFB , &TableSize);
//IFB = DeleteArrayMem( IFB , &TableSize );
}
delete []IFB;
}
我的主要看起来像我有ResizeArray = Bigger和DeleteArrayMem = Smaller。
如果我在函数内部使用它们,我的程序会崩溃,如果我尝试查看列表或尝试再次调整它。
我的计划
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
struct InfoFriendBook
{
string Name;
int Last_Talked;
bool Active;
};
InfoFriendBook NEWBOOK ()
{
InfoFriendBook NEW;
NEW.Name = "NULL";
NEW.Active = true;
return NEW;
}
int *P_IFB = NULL;
int TableSize= 10, ChooseMenu=1 ;
void HeadMenu ( InfoFriendBook *IFB , int *size );
void EditMenu ( InfoFriendBook *IFB , int *size );
int Rand ( int high , int low );
void sort ( InfoFriendBook *IFB , int *size );
int Find_Smallest_Nr ( InfoFriendBook *IFB , int *size , int *Index );
void swap ( InfoFriendBook *IFBF , int Index , int Index2 , int *size );
InfoFriendBook *ResizeArray ( InfoFriendBook *IFB , int *size );
InfoFriendBook *DeleteArrayMem ( InfoFriendBook *IFB , int *size );
int main()
{
srand(time(NULL));
InfoFriendBook *IFB = new InfoFriendBook [TableSize];
for ( int i=0; i < TableSize; i++ )
{
IFB[i] = NEWBOOK(); /// Gives a name for fast debuging
IFB[i].Last_Talked = Rand ( TableSize , 0 ); /// Gives a nr 0-10 for fast debuging
}
cout << " "; /// have no need right now
while ( ChooseMenu !=0 )
{
HeadMenu ( IFB , &TableSize );
//IFB = ResizeArray ( IFB , &TableSize);
//IFB = DeleteArrayMem( IFB , &TableSize );
}
delete []IFB;
}
void HeadMenu( InfoFriendBook *IFB , int *size )
{
int Ranklist=1;
cout << "\nFriends list\nSelect a menu\n1. For Print list.\n2. Sort list after how long it have been since last talk";
cout << "\n3. Edit List\n";
cin >> ChooseMenu;
cin.ignore();
switch ( ChooseMenu )
{
case 1: /// Print
for ( int i=0; i < *size; i++ ) // Debug Print
{
if ( IFB[i].Active == true )
{cout << endl << Ranklist << ". " << IFB[i].Name << "|" << IFB[i].Last_Talked;}
else {break;}
Ranklist++;
} cout << endl << "Table Size: " << *size << "\nBookSize: " << endl;
break;
case 2: /// Sort
sort ( IFB , size ); // sorting
break;
case 3: /// Edit List
EditMenu( IFB , size );
break;
}
}
void EditMenu ( InfoFriendBook *IFB , int *size )
{
int Ranklist=1;
int Select, Nr, nr;
cout << "\nEdit Menu\n1. Edit People\n2. Add People\n3. Delete People\n";
cin >> ChooseMenu;
cin.ignore();
switch (ChooseMenu)
{
case 1: ///Edit Profile
for ( int i=0; i < *size; i++ )
{
if ( IFB[i].Active == true )
{cout << endl << Ranklist << ". " << IFB[i].Name << "|" << IFB[i].Last_Talked;}
Ranklist++;
}
cout << "\nChoose a person to edit: ";
cin >> Select;
cin.ignore();
cout << "\nEdit Name 1. or Last time talk 2. ";
cin >> ChooseMenu;
cin.ignore();
switch (ChooseMenu)
{
case 1:
getline(cin, IFB[Select-1].Name);
break;
case 2:
cin >> Nr;
cin.ignore();
while ( nr != 0 )
{
nr = Nr;
nr = nr - Nr;
if( nr != 0)
{
cout << "You have try to place a negative nr. try again";
cin >> Nr;
cin.ignore();
}
}
IFB[Select-1].Last_Talked = Nr;
break;
}
break;
case 2: /// Add new profile
Nr=0;
IFB = ResizeArray ( IFB , size); // Grow struct array
while ( IFB[Nr].Active == true )
{
Nr++;
}
IFB[Nr] = NEWBOOK();
cout << "\nAdd new friend to list\nEnter name: ";
getline(cin,IFB[Nr].Name);
cout << "\nEnter Last time talk: ";
cin >> IFB[Nr].Last_Talked;
cin.ignore();
cout << "\nSorting started";
sort( IFB , size );
break;
case 3: /// Delete Profile
cout << "\nDelete one from list\n";
Ranklist=1;
for ( int i=0; i < *size; i++ )
{
if ( IFB[i].Active == true )
{cout << endl << Ranklist << ". " << IFB[i].Name << "|" << IFB[i].Last_Talked;}
Ranklist++;
}
cout << endl;
cin >> nr;
cin.ignore();
swap( IFB , (nr-1) , (*size-1) , size);
cout << "\nSorting started";
sort( IFB , size );
IFB = DeleteArrayMem( IFB , size ); // Delete array Element []
break;
}
}
int Rand( int high , int low ) // Gives Random nr. out to how long it have been sins last time
{
return rand() % ( high - low+ 1 ) + low;
}
void sort ( InfoFriendBook *IFB , int *size ) // find and place nr in order
{
for ( int x=0; x < *size; x++ )
{
int Index = Find_Smallest_Nr( IFB , size , &x );
swap ( IFB , x , Index , size );
}
}
int Find_Smallest_Nr ( InfoFriendBook *IFB , int *size , int *Index ) // find the smallest nr and returns it
{
int SmallIndex = *Index;
for ( int i= *Index+1 ; i < *size; i++ )
{
if ( IFB[i].Last_Talked < IFB[SmallIndex].Last_Talked )
{
SmallIndex = i;
}
}
return SmallIndex;
}
void swap ( InfoFriendBook *IFB , int Index , int Index2, int *size ) // place the smallest nr frist
{
InfoFriendBook *Swap_IFB = new InfoFriendBook [*size]; // new swap take and swap element in a struct
Swap_IFB[Index] = IFB[Index];
IFB[Index] = IFB[Index2];
IFB[Index2] = Swap_IFB[Index];
delete [] Swap_IFB;
}
InfoFriendBook *ResizeArray ( InfoFriendBook *IFB , int *size ) // Resize struct array
{
InfoFriendBook *NEW_IFB = new InfoFriendBook [*size+1];
for ( int x=0; x < *size; x++ )
{
NEW_IFB[x] = IFB[x];
}
delete []IFB;
*size += 1;
return NEW_IFB;
}
InfoFriendBook *DeleteArrayMem ( InfoFriendBook *IFB , int *size )
{
InfoFriendBook *NEW_IFB = new InfoFriendBook [*size+1];
NEW_IFB = new InfoFriendBook [*size-1];
for ( int x=0; x< *size-1; x++ )
{
NEW_IFB[x] = IFB[x];
}
delete []IFB;
*size -= 1;
return NEW_IFB;
}