需要一些C ++项目的帮助! [英] Need some help with C++ project!

查看:71
本文介绍了需要一些C ++项目的帮助!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们想写两个容器。一个是保存每日数据,另一个是保留第一个容器类型 - 索引(代码)数据。



这意味着整个文件将被保存为第一个容器中的行 - 每行代表第一个容器中的元素。例如,如果文件有10行,则第一个容器将有10个元素。第二个容器将用于保存第一个容器类型的容器。例如,如果我有4个索引行,我的第二个容器将包含4个元素。这意味着第二个容器是容器的容器。

Double Top是价格形成模式。在市场的价格与时间图表上,它似乎是两个连续的峰值,价格大致相同。这两个峰值以最小的价格分隔,一个山谷。这个最小值的价格水平称为地层的颈线。编队完成并在价格低于颈线时确认。



我们必须执行以下操作:

您要开启names.csv文件,读取一行并添加.csv以生成文件名。例如,qqq将从names.csv中的一行读取。 names.csv的样本数据如下:



qqq msft aapl amzn



一旦qqq.csv如果创建了文件名字符串,您将尝试在... / Projects / final_project / data_files中找到qqq.csv文件并读取数据。来自其中一个csv文件的示例数据如下:



日期,调整关闭,交易量

3/20 / 2017,129.719299 ,118404300

3/27 / 2017,131.457108,91838100

4/3 / 2017,131.049942,104686800

4/10/2017, 129.490891,89035900

4/17 / 2017,131.635834,88364200

4/24 / 2017,135.041916,103641400

5/1/2017 ,136.5811,100949000

5/8 / 2017,137.633743,109176100

5/15 / 2017,136.879028,175959500

5/22 / 2017,140.235474,92869300

5/29 / 2017,142.459854,110879400



需要逐行读取数据到STL容器中。每行代表第一个容器中的每个元素。创建自己的类型时,比如数据,使用string,double和long来保存每日数据。因此,您需要将STL容器定义为矢量v。您需要为类型Data重载给定的运算符。运营商a

- 将整行写入文件 - 从文件>中读取整行,< - 基于日期== - 等于运算符的比较运算符来测试两个数据对象的价格是否相同[]运算符访问您的容器和复制上述类型的构造函数。

现在,创建一个容器到那个具有Data类型的向量。接下来,根据adj close列计算是否发生了双顶。将此文件写入调用qqq-final.csv的新文件,包括发生了正好最大值的位置。跟踪发生双顶的文件名。完成所有文件处理后,将结果写入status.csv中的symbol,YES / NO - 1. YES表示在此文件中创建的双顶。 2.否则为否。我们需要确保将每日数据保存在Data容器中,并将所有文件保存在DataContainer中。



以下是我到目前为止所做的,可以使用一些帮助。



我的尝试:



We want to write two containers. One is to keep daily data and other one is to keep the first container type -- index (tickers) data.

This means that entire file will be saved line by line in first container -- each line representing an element in first container. For example, if a file has 10 lines, first container will have 10 elements. Second container will be used to keep containers of first container type. For example, if I have 4 index lines, my second container will contain 4 elements. This means that second container is a container of containers.
Double Top is a price formation pattern. It appears as two consecutive peaks of approximately the same price on a price-versus-time chart of a market. The two peaks are separated by a minimum in price, a valley. The price level of this minimum is called the neck line of the formation. The formation is completed and confirmed when the price falls below the neck line.

We have to do the following:
You are to open names.csv file, read a line and add .csv to make a filename. For example, qqq will be read from one of the lines in names.csv. sample data for names.csv is given below:

qqq msft aapl amzn

Once qqq.csv is filename string is created, you would make an attempt to locate qqq.csv file in …/Projects/final_project/data_files and read the data. Sample data from one of the csv file is given as follows:

Date,Adj Close,Volume
3/20/2017,129.719299,118404300
3/27/2017,131.457108,91838100
4/3/2017,131.049942,104686800
4/10/2017,129.490891,89035900
4/17/2017,131.635834,88364200
4/24/2017,135.041916,103641400
5/1/2017,136.5811,100949000
5/8/2017,137.633743,109176100
5/15/2017,136.879028,175959500
5/22/2017,140.235474,92869300
5/29/2017,142.459854,110879400

Need to read the data into STL container line by line. Each line representing each element in your first container. When creating your own type, say Data, with string, double, and long to hold the daily data. Thus, you need to define STL container as, say vector v. You need to overload given operators for type Data. The operators a
-- write in the entire line to a file -- read in the entire line from a file >, < -- comparison operators based on date == -- equal operator to test whether prices are same for two data objects [ ] operator to access your container and copy constructor for this above type.
Now, create a container to that have vector of type Data. Next, calculate if double top has occurred based on adj close column. Write this to new file calling qqq-final.csv including where exactly top double has occurred. Keep track of filenames where double top has occurred. Once done with all file processing, write your results into status.csv in "symbol,YES/NO" -- 1. YES representing double top created in this file. 2. NO for otherwise. We need to make sure to keep daily data in Data container and keep all the files in DataContainer.

Below is what I have done so far, could use some help.

What I have tried:

using namespace std;

Class Data {

private:
string date;
long long volume;
double close;
public:

virtual Data::~Data() {};
Data() {};
Data(string stockInfo);


void setDate(const string dates);

void setVolume(const long vol);

void setClose(const double _close);

string getDate() const;

long getVolume() const;

double getClose() const;

bool operator <(const Data& d2);
bool operator >(const Data& d2);
bool operator ==(const Data& d2);
Data operator =(Data& right);

friend ostream& operator << (ostream&, const Data&);
friend istream& operator >> (istream&, Data&);


};
Data::Data(string filePath) {
ifstream fileNames(filePath);

//make vector
vector<data> dataList;
string line;
char *token = nullptr;
int index;

//inputing file into vector
while (!fileNames.eof()) {

    getline(fileNames, line);
    stringstream dataStream(line);
    string cell;

    while (getline(dataStream, cell, ',')) {
        Data d(line);
        dataList.push_back(d);
    }

    dataStream >> date >> close >> volume;

    cout << dataList.size() << " elements in container" << endl;
 }
  fileNames.close();

 //Sort data from lowest close to hightest
 sort(dataList.begin(), dataList.end(), [](Data &d1, Data &d2) {
    return d1.getClose() > d2.getClose();
 });

 };


 void Data::setDate(const string dates) {
 date = dates;
 }

 void Data::setVolume(const long vol) {
 volume = vol;
 }

 void Data::setClose(const double _close) {
 close = _close;
 }

 string Data::getDate() const {
 return(date);
 }

 long Data::getVolume() const {
 return(volume);
 }

 double Data::getClose() const {
 return (close);
 }

 bool Data::operator <(const Data& d2) {
 if (date < d2.date)
    return true;
 return false;
}

 bool Data::operator >(const Data& d2) {
 if (date > d2.date)
    return true;
 return false;
 }

 bool Data::operator ==(const Data& d2) {
   if (close == d2.close)
    return true;
 return false;
 }

 Data Data::operator =(Data& right) {
 if (this != &right) {
    date = right.date;
 }
 return *this;
 }

 ostream& operator << (ostream& stream, const Data& obj) {
 stream << "Date: " << obj.date << " Closing Cost: " << obj.close << " 
 Volume: " << obj.volume << endl;
 return stream;
 }

 istream& operator >> (istream& strm, Data& obj)
 {
 cout << "Enter date: " << " ";
 strm >> obj.date;
 cout << endl;

 cout << "Enter closeing cost: " << " ";
 strm >> obj.close;
 cout << endl;

 cout << "Enter volume: " << " ";
 strm >> obj.volume;
 cout << endl;

 return strm;}

 class dataContainer : Data {
 public:

 virtual ~dataContainer() {};
 dataContainer() {};
 dataContainer(vector<data>);
 Data getDoubleTop();



 private:
 vector<data>vData;

 }; 

 string getFilename(string);

 int main() {

 string filename = "names.csv";
 Data data1(filename);
 vector<data> vd1;
 dataContainer dC1(vd1);
 //cout << "\n" << dC1.getDoubleTop();
 cout << vd1.size();




 system("Pause");
 return 0;}

string getFilename(string fileName) {

ofstream namesFile; 

namesFile.open(fileName);

namesFile << "QQQ\n";

namesFile << "MSFT\n";

namesFile << "APPL\n";

namesFile << "AMZN\n";

 namesFile.close();

 string dat;
 return dat;
 }</data></data></data></data>

推荐答案

你必须更好地理解使用的类的功能。使用getline,您可以阅读数据格式分隔符。所以重复调用3次,但最后一次使用换行符。

You must better understand the functions of the used classes. With getline you read to your data format delimeter. So repeat the call 3 times, but the last with a newline.
getline(dataStream, date, ',');
getline(dataStream, close, ',');
getline(dataStream, volume, '\n');
//Than you have the parts to construct your object. 
Data d(data,close,volume);

存储一些索引以同步文件之间的数据是有意义的。通常,分割属于一起的数据是个不错的主意。



学习使用调试器并进入代码以学习编码。一个很好的教程是学习C ++ ,第1.11章是解释的调试过程。

It would make sense to store some index to syncronize the data between the file. Normally it isnt a good idea to split data which belongs together.

Learn to use the debugger and step into your code to learn coding. A good tutorials is Learn C++ and in chapter 1.11 is the debugging process explained.


这篇关于需要一些C ++项目的帮助!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆