C 如何将接受合并排序的数组转换为结构数组 #包括 #包括 结构雇佣 { int-id,年龄; 字符名称[100],desg[100]; 字符秒; }; 无效合并排序(结构Empoly emp、整数低位、整数中频、整数高位) { inti,j,k,l,b[50]; l=低; i=低; j=mid+1; 而((l
您发布的代码有一些严重的问题。它甚至没有编译,更不用说修改结构数组的C 如何将接受合并排序的数组转换为结构数组 #包括 #包括 结构雇佣 { int-id,年龄; 字符名称[100],desg[100]; 字符秒; }; 无效合并排序(结构Empoly emp、整数低位、整数中频、整数高位) { inti,j,k,l,b[50]; l=低; i=低; j=mid+1; 而((l,c,data-structures,C,Data Structures,您发布的代码有一些严重的问题。它甚至没有编译,更不用说修改结构数组的mergeSort()) 问题是: 无break;案例1: 否默认值:大小写 对merge()函数的函数调用不正确 如果用户调用merge()函数而没有首先在emp数组中输入数据,那么初始化n=0 如果要退出do while循环,则无退出条件 结构的数组到数组的定义在哪里?调用的传递数组的函数如何/在哪里?在发布的代码中显示它。如果您花时间将帖子包含在一个(一)结构中,对每个人(包括您)都会更快更好。struct Empoly
mergeSort()
)
问题是:
break;
案例1:默认值:
大小写merge()
函数的函数调用不正确merge()
函数而没有首先在emp
数组中输入数据,那么初始化n=0
结构的数组到数组的定义在哪里?调用的传递数组的函数如何/在哪里?在发布的代码中显示它。如果您花时间将帖子包含在一个(一)结构中,对每个人(包括您)都会更快更好。
struct Empoly emp
将emp
声明为一个(一)结构,而不是指针(in)如果你对int
的数组有一个有效的合并排序,但是你想对struct Empoly
的数组有一个合并排序,那么你只需将int
更改为struct Empoly
,只要前者表示被排序对象的类型,就可以实现这一目标o是以结构成员的形式重写元素比较。我会考虑编写一个单独的函数,特别是如果您计划使用一个以上的成员来确定相对元素的顺序。@ J.Burnrnor我试过了,但是我仍然有错误地感谢你们。code@matrix我很乐意。如果对你有帮助,你可以考虑r投票并接受答案:)。
#include <stdio.h>
#include <stdlib.h>
struct Employ
{
int id,age;
char name[100],desg[100];
char sec;
};
void mergeSort(struct Empoly emp,int low,int mid,int high)
{
int i, j, k, l, b[50];
l = low;
i = low;
j = mid + 1;
while ((l <= mid) && (j <= high))
{
if (a[l] <= emp[j])
{
b[i] = emp[l];
l++;
}
else
{
b[i] = a[j];
j++;
}
i++;
}
if (l > mid)
{
for (k = j; k <= high; k++)
{
b[i] = a[k];
i++;
}
}
else
{
for (k = l; k <= mid; k++)
{
b[i] = a[k];
i++;
}
}
for (k = low; k <= high; k++)
{
a[k] = b[k];
}
}
void merge(struct Empoly emp,int low,int high)
{
int mid;
if(low < high)
{
mid = (low + high) / 2;
merge(a, low, mid);
merge(a, mid + 1, high);
mergeSort(a, low, mid, high);
}
void rd(struct Employ emp[], int n)
{
int i;
for(i=0; i<n; i++){
printf("\nEmployee id: ");
scanf("%d", &emp[i].id);
printf("Employee name: ");
scanf("%s", emp[i].name);
printf("Employee age: ");
scanf("%d", &emp[i].age);
printf("Designation: ");
scanf("%s", &emp[i].desg);
fflush(stdin);
printf("GRade: ");
scanf("%c",&emp[i].sec);
}
printf("\nEntered details \n");
for(i=0; i<n; i++){
printf("\nEmployee Id: %d\n", emp[i].id);
printf("Name: %s\n", emp[i].name);
printf("Age: %d\n", emp[i].age);
printf("Designation: %s\n", emp[i].desg);
printf("grade : %c", emp[i].sec);
}
printf("\n\n");
}
int main()
{
struct Employ emp[10];
int n, ch, fg = 0, id,age,in;
int i;
int mid;
char name[100];
do
{
printf("Press 1 --- to input data\n");
printf("Press 2 ---- to do mergesort\n");
printf("Enter your'e choice\n");
scanf("%d", &ch);
switch(ch)
{
case 1:
fg=0;
while(fg==0)
{
printf("Enter the total number of employees: ");
scanf("%d", &n);
if(n>=1 && n<=10)
fg = 1;
else
printf("Invalid input\n");
}
rd(emp, n);
case 2:
merge(emp, 0, n - 1);
printf("The sorted array\n");
for(i=0;i<n;i++)
printf("%d\t",emp[i].age);
printf("\n\n\n");
break;
}
#include <stdio.h>
#include <stdlib.h>
struct Employ
{
int id, age;
char name[100], desg[100];
char sec;
};
void mergeSort(struct Employ emp[], int low, int mid, int high)
{
int i, j, k, l;
// Here you need a temporary array of type struct like below
struct Employ b[50];
l = low;
i = low;
j = mid + 1;
while ((l <= mid) && (j <= high))
{
/*
the if condition below decides on which parameter
the sorting is done. Age is used below.
For sorting by name in alphabetical order, use stcmp() function
if (strcmp(emp[l].name, emp[j].name)<=0)
*/
if (emp[l].age <= emp[j].age)
{
b[i] = emp[l];
l++;
}
else
{
b[i] = emp[j];
j++;
}
i++;
}
if (l > mid)
{
for (k = j; k <= high; k++)
{
b[i] = emp[k];
i++;
}
}
else
{
for (k = l; k <= mid; k++)
{
b[i] = emp[k];
i++;
}
}
for (k = low; k <= high; k++)
{
emp[k] = b[k];
}
}
void merge(struct Employ emp[], int low, int high)
{
int mid;
if(low < high)
{
mid = (low + high) / 2;
merge(emp, low, mid);
merge(emp, mid + 1, high);
mergeSort(emp, low, mid, high);
}
}
void rd(struct Employ emp[], int n)
{
int i;
for(i=0; i<n; i++)
{
printf("\nEmployee id: ");
scanf("%d", &emp[i].id);
printf("Employee name: ");
scanf("%s", emp[i].name);
printf("Employee age: ");
scanf("%d", &emp[i].age);
printf("Designation: ");
scanf("%s", &emp[i].desg);
fflush(stdin);
printf("GRade: ");
scanf(" %c",&emp[i].sec);
}
printf("\nEntered details \n");
for(i=0; i<n; i++)
{
printf("\nEmployee Id: %d\n", emp[i].id);
printf("Name: %s\n", emp[i].name);
printf("Age: %d\n", emp[i].age);
printf("Designation: %s\n", emp[i].desg);
printf("grade : %c", emp[i].sec);
}
printf("\n\n");
}
int main()
{
struct Employ emp[10];
int n=0, ch, fg = 0, id, age, in;
int i;
int mid;
char name[100];
do
{
printf("Press 0 --- to end the program\n");
printf("Press 1 --- to input data\n");
printf("Press 2 ---- to do mergesort\n");
printf("Enter your choice\n");
scanf("%d", &ch);
switch(ch)
{
case 0:
printf("\n\n-----------Program ended----------\n\n");
return 0;
case 1:
fg=0;
while(fg==0)
{
printf("Enter the total number of employees: ");
scanf("%d", &n);
if(n>=1 && n<=10)
fg = 1;
else
printf("Invalid input\n");
}
rd(emp, n);
break;
case 2:
merge(emp, 0, n - 1);
printf("The sorted array\n");
for(i=0;i<n;i++)
printf("%d\t",emp[i].age);
printf("\n\n\n");
break;
default:
printf("Incorrect choice\n");
}
}while(1);
}
Input/Output example:
Press 0 --- to end the program
Press 1 --- to input data
Press 2 ---- to do mergesort
Enter your choice
1
Enter the total number of employees: 3
Employee Id: 1
Name: abc1
Age: 25
Designation: xyz
grade : A
Employee Id: 2
Name: abc1
Age: 45
Designation: xyz
grade : A
Employee Id: 3
Name: abc3
Age: 12
Designation: xyz
grade : A
Press 0 --- to end the program
Press 1 --- to input data
Press 2 ---- to do mergesort
Enter your choice
2
The sorted array
12 25 45
Press 0 --- to end the program
Press 1 --- to input data
Press 2 ---- to do mergesort
Enter your choice
33
Incorrect choice
Press 0 --- to end the program
Press 1 --- to input data
Press 2 ---- to do mergesort
Enter your choice
0
-----------Program ended----------