2013-10-18 86 views
1

我需要在主测试类中读取具有此格式(日期,ID,活动,数量,价格)数据的文件。我应该读取“日期”值,并将其存储为类型为日期的int日,月,年,随后是库存类的“ID,活动,数量,价格”。C++读入文件用逗号和空格分隔

02/08/2011, ABC, BUY, 100, 20.00 
05/08/2011, ABC, BUY, 20, 24.00 
06/08/2011, ABC, BUY, 200, 36.00 

我因此存储的值,以股票()构造,并通过所有的push_back存储数据()在我自己的“矢量”级。我必须添加/编辑什么,以便我可以检索Vector中的数据行并获取(日期,ID,活动,数量,价格)并计算数量和价格。

#include"Vector.h" 
#include"Date.h" 
#include"Stock.h" 
#include<iomanip> 
#include<fstream> 
#include<iostream> 
#include<string> 
using namespace std; 

int main(){ 

    string stockcode; 
    string act; 
    int qty; 
    double p; 
    int d,m,y; 
    char x; 

    //declare file stream variables 
    ifstream inFile; 
    ofstream outFile; 

    //open the file 
    inFile.open("share-data.txt"); 

    //code for data manipulation 
    cout << fixed << showpoint << setprecision(2); 

    while(!inFile.eof()){ 
    inFile>> d >> x >> m >> x >> y >> x 
    >> stockcode >> act >> qty >> x >> p; 

    Stock s1(d,m,y,stockcode,act,qty,p); 

    stockcode = stockcode.substr(0, stockcode.length()-1); 
act = act.substr(0, act.length()-1); 

Stock s1(d,m,y,stockcode,act,qty,p); 
stockList.push_back(s1); 

    } 

    inFile.close(); 

    system("PAUSE"); 
    return 0; 
} 

我有需要此任务,因为我不是允许使用的#include默认矢量

#include<iostream> 

using namespace std; 

template <class T> 
class Vector 
{ 

public: 

    Vector();    // default constructor 
    virtual ~Vector() {}; //destructor 
    bool isEmpty() const; 
    bool isFull() const; 
    void print() const; 
    void push_back(T); 
    T pop_back(); 
    T at(int i); 
    int Size(); 
    //Vector<T> operator +=(T); 

private: 
    int size; 
    T list[100]; 
}; 

template <class T> 
Vector<T>::Vector() 
{ 
    size = 0; 
} 

template <class T> 
Vector<T> Vector<T>::operator +=(T i) 
{ 
    this->push_back(i); 
    return *this; 
} 

template <class T> 
bool Vector<T>::isEmpty() const 
{ 
    return (size == 0); 
} 

template <class T> 
bool Vector<T>::isFull() const 
{ 
    return (size == 100); 
} 

template <class T> 
void Vector<T>::push_back(T x) 
{ 
    list[size] = x; 
    size++; 
} 

template <class T> 
T Vector<T>::operator[](T i) 
{ 
    return list[i]; 
} 

template <class T> 
T Vector<T>::at(int i) 
{ 
    if(i<size) 
     return list[i]; 
    throw 10; 
} 

template <class T> 
T Vector<T>::pop_back() 
{ 
    T y; 
    size--; 
    y= list[size]; 
    return y; 
} 

template <class T> 
void Vector<T>::print() const 
{ 
    for (int i = 0; i < size; i++) 
     cout << list[i] << " "; 
    cout << endl; 
} 

template <class T> 
int Vector<T>::Size() 
{ 
    return size; 
} 

,这里是我的股票类

#include"Date.h" 

    Stock::Stock() 
{ 
    stockID=""; 
    act=""; 
    qty=0; 
    price=0.0; 
} 

Stock::Stock(int d, int m , int y, string id,string a, int q, double p) 
:date(d,m,y){ 
    stockID = id; 
    act = a; 
    qty = q; 
    price = p; 

} 
. 
. 
. 

回答

0

用我自己的Vector类string::substr

act = act.substr(0, act.length()-1); 

您可以对其他字符串变量执行相同操作。

+0

好的!谢谢= D –