小弟我是不是不行了啊
我是不是不行了啊...
嘿嘿,第一次发帖,学c也有一段时间了..
"读入文件中两组二进制数表(1.bin,2.bin),转换为int,存在两个单链列表中,list1和list2,然后把两组数据中不同的数找出来,存入一个指针中,再把所有数据进行升序排列,存在指针列表中,然后打印出来"
这样的问题我都写了很长时间(3小时多),还有点小问题,大家帮看看啊
1.bin
11
0
100
111
1000
1010
2.bin
10
11
101
1000
1001
1010
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BIN 6
struct Node;
typedef struct Node* ptr_to_node;
typedef ptr_to_node List;
typedef ptr_to_node Position;
//void Insert(int ,List L,Position );
int Pow2(int);
void Sort(int [],int);
struct Node {
int element;
Position next;
};
//the power of 2
int Pow2(int var){
int result = 1;
for(int i = 0;i < var;++i){
result *= 2;
}
return result;
}
//the bubble sort
void Sort(int a[],int n){
int temp;
for(int i = 0;i < n -1;++i){
for(int j = i + 1;j < n;++j){
if(a[i] > a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
int main(void){
ptr_to_node header = (ptr_to_node)malloc(sizeof(struct Node));
header -> element = 0;
header -> next = NULL;
List my_list1 = header;
List my_list2 = header;
char** temp1;
char** temp2;
temp1 = (char**)malloc(sizeof(char*));
temp2 = (char**)malloc(sizeof(char*));
for(int i = 0;i <MAX_BIN;++i){
temp1[i] = (char*)malloc(sizeof(char));
temp2[i] = (char*)malloc(sizeof(char));
}
FILE* fp1;
FILE* fp2;
if((fp1 = fopen("1.bin","r")) && (fp2 = fopen("2.bin","r")) == 0){
printf("Error!!");
exit(0);
}
for(int i = 0;i < MAX_BIN;++i){
int int_arr1 = 0;
int int_arr2 = 0;
fscanf(fp1,"%s",temp1[i]);
fscanf(fp2,"%s",temp2[i]);
int k = strlen(temp1[i]);
int j = strlen(temp2[i]);
int q = k;
int p = j;
while(k != 0){
--k;
if(temp1[i][q - k - 1] == 48){
continue;
}
int_arr1 += Pow2(k);
}
//printf("%i\t",int_arr1);
Position TempCell1;
TempCell1 = (Position)malloc(sizeof(struct Node));
TempCell1 -> element = int_arr1;
TempCell1 -> next = my_list1 -> next;
my_list1 -> next = TempCell1;
// printf("%i\t",my_list1 -> element);
//my_list1 = my_list1 -> next;
while(j != 0){
--j;
if(temp1[i][p - j - 1] == 48){
continue;
}
int_arr2 += Pow2(j);
}
//printf("%i\t",int_arr2);
//Insert(int_arr2,my_list2,my_list2);
Position TempCell2;
TempCell2 = (Position)malloc(sizeof(struct Node));
TempCell2 -> element = int_arr2;
TempCell2 -> next = my_list2 -> next;
my_list2 -> next = TempCell2;
}
fclose(fp1);
fclose(fp2);
int all_arr[12] = {-1};
int* r_ptr = (int* )malloc(sizeof(int));
int* head = r_ptr;
int count = 0;
for(int i = 0;i < 2 * MAX_BIN;++i){
if(i < MAX_BIN){
all_arr[i] = my_list1 -> element;
//printf("%i\n\n",);
my_list1 = my_list1 -> next;
}
all_arr[i] = my_list2 -> element;
my_list2 = my_list2 -> next;
}
//for(int i = 0;i < 12;++i){
//printf("%i\n",all_arr[i]);
// }
Sort(all_arr,2 * MAX_BIN);
/*for(int i = 0;i < 12;++i){
printf("%i\n",all_arr[i]);
}*/
嘿嘿,第一次发帖,学c也有一段时间了..
"读入文件中两组二进制数表(1.bin,2.bin),转换为int,存在两个单链列表中,list1和list2,然后把两组数据中不同的数找出来,存入一个指针中,再把所有数据进行升序排列,存在指针列表中,然后打印出来"
这样的问题我都写了很长时间(3小时多),还有点小问题,大家帮看看啊
1.bin
11
0
100
111
1000
1010
2.bin
10
11
101
1000
1001
1010
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BIN 6
struct Node;
typedef struct Node* ptr_to_node;
typedef ptr_to_node List;
typedef ptr_to_node Position;
//void Insert(int ,List L,Position );
int Pow2(int);
void Sort(int [],int);
struct Node {
int element;
Position next;
};
//the power of 2
int Pow2(int var){
int result = 1;
for(int i = 0;i < var;++i){
result *= 2;
}
return result;
}
//the bubble sort
void Sort(int a[],int n){
int temp;
for(int i = 0;i < n -1;++i){
for(int j = i + 1;j < n;++j){
if(a[i] > a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
int main(void){
ptr_to_node header = (ptr_to_node)malloc(sizeof(struct Node));
header -> element = 0;
header -> next = NULL;
List my_list1 = header;
List my_list2 = header;
char** temp1;
char** temp2;
temp1 = (char**)malloc(sizeof(char*));
temp2 = (char**)malloc(sizeof(char*));
for(int i = 0;i <MAX_BIN;++i){
temp1[i] = (char*)malloc(sizeof(char));
temp2[i] = (char*)malloc(sizeof(char));
}
FILE* fp1;
FILE* fp2;
if((fp1 = fopen("1.bin","r")) && (fp2 = fopen("2.bin","r")) == 0){
printf("Error!!");
exit(0);
}
for(int i = 0;i < MAX_BIN;++i){
int int_arr1 = 0;
int int_arr2 = 0;
fscanf(fp1,"%s",temp1[i]);
fscanf(fp2,"%s",temp2[i]);
int k = strlen(temp1[i]);
int j = strlen(temp2[i]);
int q = k;
int p = j;
while(k != 0){
--k;
if(temp1[i][q - k - 1] == 48){
continue;
}
int_arr1 += Pow2(k);
}
//printf("%i\t",int_arr1);
Position TempCell1;
TempCell1 = (Position)malloc(sizeof(struct Node));
TempCell1 -> element = int_arr1;
TempCell1 -> next = my_list1 -> next;
my_list1 -> next = TempCell1;
// printf("%i\t",my_list1 -> element);
//my_list1 = my_list1 -> next;
while(j != 0){
--j;
if(temp1[i][p - j - 1] == 48){
continue;
}
int_arr2 += Pow2(j);
}
//printf("%i\t",int_arr2);
//Insert(int_arr2,my_list2,my_list2);
Position TempCell2;
TempCell2 = (Position)malloc(sizeof(struct Node));
TempCell2 -> element = int_arr2;
TempCell2 -> next = my_list2 -> next;
my_list2 -> next = TempCell2;
}
fclose(fp1);
fclose(fp2);
int all_arr[12] = {-1};
int* r_ptr = (int* )malloc(sizeof(int));
int* head = r_ptr;
int count = 0;
for(int i = 0;i < 2 * MAX_BIN;++i){
if(i < MAX_BIN){
all_arr[i] = my_list1 -> element;
//printf("%i\n\n",);
my_list1 = my_list1 -> next;
}
all_arr[i] = my_list2 -> element;
my_list2 = my_list2 -> next;
}
//for(int i = 0;i < 12;++i){
//printf("%i\n",all_arr[i]);
// }
Sort(all_arr,2 * MAX_BIN);
/*for(int i = 0;i < 12;++i){
printf("%i\n",all_arr[i]);
}*/