Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.30 KB | None | 0 0
  1. //
  2. //
  3. //
  4. //
  5.  
  6. #include <iostream>
  7. #include <iomanip>
  8. #include <string>
  9. #include <cstring>
  10. #include <cmath>
  11.  
  12. using namespace std;
  13.  
  14. const int NOT_FOUND = -1;
  15. const int MAX_LINE_LENGTH = 120;
  16. const int MAX_STOCKS = 6;
  17. const float TRANSACTION_FEE = 5.0;
  18.  
  19. class Stock
  20. {
  21. private:
  22. string name; // Name of the stock
  23. float price; // Price of the stock
  24. int shares; // Number of shares: neg means sold, pos means bought
  25.  
  26. public:
  27.  
  28. //-------------------------------------------------------------------
  29. // This method reads the name, price, and number of shares for this
  30. // stock from the standard input. It assumes the data are
  31. // separated by white space and in the correct format. It also
  32. // assumes name is a contiguous sequence of non-white characters.
  33. // A positive number for shares means the stock was bought, a
  34. // negative number means the stock was sold.
  35. // params: (none)
  36. //-------------------------------------------------------------------
  37. Stock()
  38. {
  39. name = "";
  40. price = 0.0;
  41. shares = 0;
  42. }
  43.  
  44. void Read()
  45. {
  46.  
  47. cin >> name >> price >> shares;
  48.  
  49.  
  50. }
  51.  
  52. //-------------------------------------------------------------------
  53. // This method returns the value of this stock: shares times price.
  54. // A positive number means the stock was bought, a negative number
  55. // means the stock was sold.
  56. // params: (none)
  57. //-------------------------------------------------------------------
  58. float Value() const
  59. {
  60. return (shares * price);
  61. }
  62.  
  63. //-------------------------------------------------------------------
  64. // This method prints the data for this stock in the format:
  65. // name (width of 10 characters)
  66. // shares (width of 6 characters)
  67. // price (width of 8 characters with 2 decimal places)
  68. // value (width of 10 characters with 2 decimal places)
  69. // followed by a newline.
  70. // params: (none)
  71. //-------------------------------------------------------------------
  72. void Print() const
  73. {
  74.  
  75. cout << fixed << setw ( 10 ) << name;
  76. cout << fixed << setw ( 6 ) << shares;
  77. cout << fixed << setw ( 8 ) << setprecision(2) << price;
  78. cout << fixed << setw ( 10 ) << setprecision(2) << Value();
  79. cout << endl;
  80.  
  81. }
  82.  
  83. //-------------------------------------------------------------------
  84. // This method compares the name of this stock to the name of
  85. // compareStock. It returns a negative number if the name of this
  86. // stock is lexicographically (dictionary order) less than the name
  87. // of compareStock, a positive number if it is greater, and 0 if
  88. // they are equal.
  89. // params: (in)
  90. //-------------------------------------------------------------------
  91. int CompareByName( const Stock & compareStock ) const
  92. {
  93. int ( name.compare((compareStock.name)));
  94.  
  95. }
  96.  
  97. //-------------------------------------------------------------------
  98. // This method updates this stock with the data from updStock and
  99. // returns any resulting profit.
  100. //
  101. // If the number of shares for this stock and updStock have the same
  102. // sign (both positive or both negative), then there is no profit
  103. // (just buying or selling more) and the price of this stock
  104. // is updated to be the weighted average of the prices.
  105. //
  106. // Otherwise, profit is the minimum of the absolute values of the
  107. // shares times the difference between the prices of updStock and
  108. // this stock. If the number of shares of this stock is negative,
  109. // the sign of the profit is reversed. The price of this stock is
  110. // updated to be the price of whichever of this stock or updStock
  111. // has the larger absolute value of shares.
  112. //
  113. // In all cases, the number of shares for this stock is updated by
  114. // adding in the number of shares for updStock.
  115. //
  116. // Note that a negative value for the profit indicates a loss.
  117. // params: (in)
  118. //-------------------------------------------------------------------
  119.  
  120. float Transaction( const Stock & updStock )
  121. {
  122. float profit =0; // your profit can be either negative or positive
  123. if ( shares < 0 && updStock.shares < 0 || shares > 0 && updStock.shares > 0 )
  124. {
  125. price = (shares * price + updStock.shares * updStock.price) /
  126. (shares + updStock.shares );
  127. }
  128. else if ( shares < 0 && updStock.shares > 0 || shares > 0 && updStock.shares < 0 )
  129. {
  130. if( abs( shares ) > abs(updStock.shares) )
  131. {
  132. price = (shares * price - updStock.shares * updStock.price) /
  133. (shares - updStock.shares );
  134.  
  135. }
  136. else if( abs( shares ) <= abs( updStock.shares) )
  137. {
  138. price = ( updStock.shares * updStock.price - shares * price) /
  139. (shares - updStock.shares );
  140. }
  141. }
  142. }
  143.  
  144. }; // Stock
  145. class Portfolio
  146. {
  147. private:
  148. float profit;
  149. int numStocks;
  150. Stock stocks[MAX_STOCKS];
  151.  
  152. // Finish the bodies of the private methods.
  153. // Remove the comments and replace them
  154. // with good “what” comments and do NOT
  155. // reference how they will be used.
  156. // That is for your benefit.
  157. // Make sure to remove this comment and
  158. // all other such commentary.
  159.  
  160. // Returns the index in stocks where stock with
  161. // the same name as findStock.name is found;
  162. // returns NOT_FOUND otherwise. But you can’t
  163. // access findStock.name! So
  164. // what Stock method do you have to use?
  165. int Find( const Stock & findStock ) const
  166. {
  167. for ( int i = 0; i < MAX_STOCKS; i++)
  168. {
  169. if (stocks[i].CompareByName(findStock))
  170. return i;
  171. }
  172. return NOT_FOUND;
  173. }
  174.  
  175. // Remove item from array at the specified index.
  176. // Shoves the ones below it up one.
  177. // You'll use this to delete a stock
  178. void Remove( int index )
  179. {
  180.  
  181. for (int i = index; i < MAX_STOCKS-1; i++)
  182. {
  183. stocks[i] = stocks[i + 1];
  184. }
  185.  
  186. }
  187.  
  188.  
  189. public:
  190.  
  191. // Make a default (no parameters) constructor.
  192. // What should it set the data to?
  193. Portfolio()
  194. {
  195. profit = 0.0;
  196. numStocks = 0;
  197. stocks[MAX_STOCKS];
  198. }
  199.  
  200.  
  201. // Here is a good method to have. It can’t print anything,
  202. // but it can do a bunch of heavy lifting for the T command.
  203. // It will return false if updStock isn’t in the list and
  204. // the list is full. Otherwise, it will behave as indicated
  205. // by the T command above and return true. It will make
  206. // use of the Transaction method in Stock as well as the
  207. // private Find and Remove methods. It will also update
  208. // the profit. Be sure to charge the $5 for each Transaction.
  209. // All this work and it can easily be written in under 20
  210. // lines!
  211. bool Transaction(const Stock & updStock)
  212. {
  213. if ((Find(updStock) == NOT_FOUND) &&
  214. (numStocks == MAX_STOCKS))
  215. return false;
  216. if ((Find(updStock) == NOT_FOUND) &&
  217. (numStocks < MAX_STOCKS))
  218. {
  219. stocks[numStocks] = updStock;
  220. numStocks++;
  221. profit -= TRANSACTION_FEE;
  222. }
  223. else
  224. {
  225. profit = profit - TRANSACTION_FEE +
  226. stocks[Find(updStock)].Transaction(updStock);
  227. }
  228. if (stocks[Find(updStock)].Value() == 0)
  229. {
  230. Remove(Find(updStock));
  231. return true;
  232. }
  233. }
  234.  
  235. int Print()
  236. {
  237.  
  238. }
  239. // Make other good methods (such as Print)
  240. // subject to the rules above.
  241.  
  242.  
  243.  
  244. }; // Portfolio
  245. int main()
  246. {
  247. Stock stock;
  248. Portfolio portfolio;
  249. char command;
  250. cin >> command;
  251. while (cin)
  252. {
  253. switch (command)
  254. {
  255. case 'T':
  256. stock.Read();break;
  257. case 'D':
  258. stock.Print();break;
  259. case 'V':
  260. {
  261. cin >> command;
  262. if ( command == 'P' )
  263. {
  264. stock.Print();
  265. break;
  266. }
  267. else
  268. cout <<"Not a valid value option.";
  269. }
  270. case 'Q':
  271. return 0;
  272. default:
  273. cout << "Bad command." << endl;break;
  274. }
  275. cin >> command;
  276. }
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement