Sunteți pe pagina 1din 7

Jason Chiang Wann Chun

4633775

CSCI124 Applied Programming


Feb 2014 Session
LAB TASK 7
This Lab Task is worth 2% to your subject marks

Background:
The aim of this lab is to get some experience with sorting data.

TASK
For this lab assume you work at the NASDAQ (http://www.nasdaq.com) stock market. Each day
your supervisor gives you a list of the top 100 or so traders in the IT sector. Such data is
represented in a file such as stocksdata.txt. The stock data file consists of many lines,
each representing trade information relating to a specific company. Consider the following line:
AAPL155747733
The first field represents the stock tag i.e. AAPL(Apple Inc). The second field represents the
current value of each Apple share, in this case $155.00 and the last field represents the
number of Apple shares traded during the day.
The data presented to you in such a file is not sorted. Your supervisor would like you to write a
little program that sorts the data in stocksdata.txtin various ways. Your program should
produce one of either two reports. The reports are generated as text files, which can then be
further processed.
The first report your program should produce is a descending list (largest to smallest) of
company stock values. The file your program should produce looks like this:
CorporateStockVolumeDataNASDAQ
StockTag
Cost
GOOG390
ADBE192
INFY167
AAPL155
LOGI130
ERIC110

Jason Chiang Wann Chun

4633775

As you can see, each line of the file represents a stock and its current value in order of largest to
smallest. That said what about the first three lines? Well come back to those later.
The other report your program should generate is as follows:
Corporate Stock Volume Data - NASDAQ
Stock Tag
Volume
ERIC 928238
INFY 899921
ORCL 893487
BEAS 868694
AAPL 747733
CTSH 685684
NVDA 654743
This report represents the volume of shares traded for each company from largest to smallest.
Your task
Step 1
Define a suitable data structure (possibly a struct) to represent a single stock.
Step 2
Write a program in the file main.cppthat opens the file stocksdata.txtand reads the
data into an array. The filename MUST be hard coded into your program. You can assume the
array is of type defined in step 1. You can also assume you will not have more than 100 stocks at
any given time.
Step 3
Once all data is read in and stored in an appropriate data structure, display a menu.
MainMenu:
c:SortDatabyCost
v:SortDatabyTradeVolume
q:quit
enterchoice:
If a user enters an invalid choice, the main menu should be reprinted. If the user enters the c
option, your program should sort the data stored in the array. Once the data has been sorted, it
should be written to the file in a format specified above. The program should then terminate.
If a user enters the voption, exactly the same process is repeated but this time the data is sorted
by trade volume. In either case, options cand vproduce a single output file the file should
be known as data.txt. There should be no prompting of file names in your program. Add this

Jason Chiang Wann Chun

4633775

code to main.cpp. For both sorting, you are required to implement the quicksort sorting
technique.
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
struct stock
{
char name[10];
int cost;
int tradeVolume;
};
void in_file ( fstream &, stock[100], int&);
void q_sort_cost ( stock[100], int, int);
void q_sort_tradeVolume ( stock[100], int, int);
int cost ( stock[100], int, int);
int tradevolume ( stock[100], int, int);
void printCost ( ofstream &, stock[100], int);
void printVolume ( ofstream &, stock[100], int);
int main()
{
stock detail[100];
int i = 0;
int size = 0;
int high;
char menu;
int index;
fstream infile;
ofstream writefile;
infile.open("stocks-data.txt", ios::in);
while ( infile.good() )
{
infile >> detail[i].name;
infile >> detail[i].cost;
infile >> detail[i].tradeVolume;

if ( !infile.eof())
{
i++;
size ++;
}

Jason Chiang Wann Chun

4633775

infile.close();
cout << "Option :
cout << "c : Sort
cout << "v : Sort
cout << "q : Quit
cout << "Option :
cin >> menu;

" <<
data
data
" <<
";

endl;
by cost" << endl;
by Trade Volume " << endl;
endl;

while ( menu != 'c' && menu != 'v' && menu != 'q')


{
cout << "Invalid input, please only choose from the menu above
" << endl;
cout << "Option : ";
cin >> menu;
}
for ( int low = 0; low < high; low++)
{
if (menu == 'c' || menu == 'C')
{
q_sort_cost(detail, low, high);
}
if ( menu == 'v')
{
q_sort_tradeVolume(detail, low, high);
}

if ( menu == 'q')
{
cout << "Thx for using this program ^^" << endl;
cout << "Terminating program " << endl;
return 0;
}
}

void q_sort_cost(stock detail[100], int low, int high)


{
int i;
if ( low < high)
{
i = tradevolume(detail, low, high);
q_sort_cost(detail, low, i-1 );
q_sort_cost(detail, i+1, high);

Jason Chiang Wann Chun

4633775

void q_sort_tradeVolume(stock detail[100],int low, int high)


{
int i;

if ( low < high )


{
i = tradevolume(detail, low, high);
q_sort_tradeVolume(detail, low, i-1);
q_sort_tradeVolume(detail, i+1, high);
}

void printCost(ofstream &writefile, stock detail[100], int value)


{
writefile << "Corporate stock Volume Data - NASDAQ " << endl;
writefile << "stock Tag name " << endl;
writefile << "Cost " << endl;

for ( int i = 0; i < value; i++)


{
writefile << detail[i].name << "\t" << detail[i].cost;
}

void printVolume(ofstream &writefile, stock detail[100], int value)


{
writefile << "Corporate Stock Volume Data - NASDAQ " << endl;
writefile << "Stock tag name" << endl;
writefile << "Trade Volume" << endl;

for ( int i = 0; i < value; i++)


{
writefile << detail[i].name << "\t" << detail[i].tradeVolume;
}

int cost(stock detail[100], int low, int high)


{
int x = detail[high].cost;
int y = low - 1;
for ( int i = low; i <= high-1; i++)
{
if ( detail[i].cost >= x)
{
y = y + 1;
stock temp;
strcpy ( temp.name, detail[y].name);

Jason Chiang Wann Chun

4633775

temp.cost = detail[y].cost;
temp.tradeVolume = detail[y].tradeVolume;
strcpy ( detail[y].name, detail[i].name);
detail[y].cost = detail[i].cost;
detail[y].tradeVolume = detail[i].tradeVolume;
strcpy ( detail[i].name, temp.name);
detail[i].cost = temp.cost;
detail[i].tradeVolume = temp.tradeVolume;
}
}
stock temp;
strcpy ( temp.name, detail[ y + 1 ].name);
temp.cost = detail[y + 1].cost;
temp.tradeVolume = detail[y + 1].tradeVolume;
strcpy(detail[y+1].name, detail[high].name);
detail[y+1].cost= detail[high].cost;
detail[y+1].tradeVolume=detail[high].tradeVolume;
strcpy(detail[high].name, temp.name);
detail[high].cost=temp.cost;
detail[high].tradeVolume=temp.tradeVolume;
}
int tradevolume(stock detail[100], int low, int high)
{
stock temp;
int y = low - 1;
for ( int i = low; i <= high - 1; i++)
{
if ( detail[i].tradeVolume >= detail[high].tradeVolume )
{
y = y + 1;
strcpy(temp.name, detail[y].name);
temp.cost = detail[y].cost;
temp.tradeVolume = detail[y].tradeVolume;
strcpy (detail[y].name, detail[i].name);
detail[y].cost = detail[i].cost;
detail[y].tradeVolume = detail[i].tradeVolume;
strcpy ( detail[i].name, temp.name);
detail[i].cost = temp.cost;
detail[i].tradeVolume = temp.tradeVolume;
}

Jason Chiang Wann Chun

4633775

strcpy(temp.name, detail[y + 1].name);


temp.cost = detail[y + 1].cost;
temp.tradeVolume = detail[y + 1].tradeVolume;
strcpy (detail[y + 1].name, detail[high].name);
detail[y + 1].cost = detail[high].cost;
detail[y + 1].tradeVolume = detail[high].tradeVolume;
strcpy ( detail[high].name, temp.name);
detail[high].cost = temp.cost;
detail[high].tradeVolume = temp.tradeVolume;
}

SUBMISSION:
You are to submit:

A documentation containing the following:


o The standard cover page
o The code listing
o Sample input and output

The softcopy of the source code should be uploaded to Moodle.

The completed Lab Task must be submitted on Thursday, 8th May 2014 at the end of your lab
class.

S-ar putea să vă placă și