C++ Programming by Example: Key computer programming concepts for beginners. Kindle edition
By Sergey Skudaev
http://www.amazon.com/dp/B00EUSMTUW
C++ Programming by Example: Key computer programming concepts for beginners. Paperback
http://www.amazon.com/dp/1983756512
// poet.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include#include #include #include #include // Program writes rhymed poem: 4 lines from words chosen randomly 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]); void main() { char noun1[7][20]; char adj1[7][20]; char verb1[3][20]; Initialize(noun1, verb1, adj1); WritePoem(noun1, verb1, adj1); int hold = 1; std::cin >> hold; } void Initialize(char noun1[][20], char verb1[][20], char adj1[][20]) { 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"}; char *verb[] = { "flew on", "jumped off", "ran over","rolled over","jumped on","crushed","made happy","left with","scared off","wished for","ate","caught","sought for", "twisted", "gulped down", "tricked", "punched", "stuck", "smiled to", "laughed at", "knocked off","broke", "swallowed" }; char *adj[] = { "great","red", "blue","yellow","gray","funny","smart","silly","brave","dangerous","little","jealous", "strong","pale", "furious", "green", "big", "cool", "greedy", "vicious" }; int i, k, l, j, n; srand(time(0)); i = rand() % 41; strcpy_s(noun1[0], noun[i]); i = rand() % 40; strcpy_s(noun1[1], noun[i]); i = rand() % 39; strcpy_s(noun1[3], noun[i]); i = rand() % 38; strcpy_s(noun1[4], noun[i]); for (k = 0; k<42; k++) { if (IsRhymes(noun1[1], noun[k])) strcpy_s(noun1[2], noun[k]); } 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]); } } for (k = 0; k<42; k++) { if (IsRhymes(noun1[4], noun[k])) strcpy_s(noun1[5], noun[k]); } if (strcmp(noun1[4], noun1[5]) == 0) { for (n = 41; n >0; n--) { if (IsRhymes(noun1[4], noun[n])) strcpy_s(noun1[5], noun[n]); } } i = rand() % 19; strcpy_s(verb1[0], verb[i]); i = rand() % 10; strcpy_s(verb1[1], verb[i]); i = rand() % 19; strcpy_s(adj1[0], adj[i]); i = rand() % 18; strcpy_s(adj1[1], adj[i]); i = rand() % 17; strcpy_s(adj1[2], adj[i]); i = rand() % 16; strcpy_s(adj1[3], adj[i]); i = rand() % 15; strcpy_s(adj1[4], adj[i]); i = rand() % 14; strcpy_s(adj1[5], adj[i]); for (j = 0; j <42; j++) { if (IsRhymes(noun1[2], noun[j])) strcpy_s(noun1[3], noun[j]); } if (strcmp(noun1[2], noun1[3]) == 0) { //std::cout << "The same word" << std::endl; for (l = 41; l>0; l--) { if (IsRhymes(noun1[2], noun[l])) strcpy_s(noun1[3], noun[l]); } } } char *ReturnThreeFromTheRight(char*string) { char *ptr; int length; ptr = string; length = strlen(string); ptr = ptr + (length - 3); return ptr; } int IsRhymes(char *word1, char *word2) { int i; char *ptr1, *ptr2; ptr1 = word1; ptr2 = word2; ptr1 = ReturnThreeFromTheRight(word1); ptr2 = ReturnThreeFromTheRight(word2); if (strcmp(ptr1, ptr2) == 0) i = 1; else i = 0; return i; } 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] << " " << noun1[4] << std::endl; std::cout << verb1[1] << " a " << adj1[5] << " " << noun1[5] << std::endl; std::cout << '\n'; std::cout << '\n'; }