2013-03-20 1 views
6

이 코드를 가지고 :입력 방정식, 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; 
} 

"X * X * XX-2"내 코드에서, 나는 간격의 시작을 요청하기 전에 그것을 입력에 사용자가 원하는 . 어떻게해야합니까?

변수를 사용하여 "x * x * x-x-2"를 저장하려고 시도했지만 아무 것도 작동하지 않습니다.

+1

muParser와 같은 표현식 평가 용 라이브러리가 필요합니다. –

+0

사용자가 함수를 정의하는 것은 컴파일 된 언어에서 그리 간단하지 않습니다. –

+0

possible dup http://stackoverflow.com/questions/9503455/equation-parsing-library-c –

답변

6

입력을 파싱해야합니다. 아마도 생각만큼 쉬운 것은 아니지만 도움이되는 라이브러리가 있습니다.

muparser.sourceforge.net/

code.google.com/p/expressionparser/

partow.net/programming/exprtk/index.html

여기

는 C#에서 해결책 너를 도울 수도있어.

Is there a string math evaluator in .NET?