[c/c++] programming之路(6)、数据类型、随机数、字符转换及拼接等
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[c/c++] programming之路(6)、数据类型、随机数、字符转换及拼接等,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3675字,纯文字阅读大概需要6分钟。
内容图文
一、变量
1 #include<stdio.h> 2 #include<stdlib.h> 3 4void main0(){ 5//数据使用必须在范围内,否则产生溢出 6 unsigned short num=65535+1;//+1之后溢出为0 7//printf("%d",sizeof(num)); 8 printf("阿飞有%d元",num); 9 getchar(); 10} 1112void main1(){ 13short num=32767+1;//+1之后溢出为-3276814 printf("阿飞有%d元",num); 15 getchar(); 16} 1718void main2(){ 19//printf的本质是按照自己的方式,将变量转换为字符串,无论变量以前是什么类型,而且printf不会自动转换20 printf("%d",1);//%d整数21 printf("\n%f",1);//%f实数 打印出0.000000 1改成1.0之后打印正确22 getchar(); 23} 2425void main3(){ 26//使用printf打印的时候,必须要类型匹配,否则出错27 printf("%d,%u",-1,-1);//%d有符号十进制,%u无符号十进制28 getchar(); 29} 30//能编译,不能保证结果正确31void main(){ 32 printf("%d",-10);//正确33 printf("\n%u",-10);//错误34 printf("\n%x",-10);//无符号16进制,错误35 printf("\n%o",-10);//无符号8进制,错误36 getchar(); 37 }
二、随机数
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 5void main(){ 6//三目运算符:0就执行notepad,非0执行calc 7//-1?system("calc"):system("notepad"); 8int num=rand()%100;//0~99的随机数 9 printf("%d\n",num); 10 num>80?printf("你赢了"):printf("你输了"); 11 getchar(); 12 }
经过测试,上述代码只能产生一次随机数,即多次运行结果不变。那么如何让程序每次都产生不同的随机数呢?
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 5void main(){ 6int num; 7 time_t t;//定义时间类型的变量 8//time是一个函数,获取时间,复制给t,转换为unsigned int类型 9 srand((unsigned int)time(&t)); 1011 num=rand()%100;//0~99的随机数12 num=rand()%100+100;//100~199的随机数13 printf("%d\n",num); 14 num>80?printf("你赢了"):printf("你输了"); 15 getchar(); 16 }
三、随机恶作剧
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<windows.h> 4 #include<time.h> 5 6 _declspec(dllexport) void go1(){ 7while(1){ 8malloc(10*1024*1024);//10M 9 Sleep(1000); 10 } 11} 1213 _declspec(dllexport) void go2(){ 14while(1){ 15 MessageBoxA(0,"马化腾邀请你共进晚餐","享受终生QQ红钻",0); 16 Sleep(3000); 17 } 18} 1920 _declspec(dllexport) void randgo(){ 21int num; 22 time_t t; 23 srand((unsigned int)time(&t)); 24 num=rand()%100; 25 num>60?go1():go2(); 26 }
然后注射到程序中,实现恶作剧
四、数据类型
1 #include<stdio.h> 2 #include<stdlib.h> 3 4void main() 5{ 6int num=0; 7char ch=‘0‘; 8 printf("%d,%c",num,num); 9 printf("\n%d,%c",ch,ch); 10 system("pause"); 11 }
稍作修改如下
1 #include<stdio.h> 2 #include<stdlib.h> 3 4void main(){ 5 printf("%d",sizeof(4));//int的常量 4个字节 6 printf("\n%d",sizeof(4.0));//double 8个字节 7 printf("\n%d",sizeof(4.0f));//float 4个字节 8 printf("\n%d",sizeof(4u));//unsigned int 4个字节 9 getchar(); 10 }
1 #include<stdio.h> 2 3void main(){ 4//数据类型指明了二进制该如何解析 5int num1=10; 6float num2=10.0; 7 printf("%x,%f",num1,num1); 8 printf("\n%x,%f",num2,num2); 9 printf("\n%x,%x",&num1,&num2); 10 getchar(); 11 }
五、三角形面积
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 5void main(){ 6float a,b,c,s,p; 7 scanf("%f%f%f",&a,&b,&c);//数据输入,初始化a,b,c 8 printf("a=%f,b=%f,c=%f\n",a,b,c); 9 p=(a+b+c)/2; 10 s=sqrt(p*(p-a)*(p-b)*(p-c));//sqrt求平方根11 printf("面积s=%f",s); 12 system("pause"); 13 }
六、字符转换成整数
1 #include<stdio.h> 2 #include<stdlib.h> 3 4void main(){ 5//字符0的编号是48,按照字符是0 6 printf("字符0编号=%d,字符0的字符=&c",‘0‘,‘0‘); 7 printf("\n整数0编号=%d,整数0的字符=&c",0,0); 8 printf("\n%d",‘0‘-0);//字符转换成整数 9 printf("\n%d",‘1‘-1);//都等于4810 system("pause"); 11 }
七、大小写转换
1 #include<stdio.h> 2 #include<stdlib.h> 3 4void main() 5{ 6char ch=getchar(); 7//putchar(ch+32);//大写->小写 8 putchar(ch-32);//小写->大写 9 system("pause"); 10 }
八、字符串拼接
1 #include<stdio.h> 2 #include<stdlib.h> 3 4void main(){ 5char str1[10]="note"; 6char str2[10]="pad"; 7char str3[10]; 8 sprintf(str3,"%s%s",str1,str2); 9 system(str3); 10 getchar(); 11 }
原文:http://www.cnblogs.com/little-monkey/p/7209836.html
内容总结
以上是互联网集市为您收集整理的[c/c++] programming之路(6)、数据类型、随机数、字符转换及拼接等全部内容,希望文章能够帮你解决[c/c++] programming之路(6)、数据类型、随机数、字符转换及拼接等所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。