我的程序的目的是通過io重定向接收用戶輸入,對其進行排序,並輸出沒有重複記錄。所有這些都使用指向動態分配的結構的指針數組。我的輸出有點麻煩。它重複最後一行很可能是更多的結構。提前致謝。c - 程序多次接收最後一行
功能文件
#include "lab67h.h"
void getinput(address *temp[], int *s)
{
char c[25];
gets(c);
while (c[0] != '\0' && *s < size)
{
temp[*s] = (address *) malloc(sizeof(address));
strcpy(temp[*s]->name, c);
gets(c);
strcpy(temp[*s]->street, c);
gets(c);
strcpy(temp[*s]->city, c);
gets(c);
strcpy(temp[*s]->zipcode, c);
(*s)++;
gets(c);
}
}
void sort(address *temp[], int s)
{
address *tempi;
for (int a = 0; a <= s; a++)
{
for (int b = 0; b < (s - 1); b++)
{
if (convert(temp, b) > convert(temp, b + 1))
{
tempi = temp[b];
temp[b] = temp[b + 1];
temp[b + 1] = tempi;
}
}
}
}
int convert(address *tempp[], int c)
{
int i = -1, num = 0;
char str[10];
strcpy(str, &tempp[c]->zipcode);
while (str[++i] != '\0')
num = num * 10 + (str[i] - '0');
return num;
}
void output(address *temp[], int s)
{
for (int i = 0; i < s; i++)
{
puts(temp[i]->name);
puts(temp[i]->street);
puts(temp[i]->city);
puts(temp[i]->zipcode);
while (temp[i]->name == temp[i + 1]->name && temp[i]->street == temp[i + 1]->street && temp[i]->city == temp[i + 1]->city && temp[i]->zipcode == temp[i + 1]->zipcode)
{
free(temp[i]);
i++;
}
free(temp[i]);
i++;
}
}
主文件
#include "lab67h.h"
int main(void)
{
int a = 0;
address *p[size];
getinput(p, &a);
sort(p, a);
output(p, a);
system("pause");
return 0;
}
頭文件
#ifndef LAB67H_H_INCLUDED
#define LAB67H_H_INCLUDED
#define size 50
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
typedef struct
{
char name[25];
char street[25];
char city[25];
char zipcode[25];
} address;
void getinput(address*[], int*);
void sort(address*[], int);
int convert(address*[], int);
void output(address*[], int);
#endif
輸入文件
A1, A2
20294 Lorenzana Dr
Woodland Hills, CA
91364
B1, B2
19831 Henshaw St
Culver City, CA
94023
C1, C2
5142 Dumont Pl
Azusa, CA
91112
D1, D2
20636 De Forest St
Woodland Hills, CA
91364
A1, A2
20294 Lorenzana Dr
Woodland Hills, CA
91364
E1, E2
4851 Poe Ave
Woodland Hills, CA
91364
F1, F2
20225 Lorenzana Dr
Los Angeles, CA
91111
G1, G2
20253 Lorenzana Dr
Los Angeles, CA
90005
H1, H2
5241 Del Moreno Dr
Los Angeles, CA
91110
I1, I2
5332 Felice Pl
Stevenson Ranch, CA
94135
J1, J2
5135 Quakertown Ave
Thousand Oaks, CA
91362
K1, K2
720 Eucalyptus Ave 105
Inglewood, CA
89030
L1, L2
5021 Dumont Pl
Woodland Hills, CA
91364
M1, M2
4819 Quedo Pl
Westlake Village, CA
91362
I1, I2
5332 Felice Pl
Stevenson Ranch, CA
94135
I1, I2
5332 Felice Pl
Stevenson Ranch, CA
94135
N1, N2
20044 Wells Dr
Beverly Hills, CA
90210
O1, O2
7659 Mckinley Ave
Los Angeles, CA
90001
輸出我得到
Press any key to continue . . .
K1, K2
720 Eucalyptus Ave 105
Inglewood, CA
89030
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
G1, G2
20253 Lorenzana Dr
Los Angeles, CA
90005
H1, H2
5241 Del Moreno Dr
Los Angeles, CA
91110
C1, C2
5142 Dumont Pl
Azusa, CA
91112
M1, M2
4819 Quedo Pl
Westlake Village, CA
913 62
D1, D2
20636 De Forest St
Woodland Hills, CA
91364
E1, E2
4851 Poe Ave
Woodland Hills, CA
91364
B1, B2
19831 Henshaw St
Culver City, CA
94023
I1, I2
5332 Felice Pl
Stevenson Ranch, CA
94135
它沒有顯示所有行...... – coderredoc
我建議你沒有用盡你的調試選項。這應該總是在轉向SO之前完成。例如,你在調用'sort'之前是否打印了數據,看看它是否正確?您是否嘗試過使用較小的數據集並通過調試器執行完畢?調試是學習和獲取經驗的寶貴技能,不應該輕易外包給SO。 – kaylum
請參閱[爲什麼'gets()'函數是如此危險以致不應該使用?](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-它不被使用)討論爲什麼'gets()'不應該被使用。 –