Longest held Rule

Hi Everyone
I done a project in C++ where I have to deal with stocks buying and selling in stock exchange. In question, they have asked that when calculating total capital amount for one year in buying and selling should follow the longest held rule.
Input data format
dd/mm/yyyy,share_code,activity,number_of_shares,price_of_one_share,
Sample data is

02/08/2011,ABC,buy,100,20.00,
05/08/2011,ABC,buy,20,24.00,
06/08/2011,ABC,buy,200,36.00,
15/08/2011,ABC,sell,150,30.00,
19/10/2011,ABC,sell,40,35.50,
03/11/2011,ABC,buy,100,27.25,



The longest held rule:
As an example, suppose the user buys 100 ABC shares at 
$20 on a particular day 1, 
20 shares at $24 on a day 2,
 200 shares at 36 on day 3 
and then sells shares at day 4 at $30 each.

Applying the longest held rule means that 
of the 150 shares sold on day 4, 100 were bought on day 1, 
20 were bought on day 2, 
and 30 were bought on day 3. 

To calculate capital gain or loss, 
the price differential (i.e sale price-buy price) is multiplied by the corresponding number of shares. 
For this example, the calculation is 
(100*(30-20)+20*(30-24)+30*(30-36)), 
giving a capital gain of $940. 



What I have done so far(its quite lengthy sorry for that)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

#include<fstream>
#include<iostream>
#include<string>
#include<iomanip>

using namespace std;
struct data
{
	string activity;
	int shares;
	double price;
};

int main()
{
	ifstream in("losstestfile.txt");
	const int length=28;
	data tx[length];
	for(int i=0;i<length;i++)
	{
		in>>tx[i].activity>>tx[i].shares>>tx[i].price;
	}
	int start_index=0;
	int sell_index,share_hold=0,last_hold_index=0;
	int sell_count=0;
	double total=0,sold_amount=0,buy_amount=0;
	bool found;
	int share_left;
	while(start_index<length)
	{
		for(int i=start_index;i<length;i++)
		{			 
			if(tx[i].activity=="sell")
			{
				sell_index=i;
				sell_count+=tx[i].shares;
				found=true;
				break;
			}
			else
				found=false;			
		}		
		if(found==true)
		{
			if(start_index!=sell_index)
			{
				for(int i=start_index;i<sell_index;i++)
				{
					if(share_hold+tx[i].shares<=tx[sell_index].shares)
					{
						total+=tx[i].shares*(tx[sell_index].price-tx[i].price);
						share_hold+=tx[i].shares;
						
					}
					else
					{
						total+=(tx[sell_index].shares-share_hold)*(tx[sell_index].price-tx[i].price);
						share_hold+=tx[i].shares-tx[sell_index].shares;
						last_hold_index=i;					
					}	
				}
				start_index=sell_index+1;
			}
			else if(start_index==sell_index)
			{
				total+=tx[sell_index].shares*(tx[sell_index].price-tx[last_hold_index].price);
				share_hold-=tx[sell_index].shares;
				start_index=sell_index+1;
				
			}		

		}
		else
		{			
			break;
		}
		
	}
	
	
	
	    cout<<fixed<<showpoint<<setprecision(2);
		share_left=share_hold+tx[start_index].shares;	
		cout<<"You have bought total shares of "<<sell_count+share_left<<endl;
		cout<<"You have sold "<<sell_count<<" shares"<<endl;
		cout<<"Shares left "<<share_left<<" shares"<<endl;
		if(total>0)
		cout<<"Capital Gain for the year 2011 is $"<<total<<endl;
	else if(total==0)
		cout<<"There is no loss or gain in the year 2011"<<endl;
	else
	{
		cout<<"Capital loss for the year 2011 is $"<<abs(total)<<endl;
	}
		
	

	system("pause");
	return 0;
}



Its working fine, but I want to know whether its suitable for the given rule.
Is there any other simpler way to do it?

Thank you
Topic archived. No new replies allowed.