如何在函数中使用数组元素,它由vector传递

时间:2014-02-04 16:41:54

标签: c++ arrays class methods vector

嗨我有一个方程式在类(用方法)中“制作”并传递给向量。我在这个等式中有数组元素。然后我必须在元素数组参数的另一个函数中使用这个等式。我是C ++的新手,现在已经学习了几个星期。

这是我的代码:

#include <iostream>
#include <cmath>


using namespace std;
const double Pi = 3.14159;
const double Ro = 1000;
const double v = 5.0;
const double V = 5.0;
const int P=7;

double equation[P];
double x[P];
double equationE;
vector<double> eqP;
class object
{
public:
object() {}
virtual ~object (){}

virtual void Calc(){}
virtual void SetParam(){}
};


class p : public object
{
   protected:
        double d;
        double min;
        double max;
        double ksi_m;
        double l;

    public:
       p(double diam, double leng,double z_begin, double z_end, double ksi_loc)
       { d=diam;
         l=leng;
         z_begin=min;
         z_end=max;
         ksi_loc=ksi_m;}
    virtual ~p(){};


    double Re; double ka; double lambda;
    virtual void Calc()
       {
           Re=((d*v)/V);
           ka=(Pi*pow(d,2.0)/(4.0*Ro*l));
           if (Re < 2300.0)
           lambda = (64.0/Re);
           else
           lambda = (0.3164/(pow(Re,0.25)));
        }

        virtual void SetParam()
        {   equationE = (-0.5*(x[0])*Re*ka);
            eqP.push_back(equationE);
            cout << ka << "\t" << lambda << "\t" << Re <<"\t";}



};

Inportant part是第二种方法(virtual void SetParam),我的方程式应该被创建并传递给vector。当我尝试在我的main函数中使用它并将更改值添加到x [0]时它没有正确计算时,术语x [0]实际上被认为是0并且在声明x [0] = 1之后它总是1。这是我的主要功能:

#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>
#include <stdio.h>
#include "Elementys.h"
using namespace std;

void f(double t, double x[P]);
int main ()
{
int N;
vector<object>PotVect;

for (N=0; N<6;N++)
{
object *point_pot = new p(5.0,5.0,5.0,5.0,5.0);   //there will be allways another        
                                                      // input from text file
    point_pot -> Calc();
point_pot -> SetParam();
point_pot -> vypis();
PotVect.push_back(*point_pot);

}
int i,j;
int T = 5;
double h = 0.1;
int ch=((1/h)+1);
double t[ch*T];
t[0] = 0;
x[0] = 1;
x[1] = 1;
x[2] = 1;
x[3] = 0;
x[4] = 0;
x[5] = 0;
x[6] = 0;

ofstream output;
output.open ("outputgraph1.txt");

for(i = 0; h*(i-1)< T; i++)
{

    double x_ar[P];
    vector<double> k1,k2,k3,k4, xnew;


    f(t[i],x);
    for(j = 0; j < P; j++)
    {   k1.push_back(h * equation[j]);
        x_ar[j] = x[j] + 0.5*k1[j];}


    f(t[i] + 0.5 * h, x_ar);
    for(j = 0; j < P; j++)
    {   k2.push_back(h * equation[j]);
        x_ar[j] = x[j] + (0.5 * k2[j]);}



    f(t[i] + 0.5 * h, x_ar);
    for(j = 0; j < P; j++)
    {   k3.push_back(h * equation[j]);
        x_ar[j] = x[j] + k3[j];}


    f(t[i] + h, x_ar);
    for(j = 0; j < P; j++)
    {k4.push_back(h * equation[j]);}


    for(j = 0; j < P; j++)
    {xnew.push_back(x[j] + ((k1[j] + 2*k2[j] + 2*k3[j] + k4[j])/6));}


    cout <<  t[i] << "\t";
    cout <<  x[0] << "\t\t";
    cout <<  x[1] << "\t";
    cout <<  x[2] << "\t";
    cout <<  x[3] << "\n";


    output << t[i] << "\t";
    output << x[0] << "\t\t";
    output << x[1] << "\t\t";
    output << x[2] << "\t";
    output << x[3] << "\n";


    x[0] = xnew[0];
    x[1] = xnew[1];
    x[2] = xnew[2];
    x[3] = xnew[3];
    x[4] = xnew[4];
    x[5] = xnew[5];
    x[6] = xnew[6];


    t[i+1] = t[i]+h;


    k1.clear();
    k2.clear();
    k3.clear();
    k4.clear();
    xnew.clear();

output.close();
}
return 0;
}


void f(double t, double x[P])
{
    equation[0] = eqP[0];
equation[1] = 0.5*(x[0])-0.25*(x[1]);     //there will be eq. passed by vectors too
equation[2] = 0.25*(x[1])-0.1666*(x[2]);
equation[3] = 0;
equation[4] = 0;
equation[5] = 0;
equation[6] = 0;
}

所以我的问题是:如何声明vector eqP成功包含我需要在函数void f中使用的数组元素x [0]。 谢谢你的帮助,我再说一遍,我是C ++的新手,所以也许代码中有一些愚蠢的东西(我相信你可以用更好的方式写出来)。

0 个答案:

没有答案