Created
August 19, 2013 14:43
-
-
Save yippee-ki-yay/6269898 to your computer and use it in GitHub Desktop.
For the reddit 135 easy challenge, uses rpn (probably badly implemented), very messy code, I will make a calculator using this method and try to write cleaner code and understand the whole thing better....
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 <cstdio> | |
#include <cstdlib> | |
#include <ctime> | |
#include <stack> | |
#include <vector> | |
#include <queue> | |
#include <deque> | |
#include <string> | |
#include <sstream> | |
#include <iostream> | |
using namespace std; | |
bool IsOp(char token, vector<char>& op) | |
{ | |
for(unsigned int i = 0; i < op.size(); ++i) | |
{ | |
if(token == op[i]) | |
return true; | |
} | |
return false; | |
} | |
bool IsOp(string token, vector<char>& op) | |
{ | |
for(unsigned int i = 0; i < op.size(); ++i) | |
{ | |
string tmp(1, op[i]); | |
if(tmp == token) return true; | |
} | |
return false; | |
} | |
double String2Number(string &token) | |
{ | |
istringstream ss(token); | |
double num; | |
ss >> num; | |
return num; | |
} | |
void ParseString(string& input, queue<string>& output, vector<char>& vec) | |
{ | |
stack<string> op_stack; | |
string number; | |
bool presedence = false; | |
bool in_digit = false; | |
for(unsigned int i = 0; i < input.length(); ++i) | |
{ | |
if(isdigit(input[i])) | |
{ | |
in_digit = true; | |
number += input[i]; | |
} | |
if(input[i] == ' ' || i == input.length()-1) | |
{ | |
if(in_digit == true) | |
{ | |
output.push(number); | |
if(presedence == true) | |
{ | |
string op = op_stack.top(); | |
op_stack.pop(); | |
output.push(op); | |
presedence = false; | |
} | |
in_digit = false; | |
number.clear(); | |
} | |
} | |
if(IsOp(input[i], vec)) | |
{ | |
string tmp(1, input[i]); | |
op_stack.push(tmp); | |
if(input[i] == '*') | |
{ | |
presedence = true; | |
} | |
} | |
} | |
stack<string> tmp; | |
while(!op_stack.empty()) | |
{ | |
string op = op_stack.top(); | |
op_stack.pop(); | |
tmp.push(op); | |
} | |
while(!tmp.empty()) | |
{ | |
string op = tmp.top(); | |
tmp.pop(); | |
output.push(op); | |
} | |
} | |
double Calculate(queue<string>& output, vector<char> & op) | |
{ | |
deque <double> numbers; | |
while(!output.empty()) | |
{ | |
string tmp = output.front(); | |
output.pop(); | |
if(IsOp(tmp, op)) | |
{ | |
int m, l, r = 0; | |
if(tmp == "*") | |
{ | |
m = numbers.back(); | |
numbers.pop_back(); | |
l = numbers.back(); | |
numbers.pop_back(); | |
} | |
else | |
{ | |
m = numbers.front(); | |
numbers.pop_front(); | |
l = numbers.front(); | |
numbers.pop_front(); | |
} | |
if(tmp == "+") r = m + l; | |
if(tmp == "-") r = m - l; | |
if(tmp == "*") r = m * l; | |
if(tmp == "*") | |
numbers.push_back(r); | |
else | |
numbers.push_front(r); | |
} | |
else | |
{ | |
numbers.push_back(String2Number(tmp)); | |
} | |
} | |
double result = numbers.front(); | |
return result; | |
} | |
string Int2String(int t) | |
{ | |
stringstream ss; | |
ss << t; | |
return ss.str(); | |
} | |
int main() | |
{ | |
queue<string> out_queue; | |
vector<char> operators; | |
operators.push_back('+'); | |
operators.push_back('-'); | |
operators.push_back('*'); | |
srand(time(0)); | |
int M, N; | |
cin>>M>>N; | |
while(1) | |
{ | |
vector<string> r_num; | |
for(int i = 0; i < 4; i++) | |
{ | |
int tmp = (rand() % N + M); | |
r_num.push_back(Int2String(tmp)); | |
} | |
vector<char> r_ops; | |
for(int i = 0; i < 3; i++) | |
r_ops.push_back(operators[rand()%3]); | |
string expression = r_num[0] + " " + r_ops[0] + " " + r_num[1] + " " + r_ops[1] + " " + r_num[2] \ | |
+" " + r_ops[2] + " " + r_num[3]; | |
cout<<"> "<<expression<<endl; | |
ParseString(expression, out_queue, operators); | |
int comp_result = Calculate(out_queue, operators); | |
int i_result = 0; | |
string s_result; | |
cin>>s_result; | |
if(s_result == "q") | |
return 0; | |
i_result = String2Number(s_result); | |
if(i_result == comp_result) | |
cout<<"Correct!"<<endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment