diff options
Removed old homeworks
4 files changed, 0 insertions, 254 deletions
| -rw-r--r-- | homework/81625/Brainfuck-Interpretator/src/BFI.cpp | 175 | ||||
| -rw-r--r-- | homework/81625/Brainfuck-Interpretator/src/BFI.h | 39 | ||||
| -rw-r--r-- | homework/81625/Brainfuck-Interpretator/src/main.cpp | 37 | ||||
| -rw-r--r-- | homework/81625/Brainfuck-Interpretator/src/run.sh | 3 |
diff --git a/homework/81625/Brainfuck-Interpretator/src/BFI.cpp b/homework/81625/Brainfuck-Interpretator/src/BFI.cpp deleted file mode 100644 index 23d8c3e..0000000 --- a/homework/81625/Brainfuck-Interpretator/src/BFI.cpp +++ /dev/null @@ -1,175 +0,0 @@ -#include "BFI.h" - -void BFI::moveMPLeft() -{ - --mMP; -} - -void BFI::moveMPRight() -{ - ++mMP; - if(mMP > mMemoryUsed) mMemoryUsed = mMP; -} - -void BFI::incCellAtMP() -{ - if(mMP >= 0 && mMP < MEMORY_SIZE) - { - ++mMemory[mMP]; - } - else - { - std::cout << "Out of memory bounds!!! MP: " << mMP << " IP: " << mIP << " " << mCode[mIP] << std::endl; - exit(1); - } -} - -void BFI::decCellAtMP() -{ - if(mMP >= 0 && mMP < MEMORY_SIZE) - { - --mMemory[mMP]; - } - else - { - std::cout << "Out of memory bounds!!! MP: " << mMP << " IP: " << mIP << " " << mCode[mIP] << std::endl; - exit(1); - } -} - -void BFI::printCellAtMP() -{ - if(mMP >= 0 && mMP < MEMORY_SIZE) - { - if(mMemory[mMP] == 10) - { - std::cout<<"\n"; - } - else - { - std::cout<<mMemory[mMP]; - } - } - else - { - std::cout << "Out of memory bounds!!! MP: " << mMP << " IP: " << mIP << " " << mCode[mIP] << std::endl; - exit(1); - } -} - -void BFI::readToCellAtMP() -{ - unsigned char c; - std::cin>>c; - if(mMP >= 0 && mMP < MEMORY_SIZE) - { - mMemory[mMP] = c; - } - else - { - std::cout << "Out of memory bounds!!! MP: " << mMP << " IP: " << mIP << " " << mCode[mIP] << std::endl; - exit(1); - } -} -void BFI::loopStart() -{ - if(mMP >= 0 && mMP < MEMORY_SIZE) - { - if(mMemory[mMP] == 0) - { - mIP = mBrackets[mIP]; - } - } - else - { - std::cout << "Out of memory bounds!!! MP: " << mMP << " IP: " << mIP << " " << mCode[mIP] << std::endl; - exit(1); - } -} - -void BFI::loopEnd() -{ - if(mMP >= 0 && mMP < MEMORY_SIZE) - { - if(mMemory[mMP] != 0) - { - mIP = mBrackets[mIP]; - } - } - else - { - std::cout << "Out of memory bounds!!! MP: " << mMP << " IP: " << mIP << " " << mCode[mIP] << std::endl; - exit(1); - } -} - -void BFI::interpret() -{ - while(mCode[mIP] != '\0') - { - if(mCode[mIP] == '[') - { - mStack.push(mIP); - } - else if(mCode[mIP] == ']') - { - if(mStack.empty()) - { - std::cout << "Unexpected closing bracket!!!\n"; - exit(1); - } - mBrackets[mIP] = mStack.top(); - mBrackets[mStack.top()] = mIP; - mStack.pop(); - } - ++mIP; - } - if(!mStack.empty()) - { - std::cout << "Unclosed opening bracket!!!\n"; - exit(1); - } - mIP = 0; - while(mCode[mIP] != '\0') - { - switch(mCode[mIP]) - { - case '>': moveMPRight(); break; - case '<': moveMPLeft(); break; - case '+': incCellAtMP(); break; - case '-': decCellAtMP(); break; - case '.': printCellAtMP(); break; - case ',': readToCellAtMP(); break; - case '[': loopStart(); break; - case ']': loopEnd(); break; - } - ++mIP; - } -} - -void BFI::printUsedMemory() -{ - unsigned cols = 16; - unsigned rows = mMemoryUsed/cols; - unsigned lastRow = mMemoryUsed%cols; - unsigned ind = 0; - std::cout.fill('0'); - for(unsigned i = 0; i < rows; ++i) - { - std::cout << std::setw(4) << ind << ": "; - for(unsigned j = 0; j < cols; j++) - { - std::cout << std::setw(3) << (unsigned)mMemory[ind++] << " "; - } - std::cout << std::endl; - } - if(lastRow != 0) - { - std::cout << std::setw(4) << ind << ": "; - while(ind <= mMemoryUsed) - { - std::cout << std::setw(3) << (unsigned)mMemory[ind++] << " "; - } - std::cout << std::endl; - } -} diff --git a/homework/81625/Brainfuck-Interpretator/src/BFI.h b/homework/81625/Brainfuck-Interpretator/src/BFI.h deleted file mode 100644 index a058567..0000000 --- a/homework/81625/Brainfuck-Interpretator/src/BFI.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef BFI_H -#define BFI_H - -#include<iostream> -#include<stack> -#include<vector> -#include<iomanip> - -class BFI -{ -private: - static const unsigned MEMORY_SIZE = 2048; - std::string mCode; - unsigned mIP, mMemoryUsed; - unsigned char mMemory[MEMORY_SIZE]; - int mMP; - std::stack<unsigned> mStack; - std::vector<unsigned> mBrackets; - - void moveMPLeft(); - void moveMPRight(); - void incCellAtMP(); - void decCellAtMP(); - void printCellAtMP(); - void readToCellAtMP(); - void loopStart(); - void loopEnd(); - -public: - BFI(const std::string& code) : - mCode(code), mIP(0), mMP(0), mMemoryUsed(0),mMemory{0} - { - mBrackets.reserve(mCode.length()); - } - void interpret(); - void printUsedMemory(); -}; - -#endif // BFI_H diff --git a/homework/81625/Brainfuck-Interpretator/src/main.cpp b/homework/81625/Brainfuck-Interpretator/src/main.cpp deleted file mode 100644 index 7003a78..0000000 --- a/homework/81625/Brainfuck-Interpretator/src/main.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "BFI.h" - -int main() -{ - std::string helloWorld = "++++++++++[>+>+++>+++++++>++++++++++<<<<-]>>>++.>+.+++++++..+++.<<++.>+++++++++++++++.>.+++.------.--------.<<+.<."; - std::string squares = "++++[>+++++<-]>[<+++++>-]+<+[>[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+>>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<]<<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]"; - std::string beer = ">++++++++++[<++++++++++>-]<->>>>>+++[>+++>+++<<-]<<<<+<[>[>+>+<<-]>>[-<<+>>]++++" - ">+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[[-]>>>>>>[[-]<++++++++++<->>]<-[>+>+<<-]>[<+>" - "-]+>[[-]<->]<<<<<<<<<->>]<[>+>+<<-]>>[-<<+>>]+>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<" - "<[>>+>+<<<-]>>>[-<<<+>>>]++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[>+<[-]]<[>>+<<[-]]" - ">>[<<+>>[-]]<<<[>>+>+<<<-]>>>[-<<<+>>>]++++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[>+" - "<[-]]<[>>+<<[-]]>>[<<+>>[-]]<<[[-]>>>++++++++[>>++++++<<-]>[<++++++++[>++++++<-]" - ">.<++++++++[>------<-]>[<<+>>-]]>.<<++++++++[>>------<<-]<[->>+<<]<++++++++[<+++" - "+>-]<.>+++++++[>+++++++++<-]>+++.<+++++[>+++++++++<-]>.+++++..--------.-------.+" - "+++++++++++++>>[>>>+>+<<<<-]>>>>[-<<<<+>>>>]>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<<<" - "[>>>+>+<<<<-]>>>>[-<<<<+>>>>]+>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<<[>>+<<[-]]>[>+<" - "[-]]++>>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<+<[[-]>-<]>[<<<<<<<.>>>>>>>[-]]<<<<<<<<<" - ".>>----.---------.<<.>>----.+++..+++++++++++++.[-]<<[-]]<[>+>+<<-]>>[-<<+>>]+>+<" - "[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<<[>>+>+<<<-]>>>[-<<<+>>>]++++>+<[-<->]<[[-]>>-<<]" - ">>[[-]<<+>>]<<[>+<[-]]<[>>+<<[-]]>>[<<+>>[-]]<<[[-]>++++++++[<++++>-]<.>++++++++" - "++[>+++++++++++<-]>+.-.<<.>>++++++.------------.---.<<.>++++++[>+++<-]>.<++++++[" - ">----<-]>++.+++++++++++..[-]<<[-]++++++++++.[-]]<[>+>+<<-]>>[-<<+>>]+++>+<[-<->]" - "<[[-]>>-<<]>>[[-]<<+>>]<<[[-]++++++++++.>+++++++++[>+++++++++<-]>+++.+++++++++++" - "++.++++++++++.------.<++++++++[>>++++<<-]>>.<++++++++++.-.---------.>.<-.+++++++" - "++++.++++++++.---------.>.<-------------.+++++++++++++.----------.>.<+++++++++++" - "+.---------------.<+++[>++++++<-]>..>.<----------.+++++++++++.>.<<+++[>------<-]" - ">-.+++++++++++++++++.---.++++++.-------.----------.[-]>[-]<<<.[-]]<[>+>+<<-]>>[-" - "<<+>>]++++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[[-]++++++++++.[-]<[-]>]<+<]"; - std::string triangle = ">++++[<++++++++>-]>++++++++[>++++<-]>>++>>>+>>>+<<<<<<<<<<[-[->+<]>[-<+>>>.<<]>>>[[->++++++++[>++++<-]>.<<[->+<]+>[->++++++++++<<+>]>.[-]>]]+<<<[-[->+<]+>[-<+>>>-[->+<]++>[-<->]<<<]<<<<]++++++++++.+++.[-]<]+++++"; - - - BFI bfi(beer); - bfi.interpret(); - bfi.printUsedMemory(); - return 0; -} - diff --git a/homework/81625/Brainfuck-Interpretator/src/run.sh b/homework/81625/Brainfuck-Interpretator/src/run.sh deleted file mode 100644 index edbb3e4..0000000 --- a/homework/81625/Brainfuck-Interpretator/src/run.sh +++ /dev/null @@ -1,3 +0,0 @@ -g++ -std=c++11 main.cpp BFI.cpp -o run -./run -rm run |
