1. 语句 cout<<(1&2)<<", "<<(1&&2)<<endl; 的输出结果是( )。 A.0, 0 B.0, 1 C.1, 0 D.1, 1 答题: A. B. C. D. (已提交) -->参考答案:B 问题解析: |
2. 语句 cout<<(1|2)<<", "<<(1||2)<<endl; 的输出结果是( )。 A.0, 0 B.1, 1 C.2, 0 D.3, 1 答题: A. B. C. D. (已提交) -->参考答案:D 问题解析: |
3. 语句 cout<<(3<<3)<<endl; 的输出结果是( )。 A.24 B.12 C.9 D.6 答题: A. B. C. D. (已提交) -->参考答案:A 问题解析: |
4. 语句 cout<<(24>>3)<<endl; 的输出结果是( )。 A.12 B.9 C.6 D.3 答题: A. B. C. D. (已提交) -->参考答案:D 问题解析: |
5. 语句 cout<<(2^5)<<endl; 的输出结果是( )。 A.1 B.3 C.7 D.10 答题: A. B. C. D. (已提交) -->参考答案:C 问题解析: |
6. 有以下说明语句: struct point { int x; int y; }p; 则正确的赋值语句是( )。 A.point.x = 1; point.y = 2; B.point={ 1, 2 }; C.p.x = 1; p.y = 2; D.p = { 1, 2 }; 答题: A. B. C. D. (已提交) -->参考答案:C 问题解析: |
7. 已知有职工情况结构变量emp,定义为: struct Date { int year; int month; int day; }; strnct Employee { char name[20]; long code; Date birth }; Employee emp; 下列对emp 的birth进行正确赋值的方法是( )。 A.year=1980; month=5; day=1; B.birth.year=1980; birth.month=5; birth.day=1; C.emp.year=1980; emp.month=5; emp.day=1; D.emp.birth.year=1980; emp.birth.month=5; emp.birth.day=1; 答题: A. B. C. D. (已提交) -->参考答案:D 问题解析: |
8. s1和s2是两个结构类型变量,若要使赋值s1=s2合法,则要求( )。 A.s1只接收s2中相同类型的数据成员 B.s1和s2中的数据成员个数相同 C.s1和s2是同一结构类型的变量 D.s1和s2是存储字节长度一样的变量 答题: A. B. C. D. (已提交) -->参考答案:C 问题解析: |
思考题
1.判断一个整数n的奇偶性,可以利用位运算判断吗?请你试一试。
【解答】可以利用位运算判断。对于整数n,表达式n&1 可以用来判断a的奇偶性。二进制的末位为0表示偶数,最末位为1表示奇数。
2.长度为N的数组可以表示N个元素的集合,若有:
S[i]==1,表示对应元素在集合中
如何实现集合的基本运算?请你试一试。并从内存和处理要求上与5.2.2节中集合的实现方法进行比较。
【解答】
若有
bool A[N], B[N], C[N];
则
并集 C=A∪B
for( i=0; i<N; i++) { C[i] = A[i] || B[i] };
交集 C=A∩B,
for( i=0; i<N; i++){ C[i] = A[i] && B[i]; } ;
差集 C=A-B
for( i=0; i<N; i++){ C[i] = A[i]>B[i];}
包含AB为真
bool b=true;
for( i=0; i<N; i++)
{ b=B[i]>=A[i]; if(!b) break; }
补集 A的补集 C=~A
for( i=0; i<N; i++) { C[i]=!A[i];}
判空集 若A空集,判断结果为真
bool b=true;
for( i=0; i<N; i++)
{ b=!A[i] ; if(b) break; }
判属于 若x属于A,判断结果为真
bool b=A[x-1];
用数组实现集合运算与用整数位表示集合比较,内存要求较高,而运算速度较低。
3.分析以下说明结构的语句:
struct Node
{ int data;
Node error; //错误
Node * ok; //正确
};
error和ok分别属于什么数据类型?有什么存储要求?error出错的原因是什么?
【解答】
error是Node结构类型数据成员,错误。原因是结构定义的数据成员若为本身的结构类型,是一种无穷递归。ok是指Node类型的指针,定义正确,占4字节。
4.本章例5-8中用辅助数组对结构数组进行关键字排序,有定义:
person *index[100];
index数组存放结构数组元素的地址。如果把index定义改为:
int index[100];
用于存放结构数组元素的下标,可以实现对结构数组的索引排序吗?如何修改程序?请你试一试。
【解答】
可以。关键是通过整型索引数组元素作为下标访问结构数组。表示为:
all[pi[i]].name all[pi[i]].id all[pi[i]].salary
程序如下:
#include<iostream>
using namespace std;
struct person //说明结构类型
{ char name[10];
unsigned int id;
double salary;
} ;
void Input( person[], const int );
void Sort( person[], int[],const int );
void Output( const person[], int[],const int );
int main()
{ person allone[100] ; //说明结构数组
int index[100]; //说明索引数组
int total ;
for(int i=0; i<100; i++) //索引数组元素值初始化为结构数组元素下标
index[i]=i ;
cout<<"输入职工人数:";
cin>>total;
cout<<"输入职工信息:\n";
Input(allone,total);
cout<<"以工资做关键字排序\n";
Sort(allone,index, total);
cout<<"输出排序后信息:\n";
Output(allone,index,total);
}
void Input( person all[], const int n )
{ int i ;
for( i=0; i<n; i++ ) // 输入数据
{ cout<<i<<": 姓名: ";
cin>>all[i].name;
cout<<"编号: ";
cin >> all[i].id;
cout<<"工资: ";
cin >> all[i].salary ;
}
}
void Sort(person all[], int pi[], const int n)
{ int i,j;
int t; //交换用中间变量
for(i=1; i<n; i++) //以成员salary做关键字排序
{ for(j=0; j<=n-1-i; j++)
if(all[pi[j]].salary>all[pi[j+1]].salary) //通过索引数组访问结构数组元素
{ 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<n; i++ ) // 输出排序后数据
cout<<all[pi[i]].name<<'\t'<<all[pi[i]].id<<'\t'<<all[pi[i]].salary<<endl;
}
5.有以下结构说明和遍历单向链表的函数。函数内有错误吗?是什么性质的错误?请上机验证你的分析。
struct Node
{ int data;
Node * next;
};
void ShowList( Node *head )
{ while( head )
{ cout << head->date << '\n';
head ++;
}
}
【解答】
head++错误,原因是动态链表的结点存放不是连续顺序的内存空间,它们是逐个结点通过new建立的,所以不能用++做地址偏移运算。应该用:
head=head->next
编程题
1.整数左移一位相当于将该数乘以2。编写一个函数
unsigned power2( unsigned number, unsigned pow );
使用移位运算计算number*2pow,并以整数形式输出计算结果。注意考虑数据的溢出。
【解答】
#include<iostream>
using namespace std;
unsigned power2(unsigned number, unsigned pow);
int main()
{
int n, p, t;
cout<<"input n = ";
cin>>n;
cout<<"input p = ";
cin>>p;
t=power2(n, p);
if(t==-1)
cout<<"overflow!\n";
else
cout<<"power2("<<n<<", "<<p<<" ) = "<<t<<endl;
}
unsigned power2(unsigned number, unsigned pow)
{
unsigned t, m, c;
t=number;
m=1<<31; //掩码
for(c=1; c<31; c++) //判断可以左移位数
{
if(m&t) break;
t<<=1;
}
if(pow>c-1) //判断溢出
return -1;
return number<<pow;
}
2.使用结构类型表示复数。设计程序输入两个复数,可以选择进行复数的+、-、×或÷运算,并输出结果。
【解答】
#include <iostream>
#include <iomanip>
using namespace std;
struct complex
{ double re,im;
};
int main()
{ complex a,b,c; char oper;
cout << "输入复数a的实部和虚部: ";
cin >> a.re >> a.im;
cout << "输入复数b的实部和虚部:";
cin >> b.re >> b.im;
cout << "输入运算符: ";
cin >> oper;
switch ( oper )
{ case '+': c.re=a.re+b.re; c.im=a.im+b.im; break;
case '-': c.re=a.re-b.re; c.im=a.im-b.im; break;
case '*': c.re=a.re*b.re-a.im*b.im;
c.im=a.im*b.re+a.re*b.im; break;
case '/': c.re=(a.re*b.re+a.im*b.im)/(b.re*b.re+b.im*b.im);
c.im=(a.im*b.re-a.re*b.im)/(b.re*b.re+b.im*b.im);
break;
default: cout << "input error!" << endl;
return 0;
}
cout << "c=" << c.re;
cout << setiosflags( ios::showpos );
cout << c.im << "i" << endl;
return 0;
}
3.把一个班的学生姓名和成绩存放到一个结构数组中,寻找并输出最高分者。
【解答】
#include <iostream>
using namespace std;
int main()
{ struct data
{ char name[12];
double score;
}a[ ] = {"李小平",90,"何文章",66,"刘大安",87,"汪立新",93,"罗建国",
78,"陆丰收",81,"杨勇",85,"吴一兵",55,"伍晓笑",68,"张虹虹",93};
double max = a[0].score;
int i,n = sizeof(a) / sizeof(data);
for( i=1; i<n; i++ )
if( a[i].score > max ) max = a[i].score;
for( i=0; i<n; i++ )
if( a[i].score == max ) cout << a[i].name << endl;
}
|