Code:
typedef struct record1{
int n1;
string name1;
char c1;
double d1;
} set1;
int swap2(int &n1, int &n2){
int temp;
temp=n1;
n1=n2;
n2=temp;
return 1; } // SWAP 2 NUM
int swap2(char &n1, char &n2){
char temp;
temp = n1;
n1 = n2;
n1 = temp;
return 1; } // SWAP 2 CHAR
int swap2(double &n1, double &n2){
double temp;
temp = n1;
n1 = n2;
n2 = temp;
return 1; } // SWAP 2 DOUBLE
int swap2(char n1[20], char n2[20]){
char temp[20];
strcpy(temp[20],n1[20]);
strcpy(n1[20],n2[20]);
strcpy(n2[20],temp[20]);
return 1; } // SWAP 2 STRING
int bubble1(set1 *p){
int i,j,temp,flag;
for(j=0; j<90; j++){
flag=0;
for(i=0; i<89; i++){
if(((p+i)->n1) > ((p+i+1)->n1)){
swap2(((p+i)->n1), ((p+i+1)->n1));
swap2(((p+i)->c1), ((p+i+1)->c1));
swap2(((p+i)->d1), ((p+i+1)->d1));
swap2(((p+i)->name1), ((p+i+1)->name1));
flag =1;
} // IF
} // SORT
if(flag==0)break;
} // J
return 0;
}
int main(){
clrscr();
int ct;
ifstream in1("names.dat");
set1 *s1;
s1 = new set1[100];
for(ct=0;!in1.eof();++ct){
in1>>(s1+ct)->n1;
if(((s1+ct)->n1) == -1)break;
in1>>(s1+ct)->name1;
in1>>(s1+ct)->c1;
in1>>(s1+ct)->d1;
} // FOR
for(int i=0; i<ct; i++){
cout << "\n";
cout << "\t" << (s1+i)->n1;
cout << "\t" << (s1+i)->d1;
cout << "\t\t" << (s1+i)->c1;
cout << "\t" << (s1+i)->name1;
if((i%15==0)&&(i>0)){
cout << "\n\t";
cout << "\n\tEnter to continue...";
getch();
}
} // FOR
cout << "\n\n\t Bubble Sort in Progress...\n";
for(int i=0; i<ct; i++){
bubble1(s1);
cout << "\n";
cout << "\t" << (s1+i)->n1;
cout << "\t" << (s1+i)->d1;
cout << "\t\t" << (s1+i)->c1;
cout << "\t" << (s1+i)->name1;
if((i%15==0)&&(i>0)){
cout << "\n\t";
cout << "\n\tEnter to continue...";
getch();
}
} // FOR
cout << "\n\tTotal =\t" << ct;
delete []s1;
in1.close();
cout << "\n\tComplete";
return 0;
} //MAIN
i know i'm not really supposed to post the whole source code, but i think it may be a problem in the way i'm writing it. the numbers or n1 sort, but nothing else does.
6 ACTON Y 185.000
198 CONSTANCE Q 42.182
thats a few things from the names.dat file so you could get familiar with it. thanks!
Comment