#include <iostream>
#include <map>
#include <string>
#include <iomanip>
#include <cmath>
#include <stdexcept>
#include <queue>
#include <stack>
#include <windows.h>
using namespace std;
void SetColor(int ForgC, int BackC) {
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}
void SetColor(int ForgC) {
WORD wColor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
if(GetConsoleScreenBufferInfo(hStdOut, &csbi)) {
wColor = csbi.wAttributes;
wColor = (wColor & 0xFFF0) + ForgC;
SetConsoleTextAttribute(hStdOut, wColor);
}
}
void SetConsoleColor(WORD attributes) {
HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsoleOutput, attributes);
}
struct nodef {
int d;
double lf;
string s;
string type;
string type_sq;
};
map<string, nodef> def;
map<string, queue<int>> qi_def;
map<string, stack<int>> si_def;
map<string, queue<string>> qs_def;
map<string, stack<string>> ss_def;
map<string, queue<double>> qd_def;
map<string, stack<double>> sd_def;
int retnum;
string fopr;//the first operator
const string out = "out<<< ", in = "in>>> ";
bool pd_is(int a, int b, string opr_is) {
if (opr_is == "bigger_than" || opr_is == ">") {
return (a > b);
}
if (opr_is == "smaller_than" || opr_is == "<") {
return (a < b);
}
if (opr_is == "equal_to" || opr_is == "==") {
return (a == b);
}
if (opr_is == "bigger_equal_to" || opr_is == ">=") {
return (a >= b);
}
if (opr_is == "smaller_equal_to" || opr_is == "<=") {
return (a <= b);
}
if (opr_is == "not_equal_to" || opr_is == "!=") {
return (a != b);
}
if (opr_is == "square_of" || opr_is == "#=") {
return (b * b == a);
}
throw runtime_error("No the opr_is!");
}
bool pd_is(double a, double b, string opr_is) {
if (opr_is == "bigger_than" || opr_is == ">") {
return (a > b);
}
if (opr_is == "smaller_than" || opr_is == "<") {
return (a < b);
}
if (opr_is == "equal_to" || opr_is == "==") {
return (a == b);
}
if (opr_is == "bigger_equal_to" || opr_is == ">=") {
return (a >= b);
}
if (opr_is == "smaller_equal_to" || opr_is == "<=") {
return (a <= b);
}
if (opr_is == "not_equal_to" || opr_is == "!=") {
return (a != b);
}
if (opr_is == "square_of" || opr_is == "#=") {
return (b * b == a);
}
throw runtime_error("No the opr_is!");
}
bool pd_is(string a, string b, string opr_is) {
if (opr_is == "bigger_than" || opr_is == ">") {
return (a > b);
}
if (opr_is == "smaller_than" || opr_is == "<") {
return (a < b);
}
if (opr_is == "equal_to" || opr_is == "==") {
return (a == b);
}
if (opr_is == "bigger_equal_to" || opr_is == ">=") {
return (a >= b);
}
if (opr_is == "smaller_equal_to" || opr_is == "<=") {
return (a <= b);
}
if (opr_is == "not_equal_to" || opr_is == "!=") {
return (a != b);
}
throw runtime_error("No the opr_is!");
}
string btstr(bool in__bo) {
return (in__bo) ? "True" : "False";
}
double double_value(string name) {
return def[name].lf;
}
int int_value(string name) {
return def[name].d;
}
string str_value(string name) {
return def[name].s;
}
string deftype(string name) {
return def[name].type;
}
bool isopr(string __in) {
return (__in == fopr);
}
void let(string name_a, string name_b, string let_to_opr, string type) {
if (type == "int") {
if (let_to_opr == "plus" || let_to_opr == "+=") {
def[name_a].d += def[name_b].d;
} else if (let_to_opr == "minus" || let_to_opr == "-=") {
def[name_a].d -= def[name_b].d;
} else if (let_to_opr == "times" || let_to_opr == "*=") {
def[name_a].d *= def[name_b].d;
} else if (let_to_opr == "divide" || let_to_opr == "/=") {
def[name_a].d /= def[name_b].d;
} else if (let_to_opr == "mod" || let_to_opr == "%=") {
def[name_a].d %= def[name_b].d;
} else if (let_to_opr == "and" || let_to_opr == "&&=") {
def[name_a].d &= def[name_b].d;
} else if (let_to_opr == "or" || let_to_opr == "||=") {
def[name_a].d |= def[name_b].d;
} else if (let_to_opr == "xor" || let_to_opr == "^^=") {
def[name_a].d ^= def[name_b].d;
} else if (let_to_opr == "nor" || let_to_opr == "!|=") {
def[name_a].d = ~(def[name_a].d | def[name_b].d);
} else if (let_to_opr == "lshift" || let_to_opr == "<<=") {
def[name_a].d <<= def[name_b].d;
} else if (let_to_opr == "rshift" || let_to_opr == ">>=") {
def[name_a].d >>= def[name_b].d;
} else if (let_to_opr == "nxor" || let_to_opr == "!^=") {
def[name_a].d = ~(def[name_a].d ^ def[name_b].d);
} else if (let_to_opr == "nand" || let_to_opr == "!&=") {
def[name_a].d = ~(def[name_a].d ^ def[name_b].d);
} else if (let_to_opr == "pow" || let_to_opr == "^=") {
def[name_a].d = int(pow(def[name_a].d, def[name_b].d));
} else {
throw runtime_error("The let is error.");
}
} else if (type == "string") {
if (let_to_opr == "plus" || let_to_opr == "+=") {
def[name_a].s += def[name_b].s;
} else {
throw runtime_error("The let is error.");
}
} else if (type == "double") {
if (let_to_opr == "plus" || let_to_opr == "+=") {
def[name_a].lf += def[name_b].lf;
} else if (let_to_opr == "minus" || let_to_opr == "-=") {
def[name_a].lf -= def[name_b].lf;
} else if (let_to_opr == "times" || let_to_opr == "*=") {
def[name_a].lf *= def[name_b].lf;
} else if (let_to_opr == "divide" || let_to_opr == "/=") {
def[name_a].lf /= def[name_b].lf;
} else if (let_to_opr == "mod" || let_to_opr == "%=") {
def[name_a].d = fmod(def[name_a].lf, def[name_b].lf);
} else if (let_to_opr == "pow" || let_to_opr == "^=") {
def[name_a].d = pow(def[name_a].lf, def[name_b].lf);
} else {
throw runtime_error("The let is error.");
}
}
}
int main() {
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
SetColor(FOREGROUND_BLUE);
cout<<"-------------[Cnle5.0]---------\n";
for (int line = 1; 1; line++) {
SetColor(FOREGROUND_RED);
cout<<"line("<<line<<") ";
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_BLUE);
cin >> fopr; //input
if (isopr("def")) {
//define num
string def_name, def_type;
bool flag = false;
cin >> def_type;
while (1) {
cin >> def_name;
if (def.count(def_name)) {
retnum = 100;
flag = true;
break;
}
if (def_type == "int") {
int def_value;
cin >> def_value;
def[def_name] = {def_value, 0, "", "int", "none"};
} else if (def_type == "string") {
string def_value;
cin >> def_value;
def[def_name] = {0, 0, def_value, "string", "none"};
} else if (def_type == "double") {
double def_value;
cin >> def_value;
def[def_name] = {0, def_value, "", "double", "none"};
} else if (def_type == "stack<int>") {
def[def_name] = {0, 0, "", "stack", "int"};
} else if (def_type == "stack<string>") {
def[def_name] = {0, 0, "", "stack", "string"};
} else if (def_type == "stack<double>") {
def[def_name] = {0, 0, "", "stack", "double"};
} else if (def_type == "queue<int>") {
def[def_name] = {0, 0, "", "queue", "int"};
} else if (def_type == "queue<string>") {
def[def_name] = {0, 0, "", "queue", "string"};
} else if (def_type == "queue<double>") {
def[def_name] = {0, 0, "", "queue", "double"};
} else {
retnum = 1;
flag = true;
break;
}
string and_;
cin >> and_;
if (and_ == "none" || and_ == ";") {
break;
} else if (and_ != "and" && and_ != ",") {
retnum = 100;
flag = true;
break;
}
}
if (flag) {
break;
}
//end
//def int a (value,::int) and/none
//def string a (value::string) and/none
} else if (isopr("print")) {
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
cout << out << " ";
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_BLUE);
bool flag = false;
while (1) {
string print_num_type;
cin >> print_num_type;
if (print_num_type == ":def") {
string def_print_name;
cin >> def_print_name;
if (!def.count(def_print_name)) {
retnum = 3;
flag = true;
break;
} else if (deftype(def_print_name) == "int") {
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
cout << int_value(def_print_name) << " ";
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_BLUE);
//end
} else if (deftype(def_print_name) == "string") {
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
cout << str_value(def_print_name) << " ";
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_BLUE);
//end
} else if (deftype(def_print_name) == "double") {
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
cout << double_value(def_print_name) << " ";
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_BLUE);
//end
} else {
retnum = 2;
flag = true;
break;
}
} else if (print_num_type == ":str") {
string put_string;
cin >> put_string;
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
cout << put_string << " ";
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_BLUE);
} else if (print_num_type == ":is") {
string is_typename;
cin >> is_typename;
if (is_typename == "value") {
string is_type_a, is_type_b, is_type_opr;
cin >> is_type_a >> is_type_opr >> is_type_b;
if (!def.count(is_type_a) || !def.count(is_type_b)) {
retnum = 3;
flag = true;
break;
}
if (deftype(is_type_a) == "int" && deftype(is_type_a) == "int") {
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
cout << btstr(pd_is(int_value(is_type_a), int_value(is_type_b), is_type_opr)) << " ";
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_BLUE);
} else if (deftype(is_type_a) == "string" && deftype(is_type_a) == "string") {
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
cout << btstr(pd_is(str_value(is_type_a), str_value(is_type_b), is_type_opr)) << " ";
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_BLUE);
} else if (deftype(is_type_a) == "double" && deftype(is_type_a) == "double") {
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
cout << btstr(pd_is(double_value(is_type_a), double_value(is_type_b), is_type_opr)) << " ";
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_BLUE);
} else {
retnum = 4;
flag = true;
break;
}
} else if (is_typename == "number") {
string is_type;
cin >> is_type;
if (is_type == "int") {
int number_a, number_b;
string number_opr;
cin >> number_a >> number_opr >> number_b;
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
cout << btstr(pd_is(number_a, number_b, number_opr)) << " ";
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_BLUE);
} else if (is_type == "string") {
string number_a, number_b;
string number_opr;
cin >> number_a >> number_opr >> number_b;
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
cout << btstr(pd_is(number_a, number_b, number_opr)) << " ";
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_BLUE);
} else if (is_type == "double") {
double number_a, number_b;
string number_opr;
cin >> number_a >> number_opr >> number_b;
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
cout << btstr(pd_is(number_a, number_b, number_opr)) << " ";
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_BLUE);
} else {
retnum = 100;
flag = true;
break;
}
}
} else {
retnum = 2;
break;
}
string __and;
cin >> __and;
if (__and == "none" || __and == ";") {
break;
} else if (__and != "and" && __and != ",") {
retnum = 100;
flag = true;
break;
}
}
cout << endl;
if (flag) {
break;
}
} else if (isopr("no_line")) {
string no__line;
cin >> no__line;
} else if (isopr("let")) {
string let_a, let_b, let_opr;
cin >> let_a >> let_opr >> let_b;
if (deftype(let_a) != deftype(let_b) || !(deftype(let_a) == "string" || deftype(let_a) == "int")) {
retnum = 6;
break;
} else if ((deftype(let_a) == "string" || deftype(let_b) == "string") && let_opr != "plus" && let_opr != "+=") {
retnum = 5;
break;
} else {
let(let_a, let_b, let_opr, deftype(let_a));
}
} else if (isopr("stack")) {
string name, stack_do;
cin >> name >> stack_do;
if (stack_do == "push") {
if (def[name].type_sq == "int") {
int push_v;
cin >> push_v;
si_def[name].push(push_v);
} else if (def[name].type_sq == "string") {
string push_v;
cin >> push_v;
ss_def[name].push(push_v);
} else if (def[name].type_sq == "double") {
double push_v;
cin >> push_v;
sd_def[name].push(push_v);
} else {
retnum = 100;
break;
}
} else if (stack_do == "pop") {
if (def[name].type_sq == "int") {
si_def[name].pop();
} else if(def[name].type_sq == "string") {
ss_def[name].pop();
} else if(def[name].type_sq == "double") {
sd_def[name].pop();
} else {
retnum = 100;
break;
}
} else if (stack_do == "top") {
if (def[name].type_sq == "int") {
cout<<out<<si_def[name].top()<<endl;
} else if(def[name].type_sq == "string") {
cout<<out<<ss_def[name].top()<<endl;
} else if(def[name].type_sq == "double") {
cout<<out<<sd_def[name].top()<<endl;
} else {
retnum = 100;
break;
}
} else {
retnum = 100;
break;
}
} else if (isopr("queue")) {
string name, queue_do;
cin >> name >> queue_do;
if (queue_do == "push") {
if (def[name].type_sq == "int") {
int push_v;
cin >> push_v;
qi_def[name].push(push_v);
} else if (def[name].type_sq == "string") {
string push_v;
cin >> push_v;
qs_def[name].push(push_v);
} else if (def[name].type_sq == "double") {
double push_v;
cin >> push_v;
qd_def[name].push(push_v);
} else {
retnum = 100;
break;
}
} else if (queue_do == "pop") {
if (def[name].type_sq == "int") {
qi_def[name].pop();
} else if(def[name].type_sq == "string") {
qs_def[name].pop();
} else if(def[name].type_sq == "double") {
qd_def[name].pop();
} else {
retnum = 100;
break;
}
} else if (queue_do == "front") {
if (def[name].type_sq == "int") {
cout<<out<<qi_def[name].front()<<endl;
} else if(def[name].type_sq == "string") {
cout<<out<<qs_def[name].front()<<endl;
} else if(def[name].type_sq == "double") {
cout<<out<<qd_def[name].front()<<endl;
} else {
retnum = 100;
break;
}
} else {
retnum = 100;
break;
}
} else if (isopr("to")) {
string a_type, a_name, dt;
cin >> a_name >> a_type;
dt = deftype(a_name);
if (a_type != deftype(a_name)) {
if (dt == "string") {
if (a_type == "int") {
int those_num = 0;
bool is_n = 1;
string thstr = str_value(a_name);
for (int i = 0; i < int(thstr.length()); i++) {
if (thstr[i] == '-') {
if (i == 0) {
is_n = 0;
} else {
retnum = 100;
break;
}
} else {
those_num = those_num * 10 + thstr[i] - 48;
}
}
def[a_name].d = (is_n) ? those_num : (-those_num);
def[a_name].s = "";
def[a_name].type = "int";
} else if (a_type == "double") {
double it = 0, those_num = 0, down = 0;
bool is_n = 1, flag = 0, kflag = false;
string thstr = str_value(a_name);
for (int i = 0; i < int(thstr.length()); i++) {
if (thstr[i] == '-') {
if (i == 0) {
is_n = 0;
} else {
kflag = true;
retnum = 100;
break;
}
} else if (thstr[i] == '.') {
if (i == 0) {
retnum = 100;
kflag = true;
break;
} else {
flag = true;
}
} else if (thstr[i] >= '0' && thstr[i] <= '9') {
if (flag) {
down = down * 10.0 + thstr[i] - 48.0;
it++;
} else {
those_num = those_num * 10.0 + thstr[i] - 48.0;
}
} else {
retnum = 100;
kflag = true;
break;
}
}
those_num += down / pow(10, it);
def[a_name].lf = (is_n) ? those_num : (-those_num);
if (kflag) {
break;
}
} else {
retnum = 100;
break;
}
} else if (dt == "double") {
if (a_type == "int") {
def[a_name].d = floor(int_value(a_name));
def[a_name].lf = 0;
def[a_name].type = "int";
} else if (a_type == "string") {
retnum = 100;
break;
}
} else if (dt == "int") {
if (a_type == "double") {
def[a_name].lf = double(int_value(a_name));
def[a_name].type = "double";
} else if (a_type == "string") {
string itstr = "";
int thisint = int_value(a_name);
while (thisint) {
itstr += char(thisint % 10 + '0');
thisint /= 10;
}
def[a_name].s = itstr;
def[a_name].type = "string";
} else {
retnum = 100;
break;
}
} else {
retnum = 100;
break;
}
}
} else if (isopr("type")) {
string name;
cin >> name;
SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN);
cout << out << deftype(name) << endl;
SetConsoleColor(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_BLUE);
} else if (isopr("end")) {
cout << endl;
retnum = 0;
break;
} else {
retnum = 8;
break;
}
}
cout << "--------------------------------\n";
if (retnum == 0) {
cout << "This program is return.No error.\n" << "Put the keyboard to continue. . .";
} else if (retnum == 1) {
cout << "This program is return." << "[error]the def_opr was not the type.\n" << "Put the keyboard to continue. . .";
} else if (retnum == 2) {
cout << "This program is return." << "[error]the print_opr was not the type.\n" << "Put the keyboard to continue. . .";
} else if (retnum == 3) {
cout << "This program is return." << "[error]the def was not define!\n" << "Put the keyboard to continue. . .";
} else if (retnum == 4) {
cout << "This program is return." << "[error]the print or let to num_value\'s type was diffrent!\n" << "Put the keyboard to continue. . .";
} else if (retnum == 5) {
cout << "This program is return." << "[error]the string cannot to other comput the plus and minus!\n" << "Put the keyboard to continue. . .";
} else if (retnum == 6) {
cout << "This program is return." << "[error]the let was error.\n" << "Put the keyboard to continue. . .";
} else if (retnum == 8) {
cout << "This program is return." << "[error]the type was not defined!\n" << "Put the keyboard to continue. . .";
} else {
cout << "This program is return." << "[error]unkown error\n" << "Put the keyboard to continue. . .";
}
return 0;
}剪贴板
剪贴板 r45b62lv
剪贴板信息
剪贴板信息
- 收录
- 更新