E2 Start with the stack methods, and write a function copy_stack with the following specifications:
Error_code copy_stack(stack&dest,stack&source);
precondition:none
postcondition : Stack has become an exact copy of stack source ;source is unchanged.If an error is detected ,an appropriate code is returned;otherwise, a code of success is returned.
(b) Use the stack methods and a temporary stack to retrieve entries from the stack source and add each entry to the stack dest and restore the stack source.
Answer:
Error_code copy_stack(stack&dest,stack&source)
{Error_code result=success;
stack temp;
stack_entry item;
while(!source.empty()&&result==success)
{result=source.top(item);
result=source.pop();
if(result==success) temp.push(item);
}
while(!temp.empty()&&result=success)
{result=temp.top(item);
result=temp.pop();
if(result==success)
{dest.push(item);
source.push(item);}
return result;
}
显示有一个error,但一直没找到。