|
-
C++; what's the best structure to use for words.
I have to input a bunch of words, store all of them, then take the last word inputed and see how many names including that one start and end with the same letters.
I had a problem close to this, but I didn't need to store the words, because I was told to use the first word to compare the rest. So I used a character array: words[40] to do this.
We recently went over strings, structs, and the last five minutes of class started on stacks. Not sure what to use, I understand how to use a traversel to go back into the data that I have stored, just not sure how to store it.
------------------
Schmitty
EG 278
-
Grouchy Tech
I always like arrays of char*
but hey thats me just work with whatever your most comfortable with.
------------------
Where there's a whip <crack> thers a way.
A crack on the back says were gonna fight were gonna march all day and night and more cuz we are the slaves of the dark lord's war.
-
Tiger Shark
I would use either a char*, or use the STL string class. std::string has a bunch of useful functions.
Or, use CString, if you're in Visual C++. It's even better. 
------------------
AMD 1.333 gHz 266 FSB
Lian Li Case
300W PSU
Asus A7M266 AMD 761 Motherboard
512 MB Crucial DDR RAM
Asus V8200 Deluxe Geforce 3 64 MB
2 60 GB Quantum Fireballs
16X Pioneer DVD-ROM
Plextor 12X10X32 CDRW
Soundblaster Live! X-Gamer 5.1
Cambridge Soundworks DTT3500 Digital Speakers
Viewsonic PF790 19" Flat Screen CRT
Microsoft Optical Mouse
Microsoft Natural Keyboard Elite Pro
Logitech Rumblepad USB
[CPU]: Pentium IV 3.0c gHz[Case]: Lian Li PC60 USB [Power]: Thermaltake 430W TruPower[Motherboard]: Abit IS7[RAM]: OCZ PC3500 Dual Channel Kit(2X512)[Video Card]: ATI 9700 Pro All-In-Wonder[Storage]: 160 GB Western Digital w/8 MB Cache[Optical Storage:] 16X Pioneer DVD-ROM, LG 16X8X32[Sound Card]:Soundblaster Audigy 2 [Speakers]: Creative Inspire 5.1 5700 [Monitor]: Viewsonic PF790 19" Flat Screen CRT [Peripherals]: Logitech MX700, Microsoft Natural Keyboard Elite Pro, Logitech Rumblepad USB [OS]:Windows XP Pro
New thinking is needed to solve new problems. - Albert Einstein
-
Tiger Shark
Originally posted by Schmitty:
I have to input a bunch of words, store all of them, then take the last word inputed and see how many names including that one start and end with the same letters.
I had a problem close to this, but I didn't need to store the words, because I was told to use the first word to compare the rest. So I used a character array: words[40] to do this.
We recently went over strings, structs, and the last five minutes of class started on stacks. Not sure what to use, I understand how to use a traversel to go back into the data that I have stored, just not sure how to store it.
Ok, maybe I'm missing something here, but why store the words?
Do you need them to be printed back out?
Are there thousands of words, or a couple of dozen?
If you really don't need to store them, I would simple use a int[26][26] which store the count of words with that starting/ending letter.
If you need them back, I would consider either:
A> Array of [26][26] linked list - Very Very fast
B> Array of [26] of linked list - somewhat fast
C> A simple linked list.
I'm not big into the STL, but a Vector should work (if there isn't a better linked listy thing).
------------------
I want an OS, not a hobby...
I want an OS, not a hobby...
Theres nothing more pathetic then someone who wears non-matching socks on purpose.
-
I need to store them so I can compare the last word put in to the rest of the words that came before it.
------------------
Schmitty
EG 278
-
Tiger Shark
Originally posted by Schmitty:
I need to store them so I can compare the last word put in to the rest of the words that came before it.
Ok, well if you don't really want any order other then a linear order, can't get much simpler then linked list. Any freshman better be able to do that with their eyes shut by winter qtr.
------------------
I want an OS, not a hobby...
I want an OS, not a hobby...
Theres nothing more pathetic then someone who wears non-matching socks on purpose.
-
I got it working below. It's just that I haven't had C++ in just over 2 years and I'm taking the second course now, long story. I've had VB since then and also a couple courses in Director 8, which I used lingo. So I understand the logic, but have trouble with how to code it in C++. Thx for the suggestions.
//Description: Given a list of one word names and nothing else,
//it will output the number of names,
//the last name, and the number of times the first
//and last character of the last name is duplicated in the list of names,
//including the last name.
#include <iostream>
#include <string>
#include <math>
typedef string element;
struct list
{
static const int MAX=100;
element storage[MAX];
int count;
};
//Fills list from input.
void fill_list(list & templist);
//Find the number of times the first and last characters of the last name in
//the list is repeated in the list given, plus the last name.
int freq_of_last(const list & templist);
main()
{
list nameslist;
fill_list(nameslist);
string lastname = nameslist.storage[nameslist.count-1];
char firstchar = lastname[0];
firstchar = toupper(firstchar);
char lastchar =lastname[lastname.length()-1];
lastchar = toupper(lastchar);
cout << nameslist.count << " names" << endl;
cout << "Last name: " << nameslist.storage[nameslist.count-1] << endl;
cout << freq_of_last(nameslist)<< " begin with " << firstchar << " and end with " << lastchar << endl;
}
//Calls function to fill the list
void fill_list(list & templist)
{
templist.count = 1;
cin >> templist.storage[0];
while(!cin.eof())
{
cin >> templist.storage[templist.count];
templist.count++;
}
templist.count--;
}
int freq_of_last(const list & templist)
{
int number = 0;
string lastname = templist.storage[templist.count-1];
}
//Calls function to find repeated occurances of first and last
//charater of the last word inputed.
int freq_of_last(const list & templist)
{
int number = 0;
string lastname = templist.storage[templist.count-1];
char firstchar = lastname[0];
firstchar = toupper(firstchar);
char lastchar = lastname[lastname.length()-1];
lastchar = toupper(lastchar);
string namecompare;
char firstcharcompare;
char lastcharcompare;
for (int x = 0; x < templist.count; x++)
{
namecompare = templist.storage[x];
firstcharcompare = toupper(namecompare[0]);
lastcharcompare = toupper(namecompare[(namecompare.length())-1]);
if ((firstchar == firstcharcompare) && (lastchar == lastcharcompare))
{
number++;
}
}
return number;
}
------------------
Schmitty
EG 278
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|