1. 不用循环和递归,实现打印数字0到999。
1 #include <iostream> 2 #include<stdio.h> 3 using namespace std; 4 5 #define A(x) x;x;x;x;x;x;x;x;x;x; 6 7 int main(void) 8 { 9 int n = 0; 10 11 A(A(A(printf("%d ", n++)))); 12 13 system("pause"); 14 return 0; 15 }
2. 写一个函数找出一个整数数组中第二大的数。
如:a[10]={1,2,3,4,5,6,7,8,9,10} ==> nextmax = 9;
a[10]={1,2,3,4,5,6,7,10,10,10} ==> nextmax = 7;
a[10]={8,8,8,2,8,8,8,8,8,8} ==> nextmax = 2;
a[10]={8,8,8,8,8,8,8,8,8,8} ==> nextmax = 不存在;
1 // C语言实现如下 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 int getmax(int *p, int n) 6 { 7 //假定第一个数最大,与剩下的数逐一进行比较 8 int maxdata = p[0]; //最大数的值 9 int maxi = 0; //最大数的下标 10 11 for (int i = 1; i < n; i++) 12 { 13 if (maxdata<p[i]) 14 { 15 maxdata = p[i]; 16 maxi = i; 17 } 18 } 19 20 return maxi; //返回下标 21 } 22 23 void swap(int *p1, int *p2) //根据地址交换两个变量 24 { 25 int temp = *p1; 26 *p1 = *p2; 27 *p2 = temp; 28 } 29 30 31 void main() 32 { 33 int a[10] = { 8,8,8,8,5,8,8,8,8,8 }; 34 //printf("%d ", getmax(a, 10)); 35 int maxi = getmax(a, 10); //保留最大值的下标 36 int max = a[getmax(a, 10)]; //保留最大值 37 38 swap(&a[0], &a[maxi]); 39 40 int i = 1; 41 int flag = 0; //0代表没有第二大的数,比如全部相同时 42 while (i<10) 43 { 44 int nextmaxi = i + getmax(a + i, 10-i); //注意下标+1 45 int nextmax = a[nextmaxi]; 46 47 if (max != nextmax) 48 { 49 printf(" nextmax = %d", nextmax); 50 flag = 1; //代表数组并非全部相同 51 break; 52 } 53 54 swap(&a[i],&a[nextmaxi]); 55 i++; 56 } 57 58 if (!flag) 59 { 60 printf("next max不存在"); 61 } 62 63 system("pause"); 64 return ; 65 }
3. 看程序写结果:
char str1[] = "abc"; char str2[] = "abc"; const char str3[] = "abc"; const char str4[] = "abc"; const char *str5 = "abc"; const char *str6 = "abc"; char *str7 = "abc"; char *str8 = "abc"; cout << (str1 == str2) << endl; //0 不等 cout << (str3 == str4) << endl; //0 cout << (str5 == str6) << endl; //1 相等 cout << (str7 == str8) << endl; //1
4. 结构体重要考点:
struct name1 { char t; char k; unsigned short i; unsigned long m; }; struct name2 { char str2; short x2; int num2; }; struct name3 { char str3; int num3; short x3; }; printf("%d ", sizeof(name1)); //8 printf("%d ", sizeof(name2)); //8 printf("%d ", sizeof(name3)); //12
5. 用宏定义写出swap(x,y),即交换两数。
1 #include <iostream> 2 using namespace std; 3 4 //方法一:加减 5 #define swap(x,y) (x)=(x)+(y);(y)=(x)-(y);(x)=(x)-(y); //注意需要加上() 6 7 //方法二:异或 8 #define swap(x,y) (x)=(x)^(y);(y)=(x)^(y);(x)=(x)^(y); //注意需要加上() 9 10 void main() 11 { 12 int a = 10, b = 20; 13 cout << "交换前:" << endl; 14 cout << "a:" << a << " " << "b:" << b << endl; 15 16 swap(a, b); 17 18 cout << "交换后:" << endl; 19 cout << "a:" << a << " " << "b:" << b << endl; 20 21 system("pause"); 22 return ; 23 }
6. 指针与整数相加: ====>与类型密切相关!
unsigned char *p1; unsigned long *p2; p1 = (unsigned char *)0x801000; p2 = (unsigned long *)0x810000; cout << p1 + 5 << endl; //0x801005 cout << p2 + 5 << endl; //0x810020
7. 数组指针的步长问题:
int a[5] = { 1,2,3,4,5 }; int *ptr = (int *)(&a + 1); printf("%d,%d", *(a + 1), *(ptr - 1)); // 2,5
8. 关于switch,输出结果是什么:
1 #include <iostream> 2 using namespace std; 3 4 int func(int a) 5 { 6 int b; 7 switch (a) 8 { 9 case 1: 10 b = 30; 11 case 2: 12 b = 20; 13 case 3: 14 b = 10; 15 default: 0; 16 } 17 return b; 18 } 19 20 void main() 21 { 22 printf("%d ", func(1)); //10 23 24 system("pause"); 25 return ; 26 }
9. 关于malloc的用法:
1 #include <iostream> 2 using namespace std; 3 4 void main() 5 { 6 //char *p = (char *)malloc(0); //分配字节为0,仍然会返回地址,只有分配失败才会返回NULL 7 //printf("%d ", p); //5210848 8 9 char *ptr= (char *)malloc(0); 10 if (ptr == NULL) 11 cout << "得到NULL指针" << endl; 12 else 13 cout << "得到非空指针" << endl; 14 15 system("pause"); 16 return ; 17 }
10. 编写strcpy函数:
已知strcpy函数的原型是char *strcpy(char *strDest,char *strSrc);其中strDest是目的字符串,strSrc是源字符串。
(1)不调用C/C++的字符串库函数,请编写出函数strcpy。
1 #include <iostream> 2 #include<assert.h> 3 using namespace std; 4 5 char *strcpy(char *strDest,char *strSrc) 6 { 7 assert((strDest != NULL) && (strSrc != NULL)); //异常处理 8 //if ((strDest == NULL) && (strSrc == NULL)) 9 //{ 10 // cout << "异常..." << endl; 11 //} 12 13 char *address = strDest; 14 while ((*strDest++ = *strSrc++) != '