gpt4 book ai didi

c++ - 递归返回语句没有正确返回变量

转载 作者:行者123 更新时间:2023-11-28 01:49:46 25 4
gpt4 key购买 nike

我无法让我的函数正确返回变量。

我在 return 语句上方打印了我想要返回的变量,看起来不错。一旦我尝试返回该值并将其打印到控制台上,尽管它会打印 -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)

有人知道我打印最终猜测和打印解决方案之间发生了什么吗?

最佳答案

问题是我没有在 else 语句中返回递归函数。

最终代码如下所示:

// 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++ - 递归返回语句没有正确返回变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43444144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com