fork download
  1.  
  2. /*
  3.  * (Spam Scanner) Spam (or junk email) costs U.S. organizations billions of dollars a year in spam-prevention software, equipment, network resources, bandwidth, and lost productivity.
  4.  * Research online some of the most common spam e-mail messages and words, and check your own junk e-mail folder. Create a list of 30 words and phrases commonly found in spam messages. Write a C++ application in which the user reads an e-mail message from a file. Then, scan the message for each of the 30 keywords or phrases. For each occurrence of one of these within the message, add a point to the message's "spam score." Next, rate the likelihood that the message is spam, based on the number of points it received. The likelihood is the number of points divided by the total number of words.
  5.  */
  6.  
  7. #include <iostream>
  8. #include <fstream>
  9. #include <string>
  10. #include <stdexcept>
  11.  
  12. using namespace std;
  13.  
  14. char* spanWordList[] = {
  15. "free",
  16. "congratul",
  17. "won",
  18. "diet",
  19. "opport",
  20. "smoking",
  21. "viagra",
  22. "weight",
  23. "degree",
  24. "dvd",
  25. "gambling",
  26. "horoscope",
  27. "ink",
  28. "obligation",
  29. "cash",
  30. "cheap",
  31. "credit",
  32. "deal",
  33. "loan",
  34. "income",
  35. "debt",
  36. "promo",
  37. "rate",
  38. "shop",
  39. "stock",
  40. "wealth",
  41. "trading",
  42. "merchant",
  43. "mortgage",
  44. "insurance",
  45. 0};
  46.  
  47. int main(int argc, char** argv)
  48. {
  49.  
  50. if (argc != 2)
  51. {
  52. cout << "Usage: main email" << endl;
  53. return 1;
  54. }
  55.  
  56. fstream email(argv[1], fstream::in);
  57. if (!email.is_open())
  58. {
  59. throw runtime_error("Failed to open <" + string(argv[1]) + ">, please check the file exist and has read permission.");
  60. }
  61.  
  62. string word;
  63. double wordCnt = 0;
  64. double spanCnt = 0;
  65.  
  66. while (email >> word)
  67. {
  68. for (char** cur = spanWordList; *cur !=0; cur++)
  69. {
  70. if (word == *cur)
  71. {
  72. spanCnt++;
  73. }
  74. }
  75.  
  76. wordCnt++;
  77. }
  78.  
  79. cout << "The span rate of <" << argv[1] << "> is " << (spanCnt/(wordCnt?wordCnt:1)) << endl;
  80.  
  81. return 0;
  82. }
  83.  
Runtime error #stdin #stdout 0s 3416KB
stdin
3
stdout
Usage: main email