Ich habe diesen Code:Eingangs Gleichung in Bisektionsmethode, C++
#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;
double f(double x);
double biseccion(double a, double b, double tolerancia, int maxiter);
int main()
{
double a, b, raiz;
double tolerancia=0.00000;
int maxiter=25;
cout << "Input begin of interval: ";
cin >> a;
cout << "Input end of interval: ";
cin >> b;
cout << "\n";
cout << " # de"<<"\n"<<"Iteration"<<"\t"<<" A"<<"\t"<<" B"<<"\t"<<" C"<<"\t"<<" f(c)"<<endl;
raiz=biseccion(a,b,tolerancia,maxiter);
cout << "\n";
cout << "The root is: "<< raiz <<endl;
return 0;
}
double f(double x)
{
return x*x*x-x-2;
}
double biseccion(double a, double b, double tolerancia, int maxiter)
{
double c;
int numiter=1;
do
{
c=(a+b)/2;
if(f(a)*f(c)<0)
{
b=c;
}
else
{
a=c;
}
cout<<" "<<numiter<<"\t"<<"\t"<<a<<"\t"<<b<<"\t"<<c<<"\t"<<f(c)<<endl;
numiter++;
}
while((abs(f(c))>tolerancia)&&(numiter<maxiter));
return c;
}
Statt zu schreiben "x * x * xx-2" in meinem Code, ich möchte Benutzer die Eingabe es vor Beginn des Intervalls zu fragen . Wie kann ich das machen?
Ich versuche, eine Variable zu verwenden, um die "x * x * x-x-2" zu speichern, aber keine Arbeit.
Sie erhalten eine Bibliothek zur Auswertung von Ausdrücken, wie muParser wollen. –
Einen Benutzer zu definieren, ist nicht so einfach, besonders in einer kompilierten Sprache. –
möglich dup http://stackoverflow.com/questions/9503455/equation-parsing-library-c –