Learn C++ Programming By Examples by Skudaev Sergey
Author:Skudaev, Sergey
Language: eng
Format: epub
Tags: C++ programming, code examples
Publisher: ComeToPro Consulting
Published: 2014-01-17T00:00:00+00:00
Figure 52. Rhymes game.
AN APPLICATION THAT WRITES POEMS
Let´s practice more with string manipulation and create a program that writes poems.
As the application that writes stories the poet application will take words from arrays of adjectives, nouns and verbs and compose sentences. The difference is that the last words of the sentences must rhyme. An example of C++ poem:
Figure 53. A C++ poem
I created 4 functions:
void Initialize(char noun1[][20],char verb1[][20],char adj1[][20]);
char *ReturnThreeFromTheRight(char*string);
int IsRhymes(char *word1, char *word2);
void WritePoem(char noun1[][20], char verb1[][20], char adj1[][20]);
The Initialize function creates arrays of words from which a poem is constructed.
For each noun you include in the noun array you have to include a rhyme.
You can see it from my array: switch â witch, goat-toad, deal-meal, wolf-golf and so on.
char * noun[] = { "switch", "grain", "goat", "witch", "deal"," food", "stool", "toad", "fate", "feet", "knish", "meal", "meat", "mood", "wolf", "house", "rain", "sleet", "train", "golf", "skate", "tool", "wood", "wish", "peat", "mouse", "pitch", "chain", "boat", "squeal", "drool", "pool", "plate", "beet", "catfish", "seat","rolf","grouse","coat","woad","road", "food"};
Then you are creating the array of verbs and adjectives from any words. I included verbs in the past tense.
char *verb[] = { â¦.};
char *adj[] = { â¦.};
The srand () function gives the random function a new seed.
You should include <ctime> header file.
srand( time(0) );
Then I generate a random number from 0 to 41 because I have 42 nouns..
i = rand()% 41;
I generate an index for the noun1[0], noun1[1], noun1[3], noun1[4] and copy nouns with that index from the noun array to the noun1[0], noun1[1], noun1[3], noun1[4].
strcpy_s(noun1[0], noun[i]);
In the next step I walked through the array of nouns and looked for one that is the rhyme for noun1[1].
When I found a rhyme, I copied it to the noun1[2]. The IsRhymes function is similar to one from the previous example.
for (k = 0; k<27; k++)
{
if (IsRhymes(noun1[1], noun[k]))
strcpy_s(noun1[2], noun[k]);
}
There is the possibility that I will find for noun1[2] the same word as noun1[1] in the loop.
So, I check if the words are the same. If they are, then I will walk through the noun array again but this time from the end of the array to the beginning and look for a rhyme. When I find it, I will copy it to the noun1[2]. There is still the possibility that noun1[1] and noun1[2] are the same word, but it is lower than before.
if (strcmp(noun1[1], noun1[2]) == 0)
{
for (n = 41; n >0; n--)
{
if (IsRhymes(noun1[1], noun[n]))
strcpy_s(noun1[2], noun[n]);
}
}
You can try to find a better solution to eliminate such possibility. In the same way you generate noun1 [5] that is a rhyme for noun1 [4].
In the last function WritePoem(), there are the final 4 lines of code that construct a poem.
void WritePoem(char noun1[][20], char verb1[][20], char adj1[][20])
{
std::cout << "A " << adj1[0] << " " << noun1[0] << " like a " << adj1[1] << " " << noun1[1] << std::endl;
std::cout << verb1[0] << " a " << adj1[2] << " " << noun1[2] << std::endl;
std::cout << " A " << adj1[3] << " " << noun1[3] << " like a " << adj1[4]
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Ajax | Assembly Language Programming |
Borland Delphi | C & C++ |
C# | CSS |
Compiler Design | Compilers |
DHTML | Debugging |
Delphi | Fortran |
Java | Lisp |
Perl | Prolog |
Python | RPG |
Ruby | Swift |
Visual Basic | XHTML |
XML | XSL |
Deep Learning with Python by François Chollet(12564)
Hello! Python by Anthony Briggs(9911)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9794)
The Mikado Method by Ola Ellnestam Daniel Brolund(9775)
Dependency Injection in .NET by Mark Seemann(9335)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8293)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7758)
Grails in Action by Glen Smith Peter Ledbrook(7693)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7023)
Microservices with Go by Alexander Shuiskov(6790)
Practical Design Patterns for Java Developers by Miroslav Wengner(6702)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6644)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Angular Projects - Third Edition by Aristeidis Bampakos(6050)
The Art of Crafting User Stories by The Art of Crafting User Stories(5583)
NetSuite for Consultants - Second Edition by Peter Ries(5515)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5318)
Kotlin in Action by Dmitry Jemerov(5061)
