gpt4 book ai didi

c - 如何在数组中的某个位置插入

转载 作者:行者123 更新时间:2023-11-30 19:26:45 25 4
gpt4 key购买 nike

我的代码会将我添加到列表中的每个新员工附加到列表中。我希望它取代我刚刚删除的区域。因此,如果我要删除 333 并添加 666,我希望它成为列表中的第二个而不是末尾

#include <stdio.h>
#define SIZE 4

struct Employ {
int ID;
int age;
double salary;
};

int main(void) {
struct Employ emp[SIZE]={{0}};
int option = 1;
int nEmp = 0;
int i;
int searchedI;
int check;
int sID;
printf("---=== EMPLOYEE DATA ===---\n\n");
while (option != 0) {
printf("1. Display Employee Information\n");
printf("2. Add Employee\n");
printf("3. Update Employee Salary\n");
printf("4. Remove Employee\n");
printf("0. Exit\n\n");
printf("Please select from the above options: ");
scanf("%d", &option);
printf("\n");
switch (option)
{
case 1:
printf("EMP ID EMP AGE EMP SALARY\n");
printf("====== ======= ==========\n");
for (i = 0; i < nEmp; i++) {
printf("%6d%9d%11.2lf\n", emp[i].ID, emp[i].age, emp[i].salary);
}
printf("\n");
break;
case 2:
printf("Adding Employee\n");
printf("===============\n");
if (nEmp == SIZE) {
printf("ERROR!!! Maximum Number of Employees Reached\n\n");
}
else {
check = 1;
while (check) {
printf("Enter Employee ID: ");
scanf("%d", &emp[nEmp].ID);
printf("Enter Employee Age: ");
scanf("%d", &emp[nEmp].age);
printf("Enter Employee Salary: ");
scanf("%lf", &emp[nEmp].salary);
if (emp[nEmp].ID < 0 || emp[nEmp].age < 0 || emp[nEmp].salary < 0) printf("\nAll number should be positive\n");
else check = 0;
}
printf("\n");
nEmp++;
}
break;
case 3: //update employee
printf("Update Employee Salary\n");
printf("======================\n");
if (nEmp == 0) {
printf("\nNo employee to update\n\n");
break;
}
do
{
check = 1;
printf("Enter Employee ID: ");
scanf("%d", &sID);
for (i = 0; i < nEmp; i++)
{
if (emp[i].ID == sID) break;
else if (i == nEmp - 1) printf("*** ERROR: Employee ID not found! ***\n");
}
if (i != nEmp) {
printf("The current salary is %.2f\n", emp[i].salary);
printf("Enter Employee New Salary: ");
scanf("%lf", &emp[i].salary);
check = 0;
printf("\n");
}
} while (check);
break;
case 4: //remove employee
printf("Remove Employee\n");
printf("===============\n");
if (nEmp == 0) {
printf("\nNo employee to remove\n\n");
break;
}
do
{
check = 1;
printf("Enter Employee ID: ");
scanf("%d", &sID);
for (i = 0; i < nEmp; i++)
{
if (emp[i].ID == sID) break;
else if (i == nEmp - 1) printf("*** ERROR: Employee ID not found! ***\n");
}
if (i != nEmp) {
check = 0;
searchedI = i;

printf("Employee %d will be removed\n\n", emp[searchedI].ID);

for (i = searchedI; i < nEmp; i++)
{
if (i != nEmp - 1) emp[i] = emp[i + 1];
else emp[i].ID = 0;


}
nEmp -= 1;

}

} while (check);

break;
case 0:
printf("Exiting Employee Data Program. Good Bye!!!\n");
break;
default:
printf("ERROR: Incorrect Option: Try Again\n\n");
break;
}

}
return 1;
}

原始列表222第333章第444章

我希望列表显示222第666章第444章

现在显示的结果是222第444章第666章

最佳答案

删除值时,您可以将其替换为占位符(例如无穷大),而不是将其从列表中删除。当您稍后想要向列表添加新值时,您可以扫描列表以查找此占位符,然后:

  • 如果存在:将其替换为新值
  • 如果列表不包含此占位符:将该值添加到列表末尾

关于c - 如何在数组中的某个位置插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56652008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com