C++ isnan 的实现代码
时间: 2024-01-17 08:03:28 浏览: 31
C++ 的 isnan 函数用于判断一个浮点数是否为 NaN(Not a Number)。以下是一个简单的实现代码:
```c++
bool isnan(double x) {
// 判断 x 是否等于自身
return x != x;
}
```
这是因为 NaN 与任何数都不相等,包括自身。因此,如果一个浮点数 x 不等于自身,就可以判断它是否为 NaN。当然,这个实现方式可能会有一些精度上的问题,因此在实际使用时建议使用标准库中的 isnan 函数。
相关问题
请给出代码c++
以下是一个简单的 C++ 实现,实现了基本要求中的功能,但没有添加选做内容。代码中使用了两个栈来实现括号匹配和逆波兰表达式的转换。
```c++
#include <iostream>
#include <stack>
#include <string>
#include <cctype>
#include <cstring>
using namespace std;
// 判断运算符的优先级
int getPriority(char c) {
switch (c) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default:
return 0;
}
}
// 判断运算符的结合性
bool isLeftAssociative(char c) {
return (c != '^');
}
// 将中缀表达式转换为逆波兰表达式
string infixToPostfix(string infix) {
stack<char> opStack;
string postfix;
for (int i = 0; i < infix.size(); i++) {
if (isdigit(infix[i]) || infix[i] == '.') {
postfix += infix[i];
while (isdigit(infix[i+1]) || infix[i+1] == '.') {
i++;
postfix += infix[i];
}
postfix += ' ';
}
else if (infix[i] == '(') {
opStack.push(infix[i]);
}
else if (infix[i] == ')') {
while (!opStack.empty() && opStack.top() != '(') {
postfix += opStack.top();
postfix += ' ';
opStack.pop();
}
if (!opStack.empty() && opStack.top() == '(') {
opStack.pop();
}
else {
return "Error: Mismatched parentheses\n";
}
}
else if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '^') {
while (!opStack.empty() && opStack.top() != '(' && ((isLeftAssociative(infix[i]) && getPriority(infix[i]) <= getPriority(opStack.top())) || (!isLeftAssociative(infix[i]) && getPriority(infix[i]) < getPriority(opStack.top())))) {
postfix += opStack.top();
postfix += ' ';
opStack.pop();
}
opStack.push(infix[i]);
}
else {
return "Error: Invalid character\n";
}
}
while (!opStack.empty()) {
if (opStack.top() == '(' || opStack.top() == ')') {
return "Error: Mismatched parentheses\n";
}
postfix += opStack.top();
postfix += ' ';
opStack.pop();
}
return postfix;
}
// 对逆波兰表达式求值
double evaluatePostfix(string postfix) {
stack<double> numStack;
char* token = strtok(&postfix[0], " ");
while (token != NULL) {
if (isdigit(token[0]) || (token[0] == '-' && isdigit(token[1]))) {
numStack.push(atof(token));
}
else if (token[0] == '+' || token[0] == '-' || token[0] == '*' || token[0] == '/' || token[0] == '^') {
if (numStack.size() < 2) {
return NAN;
}
double b = numStack.top();
numStack.pop();
double a = numStack.top();
numStack.pop();
switch (token[0]) {
case '+':
numStack.push(a + b);
break;
case '-':
numStack.push(a - b);
break;
case '*':
numStack.push(a * b);
break;
case '/':
numStack.push(a / b);
break;
case '^':
numStack.push(pow(a, b));
break;
}
}
else {
return NAN;
}
token = strtok(NULL, " ");
}
if (numStack.size() == 1) {
return numStack.top();
}
else {
return NAN;
}
}
// 判断中缀表达式中的括号是否匹配
bool isParenthesesMatched(string infix) {
stack<char> parenStack;
for (int i = 0; i < infix.size(); i++) {
if (infix[i] == '(') {
parenStack.push(infix[i]);
}
else if (infix[i] == ')') {
if (parenStack.empty() || parenStack.top() != '(') {
return false;
}
else {
parenStack.pop();
}
}
}
return parenStack.empty();
}
int main() {
while (true) {
cout << "Calculator\n\n";
cout << "Select an option:\n";
cout << "1. Evaluate expression\n";
cout << "2. Exit\n\n";
cout << "Option: ";
int option;
cin >> option;
if (option == 1) {
cout << "Enter an expression: ";
string infix;
cin >> infix;
if (isParenthesesMatched(infix)) {
string postfix = infixToPostfix(infix);
if (postfix.substr(0, 6) == "Error:") {
cout << postfix << endl;
}
else {
double result = evaluatePostfix(postfix);
if (isnan(result)) {
cout << "Error: Invalid expression\n";
}
else {
cout << "Result: " << result << endl;
}
}
}
else {
cout << "Error: Mismatched parentheses\n";
}
}
else if (option == 2) {
break;
}
else {
cout << "Invalid option\n";
}
cout << endl;
}
return 0;
}
```
将上个代码改成c++语言
好的,以下是将 Python 版本的一元二次方程求解代码改为 C++ 版本的代码:
```cpp
#include <iostream>
#include <cmath>
using namespace std;
pair<double, double> solveQuadraticEquation(double a, double b, double c) {
double delta = b * b - 4 * a * c;
if (delta >= 0) {
double x1 = (-b + sqrt(delta)) / (2 * a);
double x2 = (-b - sqrt(delta)) / (2 * a);
return make_pair(x1, x2);
} else {
cout << "Warning: No real roots!" << endl;
return make_pair(NAN, NAN);
}
}
// 示例用法
int main() {
double a = 1;
double b = 2;
double c = 1;
auto roots = solveQuadraticEquation(a, b, c);
if (!isnan(roots.first)) {
cout << "The roots are: " << roots.first << " and " << roots.second << endl;
}
return 0;
}
```
这个 C++ 版本的代码和 Python 版本的代码实现是类似的,不过需要注意一些语言特有的细节,比如使用 `pair` 类型来返回两个实根,使用 `isnan` 函数来判断返回值是否是 NaN。