gpt4 book ai didi

c - 修复某些部分的错误输出

转载 作者:太空宇宙 更新时间:2023-11-03 23:37:44 25 4
gpt4 key购买 nike

在我给出的指示中,接近尾声时,员工数据必须在之前删除“333”列后按顺序显示每列 222、666、444 和 555。我需要帮助才能将“666”列放入“333”列的原始位置。

#define _CRT_SECURE_NO_WARNINGS
#define SIZE 4

#include <stdio.h>

struct employee {

int number;
int age;
double salary;
};

int main(void) {
struct employee emp[SIZE] = { { 0,0,0 } }; //struct w/ array
int option = 1; //option variable
int nEmp = 0; //counting how many employee we have so far
int empIndex = 0;
int i, check, sNumber;


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: //print employee information
printf("EMP ID EMP AGE EMP SALARY\n");
printf("====== ======= ==========\n");

for (i = 0; i < nEmp; i++) {

if (emp[i].number > 0 && emp[i].age > 0 && emp[i].salary)

printf("%d %d %.2lf\n", emp[i].number, emp[i].age, emp[i].salary);
}
printf("\n");

break;

case 2: //add employee
printf("Adding Employee\n");
printf("===============\n");

if (nEmp < SIZE) {

empIndex = 0;

while ((emp[empIndex].number != 0) && (empIndex < SIZE)) {
empIndex++;
}

printf("Enter Employee ID: ");
scanf("%d", &emp[empIndex].number);
printf("Enter Employee Age: ");
scanf("%d", &emp[empIndex].age);
printf("Enter Employee Salary: ");
scanf("%lf", &emp[empIndex].salary);
printf("\n");
nEmp++;

}
else {

printf("ERROR!!! Maximum Number of Employees Reached\n\n");
}

break;

case 3: //update employee
printf("Update Employee Salary\n");
printf("======================\n");

if (nEmp == 0) { //alternative just in case there is no employee yet
printf("\nNo employee to update\n\n");

break;
}
do
{
check = 1;

printf("Enter Employee ID: ");
scanf("%d", &sNumber);

for (i = 0; i < SIZE; i++)
{
if (emp[i].number == sNumber)

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) { //in case there is no employee yet

printf("\nNo employee to remove\n\n");

break;
}
do
{
check = 1;

printf("Enter Employee ID: ");
scanf("%d", &sNumber);

for (i = 0; i < SIZE; i++)
{
if (emp[i].number == sNumber)

break;

else if (i == nEmp - 1)

printf("*** ERROR: Employee ID not found! ***\n");
}

if (i != nEmp) {
check = 0;

printf("Employee %d will be removed\n\n", emp[i].number);
emp[i].number = 0;
emp[i].age = 0;
emp[i].salary = 0.0;

nEmp -= 1;
}

} while (check);

break;

case 0: //exiting process

printf("Exiting Employee Data Program. Good Bye!!!\n");

break;

default: //not valid option input

printf("ERROR: Incorrect Option: Try Again\n\n");

break;
}
//if (option != 0) option = -1;
}
return 1;
}

我希望最终输出是:

EMP ID  EMP AGE EMP SALARY
====== ======= ==========
222 22 22222.22
666 66 66666.66
444 44 44444.44
555 55 55555.55

而不是(我目前得到的)

EMP ID  EMP AGE EMP SALARY
====== ======= ==========
222 22 22222.22
444 44 44444.44
555 55 55555.55
666 66 66666.66

在退出程序之前。

最佳答案

您的 for 循环 ( for (i = searchedI; i < nEmp; i++) { ... } ) 将所有元素移到前面一个位置。要获得所需的结果,只需将最后一个元素移动到要删除的位置(下面的代码显示了完整的 if block ):

if (i != nEmp)
{
check = 0;
printf("Employee %d will be removed\n\n", emp[i].number);

--nEmp; // doing this first spares you additional subtractons later...
if (i != nEmp) // last element does not have to be moved...
emp[i] = emp[nEmp];
emp[nEmp].number = 0; // actually redundant
}

关于c - 修复某些部分的错误输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54760015/

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