1

如何在不更改 print_vector 函数的情况下从 C 中的向量中删除元素?

1)这是我为删除键盘给定位置上的元素而编写的代码:

void remove_a_cost(int a)
{
int nr, c;
printf("Give the number of cost for remove: ");
scanf("%d", &nr);
if(nr>a)
{
    printf("The remove is impossible!\n");
}
else
{
    for(c=nr;c<=a;c++)
    {
            chelt[c]=chelt[c+1];
    }
}
}

2)这是打印功能

void print_costs(int a)
{
int i;
if(a>0 && a<=n)
{
    for(i=1;i<=a;i++)
    {
        printf("\nCost %d\n\n",i);
        printf("Day: %s\n", chelt[i].day);
        printf("Sum: %d\n", chelt[i].sum);
        printf("Type: %s\n", chelt[i].type);
    }
}

}

3) 这是 add_new_cost() 函数

int add_new_cost()
{
    int a,i;
    printf("Nr of costs = ");
    scanf("%d", &a);
    if(a>0 && a<=n)
        {
            for(i=1;i<=a;i++)
                {
                    printf("\nType the attributes for cost %d",i);
                    printf("\nDay = ");
                    scanf("%s",chelt[i].day);
                    printf("Sum = ");
                    scanf("%d", &chelt[i].sum);
                    printf("Type = ");
                    scanf("%s",chelt[i].type);
                }
        }
        return a;
}

4)这是主要功能

int main()
{
    setbuf(stdout,NULL);
    int b,choice;

    do
    {
     printf("\nMenu\n\n");
     printf("1 - Add a cost\n");
     printf("2 - Print a cost\n");
     printf("3 - Update a cost\n");
     printf("4 - Delete a cost\n");
     printf("5 - Exit\n\n");
     printf("Command = ");
     scanf("%d",&choice);

     switch (choice)
     {
     case 1: b=add_new_cost();
              break;
     case 2: print_costs(b);
              break;
     case 3: update_cost(b);
             break;
     case 4: remove_a_cost(b);
             break;
     case 0: printf("Goodbye\n");
             break;
     default: printf("Wrong Choice. Enter again\n");
              break;
     }

    } while (choice != 0);
    return 0;
}

示例:如果向量上有 4 个元素:

1)Type the attributes for cost
Day = luni
Sum = 2
Type = dsasa

Type the attributes for cost 2
Day = marti
Sum = 23
Type = adsds

Type the attributes for cost 3
Day = miercuri
Sum = 23
Type = asd

Type the attributes for cost 4
Day = joi
Sum = 232
Type = asdas

我尝试删除,比如说第三个元素,这是我打印时收到的:

Cost 1
Day: luni
Sum: 20
Type: maradf

Cost 2
Day: marti
Sum: 23
Type: afas

Cost 3
Day: joi
Sum: 45
Type: sdfadsf

Cost 4
Day: 
Sum: 0
Type:

元素(COST 4)在它应该被删除时出现。是否有在不更改打印功能的情况下删除元素的解决方案?

4

2 回答 2

1

更新问题后,一切都清楚了,请进行以下修改:

int remove_a_cost(int a)
{
    int nr, c;
    printf("Give the number of cost for remove: ");
    scanf("%d", &nr);
    if (nr > a)
    {
        printf("The remove is impossible!\n");
    }
    else
    {
        for (c = nr; c <= a; c++)
        {
            chelt[c] = chelt[c + 1];
        }
        a--; // decrease a
    }
    return a; // return new size
}

switch (choice)
{
case 1: b = add_new_cost();
    break;
case 2: print_costs(b);
    break;
case 3: update_cost(b);
    break;
case 4: b = remove_a_cost(b); // <- store returned value in b
    break;
case 0: printf("Goodbye\n");
    break;
default: printf("Wrong Choice. Enter again\n");
    break;
}
于 2013-03-09T17:21:05.933 回答
0

你的代码有点乱。您应该尝试为变量赋予有意义的名称。

但是,据我了解, print_costs 函数接收它应该打印的最后一个“成本”的值。

删除“成本”后,您可能传递了错误的值。

于 2013-03-09T16:36:22.243 回答