Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <regex>
- void printLyrics() {
- for (int i = 99; i > 0; --i) {
- std::cout << i << (i == 1 ? " bottle" : " bottles") << " of beer\n";
- std::cout << "you take one down, pass it around,\n";
- std::cout << ((i - 1) == 0 ? "no more" : std::to_string(i - 1)) << " bottles of beer on the wall.\n\n";
- }
- }
- void processLetter(char c, const std::string& source) {
- switch (c) {
- case 'h':
- std::cout << "Hello World.\n";
- break;
- case 'q':
- std::cout << source << "\n";
- break;
- case '9':
- printLyrics();
- break;
- case '+':
- #ifdef _MSC_VER
- _asm {
- inc eax
- }
- #else
- __asm__("inc %eax")
- #endif
- break;
- }
- }
- void interpret(const std::string& source) {
- std::string src = source;
- std::regex spaces("\\s+");
- std::regex_replace(src, spaces, "");
- for (auto it = src.begin(); it != src.end(); ++it) {
- char c = *it;
- processLetter(c, source);
- }
- }
- void runFile(const std::string& filename) {
- std::ifstream in(filename);
- std::string line;
- std::string source;
- while (std::getline(in, line)) {
- source += line;
- }
- interpret(source);
- }
- void runConsole() {
- while (true) {
- std::string input;
- std::cout << "> ";
- std::cin >> input;
- if (input.empty()) {
- continue;
- }
- interpret(input);
- }
- }
- int main(int argc, char** argv) {
- if (argc == 1) {
- runConsole();
- } else if (argc == 2) {
- runFile(argv[1]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment