- 1、本文档共8页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
(精品课件)第二次作业(10.10评讲)
P84 Exercise 3.1 E2 E3(b)(e)
E2. Suppose that you are a financier and purchase 100 shares of stock in Company X in each of January, April, and September and sell 100 shares in each of June and November. The prices per share in these months were
Jan Apr Jun Sep Nov
$10 $30 $20 $50 $30
Determine the total amount of your capital gain or loss using
(a) FIFO (first-in, first-out) accounting and
(b) LIFO (last-in, first-out) accounting [that is, assuming that you keep your stock certificates in (a) a queue or (b) a stack]. The 100 shares you still own at the end of the year do not enter the calculation.
Answer Purchases and sales:
January: purchase 100* $10 = $1000
April: purchase 100 * $30= $3000
June: sell 100 *$20=$2000
September: purchase 100* $50 = $5000
November: sell 100* $30= $3000
With FIFO accounting, the 100 shares sold in June were those purchased in January, so the profit is $2000 - $1000 = $1000. Similarly, the November sales were the April purchase, both at $3000,so the transaction breaks even. Total profit = $1000.
With LIFO accounting, June sales were April purchases giving a loss of $1000 ($2000-$3000), and November sales were September purchases giving a loss of $2000 ($3000 - $5000). Total loss = $3000.
E3. Use the methods for stacks and queues developed in the text to write functions that will do each of the following tasks. In writing each function, be sure to check for empty and full structures as appropriate. Your functions may declare other, local structures as needed.
(b) Move all the entries from a Queue onto a Stack.
Error_code queue_to_stack(Stack s, Queue q)
/* Pre: The Stack s and the Queue q have the same entry type.
Post: All entries from q have been moved to s. If there is not enough room in s to hold all entries in q return a code of overflow, otherwise return success. */
{
Error_code outcome = success;
Entry item;
while (!q.empty( )) {
q.retrieve(item);
outcome=s.push(item);
if (outcome == overflow)
return outcome;
q.serve( );
}
ret
文档评论(0)