1. What does the following program print?
#include <iostream>
Using namespace std;
int main()
{
int x=2,y,z;
x*=(y=z=5); cout<<x<<endl ;
z=3 ;
x= =(y=z) ; cout<<x<<endl ;
x=(y= =z) ; cout<<x<<endl ;
x=(y&z) ; cout<<x<<endl ;
x=(y&&z) ; cout<<x<<endl ;
y=4 ;
x=(y|z) ; cout<<x<<endl ;
x=(y||z) ; cout<<x<<endl ;
return 0 ;
}
主要考查赋值,比较,与,或等运算符
答案:10,10,1,3,1,7
2.下面两段代码输出结果有什么不同? 1:#include <iostream> using namespace std; main() { int a,x; for(a=0,x=0;a<=1&&!x++;a++)a++ cout<<a<<x<<endl; } 2: #include <iostream> using namespace std; main() { int a,x; for(a=0,x=0;a<=1&&!x++;)a++; cout<<a<<x<<endl; } 2,1 1,2 3用一个表达式,.判断一个数X是否是 次方(2,4,8,16…..),不可用循环语句。 !(X&(X-1)) 4.下面程序的结果是多少? #include <iostream> #include <string> using namespace std; main() { int count; int m=999; while(m) { count++; m=m&(m-1); } cout<<count; } m二进制中1的个数 5、a、b交换 如何将a、b的值进行交换,不用任何中间变量。 a=a+b; b=a-b; a=a-b; a=a^b; b=a^b; a=a^b; 6.在c++程序中调用C编译器后的函数,为什么要加extern “C”? C++语言支持函数重载,C语言不支持函数重载。函数被C++编译后在库中的名字与C语言的不同。C++提供了C连接交换指定符号extern “C”解决名字匹配问题。 7、头文件中的ifndef/define/endif是干什么用的? 防止该头文件被重复引用。
8、一个5位数字ABCDE*4=EDCBA,这5个数字不重复,请编程求出来这个数字是多少? #include <iostream> using namespace std; int main() { for(int i=10000;i<100000;i++) { int j=0; int t=I; while(t!0) { j=j*10+t%10 ; t/=10 ; } if(i<<2)==j){ cout<<i ; break ; } } 9用预处理命令#define声明一个常数,用以表明1年中有多少秒? #define SECONDS_PER_YEAR (60*60*24*365)UL 10.const和 #define相比有什么不同? C++语言可以用const定义常量,也可以用#define定义常量,但是前者比后者又更多的优点: (1) const常量有数据类型,而宏常量没有数据类型。编译器可以对前者进行类型安全检查,而对后者只进行字符替换,没有类型安全检查,并且在字符替换中可能会产生意料不到的错误。 (2) 有些集成化的调试工具可以对const常量进行调试,但是不能对宏常量进行调试。在C++程序中只使用const常量不使用宏常量,即const常量完全取代宏常量。 10.代码输出结果。 #include<iostream> #include<stdio.h> #include<string.h> using namespace std; struct{ short a1; short a2; short a3; }A; struct{ long a1; short a2; }B; int main() { char* ss1 = “0123456789”; char ss2[] = “0123456789”; char ss3[100]= “0123456789”; int ss4[100]; char q1[]=”abc”; char q2[]= ”a/n”; char* q3=”a/n”; char* str1=(char*)malloc(100) ; void* str2=(void*)malloc(100) ; cout<<sizeof(ss1)<< “ ”; 4 cout<<sizeof(ss2)<< “ ”; 11 cout<<sizeof(ss3)<< “ ”; 100 cout<<sizeof(ss4)<< “ ”; 400 cout<<sizeof(q1)<< “ ”; 4 cout<<sizeof(q2)<< “ ”; 3 cout<<sizeof(q3)<< “ ”; 4 cout<<sizeof(A)<< “ ”; 6 cout<<sizeof(B)<< “ ”; 8 cout<<sizeof(str1)<< “ ”; 4 cout<<sizeof(str2)<< “ ”; 4 return 0; }