Created
June 27, 2016 16:00
-
-
Save NotFounds/345d98311600e72f7c07ff73ef2cca77 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <cctype> | |
#include <vector> | |
using namespace std; | |
void printH() | |
{ | |
cout << "Hello, world!" << endl; | |
} | |
void printQ(vector<string> buffer) | |
{ | |
for (auto v : buffer) cout << v << endl; | |
} | |
void print9() | |
{ | |
cout << "99 bottles of beer on the wall, 99 bottles of beer." << endl; | |
for (int i = 98; i > 1; --i) | |
{ | |
cout << "Take one down and pass it around, " << i << " bottles of beer on the wall." << endl << endl; | |
cout << i << "bottles of beer on the wall, " << i << "bottles of beer." << endl; | |
} | |
cout << "Take one down and pass it around, 1 bottle of beer on the wall." << endl << endl; | |
cout << "1 bottle of beer on the wall, 1 bottle of beer." << endl; | |
cout << "Take one down and pass it around, no more bottles of beer on the wall." << endl << endl; | |
cout << "No more bottles of beer on the wall, no more bottles of beer." << endl; | |
cout << "Go to the store and buy some more, 99 bottles of beer on the wall." << endl; | |
} | |
void printF() | |
{ | |
for (int i = 1; i <= 100; ++i) | |
{ | |
if (i % 3 == 0) cout << "Fizz"; | |
if (i % 5 == 0) cout << "Buzz"; | |
if (i % 3 != 0 && i % 5 != 0) cout << i; | |
cout << endl; | |
} | |
} | |
void printHelp() | |
{ | |
cout << "This is an interpreter of HQ9F+." << endl; | |
cout << "HQ9F+ is a joke programming language. It has four operations." << endl; | |
cout << " -- H: print \"Hello, world!\"" << endl; | |
cout << " -- Q: print the program's source code (sometimes called a quine)" << endl; | |
cout << " -- 9: print the lyrics to 99 Bottles of Beer" << endl; | |
cout << " -- F: print the fizzbuzz to 100 from 1" << endl; | |
cout << " -- +: add one to the accumulator (the value of the accumulator cannot be accessed)" << endl; | |
} | |
void printVersion() | |
{ | |
cout << "Version: 1.0" << endl; | |
} | |
int main(int argc, char **argv) | |
{ | |
if (argc < 2) | |
{ | |
printHelp(); | |
printVersion(); | |
} | |
else | |
{ | |
ifstream ifs(argv[1]); | |
if (ifs.fail()) // fail : file not found | |
{ | |
cerr << "Cannot open the file: \"" << argv[1] << "\"" << endl; | |
return 1; | |
} | |
int accumulator = 0; | |
vector<string> buffer; | |
string buf; | |
while (getline(ifs, buf, '\n')) buffer.push_back(buf); | |
for (int i = 0; i < buffer.size(); ++i) | |
{ | |
int length = buffer[i].length(); | |
for (int j = 0; j < length; ++j) | |
{ | |
switch (toupper(buffer[i][j])) | |
{ | |
case 'H': | |
printH(); | |
break; | |
case 'Q': | |
printQ(buffer); | |
break; | |
case '9': | |
print9(); | |
break; | |
case 'F': | |
printF(); | |
case '+': | |
++accumulator; | |
break; | |
default: | |
cerr << "undefined token : " << "\'" << buffer[i] << "\' (line: " << i + 1 << ", " << j + 1 << ")" << endl; | |
return 1; | |
} | |
} | |
} | |
ifs.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment