Part 1 - LIST
Create an unsorted LIST class ( You should already have this code from a prior assignment ). Each list should be able to store 100 names.
Part 2 - Create a Class ArrayListClass
It will contain an array of 27 "list" classes.
Next, create a Class in which is composed a array of 27 list classes.
Ignore index 0... Indexes 1...26 correspond to the first letter of a Last name.
Again - ignore index 0. index 1 is names starting with A, index 2 is B, index 3 is C... index 26 is Z.
Create a file of 260 random ordered last names... 10 each for each letter… FileName: Names.txt
Have an constructor function that Reads in the Names.txt file, one name at a time, store/add each Name in the correct index LIST element.
Write a function that can Search for a Name to see if it in the LIST class.
Write a function that will print out the names starting with a specific letter, prints just ONE list.
Write a Function that can print out the ALL names found on each list..
HUGE HINT.. create a LIST class first, each that can hold 10 names.. Then create a class the can hold an array of 27 list classes.
C++ CODE:
#include<bits/stdc++.h>
using namespace std;
string Last(string p){
int i,l=p.size();
string k;
for( i = 0 ; i < l ; i++ ){
if(p[i] == ' ')
k="";
else
k+=p[i];
}
return k;
}
class LIST{
list<string> List;
public :
void push(string name){
(this->List).push_back(name);
}
int Size(){
return (this->List).size();
}
void print(){
list <string> :: iterator it;
for(it = (this->List).begin(); it != (this->List).end();
++it)
cout <<*it<<" ";
}
bool check(string name){
list <string> :: iterator it;
for(it = (this->List).begin(); it != (this->List).end();
++it)
if(name == *it)return true;
return false;
}
};
class Array_ListClass{
LIST arr[27];
public :
Array_ListClass(){
ifstream fin;
fin.open("Output.txt");
if(!fin){
printf("Error in file opening\n");
}else{
printf("File is ready to read\n");
while(fin){
string name;
getline(fin,name);
if(name.size() > 0){
//cout<<name<<endl;
string s=Last(name);
int n = (int)s[0];
(this->arr[n-'A'+1]).push(name);
}
}
fin.close();
}
}
void print_All_Names(){
for( int i =1 ; i < 27 ; i++ ){
printf("Names at index %d : ",i);
(this->arr[i]).print();
cout<<endl;
}
}
void print(int n){
printf("Names with %c : ",n+'A'-1);
(this->arr[n]).print();
cout<<endl;
}
bool check(string name,string s){
int n = (int)s[0];
return (this->arr[n-'A'+1]).check(name);
}
};
int main(){
ofstream fout;
fout.open("Output.txt");
string s="";
int i,j,m;
if(!fout){
printf("Error in creating file\n");
}else{
for( i = 0 ; i < 260 ; i++ ){
s += 'A' + (i/10);
for( m =1 ; m < 10 ; m++ ){
j = rand()%26;
s += 'A'+j;
}
fout<<s<<"\n";
s="";
}
}
printf("File is created\n");
fout.close();
Array_ListClass obj;
printf("Enter name to search\n");
getline(cin,s);
if(obj.check(s,Last(s))){
printf("Present\n");
}
else{
printf("Not Present\n");
}
printf("Enter character to print name with that
character\n");
getline(cin,s);
s=Last(s);
obj.print(s[0]-'A'+1);
obj.print_All_Names();
return 0;
}
OUTPUT SCREENSHOT:
I hope you like the code and explanation, don't forget to give UPVOTE as it means a lot...THANKS:)
Get Answers For Free
Most questions answered within 1 hours.