Home > Back-end >  C sort out problems or read the file out trouble bosses to look at!!!!!!!!!! Integral 50
C sort out problems or read the file out trouble bosses to look at!!!!!!!!!! Integral 50

Time:09-20

Input two data after sorting out the same data don't know why

Code the following trouble help me find the bug

# include
# include
# include
using namespace std;
//int n=0;
Struct Worker {
int num;
char name[20];
int age;
Float wage;
};
Void input (int n, Worker * w) {


for(int i=0; iCout<& lt;" Num: ";
Cin> W [I] num;
Cout<& lt;" Name: ";
Cin> W [I]. Name;
Cout<& lt;" Age: ";
Cin> W [I] the age;
Cout<& lt;" Wage: ";
Cin> W [I]. Wage;
}
}

Void bubble_sort (int n, Worker * w) {//sorting
The Worker w1.
Bool change=true;
For (int I=n - 1; I>=1 & amp; & Change==true; I -) {
Change=false;
for(int j=0; JIf (w [j] num> W [j + 1] {num)
W1=w [j];
[j]=w w [j + 1);
W [j + 1)=w1;
Change=true;
}
}
}
}

Void write_file (char * filename, int n, Worker * w) {
Fstream outfile (filename, the ios: : out | ios: : binary);
if(! Outfile) {
Cerr<& lt;" Open the file error!" exit(1);
}
//outfile. Write (sizeof (char *) w, [I] (w));
for(int i=0; iOutfile. Write (sizeof (char *) w, [I] (w));
//cout}
Outfile. Close ();
}

Void show (int n, Worker * w) {
for(int i=0; iCout<& lt;" Num: "& lt; Cout<& lt;" Name: "& lt; Cout<& lt;" Age: "& lt; Cout<& lt;" Wage: "& lt; }
}

Void read_file (char * filename, int n, Worker * w) {
Fstream infile (filename, the ios: : | in ios: : binary);
if(! Infile) {
Cout<& lt;" The open ifle error!" exit(1);
}
//Worker * w2=new Worker [n].
//ifile. Read (w2 (char *), sizeof (w2));
for(int i=0; iInfile. Read (sizeof (char *) w, [I] (w));
Cout<& lt;" Num: "& lt; Cout<& lt;" Name: "& lt; Cout<& lt;" Age: "& lt; Cout<& lt;" Wage: "& lt; //cout}
Infile. Close ();
}

The Worker * add (int & amp; Num, Worker * w) {
Int num1.
If (w==NULL | | num==0) {
Cout<& lt;" You have not creat a list!" Return NULL;
}
Cout<& lt;" Both please input number of extra workers: ";
Cin> Num1.
The Worker * w1=new Worker [num + num1];
for(int i=0; iIf (i>=0 & amp; & iW1=w [I] [I];
}
If (i>=num& & iCout<& lt;" Num: ";
Cin> W1 [I] num;
Cout<& lt;" Name: ";
Cin> W1 [I]. Name;
Cout<& lt;" Age: ";
Cin> W1 [I] the age;
Cout<& lt;" Wage: ";
Cin> W1 [I]. Wage;
}
}
Num +=num1;
Return w1.
}

Int the select (char * filename, int n, Worker * w) {
Cout<& lt;" Both please input the worker num: ";
Int num1.
Cin> Num1.
If (num1==0) {
return 0;
}
Fstream infile (filename, the ios: : | in ios: : binary);
if(! Infile) {
Cout<& lt;" The open error!" exit(1);
}
int i;
for(i=0; iInfile. Read (sizeof (char *) w, [I] (w));
If (num1==w [I] num) {
Cout<& lt;" Num: "& lt; Cout<& lt;" Name: "& lt; Cout<& lt;" Age: "& lt; Cout<& lt;" Wage: "& lt; break;
}
}
If (I==n) {
Cout<& lt;" No this person "& lt; }
return 0;
}

Int main () {
int n=0;
Int num=0;
The Worker * w=NULL;
Cout<& lt;" Worker_system "& lt; Cout<& lt;" 1. Creat "& lt; Cout<& lt;" 2. Add "& lt; Cout<& lt;" 3. The sort "& lt; Cout<& lt;" 4. Select "& lt; Cout<& lt;" 5. Show "& lt; Cout<& lt;" 6. The exit "& lt; While (1) {
Cout<& lt;" Do your choice: ";
Cin> n;
{if (n==6)
break;
}
The switch (n) {
Case 1:
//coutIf (num==0) {
Cout<& lt;" Do the import number of worker: ";
Cin> Num.
W=new Worker (num);
Input (num, w);
Write_file (" worker_sys. TXT ", num, w);
break;
}
The else {
Cout<& lt;" Already creat a list!" break;
}
Case 2:
W=add (num, w);
//coutWrite_file (" worker_sys. TXT ", num, w);
//cout<1break;
Case 3:
If (num==0) {
//bubble_sort (num, w); break;
Cout<& lt;" You have not creat a list!" break;
}
The else {
Bubble_sort (num, w);
break;
}
Case 4:
If (num==0) {
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related