我试图从并行数组中删除一个元素
im trying to remove an element from parallel arrays
所以我有 4 个并行数组,我试图同时从它们中删除一个元素并在此过程中将它的大小减小一个,但是这段代码有问题:
void removeAccount(char names[MAXSIZE][MAXSIZE], int stdID[MAXSIZE], float phones[MAXSIZE], int registrationYear[MAXSIZE])
{
int ti; // to save the student number entered from the keyboard.
int tii; // to save the index of the entered student number.
if (size <= 0)
{
printf("the list is empty\n");
}
printf("please enter a studnet number\n");
scanf("%d", &ti);
for (int f = 0; f < size; f++)
{
if (stdID[f] != ti)
{
printf("The student doesn't exists\n");
printf("Please enter a student number\n");
scanf("%d", &ti);
}
if (stdID[f] == ti)
{
tii = f;
break;
}
}
for (int v = tii - 1; v < size - 1; tii++)
{
strcpy(names[v], names[v + 1]);
phones[v] = phones[v + 1];
stdID[v] = stdID[v + 1];
registrationYear[v] = registrationYear[v + 1];
}
size--;
对于初学者来说,当函数依赖于全局变量时,这是一个糟糕的函数设计,因为您的函数依赖于全局变量 size
。
这段代码
printf("please enter a studnet number\n");
scanf("%d", &ti);
for (int f = 0; f < size; f++)
{
if (stdID[f] != ti)
{
printf("The student doesn't exists\n");
printf("Please enter a student number\n");
scanf("%d", &ti);
}
if (stdID[f] == ti)
{
tii = f;
break;
}
}
没有意义。
你应该这样写
int pos;
do
{
printf("please enter a student number\n");
scanf("%d", &ti);
pos = 0;
while ( pos < size && stdID[pos] != ti ) ++pos;
if ( pos == size )
{
printf("The student doesn't exists\n");
}
} while ( pos == size );
这个for循环
for (int v = tii - 1; v < size - 1; tii++)
{
strcpy(names[v], names[v + 1]);
phones[v] = phones[v + 1];
stdID[v] = stdID[v + 1];
registrationYear[v] = registrationYear[v + 1];
}
也不正确。
它应该看起来像
while ( ++pos < size )
{
strcpy(names[pos - 1], names[pos]);
phones[pos - 1] = phones[pos];
stdID[pos - 1] = stdID[pos];
registrationYear[pos - 1] = registrationYear[pos];
}
所以我有 4 个并行数组,我试图同时从它们中删除一个元素并在此过程中将它的大小减小一个,但是这段代码有问题:
void removeAccount(char names[MAXSIZE][MAXSIZE], int stdID[MAXSIZE], float phones[MAXSIZE], int registrationYear[MAXSIZE])
{
int ti; // to save the student number entered from the keyboard.
int tii; // to save the index of the entered student number.
if (size <= 0)
{
printf("the list is empty\n");
}
printf("please enter a studnet number\n");
scanf("%d", &ti);
for (int f = 0; f < size; f++)
{
if (stdID[f] != ti)
{
printf("The student doesn't exists\n");
printf("Please enter a student number\n");
scanf("%d", &ti);
}
if (stdID[f] == ti)
{
tii = f;
break;
}
}
for (int v = tii - 1; v < size - 1; tii++)
{
strcpy(names[v], names[v + 1]);
phones[v] = phones[v + 1];
stdID[v] = stdID[v + 1];
registrationYear[v] = registrationYear[v + 1];
}
size--;
对于初学者来说,当函数依赖于全局变量时,这是一个糟糕的函数设计,因为您的函数依赖于全局变量 size
。
这段代码
printf("please enter a studnet number\n");
scanf("%d", &ti);
for (int f = 0; f < size; f++)
{
if (stdID[f] != ti)
{
printf("The student doesn't exists\n");
printf("Please enter a student number\n");
scanf("%d", &ti);
}
if (stdID[f] == ti)
{
tii = f;
break;
}
}
没有意义。
你应该这样写
int pos;
do
{
printf("please enter a student number\n");
scanf("%d", &ti);
pos = 0;
while ( pos < size && stdID[pos] != ti ) ++pos;
if ( pos == size )
{
printf("The student doesn't exists\n");
}
} while ( pos == size );
这个for循环
for (int v = tii - 1; v < size - 1; tii++)
{
strcpy(names[v], names[v + 1]);
phones[v] = phones[v + 1];
stdID[v] = stdID[v + 1];
registrationYear[v] = registrationYear[v + 1];
}
也不正确。
它应该看起来像
while ( ++pos < size )
{
strcpy(names[pos - 1], names[pos]);
phones[pos - 1] = phones[pos];
stdID[pos - 1] = stdID[pos];
registrationYear[pos - 1] = registrationYear[pos];
}