C++ TwoStudy

来源:互联网 发布:外卖软件闪退 编辑:程序博客网 时间:2024/05/16 12:32
变量和数据如何存储在计算机中:插图:内存之间的转换:二进制,八进制,十六进制;早起的计算机一次能发送8位数字,很自然的就用到8位二进制编写代码,8位二进制数字也叫字节(00000000--->>11111111)。如果是16位(0000000000000000--->>1111111111111111);比如:10011000,1001(高8位) 1000(低8位);布尔值bool:bool=true=1;bool=false=0;# include <stdio.h>int deal(){printf("HelloWorld\n");return 1;}int main(){bool a=1;if(a){deal();}return 0;}//输出结果:HelloWorld//Press any key to continueASCll(American Standard Code for information interchange)美国标准信息交换码,它是由美国国家标准局ANSI制定,有7位码和8位码两种表示:# include<iostream>using namespace std;int main(){char test='a';    cout<<(int)test<<endl;return 0;}//97   'a'--->>ascll码97;//Press any key to continue# include<iostream>using namespace std;int main(){for(int i=32;i<128;i++){cout<<(char)i;}cout<<endl;return 0;}//!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmno//pqrstuvwxyz{|}~//Press any key to continue//'0'--->>48,A--->>65,a--->>97;c++中char不能存放汉子,韩文,日文,引入宽字符wchar_t;# include<iostream># include<locale>using namespace std;int main(){setlocale(LC_ALL,"chs");//此函数在头文件locale中定义的,注意在头文件中添加locale头文件。//第一个参数:表示设置所有的选项,包括金融货币,小数点,时间日期格式,语言字符串的使用习惯等等。//第二个参数:语言设置,"chs"中文简体。    wchar_t wt[]=L"中";//L告诉编译器给wt分配两个字节的大小。    wcout<<wt<<endl; //wcout,替代cout对宽字符的输出和cout用法一样。return 0;}//输出结果:中//Press any key to continue整型:int 在16位的计算机中占2个字节,在32位的计算机中占4个字节。# include<iostream># include<locale>using namespace std;int main(){int a;short b;long c;cout<<"int:"<<sizeof(a);cout<<endl;cout<<"short:"<<sizeof(b);cout<<endl;cout<<"long:"<<sizeof(c);//sizeof表示占的字节数cout<<endl;return 0;//输出结果://int:4//short:2//long:4//Press any key to continue}短整型:short int简称short,长整形:long int简称long;shor占2个字节,int,long占四个字节。整形变量可以分为:有符号和无符号整形(unsigned)无符号整形只能表示正整数不能表示负整数。无符号的短整型:2个字节:16位:1111111111111111(最大值转化为十进制65535,它的取值范围也就是0~65535);有符号的短整型:2个字节:16位:1111111111111111(最高位表示的是符号位:1表示负,0表示正,也就是取111111111111111<-32768~32767>之间);int和long的取值范围:-2147483648-->>2147483647unsigned int 和unsigned long的取值范围:0~4294967295;float:float长度为4个字节:double占8个字节开辟的空间比float大:# include <iostream># include<iomanip>//使用setprecision函数的头文件using namespace std;int main(){double a=12.132157574;cout<<setprecision(15)<<a<<endl;//setprecision精确度在6~7之间。即使长度不够,自己拼凑到指定位数。return 0;}//输出结果:12.132157574//Press any key to continue常量:常量的值不能改变;const关键字表示;枚举型常量:# include <iostream>using namespace std;int main(){enum num{zero=100,two,ten,six};//枚举型常量cout<<zero<<"\t"<<two<<"\t"<<ten<<"\t"<<six<<endl;return 0;}//输出结果:100     101     102     103//Press any key to continue # include <iostream>using namespace std;int main(){enum day{Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday};//枚举型常量    day today;today=Wednesday;if(today==Saturday||today==Sunday)cout<<"今天是周末";elsecout<<"今天是工作日";cout<<endl;return 0;}//今天是工作日//Press any key to continue

原创粉丝点击