C语言小测试
本博文为本人平时学习时自己写的测试代码(部分借鉴他人)。有时候有看不懂或不确定的代码,需要用这些基础性的小测试来验证确认。算法不一定是最好的,但管用。每天一点新发现,持续更新中。。。
测试平台:Ideone / Code::Bolcks / C在线工具|菜鸟工具
1. 把一个更大位数的数赋值给比它位数小的变量,会发生什么?
1 #include <stdio.h> 2 3 int main(void) { 4 5 short A = 0x111; // 2byte 16位 6 char B; // 1byte 8位 7 B = A ; 8 printf("%d",B); 9 10 return 0; 11 12 }
执行结果:
17
0x11对应十进制为 17
显然,把16位变量A的值赋值给比它位数小变量B,结果自动截去了前面多余的8位。
补充知识点:
C语言中各种类型变量的大小:
char 1byte
short 2byte
int 4byte
long 4byte
float 4byte
double 8byte
2. 一个数组n个元素a[n],每个元素1byte,现将首个元素高4位和最后一个元素低4位裁去, 使得剩余的(n-1)*8位数据组合成一个有(n-1)个元素的数组b[n-1]
1 #include <stdio.h> 2 3 int main(void) { 4 5 char a[6]={0,1,0,1,0,1}; 6 char b[5]; 7 int i,j; 8 9 for(i=0;i<5;i++) 10 for(j=0;j<5;j++) 11 b[j] = (a[i]<<4)|(a[i++]>>4); 12 13 for(i=0;i<5;i++) 14 printf("%x\n",b[i]); 15 16 return 0; 17 18 }
执行结果:
00 10 00 10 00
3. 数组定义与赋值
1 #include <stdio.h> 2 3 int main(void) { 4 5 int i,j; 6 int a[3],b[3]; 7 8 //Method 1 9 a[0] = 0; 10 a[1] = 1; 11 a[2] = 2; 12 for(i=0;i<3;i++) //for循环显示数组内容 13 printf("%d\n",a[i]); 14 15 //Method 2 16 int b[3]={ 0,1,2 }; 17 for(j=0;j<3;j++) //for循环显示数组内容 18 printf("%d\n",b[j]); 19 20 return 0; 21 22 }
执行结果:
0
1
2
0
1
2
4. 验证“字符串”宏的引用
①简单打印宏定义的字符串
1 #include <stdio.h> 2 #define Ver "1.00" 3 int main(void) { 4 printf("%s",Ver); 5 return 0; 6 }
执行结果:
1.00
②指针打印字符串
1 #include <stdio.h> 2 #define Ver "1.00" 3 int main(void) { 4 int i; 5 for(i=0;i<4;i++) 6 printf("%c",*(Ver+i)); 7 8 return 0; 9 }
执行结果:
1.00
③作为数组打印出来
1 #include <stdio.h> 2 #define Ver "1.00" 3 /*****字符串作为数组打印出来*****/ 4 int main(void) { 5 int i; 6 for(i=0;i<4;i++) //打印数组 7 printf("%c",Ver[i]); 8 9 printf("\n"); 10 11 for(i=0;i<4;i++) //每个字符的十六进制ASCII码值 12 printf("%x ",Ver[i]); 13 14 return 0; 15 }
执行结果:
1.00
31 2e 30 30
5. 库函数char *strstr(const char *haystack, const char *needle)。注意函参为实参,const char* 地址可以变但内容是常量,返回值类型为char *。
1 /*use strstr() get substring*/ 2 #include <stdio.h> 3 #include <string.h> 4 5 int main(void) 6 { 7 char *substr; 8 char buf1[3] = "bcd"; 9 char buf2[5] = "abcde"; 10 substr = strstr(buf2,buf1); 11 printf("substr is: %s",substr); 12 return 0; 13 }
执行结果:
substr is:bcd
6. 求两个日期(年月日)之间相差的天数
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int days(int year_st, int month_st, int day_st, int year_ed, int month_ed, int day_ed) 5 { 6 int y2, m2, d2; 7 int y1, m1, d1; 8 9 m1 = (month_st + 9) % 12; 10 y1 = year_st - m1/10; 11 d1 = 365*y1 + y1/4 - y1/100 + y1/400 + (m1*306 + 5)/10 + (day_st - 1); 12 13 m2 = (month_ed + 9) % 12; 14 y2 = year_ed - m2/10; 15 d2 = 365*y2 + y2/4 - y2/100 + y2/400 + (m2*306 + 5)/10 + (day_ed - 1); 16 17 return (d2 - d1); 18 } 19 20 int main(void) 21 { 22 int year1,year2,month1,month2,day1,day2; 23 scanf("%d %d %d",&year1,&month1,&day1); 24 printf("start:%d-%d-%d\n",year1,month1,day1); 25 scanf("%d %d %d",&year2,&month2,&day2); 26 printf("end:%d-%d-%d\n",year2,month2,day2); 27 28 printf("last %d days\n ", days(year1,month1,day1,year2,month2,day2)); 29 30 return 0; 31 }
输入:
2018 9 8 2018 9 28
执行结果:
start:2018-9-8
end:2018-9-28
last 20 days
7. 建立字符串数组模型
1 /*18.10.9 2 有10个盒子,向10个盒子中放入指定个数的小球,列出每个盒子中小球的个数。如 box00=0; box01=1;*/ 3 #include <stdio.h> 4 #include <string.h> 5 int i; 6 char Ball[10] = {'0','2','4','6','8','1','3','5','7','9'}; 7 char box[] = "box00=";//建立字符串数组模型 8 int main(void) { 9 for(i=0;i<10;i++) 10 { 11 box[3] = i/10 + 0x30;//取十位 12 box[4] = i%10 + 0x30;//取个位 13 box[6] = Ball[i]; 14 printf("%s ",box); 15 } 16 return 0; 17 }
执行结果:
box00=0 box01=2 box02=4 box03=6 box04=8 box05=1 box06=3 box07=5 box08=7 box09=9
8. 建立字符串数组模型(另一种方法) 备用位置
9. 十进制转二进制字符串
1 /*2018.10.15 2 十进制整数转二进制码(高位为0不显示) 3 注意itoa()函数引用*/ 4 #include <stdio.h> 5 #include <stdlib.h> 6 7 int main(void) 8 { 9 int i; 10 char status[5]; 11 for(i=0;i<10;i++) 12 { 13 itoa(i,status,2);//2即是代表转换为2进制 14 printf("%s\n",status); 15 } 16 return 0; 17 }
执行结果:
0
1
10
11
100
101
110
111
1000
1001
补充知识点:关于函数itoa()
char* itoa(int value, char* string, int radix);
value待转化的整型变量值,char *string存放字符的数组,radix进制数(可填2,8,10,16)
注意这个函数并不是标准的C/C++函数,所以并不是所有编译器都能编译成功。包含<stdlib.h>头文件的编译器才可编译成功。因此头文件必须包含<stdlib.h>。
小插曲,此行上下字体抽风了!颜色设置的明明是黑色,出来了是晶蓝色!改不过来了,呵呵。
10.十进制整型转二进制显示
注意:这里的二进制显示并非真正的二进制,比如11在二进制中就是十进制的3,但在下面的程序中,11虽然形式上和二进制的11一样,但实际是十进制的十一,只不过是把十进制的3显示为11。所以这里的标题才写的十进制转二进制显示。
1 /*主要看转换函数tran()*/ 2 #include <stdio.h> 3 4 int main(void) { 5 int i; 6 for(i=0;i<10;i++) 7 tran(i); 8 return 0; 9 } 10 11 void tran( int n ){ 12 int result=0,a=1,k,temp; 13 temp = n; 14 while(temp){ 15 a = temp%2; 16 result = a * k + result; 17 a = a*10; 18 temp = temp/2; 19 } 20 printf("%d\n", result); 21 }
运行结果:
0
1
10
11
100
101
110
111
1000
1001
11.整数转一定位数字符串,不够的位数用0补上
1 #include <stdio.h> 2 int main(void) 3 { 4 char buff[17];//注意这里必须是大小一定的数组,若定义一个指针变量来放要转换的字符串,编译能通过但执行不出结果 5 long x; 6 scanf("%ld",&x); 7 printf("请输入要转换的整数:%ld\n",x); 8 sprintf(buff,"转换后的字符串为:%08ld",x); 9 puts(buff); 10 return 0; 11 }
执行结果:
请输入要转换的整数:566
转换后的字符串为:00000566
12.if(a%10)和if(a%10==0)的区别
这个测试代码太简单这里不贴出来了。主要区分两者:
if(a%10):若a不能被10整除,则执行if下面的语句
【解释】a%10的值如果为0,其默认逻辑值也为0,即“a不能被10整除”事件的逻辑值为1,反之为0
if(a%10==0):若a能被10整除,则执行if下面的语句
【解释】若a%10==0这个判断条件成立,则“a%10==0”这个事件的逻辑值为1,反之为0
由此可见,这两个判断语句的效果截然相反。切勿混淆。
13.众数问题(Mode Program in C):找出数组中出现次数最多的数(这里以整数数组为例)
提醒:众数(Mode)是指在统计分布上具有明显集中趋势点的数值,代表数据的一般水平。 也是一组数据中出现次数最多的数值,有时众数在一组数中有好几个。用M表示。比如1 1 1 2 2 1 3 1。再如1 1 2 2 3 3 4 4这种情况不存在众数,不能用这种方法。
思路:
①准备五个变量a[ ]数组变量、n数组元素个数、count计出现次数、maxCount最大次数、maxValue出现次数最多的数
②作对等比较。数组中每个数与数组中所有数比较看是否相等。用 i j 嵌套循环来实现。
③在每次对等比较后,比较count和maxCount。若count>maxCount,用count值覆盖maxCount,并取a[i]值覆盖maxCount。
1 #include <stdio.h> 2 3 int mode(int a[],int n) { 4 int maxValue = 0, maxCount = 0, i, j; 5 6 for (i = 0; i < n; ++i) { 7 int count = 0; 8 9 for (j = 0; j < n; ++j) { 10 if (a[j] == a[i]) 11 ++count; 12 } 13 14 if (count > maxCount) { 15 maxCount = count; 16 maxValue = a[i]; 17 } 18 } 19 20 return maxValue; 21 } 22 23 int main() { 24 int n = 5; 25 int a[] = {0,6,7,2,7}; 26 27 printf("Mode = %d ", mode(a,n)); 28 29 return 0; 30 }
执行结果:
Mode = 7
注意:若数组中没有众数如下两种情况a[ ]={1,2,3,4,5}或者a[ ]={5,5,2,2,3}执行结果分别是:Mode = 1,Mode = 5
【声明】本博文为个人学习笔记,仅供参考。转发请注明出处。如发现有误,还请不吝赐教,谢谢!