簡體   English   中英

C ++讀取inFile,以逗號和空格分隔

[英]C++ Read inFile separated by a comma and a space

我需要在主要測試課程中讀取包含此格式的數據(日期,ID,活動,數量,價格)的文件。 我應該讀取“日期”值並將其存儲到日期類型的int日,月,年中,然后存儲類的“ ID,活動,數量,價格”。

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

我將值相應地存儲到Stock()構造函數中,並通過push_back()將所有數據存儲在我自己的“ Vector”類中。 我必須添加/編輯什么,以便我可以檢索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;
}

我有我自己的Vector類,因此需要進行此分配,因為不允許使用#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;

}
.
.
.

使用string::substr

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

您可以對其他字符串變量執行相同的操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM