int calculate(std::string const& s) { // 44 ms
        if (s.empty() || s.find_first_not_of(" ") == std::string::npos) { return 0; }
        /*
         * since we only have +*-/ four operations, we follow the rules below
         * whenver we see * or /, performs the operation
         * if we see -, just push negative number to stack
         * if we see +, just push the number to stack
         *
         * sume the stack up and get the results.
         */

        std::stack<int> nums;
        int pos = 0;
        nums.push(getVal(s, pos));
        while (pos < s.size() && s.find_first_not_of(" ", pos) != std::string::npos) {
            auto const nextOp  = getOps(s, pos);
            auto nextNum = getVal(s, pos);
            auto curNum  = nums.top();
            switch (nextOp) {
                case '*': nextNum = curNum * nextNum; nums.pop(); break;
                case '/': nextNum = curNum / nextNum; nums.pop(); break;
                case '-': nextNum = -nextNum; break;
                case '+': break;
            }
            nums.push(nextNum);
        }

        int res = 0;
        while (!nums.empty()) { res += nums.top(); nums.pop(); }
        return res;
    }
Exemple #2
0
/*This function is passed the completed binary array, splits it into individual lines of instruction (called newCommand), and calls the method getOps() on each instruction to find the opcode / operand. 
*/
int binaryToLC3(char binaryArray[], char *address){
	
	char tempArray[400] = "";
	char newCommand[100] = "";
	
	strcpy(tempArray, binaryArray);
	
	int i = 0;
	int j = 0;
	while(binaryArray[i]){
		if(binaryArray[i] != '2'){
			newCommand[j] = binaryArray[i];
		}
		else{
			if(binaryArray[i - 1] != '2'){
				getOps(newCommand, address);
				i++;
				j = -1;
			}
		}
		i++;
		j++;
	}
	
	return 0;
}
/*	vector index 0 = lkeys
 *	vector index 1 = lvars
 *	vector index 2 = lops
 *	vector index 3 = llits
 */
std::vector<std::map<std::string, size_t>> Tokenizer::findTokens(std::string line)
{
	std::vector<std::map<std::string, size_t>> vec;
	std::map<std::string, size_t> lkeys;
	std::map<std::string, size_t> lvars;
	std::map<std::string, size_t> lops;
	std::map<std::string, size_t> llits;
	std::string words = seperateWords(line);
	auto keys = getKeys(words);
	auto vars = getVars(words);
	auto ops = getOps(line);	// getOps needs non-seperated line
	auto lits = getLits(words);
	size_t pos = 0;
	//for (int i = 0; i < keys.size(); i++)
	for (auto x : keys)
	{
		//pos = line.find(keys[i], pos);
		pos = line.find(x, pos);
		//lkeys.insert(std::pair<size_t, std::string>(pos++, keys[i]));
		lkeys.insert(std::make_pair(x, pos++));
	}
	pos = 0;
	//for (int i = 0; i < vars.size(); i++)
	for (auto x : vars)
	{
		//pos = line.find(vars[i], pos);
		pos = line.find(x, pos);
		//lvars.insert(std::pair<size_t, std::string>(pos++, vars[i]));
		lvars.insert(std::make_pair(x, pos++));
	}
	pos = 0;
	//for (int i = 0; i < ops.size(); i++)
	for (auto x : ops)
	{
		//pos = line.find(ops[i], pos);
		pos = line.find(x, pos);
		//lops.insert(std::pair<size_t, std::string>(pos++, ops[i]));
		lops.insert(std::make_pair(x, pos++));
	}
	pos = 0;
	//for (int i = 0; i < lits.size(); i++)
	for (auto x : lits)
	{
		//pos = line.find(lits[i], pos);
		pos = line.find(x, pos);
		//llits.insert(std::pair<size_t, std::string>(pos++, lits[i]));
		llits.insert(std::make_pair(x, pos++));
	}
	vec.push_back(lkeys);
	vec.push_back(lvars);
	vec.push_back(lops);
	vec.push_back(llits);
	return vec;
}