7

私はこのコードを持っています:

#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*xx-2」を格納するために変数を使用しようとしましたが、どれも機能しません。

4

1 に答える 1

7

入力を解析する必要があります。おそらくあなたが考えているほど簡単ではありませんが、役立つライブラリがいくつかあります。

muparser.sourceforge.net/

code.google.com/p/expressionparser/

partow.net/programming/exprtk/index.html

これもあなたを助けるかもしれないc#の解決策です。

.NET に文字列数学エバリュエーターはありますか?

于 2013-03-20T14:23:08.127 に答える