1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| #include <iostream>
using namespace std;
const int SIZE=200;
int main()
{
char letter;
char **p=NULL,**temp1=NULL;
unsigned int choice,create,free=0,count=0,index,i,j,k;
float temp;
bool found,first=true;
while(1)
{
cout<<"1.Enter a new string"<<endl
<<"2.Delete a string"<<endl
<<"3.Search a string"<<endl
<<"4.Print all word which begin by the letter you'll choice"<<endl
<<"5.Print all words of all strings"<<endl
<<"6.Exit"<<endl
<<"Enter your choice : ";
cin>>choice;
switch (choice)
{
case 1:if (first)
{
p=new char*[5];
free=free+5;
create=5;
first=false;
}
if (free==0)
{
temp1=new char*[create+5];
for (i=0;i<count;++i)
{
*(temp1+i)=new char[SIZE];
strcpy(*(temp1+i),*(p+i));
}
for (i=0;i<count;++i)
delete *(p+i);
delete p;
p=temp1;
temp1=NULL;
free=free+5;
}
*(p+count)=new char[SIZE];
cout<<"Enter the string : "<<endl;
gets(p[count]);
++count;
--free;
break;
case 2:
for (j=0;j<count;++j)
{
cout<<"String index "<<j<<" : "<<endl;
puts(*(p+j));
cout<<endl;
}
cout<<"What is the index of the string that you want to delete : ";
cin>>temp;
while(temp-(int)temp!=0 || temp<0 || temp>=count)
{
cout<<"Wrong value. Enter again : ";
cin>>temp;
}
index=(int)temp;
delete *(p+index);
*(p+index)=NULL;
for (i=index;i<count-1;++i)
*(p+i)=*(p+i+1);
*(p+count-1)=NULL;
--count;
++free;
if (free==7)
{
temp1=new char*[create-5];
for (i=0;i<count;++i)
{
*(temp1+i)=new char[SIZE];
strcpy(*(temp1+i),*(p+i));
}
for (i=0;i<count;++i)
delete *(p+i);
delete p;
p=temp1;
temp1=NULL;
free=free-5;
}
break;
case 3:
cout<<"Enter the string you're searching : ";
char str[SIZE];
gets(str);
for (j=0;j<count;++j)
{
found=true;
if (strlen(str)==strlen(*(p+j)))
{
for (i=0;i<strlen(str);++i)
if (*(*(p+j)+i) != *(str+i))
{
found=false;
break;
}
}
else found=false;
if (found)
break;
}
if (j==count)
cout<<"The string isn't in the board"<<endl;
else
cout<<"The string's index in the board is "<<j<<endl;
break;
case 4:
cout<<"Enter the letter : ";
cin>>letter;
for (j=0;j<count;++j)
for(i=0;i<strlen(*(p+j));++i)
{
if (i==0)
{
if(**(p+j)==letter)
{
for(k=0;*(*(p+j)+k)!=' ' && *(*(p+j)+k)!='\0';++k)
cout<<*(*(p+j)+k);
cout<<endl;
}
}
else
if(*(*(p+j)+i)==letter && *(*(p+j)+i-1)==' ')
{
for(k=i;*(*(p+j)+k)!=' ' && *(*(p+j)+k)!='\0';++k)
cout<<*(*(p+j)+k);
cout<<endl;
}
}
break;
case 5:
for (j=0;j<count;++j)
for(i=0;i<strlen(*(p+j));++i)
{
if (i==0)
{
for(k=0;*(*(p+j)+k)!=' ' && *(*(p+j)+k)!='\0';++k)
cout<<*(*(p+j)+k);
cout<<endl;
}
else
{
for(k=i;*(*(p+j)+k)!=' ' && *(*(p+j)+k)!='\0';++k)
cout<<*(*(p+j)+k);
cout<<endl;
}
}
break;
case 6:
return 0;
}
}
} |
Partager