递归返回语句无法正确返回变量
Recursive Return Statement Does Not Return Variable Correctly
我很难正确返回变量。
我打印要返回返回语句上方的变量,看起来还不错。一旦我尝试将值返回并将其打印到控制台上,而不是打印-Nan(ind(。我不明白为什么会发生这种情况。
我使用Visual Studio在C 中编程。我正在使用此库将字符串解析为表达式:http://www.partow.net/programming/exprtk/index.html
这是打印结果的功能和语句:
#include "stdafx.h"
#include "exprtk.hpp"
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
typedef double T; // numeric type (float, double, mpfr etc...)
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
expression_t expression;
parser_t parser;
bool closeEnough(std::string value1, std::string value2, double levelOfSimilarity) {
if (abs( std::stod(value1) ) - abs (std::stod(value2) ) > levelOfSimilarity) {
return false;
}
else {
return true;
}
}
std::string replaceChars2Strings(std::string string, const std::string& start, const std::string& end) {
size_t init_pos = 0;
while ((init_pos = string.find(start, init_pos)) != std::string::npos) {
string.replace(init_pos, start.length(), end);
}
return string;
}
double FofX(std::string function, std::string value) {
std::string newfunction = replaceChars2Strings(function, std::string("x"), value);
if (!parser.compile(newfunction, expression))
{
printf("Something went wrong when the expression was being parsed");
}
T result = expression.value();
return result;
}
double DofFofX(std::string function, std::string value) {
std::string SDplus = replaceChars2Strings(function, std::string("x"), "(" + value + "+" + "0.00001" + ")");
std::string SDminus = replaceChars2Strings(function, std::string("x"), "(" + value + "-" + "0.00001" + ")");
if (!parser.compile(SDplus, expression))
{
printf("Something went wrong when Dplus was being parsed");
}
T Dplus = expression.value();
if (!parser.compile(SDminus, expression))
{
printf("Something went wrong when Dminus was being parsed");
}
T Dminus = expression.value();
return (Dplus - Dminus) / 0.00002;
}
double newton(std::string function, std::string guess) {
double guess2;
//std::cout << "guess:" << guess << std::endl;
//in here () are taken off so that the compiler can calculate the value of guess 2 easier
guess2 = std::stod(guess.substr(1, guess.size() - 2)) - FofX(function, guess) / DofFofX(function, guess);
//std::cout << "guess 2:" << guess2 << std::endl;
//take the () off of guess before we give it away
if (closeEnough(guess.substr(1, guess.size() - 2), std::to_string(guess2), 0.001)) {
std::cout << "final guess = " << guess2 << std::endl;
return guess2;
}
else {
//put the () back on before we give it away so that the parser can read things as multiplication right
newton(function, "(" + std::to_string(guess2) + ")");
}
}
int main()
{
std::string function = "x*x";
//remember to put () around guess
std::string guess = "(5)";
double answer = newton(function, guess);
return 0;
}
当此程序运行时,它会打印以下内容:
final guess = 0.0006105
solution = -nan(ind)
有人对我打印最终猜测与打印解决方案之间正在发生的事情有一个想法吗?
问题是我没有在其他语句中返回递归函数。
最终代码看起来像这样:
// Newtons Method V1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "exprtk.hpp"
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
typedef double T; // numeric type (float, double, mpfr etc...)
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
expression_t expression;
parser_t parser;
bool closeEnough(std::string value1, std::string value2, double levelOfSimilarity) {
if (abs( std::stod(value1) ) - abs (std::stod(value2) ) > levelOfSimilarity) {
return false;
}
else {
return true;
}
}
std::string replaceChars2Strings(std::string string, const std::string& start, const std::string& end) {
size_t init_pos = 0;
while ((init_pos = string.find(start, init_pos)) != std::string::npos) {
string.replace(init_pos, start.length(), end);
}
return string;
}
double FofX(std::string function, std::string value) {
std::string newfunction = replaceChars2Strings(function, std::string("x"), value);
if (!parser.compile(newfunction, expression))
{
printf("Something went wrong when the expression was being parsed");
}
T result = expression.value();
return result;
}
double DofFofX(std::string function, std::string value) {
std::string SDplus = replaceChars2Strings(function, std::string("x"), "(" + value + "+" + "0.00001" + ")");
std::string SDminus = replaceChars2Strings(function, std::string("x"), "(" + value + "-" + "0.00001" + ")");
if (!parser.compile(SDplus, expression))
{
printf("Something went wrong when Dplus was being parsed");
}
T Dplus = expression.value();
if (!parser.compile(SDminus, expression))
{
printf("Something went wrong when Dminus was being parsed");
}
T Dminus = expression.value();
return (Dplus - Dminus) / 0.00002;
}
double newton(std::string function, std::string guess) {
double guess2;
//std::cout << "guess:" << guess << std::endl;
//in here () are taken off so that the compiler can calculate the value of guess 2 easier
guess2 = std::stod(guess.substr(1, guess.size() - 2)) - FofX(function, guess) / DofFofX(function, guess);
//std::cout << "guess 2:" << guess2 << std::endl;
//take the () off of guess before we give it away
if (closeEnough(guess.substr(1, guess.size() - 2), std::to_string(guess2), 0.00001)) {
std::cout << "final guess = " << guess2 << std::endl;
return guess2;
}
else {
return newton(function, "(" + std::to_string(guess2) + ")");
}
}
int main()
{
std::string function = "2^x - x^2";
//remember to put () around guess
std::string guess = "(-2)";
double answer = newton(function, guess);
std::cout << answer << std::endl;
return 0;
}
相关文章:
- 如何返回变量而不是变量的数值
- 函数,返回变量c++占用的内存位置的大小
- C++中局部变量和返回变量的构造和析构
- 我们如何在范围消失时通过引用返回变量
- 沿枚举 c++ 返回变量类型
- 在 Py_BuildValue 中返回变量字符串
- 递归返回语句无法正确返回变量
- 返回变量为 void (*)(void)
- 地址运算符是否返回变量引用的对象的地址
- 从函数返回变量地址时如何修复"与局部变量关联的堆栈内存地址"?
- 如何访问Python中编译的多个返回变量
- Binary_search通过其成员函数的返回变量[C ]找到类对象
- C++函数中声明和返回变量,并在主函数中使用它
- 从 C++ 返回对对象的引用时,是否需要在返回变量中使用 "&" 运算符?
- 函数返回看似随机的数字,而不是返回变量的值
- 从函数返回变量的链表
- 通过函数参数中的引用返回变量
- C++动态内存分配.将动态分配的 char* 返回变量数组分配给 char 变量
- 传递或返回变量时并不总是调用复制构造函数
- 尝试在模板 c++ 中返回变量时出错