首页
统计
关于
友联
Search
1
断链是什么意思?
607 阅读
2
判断点的凹凸性
555 阅读
3
C++中的const
442 阅读
4
Python基础
437 阅读
5
C++ map遍历的几种方式
429 阅读
默认分类
Cpp
Python
登录
/
注册
Search
标签搜索
Python
Dare to Dream
累计撰写
24
篇文章
首页
栏目
默认分类
Cpp
Python
页面
统计
关于
友联
搜索到
24
篇与
的结果
2022-12-19
C++解析cstring四则运算表达式
// codes go here#include<iostream> #include<stack> #include<string> using namespace std; string nums = "0123456789"; //比较函数,用于比较当前操作符和栈顶操作符的优先级 //如果当前操作符优先级大于栈顶优先级,返回false //如果栈顶操作符为左括号( //如果当前操作符优先级小于栈顶优先级,true //还要满足从左往右计算顺序,如果当前操作符与栈顶操作符为同级关系,返回true //返回true说明此时需要将栈顶操作符弹出,用于计算 bool cmp(char top, char now){ if(top == '('){ return false; }else if((top == '-' || top == '+') && (now == '*' || now == '/')){ return false; } return true; } //计算,从数字栈中弹出两个数字,操作符栈中弹出操作符 void calculate(stack<double> &num,stack<char> &op){ // 数字栈中数字的顺序也有要求 // 第一个弹出的数字在后面,第二个弹出的在前面 double b = num.top(); num.pop(); double a = num.top(); num.pop(); char c = op.top(); op.pop(); if(c == '+') a = a+b; else if(c == '-') a = a-b; else if(c == '*') a = a*b; else if(c == '/') a = a/b; //将结果放入数字栈 num.push(a); return ; } int main(void){ string s; while(getline(cin, s)){ //数字栈 stack<double> num_stk; //操作符栈 stack<char> op_stk; //预处理,将算式用括号括起来 op_stk.push('('); s += ')'; //算式的第一个应该是数字 bool isNextOp = false; for(int i=0; i<s.size(); ++i){ //左括号的话,直接压入操作符栈,将大中小括号都当做小括号处理 if(s[i] == '(' || s[i] == '[' || s[i] == '{'){ op_stk.push('('); } //右括号的话就可以开始计算,直到遇到与之对应的左括号 else if(s[i] == ')' || s[i] == ']' || s[i] == '}'){ while(op_stk.top() != '(') //计算 calculate(num_stk,op_stk); //弹出此时的( op_stk.pop(); } //除括号外,数字和操作符应该是间隔排列的,而且算式的第一个应该是数字 //若没有括号,需要考虑操作符优先级问题,并且是从左往右计算 else if(isNextOp){ while(cmp(op_stk.top(),s[i])){ //计算 calculate(num_stk,op_stk); } op_stk.push(s[i]); isNextOp = false; } //将数字从字符串中提取出来 else{ int j = i; //+ -符号可能是正负符号 if(s[j] == '+' || s[j] == '-') ++i; //数字可能有多位,在0-9中 while(nums.find(s[i]) != nums.npos) ++i; //i和j之间的字符即为数字 //将数字提取,并压入数字栈 //num_stk.push(stod(nums.substr(j,i-j))); num_stk.push((double)stoi(s.substr(j,i-j))); --i; //当前为数字,所以下一个为操作符 isNextOp = true; } } //数字栈顶的元素即为最后结果 cout<<num_stk.top()<<endl; } return 0; }
2022年12月19日
137 阅读
1 点赞
2022-12-05
此内容被密码保护
加密文章,请前往内页查看详情
2022年12月05日
144 阅读
0 点赞
2022-10-30
此内容被密码保护
加密文章,请前往内页查看详情
2022年10月30日
150 阅读
0 点赞
2022-10-29
此内容被密码保护
加密文章,请前往内页查看详情
2022年10月29日
138 阅读
1 点赞
2022-10-27
C++find_if算法
C++find_if算法功能描述:按条件查找元素函数原型:find_if(iterator beg, iterator end, _Pred); //按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置 //beg开始迭代器 //end结束迭代器 //_Pred函数或者谓词(返回bool类型的仿函数)代码示例:#include<iostream> #include<vector> #include<algorithm> #include<string> using namespace std; //常用查找算法find_if //1.查找内置数据类型 class GreaterFive { public: bool operator()(int val) { return val > 5; } }; void test01() { vector<int>v; for (int i = 0; i < 10; i++) { v.push_back(i); } vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive()); if (it == v.end()) { cout << "没有找到" << endl; } else { cout << "找到大于5的数字为:" << *it << endl; } } //2.查找自定义数据类型 class Person { public: Person(string name, int age) { this->m_Name = name; this->m_Age = age; } string m_Name; int m_Age; }; class Greater20 { public: bool operator()(Person &p) { return p.m_Age > 20; } }; void test02() { vector<Person>v; //创建数据 Person p1("aaa", 10); Person p2("bbb", 20); Person p3("ccc", 30); Person p4("ddd", 40); v.push_back(p1); v.push_back(p2); v.push_back(p3); v.push_back(p4); //找年龄大于20的人 vector<Person>::iterator it= find_if(v.begin(), v.end(), Greater20()); if (it == v.end()) { cout << "没有找到" << endl; } else { cout << "找到姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl; } } int main() { test01(); test02(); system("pause"); return 0; }
2022年10月27日
140 阅读
1 点赞
1
...
3
4
5