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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <conio.h>
using namespace std;
struct transactiontxt{string timestr; string type; string description; float amount;float c_balance;};
void vectorswap(transactiontxt &temptx1, transactiontxt &temptx2);
vector<transactiontxt> getinfo(string name);
string parseCsvRow ( string row, int commaOffset );
int partition(vector<transactiontxt> txs,int start, int end);
void quicksort (vector<transactiontxt> txs, int start, int end );
int sorttransaction(vector<transactiontxt> txs);
int main(){
vector<transactiontxt> txs;
int a;
txs=getinfo("lab3");
/*vectorswap(txs[0],txs[5]);
for (int x=0;x<6;x++){
cout<<txs[x].timestr<<","<<txs[x].type<<","<<txs[x].description<<","<<txs[x].amount<<","<<txs[x].c_balance<<endl;}*/
sorttransaction(txs);
system("pause");
return 0;
}
void vectorswap(transactiontxt &temptx1, transactiontxt &temptx2){
transactiontxt temptx3=temptx1;
temptx1=temptx2;
temptx2=temptx3;
}
string parseCsvRow ( string row, int commaOffset ) {
int commaIndex = row.find(",",0);
if ( commaIndex == -1 )
return "";
commaOffset++;
row += ",";
commaIndex = 0;
int lastCommaIndex = 0;
string data;
for(int i = 0; i < commaOffset; i++ ) {
lastCommaIndex = commaIndex;
if(lastCommaIndex > 0) {
lastCommaIndex++;
}
commaIndex = row.find(",",lastCommaIndex);
if(commaIndex == -1)
return "";
data = row.substr(lastCommaIndex, commaIndex - lastCommaIndex);
}
return data;
}
vector<transactiontxt> getinfo(string name){
name+=".csv";
ifstream infile;
infile.open(name.c_str());
string row;
transactiontxt tm;
vector<transactiontxt> tx;
while (infile.good()){
getline(infile,row);
if(row.length()<3)continue;
tm.timestr=parseCsvRow(row,0);
tm.type=parseCsvRow(row,1);
tm.description=parseCsvRow(row,2);
tm.amount=atof(parseCsvRow(row,3).c_str());
tm.c_balance=atof(parseCsvRow(row,4).c_str());
tx.push_back(tm);
}
return tx;
infile.close();
}
int partition(vector<transactiontxt> txs,int start, int end){
float pivotvalue; int pivotindex,mid; string pivotstring;
mid=(start+end)/2;
//sort by amount;
vectorswap(txs[start],txs[mid]);
pivotindex=start;
pivotvalue=txs[start].amount;
for(int scan=start+1;scan<=end;scan++){
if(txs[scan].amount<pivotvalue){
pivotindex++;
vectorswap(txs[pivotindex],txs[scan]);}
}
vectorswap(txs[start],txs[pivotindex]);
return pivotindex;
}
void quicksort (vector<transactiontxt> txs, int start, int end ){
int pivot;
if (start<end)
{
pivot=partition(txs,start,end);
quicksort (txs,start,pivot-1);
quicksort (txs,pivot+1,end);
}
}
int sorttransaction(vector<transactiontxt> txs){
int transactionnum=txs.size();
quicksort(txs,0,transactionnum-1);
for (int x=0;x<transactionnum;x++){
cout<<txs[x].timestr<<","<<txs[x].type<<","<<txs[x].description<<","<<txs[x].amount<<","<<txs[x].c_balance<<endl;}
getch();
return 0;
}
|