Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cctype>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. void printH()
  10. {
  11. cout << "Hello, world!" << endl;
  12. }
  13.  
  14. void printQ(vector<string> buffer)
  15. {
  16. for (auto v : buffer) cout << v << endl;
  17. }
  18.  
  19. void print9()
  20. {
  21. cout << "99 bottles of beer on the wall, 99 bottles of beer." << endl;
  22. for (int i = 98; i > 1; --i)
  23. {
  24. cout << "Take one down and pass it around, " << i << " bottles of beer on the wall." << endl << endl;
  25. cout << i << "bottles of beer on the wall, " << i << "bottles of beer." << endl;
  26. }
  27. cout << "Take one down and pass it around, 1 bottle of beer on the wall." << endl << endl;
  28. cout << "1 bottle of beer on the wall, 1 bottle of beer." << endl;
  29. cout << "Take one down and pass it around, no more bottles of beer on the wall." << endl << endl;
  30. cout << "No more bottles of beer on the wall, no more bottles of beer." << endl;
  31. cout << "Go to the store and buy some more, 99 bottles of beer on the wall." << endl;
  32. }
  33.  
  34. void printF()
  35. {
  36. for (int i = 1; i <= 100; ++i)
  37. {
  38. if (i % 3 == 0) cout << "Fizz";
  39. if (i % 5 == 0) cout << "Buzz";
  40. if (i % 3 != 0 && i % 5 != 0) cout << i;
  41. cout << endl;
  42. }
  43. }
  44.  
  45. void printHelp()
  46. {
  47. cout << "This is an interpreter of HQ9F+." << endl;
  48. cout << "HQ9F+ is a joke programming language. It has four operations." << endl;
  49. cout << " -- H: print \"Hello, world!\"" << endl;
  50. cout << " -- Q: print the program's source code (sometimes called a quine)" << endl;
  51. cout << " -- 9: print the lyrics to 99 Bottles of Beer" << endl;
  52. cout << " -- F: print the fizzbuzz to 100 from 1" << endl;
  53. cout << " -- +: add one to the accumulator (the value of the accumulator cannot be accessed)" << endl;
  54.  
  55. }
  56.  
  57. void printVersion()
  58. {
  59. cout << "Version: 1.0" << endl;
  60. }
  61.  
  62. int main(int argc, char **argv)
  63. {
  64. if (argc < 2)
  65. {
  66. printHelp();
  67. printVersion();
  68. }
  69. else
  70. {
  71. ifstream ifs(argv[1]);
  72. if (ifs.fail()) // fail : file not found
  73. {
  74. cerr << "Cannot open the file: \"" << argv[1] << "\"" << endl;
  75. return 1;
  76. }
  77.  
  78. int accumulator = 0;
  79. vector<string> buffer;
  80. string buf;
  81. while (getline(ifs, buf, '\n')) buffer.push_back(buf);
  82.  
  83. for (int i = 0; i < buffer.size(); ++i)
  84. {
  85. int length = buffer[i].length();
  86. for (int j = 0; j < length; ++j)
  87. {
  88. switch (toupper(buffer[i][j]))
  89. {
  90. case 'H':
  91. printH();
  92. break;
  93. case 'Q':
  94. printQ(buffer);
  95. break;
  96. case '9':
  97. print9();
  98. break;
  99. case 'F':
  100. printF();
  101. case '+':
  102. ++accumulator;
  103. break;
  104. default:
  105. cerr << "undefined token : " << "\'" << buffer[i] << "\' (line: " << i + 1 << ", " << j + 1 << ")" << endl;
  106. return 1;
  107. }
  108. }
  109. }
  110. ifs.close();
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement