气泡排序在C中动态创建的结构数组 [英] Bubble sort dynamically created struct array in c
问题描述
这里真的很新,对于任何其他错误,我们深表歉意. 我有一些新的学校项目(学习C),我必须要做的是连接到服务器 使用套接字,而不是从服务器下载所有代码行. 之后,我需要对行进行排序,以便它们在排序的顺序上..好吧,我已经下载了代码行,将它们保存在struct数组中,但是现在我的冒泡排序向我显示了一些错误,但我没有知道出什么问题了.向您寻求帮助.
Really new here so sorry for any further mistakes.. I've got some new school project (Learning C), what I must to is to connect to the server using sockets than download all code lines from the server. After that I need to sort the lines so they be in order, where I got is on the sort.. well I already downloaded the code lines saved them in struct array but now my bubble sort shows some error to me and I don't know what's the problem.. thx for any help.
typedef struct DATA{
char* buf;
}DATA;
// this fucntion creates a socket.
void sort_array(DATA *to_sort, int len){
int i, j;
char tmp[1024] = "";
for (i = 0; i < len - 1; i++){
for (j = 0; j < len - i - 1; j++){
if (strcmp(to_sort[j].buf, to_sort[j + 1].buf) < 0){
strcpy(tmp, to_sort[j + 1].buf);
strcpy(to_sort[j + 1].buf, to_sort[j].buf);
strcpy(to_sort[j].buf, tmp);
}
}
}
}
int main(){
WSADATA info;
int error, s,j;
int sendError, recvError;
char buffer[1024] = "100",readbuf[1024] = "";
char recvbuf[1024] = "";
int numberLines, i, temp, convert;
char converted_num[1024] = "";
char *sub;
struct sockaddr_in ClientService;
FILE *fp = fopen("stored_data.txt", "w");
FILE *ofp = fopen("final_result.txt", "w");
DATA *to_sort = NULL;
error = WSAStartup(MAKEWORD(2, 0), &info);
//check if error occurred while configuring.
if (error != 0){
printf("WSAstartup failed with error: %d\n", error);
exit(1);
}
s = socket_creation(fp);
// configuration of the socket.
ClientService.sin_family = AF_INET;
ClientService.sin_addr.s_addr = inet_addr("54.209.143.42");
ClientService.sin_port = htons(6714);
connection(s, ClientService, fp); // function connecting to the server.
error = WSAStartup(MAKEWORD(2, 0), &info);
// send '100' login command to server.
strcpy(buffer, "100");
sendError = send_to_serv(buffer, s);
// receiving respond from the server.
recvError = recv_from_serv(s, &numberLines, fp,buffer);
// send '400' get number lines command to server.
strcpy(buffer, "400");
sendError = send_to_serv(buffer, s);
// receiving respond from the server.
recvError = recv_from_serv(s, &numberLines, fp,buffer);
printf("\nNumber of Lines are: %d\n", numberLines);
temp = numberLines; // number of all lines received.
/* allocate mmoery for struct array to store the data from server */
to_sort = (DATA*)malloc(sizeof(DATA)* temp);
// getting the lines from the server.
for (i = 0; i < temp; i++){
j = 0;
convert = 5000001 + i; // creating number of line wanted.
_itoa(convert, converted_num, 10); // converting the int to a string (wanted line).
sendError = send_to_serv(converted_num, s); // sending the server request of line wanted.
recv_from_serv(s, &numberLines, fp, buffer); // receive the line wanted.
sub = substring(buffer, 0, 3);
// checks if the server returned '502 OK' or '501 REJECT'
if (strcmp(sub, "502") != 0){
to_sort[j].buf = buffer;
j++;
}
}
sort_array(to_sort, temp); // sorting the struct array.
// printing the final result.
// clean memoery.
free(to_sort);
fclose(fp);
system("PAUSE>nul");
return 0;
}
推荐答案
void bsort(char **data, size_t size) {
size_t i, j;
char *tmp = NULL;
for (i = 1; i < size; i++) {
for (j = 1; j < size; j++) {
if (strcmp(data[j-1], data[j]) > 0) {
tmp = data[j-1];
data[j-1] = data[j];
data[j] = tmp;
}
}
}
}
此代码会进行排序.我检查了)))需要更多的时间.请尝试使用此代码,如果不起作用,请继续检查并再次检查错误.
This code do sort. I checked))) Need more time. Mean try this code, if not work then go further and check error again.
to_sort[j] = (char*) malloc(1024);
memcpy(to_sort[j].buf, buffer, 1024);
j++;
我读取数据,直到获得完整的缓冲区.当数据即将到来时,请不要尝试每次都检查502.只有一次,得到回复后.之后,您可以将响应流式传输到结尾.
I read data until I get full buffer. Don't try to check 502 every time while data is coming. Only once, after got reply. After that you stream response until the end.
#define REPLY 1024
do {
bytes_read = recv(sock, server_reply, REPLY, 0);
if (bytes_read == SOCKET_ERROR) {
perror("error recieving data");
exit(1);
}
if (bytes_read > 0) {
to_sort[j] = (char*) malloc(1024);
memcpy(to_sort[j].buf, server_reply, 1024);
j++;
}
} while (bytes_read == REPLY);
UPD
void bsort(DATA *data, size_t size) {
size_t i, j;
DATA tmp;
for (i = 1; i < size; i++) {
for (j = 1; j < size; j++) {
if (strcmp(data[j-1].buf, data[j].buf) > 0) {
tmp = data[j-1];
data[j-1] = data[j];
data[j] = tmp;
}
}
}
}
UPD2
//1) Simpliest way
//pointer to array
DATA *s = NULL;
//size of array
size_t array_size = 50;
//create array of DATA elements
s = (DATA *) malloc(sizeof(DATA) * array_size);
//now you have array of DATA. But each element contains uninitialized pointer
//of type char
//set each pointer to proper address on heap
//each element now can handle string length 1023
//note here all strings have same size
for (size_t i = 0; i < array_size; i++) {
s[i].buf = (char*) malloc(1024);
}
免费
for (size_t i = 0; i < array_size; i++) {
free(s[i].buf);
}
free(s);
带有排序
DATA *s = NULL;
size_t array_size = 5;
size_t i;
s = (DATA *) malloc(sizeof(DATA) * array_size);
for (i = 0; i < array_size; i++) {
s[i].buf = (char*) malloc(1024);
}
strcpy(s[0].buf, "AAAAA");
strcpy(s[1].buf, "CCCCC");
strcpy(s[2].buf, "XXXXX");
strcpy(s[3].buf, "AAAAA");
strcpy(s[4].buf, "BBBBB");
bsort(s, 5);
for (i = 0; i < array_size; i++) {
printf("%s\n", s[i]);
}
for (i = 0; i < array_size; i++) {
free(s[i].buf);
}
free(s);
UPD3分配内存的方法更复杂,但速度更快
UPD3 more sophisticated way to allocate memory, but much faster
DATA *s = NULL;
size_t array_size = 5;
size_t item_size = 1024;
size_t i;
s = (DATA *) malloc(sizeof(DATA) * array_size + array_size * item_size);
s[0].buf = (char*) (s + array_size);
for (size_t i = 0; i < array_size; i++) {
s[i].buf = s[0].buf + i * item_size;
}
strcpy(s[0].buf, "AAAAA");
strcpy(s[1].buf, "CCCCC");
strcpy(s[2].buf, "XXXXX");
strcpy(s[3].buf, "AAAAA");
strcpy(s[4].buf, "BBBBB");
bsort(s, 5);
for (i = 0; i < array_size; i++) {
printf("%s\n", s[i]);
}
free(s);
这篇关于气泡排序在C中动态创建的结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!