当前位置:首页 > 第五章习题答案.
t=pi[j]; //交换索引数
组元素值
pi[j]=pi[j+1]; pi[j+1]= t;
} } }
void Output(const person all[], int pi[], const
int n)
{
for( int i=0; i 后数据 cout< } 5.有以下结构说明和遍历单向链表的函数。函数内有错误吗?是什么性质的错误?请上机验证你的分析。 struct Node { int data; Node ? next; }; void ShowList( Node ?head ) { while( head ) { cout << head->date << '\\n'; head ++; } } 【解答】 head++错误,原因是动态链表的结点存放不是连续顺序的内存空间,它们是逐个结点通过new建立的,所以不能用++做地址偏移运算。应该用: head=head->next; 5.4 编程题 1.编写程序,将一个整型变量右移4位,并以二进制形式输出该整数在移位前和移位后的数值。观察系统填补空缺的数位情况。 #include void bitDisplay(unsigned value); int main() { unsigned x; cout << \ cin >> x; bitDisplay(x); x>>=4; 2.数 cout<<\ bitDisplay(x); } void bitDisplay(unsigned value) { unsigned c; unsigned bitmask = 1<<31; cout << value << \ for( c=1; c<=32; c++ ) { cout << ( value&bitmask ? '1' : '0' ); value <<= 1; if( c%8 == 0 ) cout << ' '; } cout << endl; } 整数左移一位相当于将该数乘以2。编写一个函 unsigned power2( unsigned number, unsigned pow ); 使用移位运算计算number*2pow,并以整数形式输出计算结果。注意考虑数据的溢出。 【解答】 unsigned power2( unsigned number, unsigned pow ) { unsigned c=1; unsigned bitmask = 1<<31; while(c<31) //溢出判断 { if( number&bitmask ) break; //查找最 高位的1 即判断c为何值时最高位为1,判断可左移的最大次数 c++; bitmask>>=1; } if(pow cout<<\
共分享92篇相关文档