作者 主題: 可以help me?不知為何都沒輸出  (閱讀 3395 次)

0 會員 與 1 訪客 正在閱讀本文。

mardengen

  • 可愛的小學生
  • *
  • 文章數: 1
    • 檢視個人資料
可以help me?不知為何都沒輸出
« 於: 2011-10-11 20:40 »
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <fstream>
using namespace std;

class Vector
{
public:
    Vector ():dim(0) {}  //constructor1
    Vector (int d):dim(d) //constructor2
    {
        component = new int[d];
        for (int i=0; i< d; ++i)
            component=0;
    }
    Vector(const Vector & v):dim(v.dim) //constructor3
    {
        component = new int[dim];
        for (int i=0; i< dim; ++i)
            component=v.component;
    }
    ~Vector() {delete [] component;}
    int & operator [] (int i)
    {
        if (i >= dim)
            {cout << "Out of bound" << endl;
            system("PAUSE");
            exit(-1);
        }
        return component;
    }
    int * component;
    const int dim;
int getDim();
double getLength(Vector v);
Vector getUnitVector(Vector v);
Vector scalarProduct( double,Vector  );
Vector operator * ( double );
Vector addVector( Vector );
Vector operator + ( Vector );
Vector subtractVector( Vector );
Vector operator - ( Vector );
double operator * ( Vector );
};

void outputVector(Vector v)
{
    cout << "(";
    for (int i=0;i < v.dim-1; ++i)
        cout << v.component << ", ";
    cout << v.component[v.dim-1];
    cout << ")\n" ;
}

int dotProduct(Vector a1, Vector b1)
{
    Vector u1(50000000);

    if (a1.dim != b1.dim) {
        cout << "Error: vectors of diff dim\n";
        system("PAUSE");
        exit (-1); // brutal
    }
    else {
        int dp =0;
        for (int i=0; i< a1.dim; i++)
        dp += a1.component*b1.component;
        return dp;
    }
}

int main(int argc, char *argv[])
{
    Vector v1(3); // 3d zero vec
//    outputVector(v1);
    v1.getLength(v1);
    Vector v2(10); // 3d zero vec
//    outputVector(v2);
//    outputVector(v2);
//    outputVector(v2);
//    outputVector(v2);
//    outputVector(v2);
//    outputVector(v2);
//    outputVector(v2);
//    outputVector(v2);
//    outputVector(v2);

    Vector a1(2);
    a1.component[0] = 1;
    a1.component[1] = 0;

//    outputVector(a1);

    Vector b1(2);
    b1.component[0] = 1;
    b1.component[1] = 1;

//    outputVector(b1);
//    cout << dotProduct(a1,b1) << endl;

    Vector c1(3);
    c1.component[0] = 1;
    c1.component[1] = 0;
    c1.component[2] = 1;

    for (int i=0; i<10; ++i)
    {
  //      cout << dotProduct(a1,b1) << endl;;
        system("PAUSE");
    }

//    outputVector(c1);
    cout << dotProduct(a1,c1) << endl;;

    cout << v1.component[3] << endl;
    cout << v1[3] << endl;
    cout<<v1.dim;//<<v2.getDim()<<a1.getDim()<<b1.getDim()<<c1.getDim();
    //not work
  //  getLength();
  //  getUnitVector();
  //  scalarProduct( double );
  //  operator * ( double );
  //  addVector( Vector );
  //  operator + ( Vector );
  //  subtractVector( Vector );
  //  operator - ( Vector );
  //  operator * ( Vector );

    system("PAUSE");
    return EXIT_SUCCESS;
}

int Vector::getDim()
{

    return this->dim;

    }
double Vector::getLength(Vector v)
{
      double length=0.0;
      int sum;
      for (int i=0;i < v.dim-1; ++i)
      sum=sum+((v.component)^2);
      length=sqrt((double)(sum));
      return length;
    }
Vector Vector::getUnitVector(Vector v)
{
     Vector unit;
        for (int i=0;i < v.dim-1; ++i)
        unit.component=(double)(v.component)/v.getLength(v);
     return unit;
}

Vector Vector::scalarProduct( double scalar,Vector v)
{
    Vector s_v;//vector after scalar
    double _scalar=scalar;
       for (int i=0;i < v.dim-1; ++i)
      //    s_v.component=v.component*_scalar;
    return s_v;
}

Vector Vector::operator * (double scalar)
{


      return Vector();//return a constructor but We don't have this constructor so use traditional method


}

//const Vector Vector::operator * (  Vector& v1,Vector v2)
//{
//    if (v1.dim != v2.dim) {
//        cout << "Error: vectors of diff dim\n";
//        system("PAUSE");
//        exit (-1); // brutal
//
//
//    }
Vector Vector::addVector( Vector )
{



    }
Vector Vector::operator + ( Vector )
{

;

    }
Vector Vector::subtractVector( Vector )
{

;

    }
Vector Vector::operator - ( Vector )
{

;

    }
double Vector::operator * ( Vector )
{
;


    }

elleryq

  • 區域板主
  • 鑽研的研究生
  • *****
  • 文章數: 908
  • 性別: 男
    • 檢視個人資料
    • Thinking more...
Re: 可以help me?不知為何都沒輸出
« 回覆 #1 於: 2011-10-13 11:38 »
1. 你程式有問題,拿下來以後還要改才能執行...  :(  很多都是使用 component 的問題,component 不是配置以後就可以直接 component 這樣用的,記得要加 [],例如存取第1個元素,是 component[0]...
2. 問題出在 getLength,在執行到那個點的時候,v.component 是 null,然後就死掉了...

Plan your work, then work your plan.
我的首頁:http://blog.elleryq.idv.tw
351899by http://counter.li.org