Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <map>
- #include <stack>
- #include <string>
- #include <iostream>
- using namespace std;
- template<class Data, class Argv, class Ch>
- class bf{
- public:
- Argv N = 0;
- stack<Argv> stack;
- map<Argv, Ch> symbol;
- void compile(Data &input, Argv &x)
- {
- for (unsigned int i = 0; i < input.length(); i++)
- {
- switch (input[i])
- {
- case '+':
- symbol[N]++;
- break;
- case '-':
- symbol[N]--;
- break;
- case '>':
- N++;
- break;
- case '<':
- N--;
- break;
- case '[':
- stack.push(i);
- break;
- case ']':
- if (symbol[N] != 0)
- {
- if (stack.empty())
- {
- cout << "Ошибка: различное количество скобок" << endl;
- break;
- }
- i = stack.top();
- }
- else
- stack.pop();
- break;
- case '.':
- cout << symbol[N];
- break;
- case ',':
- cin >> symbol[N];
- break;
- }
- }
- if (stack.empty())
- {
- cout << endl << "f(x) = " <<pow(sin(x), 2);
- }
- }
- };
- void main()
- {
- setlocale(0, "");
- string input;
- bf<string,int,char> bf;
- int x = 0;
- input = "+[------->++<]>++.-.++++++++.+[--->+<]>.--[->+++<]>+.----------.+++++.++[++>---<]>.[->+++<]>.[--->+<]>+.+++.++++++.---------.-[-->+++<]>-.";
- cout << "x = "; cin >> x;
- bf.compile(input,x);
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement