当前位置: 首页>>代码示例>>C++>>正文


C++ SS::top方法代码示例

本文整理汇总了C++中SS::top方法的典型用法代码示例。如果您正苦于以下问题:C++ SS::top方法的具体用法?C++ SS::top怎么用?C++ SS::top使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SS的用法示例。


在下文中一共展示了SS::top方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: play

void play(MSI& rank, VS& toks) {
	SS oprs;
	SD nums;
	string out = "";
	char str[100];

	cout << "# from infix to postfix!!!" << endl;
	
	for (VS::iterator i = toks.begin(); i != toks.end(); i++) {
		string tok = *i;
		
		cout << "There is a " << tok << ": ";

		if (is_num(tok)) {
			cout << "push to output" << endl;
			sprintf(str, "%.6lf ", stod(tok));
			out += str;
			nums.push(stod(tok));
		}
		else if (tok == ",") {
			cout << "flush the stack until '('" << endl;
			while (oprs.top() != "(")
				calc(oprs, nums, out);
		}
		else if (tok == ")") {
			cout << "flush the stack until '(' and check if there's a func" << endl;
			while (oprs.top() != "(")
				calc(oprs, nums, out);
			oprs.pop();
			if (!oprs.empty() && is_func(oprs.top()))
				calc(oprs, nums, out);
		}
		else {
			cout << "push to stack" << endl;
			if ( is_unary(tok) && (i == toks.begin() || rank[*(i - 1)] > 0) )
				oprs.push("u" + tok);
			else { 
				while (!is_func(tok) && tok != "(" && !oprs.empty() && rank[oprs.top()] <= rank[tok]) {
					cout << "\t*** stack.top() has higher precedence" << endl;
					calc(oprs, nums, out);
				}
				oprs.push(tok);
			}
		}

		cout << "\tcurrent output: " << out << endl
			 << "\tcurrent  stack: " << oprs << endl;
	}

	while (!oprs.empty())
		calc(oprs, nums, out);

	cout << "There is nothing left, so flush the stack to output" << endl
		 << "\tcurrent output: " << out << endl
		 << "#transformation completed!!!" << endl
		 << "Postfix Exp: " << out << endl
		 << "RESULT: " << nums.top() << endl << endl;
}
开发者ID:kevin1kevin1k,项目名称:dsa2015,代码行数:58,代码来源:3_4_2.cpp

示例2: calc

void calc(SS& oprs, SD& nums, string& s) {
	string opr = oprs.top(); oprs.pop();
	if (opr[0] == 'u') s += opr[1];
	else s += opr;
	s += ' ';
	
	double b = nums.top(); nums.pop();
	if (opr[0] == 'u' || (is_func(opr) && opr != "pow")) {
			 if (opr == "u+") nums.push(b);
		else if (opr == "u-") nums.push(-b);

		else if (opr == "sin") nums.push(sin(b));
		else if (opr == "cos") nums.push(cos(b));
		else if (opr == "exp") nums.push(exp(b));
		else if (opr == "log") nums.push(log(b));
		else if (opr == "sqrt") nums.push(sqrt(b));
		else if (opr == "fabs") nums.push(fabs(b));
		return;
	}

	double a = nums.top(); nums.pop();
		 if (opr == "*") nums.push(a * b);
	else if (opr == "+") nums.push(a + b);
	else if (opr == "-") nums.push(a - b);
	else if (opr == "pow") nums.push(pow(a, b));
}
开发者ID:kevin1kevin1k,项目名称:dsa2015,代码行数:26,代码来源:3_4_2.cpp

示例3:

ostream& operator<<(ostream& out, const SS& s) {
	VS v;
	SS ss = s;
	while (!ss.empty()) {
		v.PB(ss.top());
		ss.pop();
	}
	for (int i = v.size() - 1; i >= 0; i--)
		out << v[i] << ' ';
	return out;
}
开发者ID:kevin1kevin1k,项目名称:dsa2015,代码行数:11,代码来源:3_4_2.cpp


注:本文中的SS::top方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。