// Assignment 10 Solution #include #include #include #include #include using namespace std; // Conditional compilation (Code::Blocks or MS Visual Studio) #if defined(__MINGW32__) || defined(_MSC_VER) const string Path = "C:\\temp\\"; const string EraseCommand = "erase "; #else const string Path = "./"; const string EraseCommand = "rm "; #endif void convertCSV_file(const string&, const string&); void getInputData(const string& filename, string* dates, float* open, float* close/*, float* gain*/); int getUpDaysForPeriod(int numdays,const float* open, const float* close); int getDownDaysForPeriod(int numdays, const float* open, const float* close); float getMaxCloseForPeriod(int numdays,const float* close); float getAvgCloseForPeriod(int numdays,const float* close); void processData(int numdays,const string* dates,const float* open, const float* close, ostream& out); const int ArraySize = 1000; int main() { const string StockTicker = "WMT"; const string originalInputFile = Path + StockTicker + ".csv"; const string modifiedInputFile = Path + StockTicker + "_modified.txt"; const string outputFile = Path + StockTicker + "_report.txt"; string dates[ArraySize]; float open[ArraySize]; float close[ArraySize]; cout << "Do you want to print to a file or to the screen (Enter F for file, S for screen) => "; char outputPreference; cin >> outputPreference; ofstream fout(outputFile); if (!fout) { cerr << "Unable to open output file " << outputFile << endl; exit(5); } ostream& out = (outputPreference == 'F') ? fout : cout; convertCSV_file(originalInputFile,modifiedInputFile); getInputData(modifiedInputFile,dates,open,close); out << setprecision(2) << fixed; out << "Stock: " << StockTicker << endl << "Current Date: " << dates[0] << endl << "Current Close: $" << close[0] << endl; out << "Market Days Start Date Open Price Up Days Down Days Gain Pct Gain Max Close Avg Close" << endl; out << "----------- ---------- ---------- ------- --------- ---- -------- --------- ---------" << endl; processData(5,dates,open, close,out); processData(10,dates,open, close,out); processData(20,dates,open, close,out); processData(50,dates,open, close,out); processData(100,dates,open, close,out); processData(200,dates,open, close,out); processData(500,dates,open, close,out); processData(1000,dates,open, close,out); // Erase modified file string command = EraseCommand + modifiedInputFile; cout << command << endl; system(command.c_str()); } void convertCSV_file(const string& in, const string& out) { ifstream fin(in.c_str()); char ch; if (!fin) { cerr << "Unable to open original input file: " << in << endl; exit(1); } ofstream fout(out.c_str()); if (!fout) { cerr << "Unable to open modified input file: " << out << endl; exit(2); } while ((ch = fin.get())) { if (fin.eof()) break; if (ch == ',') fout << ' '; else fout << ch; } fin.close(); fout.close(); } void getInputData(const string& filename, string dates[], float* open, float* close) { ifstream fin(filename.c_str()); string dummy; if (!fin) { cerr << "Unable to open modified input file: " << filename << endl; exit(3); } // get rid of headings while ((fin.get() != '\n')) ; for (int i = ArraySize - 1; i >= 0; i--) { fin >> dates[i] >> open[i] >> dummy >> dummy >> close[i] >> dummy >> dummy; if (fin.fail()) { cerr << "File read error on record " << i << endl; exit(4); } } fin.close(); } int getUpDaysForPeriod(int numDays,const float* open, const float* close) { int numberUp = 0; for (int i = 0; i < numDays; i++) if (open[i] < close[i]) numberUp++; return numberUp; } int getDownDaysForPeriod(int numDays, const float* open, const float* close) { int numberDown = 0; for (int i = 0; i < numDays; i++) if (open[i] > close[i]) numberDown++; return numberDown; } float getMaxCloseForPeriod(int numDays, const float* close) { float maxClose = 0; for (int i = 0; i < numDays; i++) if (close[i] > maxClose) maxClose = close[i]; return maxClose; } float getAvgCloseForPeriod(int numDays, const float* close) { float totalClose = 0; for (int i = 0; i < numDays; i++) totalClose += close[i]; return totalClose/numDays; } void processData(int numdays,const string* dates, const float* open, const float* close, ostream& out) { float openAtStart=open[numdays-1]; float currentClose = close[0]; float gain = currentClose - openAtStart; out << setw(11) << numdays << setw(12) << dates[numdays-1] << setw(10) << open[numdays-1] << setw(9) << getUpDaysForPeriod(numdays,open,close) << setw(10) << getDownDaysForPeriod(numdays,open,close) << setw(10) << close[0] - openAtStart << setw(9) << gain/openAtStart << '%' << setw(11) << getMaxCloseForPeriod(numdays,close) << setw(11) << getAvgCloseForPeriod(numdays,close) <