为了防止在评论中进行长时间的讨论,我将尝试编写一些前置条件和后置条件。
由于无法在函数内部测试是否使用指向有效列表对象的指针调用它,这属于父/调用者:
// The following function must be called with pointers that are either null
// or point to valid list elements. The lists must be correct (no malloc bugs etc).
// The compiler must have checked that it is called with pointers to the proper types,
// as C has no typeof operator.
//
int uguaglianza_insiemi(elem_lista_t *insieme_A,
elem_lista_t *insieme_B)
{
int esito;
if ((insieme_A == NULL) &&
(insieme_B == NULL))
esito = 1; // both pointers are null: equal
// not both pointes are null
else if (insieme_A == NULL ||
insieme_B == NULL)
esito = 0; // not both pointers are null, but one is: not equal
// neither pointer is null and so they may be dereferenced
else if (insieme_A->valore != insieme_B->valore)
esito = 0; // neither pointer is null, but their element values aer not equal: not equal
// the function can be called recursively now because its precondition has been met,
// that both successor pointers are null or point to valid list elements (induction).
else esito = uguaglianza_insiemi(insieme_A->succ_p,
insieme_B->succ_p);
// the post condition is that esito reflects equality of both (partial) lists
return (esito);
}
我希望这是你和你教授可以合作的东西。
{P}:必须使用为空或指向有效列表元素的指针调用该函数。
C:uguaglianza_insiemi( *A, *B)
{Q}:函数结果反映列表的相等性
在函数内部,这继续if
使用组合规则的语句。