Luk*_*těk 50 c++ algorithm if-statement while-loop simplify
while (temp->left->oper == '+' ||
temp->left->oper == '-' ||
temp->left->oper == '*' ||
temp->left->oper == '/' ||
temp->right->oper == '+' ||
temp->right->oper == '-' ||
temp->right->oper == '*' ||
temp->right->oper == '/')
{
// do something
}
Run Code Online (Sandbox Code Playgroud)
为了清楚起见:temp是一个指向以下node结构的指针:
struct node
{
int num;
char oper;
node* left;
node* right;
};
Run Code Online (Sandbox Code Playgroud)
pad*_*ddy 58
当然,您可以只使用一串有效的运算符并进行搜索。
#include <cstring>
// : :
const char* ops = "+-*/";
while(strchr(ops, temp->left->oper) || strchr(ops, temp->right->oper))
{
// do something
}
Run Code Online (Sandbox Code Playgroud)
如果您担心性能,那么也许进行表查找:
#include <climits>
// : :
// Start with a table initialized to all zeroes.
char is_op[1 << CHAR_BIT] = {0};
// Build the table any way you please. This way using a string is handy.
const char* ops = "+-*/";
for (const char* op = ops; *op; op++) is_op[*op] = 1;
// Then tests require no searching
while(is_op[temp->left->oper] || is_op[temp->right->oper])
{
// do something
}
Run Code Online (Sandbox Code Playgroud)
JeJ*_*eJo 35
是的,的确可以!
将有效字符存储到一个std::array或什至一个普通数组,然后将标准算法应用于该数组std::any_of以检查条件。
#include <array> // std::array
#include <algorithm> // std::any_of
const std::array<char, 4> options{ '+', '-', '*', '/' };
const auto tester = [&temp](char c) { return temp->left->oper == c || temp->right->oper == c; };
const bool isValid = std::any_of(options.cbegin(), options.cend(), tester);
while(isValid) // now the while-loop is simplified to
{
// do something
}
Run Code Online (Sandbox Code Playgroud)
可以通过打包到接受node要检查的对象的函数中来对其进行清理。
#include <array> // std::array
#include <algorithm> // std::any_of
bool isValid(const node& temp)
{
static constexpr std::array<char, 4> options{ '+', '-', '*', '/' };
const auto tester = [&temp](char c) { return temp->left->oper == c || temp->right->oper == c; };
return std::any_of(options.cbegin(), options.cend(), tester);
}
Run Code Online (Sandbox Code Playgroud)
可以在 while-loop
while (isValid(temp)) // pass the node to be checked
{
// do something
}
Run Code Online (Sandbox Code Playgroud)
Jar*_*d42 30
创建一个子函数,
bool is_arithmetic_char(char)
{
// Your implementation or one proposed in another answers.
}
Run Code Online (Sandbox Code Playgroud)
然后:
while (is_arithmetic_char(temp->left->oper)
|| is_arithmetic_char(temp->right->oper))
{
// do something
}
Run Code Online (Sandbox Code Playgroud)
MCC*_*CCS 14
C样式:
int cont = 1;
while(cont)
switch(temp->left->oper) {
case '+':
case '-':
...
case '/':
// Do something
break;
default:
cont = 0;
}
Run Code Online (Sandbox Code Playgroud)
// Do something如果要声明变量,则可能需要用大括号括起来。
您可以构造一个包含选项的字符串并搜索字符:
#include <string>
// ...
for (auto ops = "+-*/"s; ops.find(temp-> left->oper) != std::string::npos ||
ops.find(temp->right->oper) != std::string::npos;)
/* ... */;
Run Code Online (Sandbox Code Playgroud)
的"+-*/"s是一个C ++ 14功能。std::string ops = "+-*/";在C ++ 14之前使用。
编程是寻找冗余并消除冗余的过程。
struct node {
int num;
char oper;
node* left;
node* right;
};
while (temp->left->oper == '+' ||
temp->left->oper == '-' ||
temp->left->oper == '*' ||
temp->left->oper == '/' ||
temp->right->oper == '+' ||
temp->right->oper == '-' ||
temp->right->oper == '*' ||
temp->right->oper == '/') {
// do something
}
Run Code Online (Sandbox Code Playgroud)
这里的“重复单元”是什么?好吧,我看到了两个实例
(something)->oper == '+' ||
(something)->oper == '-' ||
(something)->oper == '*' ||
(something)->oper == '/'
Run Code Online (Sandbox Code Playgroud)
因此,让我们将重复的部分分解为一个函数,这样我们只需要编写一次即可。
struct node {
int num;
char oper;
node* left;
node* right;
bool oper_is_arithmetic() const {
return this->oper == '+' ||
this->oper == '-' ||
this->oper == '*' ||
this->oper == '/';
}
};
while (temp->left->oper_is_arithmetic() ||
temp->right->oper_is_arithmetic()) {
// do something
}
Run Code Online (Sandbox Code Playgroud)
- 缩短!
(原始代码:17行,其中8个为循环条件。修订后的代码:18行,其中2个为循环条件。)
| 归档时间: |
|
| 查看次数: |
6566 次 |
| 最近记录: |