Guest User

Untitled

a guest
Feb 21st, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.28 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  project 3
  4. //
  5. //  Created by Cunpu Bo on 11-10-6.
  6. //  Copyright 2011年 __MyCompanyName__. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <string>
  11. #include <cctype>
  12. using namespace std;
  13.  
  14. string getFirstWord(string text);
  15. string getLastWord(string text);
  16. string extractWord(string& text);
  17. bool isUppercase(string text);
  18. string makeUppercase(string text);
  19. bool hasMultipleExclamations(string text);
  20. bool isGibberishWord(string text);
  21.  
  22. bool isConsonant(char letter);      //returns true if the argument is a consonant, return it it is a non-letter.
  23. bool keyWord(string word);          //returns true if the argument is a keyword regardless of case
  24.  
  25. void checkTheTitle(string title,int& point);        //will change the point according to spam rule.
  26. void checkThebody(string body,int& point);          //will change the point according to spam rule.
  27.  
  28. int main ()
  29. {
  30.     int numOfSpam = 0,numOfLegi = 0;
  31.     string ans;
  32.    
  33.     do
  34.     {
  35.         string title,body,newLine;
  36.         int point = 0;              //the point is reset every time.
  37.        
  38.        
  39.         cout << "Enter the subject line of the email: ";
  40.         getline(cin,title);
  41.         checkTheTitle(title,point);        
  42.        
  43.         cout << "Enter the body of the email.  Press Enter on an empty line to finish." << endl;
  44.         while(true)
  45.         {
  46.             getline(cin,newLine);
  47.             if (newLine == "")          //if the input is empty, the exit the input.
  48.                 break;
  49.            
  50.             body += '\n';
  51.             body += newLine;
  52.         }
  53.         checkThebody(body,point);
  54.        
  55.        
  56.         if (point > 100)
  57.         {
  58.             cout << "This email is classified as spam, because its spam score is " << point << "." <<endl;
  59.             numOfSpam++;
  60.         }
  61.         else
  62.         {
  63.             cout << "This email is classified as legitimate, because its spam score is " << point << "." <<endl;
  64.             numOfLegi++;
  65.         }
  66.        
  67.         cout << "Would you like to classify another email (y or n)?";
  68.         getline(cin,ans);
  69.        
  70.         while (ans != "y" && ans != "n" )
  71.         {
  72.             cout << "Please enter y or n." << endl;
  73.             cout << "Would you like to classify another email (y or n)?";
  74.             getline(cin,ans);
  75.            
  76.         }
  77.     }while (ans=="y");
  78.    
  79.     cout <<endl;
  80.     cout << "Number of spam messages: " << numOfSpam << endl;
  81.     cout << "Number of legitimate messages: " << numOfLegi << endl;
  82. }
  83.  
  84.  
  85. //search for the first letter, and then search for the fisrt non-letter; the length between them will be the length of the substring starting from the first returned.
  86. string getFirstWord(string text)                
  87. {
  88.     int first = 0,last = 0;
  89.    
  90.     for (; first < text.size(); first++)          
  91.     {
  92.         if (isalpha(text[first]))
  93.         {
  94.             last = first;
  95.             for (; last < text.size(); last++)
  96.                 if (!isalpha(text[last]))
  97.                     break;
  98.         }
  99.        
  100.         if (last != 0)                      //break if the last if changed
  101.             break;
  102.     }    
  103.     return text.substr(first,last - first);    
  104. }
  105. //the process if similar to getFirstWord, but the search starts from the end.
  106. string getLastWord(string text)            
  107. {
  108.     int size = static_cast<int>(text.size());
  109.     int first = size - 1,last = size - 1;
  110.    
  111.    
  112.     for (; last < text.size(); last--)
  113.     {
  114.         if (isalpha(text[last]))
  115.         {
  116.             first = last;
  117.             for (; first >= 0; first--)
  118.                 if (!isalpha(text[first]))
  119.                     break;
  120.         }
  121.        
  122.         if (first != size - 1)          //break if the first is changed
  123.             break;
  124.     }
  125.    
  126.    
  127.     return text.substr(first + 1,last - first);
  128. }
  129.  
  130. string extractWord(string& text)
  131. {
  132.     string result = getFirstWord(text);
  133.    
  134.     int index = static_cast<int>(text.find(result));// find the postion the first word
  135.    
  136.     if (result == "")                       //if there is no word in the string, then the string itself also is changed to empty string.
  137.         text = "";
  138.     else
  139.         text = text.substr(index + result.size());   //change the text to a string starting from the next character of the first word.
  140.    
  141.     return result;
  142. }
  143.  
  144. bool isUppercase(string text)
  145. {
  146.     if (text == makeUppercase(text))
  147.         return true;
  148.     else
  149.         return false;
  150. }
  151.  
  152. string makeUppercase(string text)
  153. {
  154.     string temp;
  155.     for (int i = 0; i < text.size(); i++)
  156.     {
  157.         temp += toupper(text[i]);  
  158.     }
  159.     return temp;
  160. }
  161.  
  162. //search for the first exclamation, and the search for the first non-exclamation;if the length between them is no less than 3, then return true.
  163. bool hasMultipleExclamations(string text)
  164. {
  165.     int first = 0, last = 0;
  166.    
  167.     for (; first < text.size(); first++)
  168.     {
  169.         if (text[first] == '!')
  170.         {
  171.             last = first;                              //set the last equal to fisrt.
  172.             for (; last < text.size(); last++)
  173.                 if (text[last] != '!')
  174.                 {
  175.                     first = last;                   //restart search from the last.
  176.                     break;
  177.                 }
  178.         }
  179.        
  180.        
  181.         if (last - first >= 3)          //if the length between is over 3,then break.
  182.             break;
  183.     }    
  184.    
  185.     if (last - first >= 3)
  186.         return true;
  187.     else
  188.         return false;
  189. }
  190. //search for the first consonant, and the search for the first non-econsonant;if the length between them is no less than 3, then return true.
  191. bool isGibberishWord(string text)
  192. {
  193.     int first = 0, last = 0;
  194.    
  195.     for (; first < text.size(); first++)
  196.     {
  197.         if (isConsonant(text[first]))
  198.         {
  199.             last = first;                           //set the last equal to fisrt.
  200.             for (; last < text.size(); last++)
  201.                 if (!isConsonant(text[last]))
  202.                 {
  203.                     first = last;                      //restart search from the last.
  204.                     break;
  205.                 }
  206.         }
  207.        
  208.         if (last - first >= 3)                          //if the length between is over 3,then break.
  209.             break;
  210.     }    
  211.    
  212.     if (last - first >= 3)
  213.         return true;
  214.     else
  215.         return false;    
  216. }
  217.  
  218.  
  219. bool isConsonant(char letter)
  220. {
  221.     if (!isalpha(letter))               //if not a letter, return false.
  222.         return false;
  223.    
  224.     char upperLetter = toupper(letter);
  225.    
  226.     if (upperLetter == 'A' || upperLetter == 'E' || upperLetter == 'I' || upperLetter == 'O' || upperLetter == 'U')
  227.         return false;
  228.     else
  229.         return true;
  230. }
  231.  
  232. bool keyWord(string word)
  233. {
  234.     word = makeUppercase(word);
  235.     if (word == "BUY" || word == "CHEAP" || word == "CLICK" || word == "DIPLOMA" || word == "FREE" || word == "LIMITED" || word == "ENLARGE" ||
  236.         word == "MONEY" || word == "NOW" || word == "OFFER" || word == "ONLY" || word == "PILLS" || word == "SEX")
  237.         return true;
  238.     else
  239.         return false;
  240. }
  241.  
  242. void checkTheTitle(string line,int& point)
  243. {
  244.     int numOfUpper = 0,totalNum = 0;                //count the number of upper words and the total words.
  245.    
  246.     if (hasMultipleExclamations(line))
  247.     {
  248.         point += 20;
  249.     }
  250.        
  251.        
  252.     string lastWord;
  253.     lastWord = getLastWord(line);
  254.     if (isGibberishWord(lastWord))
  255.     {
  256.         point += 40;
  257.     }
  258.    
  259.     while (true)
  260.     {  
  261.         string newWord;
  262.         newWord = extractWord(line);
  263.                
  264.         if (newWord == "")      
  265.             break;
  266.         if (isUppercase(newWord))
  267.             numOfUpper++;
  268.         totalNum++;
  269.     }
  270.     if ((numOfUpper*1.0/totalNum) > 0.9)
  271.     {
  272.         point+=30;
  273.     }
  274.    
  275. }
  276.  
  277. void checkThebody(string line,int& point)
  278. {
  279.     int numOfUpper = 0,totalNum = 0;
  280.     while (true)
  281.     {
  282.         string newWord;
  283.         newWord = extractWord(line);
  284.        
  285.         if (newWord == "")        
  286.             break;
  287.         if (keyWord(newWord))
  288.             point += 5;
  289.         if (isUppercase(newWord))
  290.             numOfUpper++;
  291.         totalNum++;
  292.        
  293.     }
  294.     if ((numOfUpper*1.0/totalNum) > 0.5)
  295.     {
  296.         point+=40;
  297.     }
  298. }
Add Comment
Please, Sign In to add comment