在某些部分修复错误的输出

问题描述 投票:2回答:2

在我给出的说明中,接近结尾,员工数据必须在删除列'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;
}

我希望FINAL输出为:

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

在退出程序之前。

c switch-statement
2个回答
0
投票

我猜你的问题其实是这个。最初,您的阵列看起来如下所示:

EMP ID  EMP AGE EMP SALARY
======  ======= ========== 
222       22   22222.22 
333       33   33333.33 
444       44   44444.44 
555       55   55555.55

您希望将其更改为以下内容:

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

在这种情况下,我建议你做的是当用333删除列时,用0替换所有struct成员(在这种情况下0表示该列为空)而不是移动列。在添加新员工的代码中,检查是否有任何员工编号为0.这将为您提供新员工添加位置的新位置。

基本上,对“删除员工”代码进行以下更改

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;
   }

并对您的“添加员工”代码进行此更改

  if (nEmp < SIZE) {

   int empIndex = 0; //new variable to find the next possible location to store the employee

   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++;
  }

确保使用empIndex而不是nEmp来存储新员工。请注意,如果您删除了某个员工,但未添加任何员工,则会为您删除的该条目打印出零。另外,请注意,这意味着您需要打印出阵列中的所有成员而不是前几个成员,因为您可能在阵列的最后一个位置有一个员工,但中间没有员工。


1
投票

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

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
}
© www.soinside.com 2019 - 2024. All rights reserved.