代码如下:
#include "stdafx.h"
#include<iostream>
#include<cstddef>
using std::endl;
using std::cin;
using std::cout;
void find(int (&a)[5],int n,int &min,int &max);
typedef char * pchar;
typedef pchar * ppchar;
void getstrings(ppchar &refptrstring,unsigned &refnumberofstrings);
void displaystrings(ppchar ptrstring,unsigned numberofstrings);
void releasestrings(ppchar ptrstring,unsigned numberofstrings);
int main()
{
ppchar ptrstring=0;
unsigned numberofstrings=0;
getstrings(ptrstring,numberofstrings);
displaystrings(ptrstring,numberofstrings);
releasestrings(ptrstring,numberofstrings);
}
void getstrings(ppchar &refptrstring,unsigned &refnumberofstrings)
{
cout<<"Enter your string:";
unsigned const buffersize=256;
char buffer[buffersize];
while(!(cin>>buffer).eof())
{
pchar newstring=new char[std::strlen(buffer)+1];
//std::strcpy(newstring,buffer);
strcpy_s(newstring,strlen(newstring),buffer);
ppchar array=new pchar[refnumberofstrings+1];
for(unsigned i=0;i<refnumberofstrings;i++)
array[i]=refptrstring[i];
array[refnumberofstrings++]=newstring;
delete[]refptrstring;
refptrstring=array;
cout<<"Next string:";
}
cout<<endl<<endl;
}
void displaystrings(ppchar ptrstring,unsigned numberofstrings)
{
if(numberofstrings==0)
cout<<"Nothing to display"<<endl;
else
{
cout<<"The strings:"<<endl;
char const quote='\"';
for(unsigned i=0;i<numberofstrings;++i)
cout<<quote<<ptrstring[i]<<quote<<endl;
}
}
void relaasestrings(ppchar ptrstring,unsigned numberofstrings)
{
for(unsigned i=0;i<numberofstrings;++i)
delete[]ptrstring[i];
delete[]ptrstring;
}