wheelsmanx

CPS 271 Machine Problem 5

Apr 18th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <fstream>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. int isFirstLine = 0;
  11. string tempSymbol;
  12. double tempOpen;
  13. double tempClose;
  14. double tempHigh;
  15. double tempLow;
  16. double tempPrev;
  17. double tempPerc = 1;
  18. double tempNum;
  19.  
  20. template <class T>
  21. class stockListType {
  22. public:
  23. vector <T> stockList;
  24. stockListType(T tempStock) {
  25. stockList.push_back(tempStock);
  26. }
  27. stockListType() {}
  28. void add(T tempStock) {
  29. this->stockList.push_back(tempStock);
  30. }
  31. void printAllStocks() {
  32. for (T temp : this->stockList) {
  33. temp.printStock();
  34. }
  35. }
  36. void calForAll() {
  37. for (int i = 0; i < stockList.size(); i++) {
  38. stockList[i].calGAL();
  39. }
  40. }
  41. };
  42.  
  43.  
  44. class stockType {
  45. public:
  46. string symbol;
  47. double openPrice;
  48. double closePrice;
  49. double highPrice;
  50. double lowPrice;
  51. double prevPrice;
  52. double percentGainOrLoss;
  53. double numOfShares;
  54. void setStock(string sym, double open, double close, double high, double low, double prev, double per, double num) {
  55. this->symbol = sym;
  56. this->openPrice = open;
  57. this->closePrice = close;
  58. this->highPrice = high;
  59. this->lowPrice = low;
  60. this->prevPrice = prev;
  61. this->percentGainOrLoss = 0;
  62. this->numOfShares = num;
  63. }
  64. void printStock() {
  65. int widthInt = 10;
  66. cout << setw(widthInt) << left << this->symbol;
  67. cout << setw(widthInt) << left << this->openPrice;
  68. cout << setw(widthInt) << left << this->closePrice;
  69. cout << setw(widthInt) << left << this->highPrice;
  70. cout << setw(widthInt + 15) << left << this->lowPrice;
  71. cout << setw(widthInt) << left << this->prevPrice;
  72. cout << setw(widthInt) << left << this->numOfShares;
  73. cout << setw(widthInt) << left << this->percentGainOrLoss;
  74. cout << endl;
  75. }
  76. void showDifferentPrices() {
  77. cout << this->openPrice << " " << this->closePrice << " " << this->highPrice << " " << this->lowPrice << " " << this->prevPrice << endl;
  78. }
  79. void calGAL() {
  80. double percent = (this->closePrice - this->prevPrice) / this->prevPrice * 100;
  81. this->percentGainOrLoss = percent;
  82. }
  83.  
  84. bool operator=(const stockType& tempStock) {
  85. bool returnVariable = false;
  86. if (this->symbol == tempStock.symbol) {
  87. returnVariable = true;
  88. }
  89. else {
  90. returnVariable = false;
  91. }
  92. return returnVariable;
  93. }
  94. void operator << (const stockType& tempStock) {
  95. stockType *stockTypePointer = this;
  96. *stockTypePointer = tempStock;
  97. }
  98. void operator >> (stockType& tempStock) {
  99. stockType *stockTypePointer = this;
  100. tempStock = *stockTypePointer;
  101. }
  102. };
  103.  
  104.  
  105. void main() {
  106. ifstream fileInput; //here we are going to define the file that we are trying to stream
  107. fileInput.open("C:\\temp\\New Text Document.txt");
  108. stockListType <stockType> newStockList;
  109. while (!fileInput.eof()) {
  110. if (isFirstLine < 1) {
  111. string bufferString;
  112. fileInput >> tempSymbol >> bufferString >> bufferString >> bufferString >> bufferString >> bufferString >> bufferString;
  113. isFirstLine++;
  114. }
  115. else {
  116. fileInput >> tempSymbol >> tempOpen >> tempClose >> tempHigh >> tempLow >> tempPrev >> tempNum;
  117. stockType newStockType;
  118. newStockType.setStock(tempSymbol, tempOpen, tempClose, tempHigh, tempLow, tempPrev, tempPerc, tempNum);
  119. newStockList.add(newStockType);
  120. }
  121. }
  122. newStockList.calForAll();
  123. cout << setw(50) << "Financial Report" << endl;
  124. vector<string> BufferString;
  125. BufferString.push_back("SYMBOL");
  126. BufferString.push_back("OPEN");
  127. BufferString.push_back("CLOSE");
  128. BufferString.push_back("HIGH");
  129. BufferString.push_back("LOW");
  130. BufferString.push_back("PRE CLOSE");
  131. BufferString.push_back("SHARES");
  132. BufferString.push_back("G/L");
  133. int i = 0;
  134. for (string c : BufferString) {
  135. if (i != 4) {
  136. i++;
  137. cout << setw(10) << left << c;
  138. }
  139. else {
  140. i++;
  141. cout << setw(25) << left << c;
  142. }
  143. }
  144. cout << endl;
  145. newStockList.printAllStocks();
  146. system("pause");
  147. }
Advertisement
Add Comment
Please, Sign In to add comment