Advertisement
Cinestra

HW6

May 6th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. /*
  2.  
  3. Nick Ku PIC10A Intro. to Programming
  4.  
  5. ID: 304491540 Spring 2016
  6.  
  7. Email: cinestra@ucla.edu HW #3
  8.  
  9. Section: 3A
  10.  
  11. Honesty Pledge:
  12.  
  13.  
  14.  
  15. I, Nichokas Ku, pledge that this is my own independent work, which conforms to
  16.  
  17. the guidelines of academic honesty as described in the course syllabus.
  18.  
  19. List of none bugs: None.
  20.  
  21. */
  22.  
  23.  
  24.  
  25. #include<iostream>
  26. #include<iomanip>
  27. #include <string>
  28.  
  29. using namespace std;
  30.  
  31. bool is_vowel(string letter)
  32. // Taken from lecture slides
  33. {
  34. if (letter.length() > 1)
  35. return false;
  36. if (letter == "a" || letter == "e" || letter == "i" || letter == "o" || letter == "u" || letter == "y")
  37. return true;
  38. else if (letter == "A" || letter == "E" || letter == "I" || letter == "O" || letter == "U" || letter == "Y")
  39. return true;
  40. else
  41. return false;
  42. }
  43.  
  44. bool is_punctuation(string letter)
  45. // Exactly the same as is_vowel but with different characters and less cases
  46. {
  47. if (letter.length() > 1)
  48. return false;
  49. if (letter == "." || letter == "!" || letter == ";" || letter == ":")
  50. return true;
  51. else
  52. return false;
  53. }
  54.  
  55. int number_of_sentences(string phrase)
  56. // Returns the number of sentences from the whole phrase by counting the number of punctuations
  57. {
  58. int count = 0;
  59.  
  60. for (int i = 0; i<phrase.length(); i++)
  61. {
  62. if (is_punctuation(phrase.substr(i, 1)))
  63. count++;
  64. }
  65.  
  66. return count;
  67. }
  68.  
  69. int number_of_words(string phrase)
  70. // Returns the number of words from the whole phrase by counting the number of spaces and adding one
  71. {
  72. int spaces = 0;
  73. for (int i=0; i < phrase.length(); i++)
  74. {
  75.  
  76. if ((phrase.substr(i, 1) == " ") && (phrase.substr(i+1, 1) != " "))
  77. {
  78. // Increments the number of spaces whenever a space is found
  79. // Will not increment if the following space is also blank
  80. spaces++;
  81. }
  82. }
  83.  
  84. return spaces+1;
  85. }
  86.  
  87. string next_word(string phrase)
  88. // Returns the next word in the string phrase by taking the substring from 0 to the next open space or taking the substring from 0 to the end length
  89. // Runs into a problem when there are two spaces next to each other since it will count one space character by itself as a letter
  90. // Resolve this problem later by setting the number of syllables for that one space character equal to zero
  91. {
  92. string phrase_word;
  93. int i = 0;
  94.  
  95.  
  96.  
  97. while (((phrase.substr(i, 1) != " ") || !(is_punctuation(phrase.substr(i,1)))) && (i<phrase.length()))
  98. {
  99. i++;
  100. }
  101. phrase_word = phrase.substr(0, i);
  102. // Using i-1 instead of simply i because i locates the space whereas i-1 locates the letter before the space
  103.  
  104. return phrase_word;
  105. }
  106.  
  107.  
  108. int number_of_syllables(string word)
  109.  
  110. {
  111. int n_syllables = 0;
  112. string prev_char;
  113.  
  114. for (int i = 0; i < word.length(); ++i)
  115. {
  116. string cur_char = word.substr(i, 1);
  117.  
  118. if (i == 0) prev_char = "b";
  119. else prev_char = word.substr(i - 1, 1);
  120.  
  121. if ((cur_char == "e") && (i == word.length()))
  122. n_syllables = n_syllables + 0;
  123. // Do nothing
  124. else if ((is_vowel(cur_char)) && !(is_vowel(prev_char)))
  125. n_syllables++;
  126. }
  127.  
  128. return n_syllables;
  129. }
  130.  
  131. int readability_index(int nsyllables, int nwords, int nsentences)
  132. {
  133. double x = nsyllables;
  134. double y = nwords;
  135. double z = nsentences;
  136.  
  137. double index = 206.835 - 84.6*(x / y) - 1.015 * (y / z);
  138. return index;
  139. }
  140.  
  141. int main()
  142. {
  143. string phrase;
  144. string first_word;
  145. string phrase_copy;
  146. cout << "Please enter a phrase: " << "\n";
  147. getline(cin, phrase);
  148.  
  149. phrase_copy = phrase;
  150.  
  151. long count = 0;
  152.  
  153. while (phrase_copy.length() > 0)
  154. {
  155. first_word = next_word(phrase_copy);
  156. long a = number_of_syllables(first_word);
  157. long b = first_word.length();
  158.  
  159. phrase_copy = phrase_copy.substr(b, phrase_copy.length());
  160. count = count + a;
  161. }
  162.  
  163. int nSyllables = count;
  164. // Recasts count into an integer
  165.  
  166. int nWords = number_of_words(phrase);
  167. int nSentences = number_of_sentences(phrase);
  168. int index = readability_index(nSyllables, nWords, nSentences);
  169.  
  170. cout << "number of syllables is: " << nSyllables << "\n";
  171. cout << "number of words is: " << nWords << "\n";
  172. cout << "number of sentences is: " << nSentences << "\n";
  173. cout << "readibility index is : " << index << "\n";
  174. system("pause");
  175.  
  176. return 0;
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement