C++基础语法知识点归纳

 

在看《c++ Primer Plus 》的学习笔记,和大家分享,都是些很简单的东西,但是很少能看得那么仔细,大家一起进步~~~

Code:
  1. #include<iostream>  
  2. #include<climits> //该头文件中定义了很多数据类型的大小,长度的宏  
  3. #include<string>  //string类头文件  
  4. #include<cstring>  //c语言中的string库   
  5.   
  6. using namespace std;   
  7. char * getname(void);   
  8. struct inflatable  //该结构体的申明可以在main函数中申明,   
  9. {                    //但是就只能被main使用,如果有其他函数,不能使用该结构体   
  10.     char name[20];   
  11.     float volume;   
  12.     double price;   
  13. };   
  14.   
  15. int main()   
  16. {   
  17.     int n_int = INT_MAX;   
  18.     short n_short = SHRT_MAX;   
  19.     long n_long = LONG_MAX;   
  20.        
  21.     cout << "int is " << sizeof (int) << " bytes." << endl;   
  22.     cout << "short is " << sizeof n_short << " bytes." << endl;   
  23.     cout << "long is " << sizeof n_long << " bytes." << endl << endl;   
  24.   
  25.     cout << "Maxinum values:" << endl;   
  26.     cout << "int: " << n_int << endl;   
  27.     cout << "short: " << n_short << endl;   
  28.     cout << "long: " << n_long << endl << endl;   
  29.   
  30.     cout << "Mininum int value = " << INT_MIN << endl;   
  31.     cout << "Bits per byte = " << CHAR_BIT << endl;   
  32.   
  33.     int R(321);      //c++中的另一种赋值方式   
  34.   
  35.     cout << "R" << " = " << R << endl << endl;   
  36.   
  37.     int chest = 42;   
  38.     int waist = 42;   
  39.     int inseam = 42;   
  40.   
  41.     cout << "Monsieur cuts a striking figure!" << endl;   
  42.     cout << "chest = " << chest << " (decimal)" << endl;   
  43.     cout << hex;   
  44.     cout << "waist = " << waist << " (hexadecimal)" << endl;   
  45.  // cout << oct;   
  46.     cout << oct << "inseam = " << inseam << " (octal)" << endl << endl;   
  47.        
  48.     cout << dec << 8 << endl;   
  49. 字符验证   
  50.   
  51.     char ch = 'M';   
  52.     int i = ch;   
  53.     cout << "The ASCII code for " << ch << " is " << i << endl;   
  54.   
  55.     cout << "Add one to the character code: "<< endl;   
  56.     ch = ch + 1;   
  57.     i = ch;   
  58.     cout << "The ASCII code for " << ch << " is " << i << endl;   
  59.   
  60.     cout << "Displaying char ch using cout.put(ch): ";   
  61.     cout.put(ch);   
  62.     cout.put('!');   
  63.   
  64.     cout << endl << "Done" << endl;   
  65.   
  66.     char alarm = '/a';   
  67.     cout << alarm << "Don't do that again!/a/n";   
  68.     cout << "Ben /"Buggsie/" Hacker/nwas here!/n";   
  69.   
  70.     wchar_t bob = L'P';   
  71.     wcout << L"tall" <<endl;   //用两个字节来存储一个字符   
  72.   
  73.     cout << 3.15E+3 << endl;   
  74.   
  75.     cout << sizeof (float) << " : " << sizeof (double) << " : " << sizeof (long double) << endl;   
  76.   
  77.     float a = 2.34E22f;   
  78.     float b = a + 1.0f;   //这个例子验证浮点数的精度问题   
  79.        
  80.     cout << endl << "b - a = " << b - a << endl;   
  81.   
  82.     cout << endl << int (3.9) << ":" << int (3.4) << endl;   
  83.        
  84.     int modee = 13 % 2 * 4;   
  85.     cout << "modee = " << modee << endl;   
  86.   
  87.        
  88.     int cards[4]={1,2,3,4};   
  89.     cout << "the size of cards : " << sizeof (cards) << endl;   
  90.     cout << "the size of the element of cards : " << sizeof (cards[0]) << endl;   
  91.   
  92.     ///   
  93.     char dog[5] = {'a','p','p','l','e'};   
  94.     char cat[5] = {'k','i','t','y','/0'};   
  95.   
  96.     cout << "the name of dog is : " << dog <<endl;  //会打印无效的字符,直到遇到空字符为止   
  97.     cout << "the name of cat is : " << cat << endl;   
  98.   
  99.     ///get() 和 getline()的用法    
  100.     const int ArSize = 20;   
  101.     char name[ArSize];   
  102.     char dessert[ArSize];   
  103.   
  104.     cout << "/nEnter your name :/n";   
  105. //  cin.getline(name,ArSize);   
  106.     cin.get(name,ArSize);   
  107.     cin.get();                     //必须要有这一句,不然dessert储存的便是换行符   
  108.   
  109.     cout << "Enter your favorite dessert :/n";   
  110. //  cin.getline(dessert,ArSize);   
  111.     cin.get(dessert,ArSize);   
  112.     cin.get();                    //刚开始没有加这个,导致后面的name里面放的换行符   
  113.   
  114.     cout << "I have some delicious " << dessert << " for you, " << name << "./n";   
  115.        
  116.     cout << endl << "Try again in another way " << endl;   
  117.     cin.getline(name,ArSize).getline(dessert,ArSize);   
  118.     cout << "I have some delicious " << dessert << " for you, " << name << "./n";   
  119. // get()可以用来检查错误,通过查看最后一个字符是不是‘/0’   
  120.        
  121.     cin.clear() 的用法  读取空行时候面对的问题 ///   
  122.     char blank[ArSize];   
  123.     cout <<endl << "Enter /"enter/" key derectly ,in order to create a blank line" << endl;   
  124.     cin.get(blank,ArSize).get();   
  125. //  cin.get(blank,ArSize);        //这里不管用这两种方式中哪一种,都会阻断后面name的输入,   
  126.     cin.clear();                  //解决这种问题的方法只能在这里加入一个cin.clear();   
  127.                                   //但是这里加了这个也不管作用,貌似只要输入空行就会阻断后面的输入,   
  128.                                     //  大家能不能帮忙看看这个   
  129.     cout << "Enter your name: " ;   
  130.        
  131.     cin.get(name,ArSize).get();   
  132.        
  133.     cout <<  "blank is : " << blank << endl;   
  134.     cout <<  "name  is : " << name << endl;   
  135.   
  136.     // string 的用法 ///   
  137.     string s1 = "penguin";   
  138.     string s2,s3;   
  139.     cout << endl << "you can assign one string object to another : s2 = s1/n";   
  140.     s2 = s1;   
  141.     cout << "s1: " << s1 << ", s2: " << s2 << endl;   
  142.     cout << "You can assign a C-style string to a string object./n";   
  143.     cout << "s2 =/"buzzard/"/n";   
  144.     s2 = "buzzard";   
  145.     cout << "s2: " << s2 << endl;   
  146.     cout << "You can concatenate strings: s3 = s1 + s2/n";   
  147.     s3 = s1 + s2;   
  148.     cout << "s3: " << s3 << endl;   
  149.     cout << "you can append strings./n";   
  150.     s1 += s2;   
  151.     cout << "s1 += s2 yields s1 = " << s1 << endl;   
  152.     s2 += " for a day";   
  153.     cout << "s2 += /" for a day/" yields s2 = " << s2 << endl;   
  154.   
  155.    /// string类 I/O 的用法 /   
  156.     char charr[20];   
  157.     string str;   
  158.     cout << endl;   
  159.     cout << "Length of string in charr before input: "    
  160.         << strlen(charr) << endl;                    //这里输出的结果很可能不是20,   
  161.     cout << "Length of string in str before input: " //因为未初始化的数组的内容是未定义的   
  162.         << str.size() << endl;   
  163.     cout << "Enter a line of text: /n";   
  164.     cin.getline(charr,20);   
  165.     cout << "You entered: " << charr << endl;   
  166.     cout << "Enter another line of text: /n";   
  167.     getline(cin,str);                         //这里只算第一行输入的字符串   
  168.     cout << "You entered: " << str << endl;   
  169.     cout << "Length of string in charr after input: "  
  170.         << strlen(charr) << endl;   
  171.     cout << "length of string in str after input: "  
  172.         << str.size() << endl;   
  173.        
  174.     cin >> str;                   // 注意,这种方法只能输入一个词,不能输入一个句子   
  175.     cout << "str : " << str << endl;  //这里还有一个问题,如果在上一个getline(cin,str)的输入中,   
  176.                                       //你输入了两行,那么后一行会排在输入队列中,作为这里的str输入,   
  177.                                         //这样str直接输出结果,不会给你输入的机会   
  178.        
  179.     /// C++ 中结构体的用法 /   
  180.     inflatable guest =    
  181.     {   
  182.         "Glorious Gloria",   
  183.             1.88f,   
  184.             29.99   
  185.     };   
  186.   
  187.     inflatable pal =   
  188.     {   
  189.         "Audacious Arthur",   
  190.             3.12f,   
  191.             32.99   
  192.     };   
  193.   
  194.     cout << endl;   
  195.     cout << "Expand your guest list with "<< guest.name;   
  196.     cout << " and " << pal.name << endl;   
  197.   
  198.     cout << "You can have both for $";   
  199.     cout << guest.price + pal.price << "!/n";   
  200.   
  201.     // 枚举的使用方法 ///   
  202.     enum spectrum {red, orange, yellow, green, blue, biolet, indigo, ultraviolet};   
  203.     spectrum band;   
  204.     band = blue;   
  205. //  band = 2000;    // 这种赋值是非法的   
  206.   
  207.     cout << endl;    
  208.     cout << "band = blue" << endl;   
  209.     cout << "band = " << band << endl;   
  210.        
  211.     band = orange;   
  212. //  ++band;              // 这种操作是非法的   
  213. //  band = orange + red; //  非法操作   
  214.   
  215.     int color = blue;   
  216. //  band = 3; // 非法,不能将3赋值给一个枚举变量   
  217.     color = 3 + red;   
  218.   
  219.     cout << endl;    
  220.     cout << "color = 3 + red" << endl;   
  221.     cout << "color = " << color << endl;   
  222.        
  223.     band = spectrum(3);     //强制转换类型  c++通过强制转换类型,增加了可赋给枚举变量的合法值   
  224.     cout << endl;    
  225.     cout << "band = spectrum(3)" << endl;   
  226.     cout << "band = " << band << endl;   
  227.   
  228.     // 指针的使用方法 /   
  229. //    long * fellow;       // 这种用法不正确,一定要在将fellow指定一个具体的地址   
  230. //  *fellow = 223323 ;      
  231.     int *pt;   
  232.     pt= (int *) 0xB8000000;    // 可以给pt指定地址   
  233.     cout << endl;   
  234.     cout << "location of pt is : " << pt << endl;   
  235.   
  236.      new 和 delete的用法 ///   
  237.     double *p3 = new double [3];   
  238.     p3[0] = 0.2;    
  239.     p3[1] = 0.5;   
  240.     p3[2] = 0.8;   
  241.     cout << endl;   
  242.     cout << "p3[1] is " << p3[1] << endl;   
  243.     p3 = p3 + 1;             // 这里将指针当数组名使用,如果定义的时候不是指针,   
  244.     cout << "now p3[0] is " << p3[0] << endl;  // 是数组名形式,那么不可以加1,但是这里可以加1   
  245.     cout << "p3[1] is" << p3[1] << "./n";   
  246.     p3 = p3 - 1;   
  247.     delete [] p3;   
  248.   
  249.      一个使用new 和 delete的例子    
  250.     char * sname;         // 这种方法是根据需要分配内存空间   
  251.     sname = getname();        // 同时要注意的是 new 和 delete 在不同的函数中,   
  252.     cout << sname << " at " << (int *)sname << "/n";   // delete很容易忘记,但是这样是可以的   
  253.     delete [] sname;   
  254.   
  255.     sname = getname();   
  256.     cout << sname << " at " << (int *)sname << "/n";   
  257.     delete [] sname;   
  258.   
  259.         return 0;   
  260. }          
  261.   
  262. char *getname()   
  263. {   
  264.     char temp[80];   
  265.     cout << "Enter last name: ";   
  266.     cin >> temp;   
  267.     char *pn = new char[strlen(temp) + 1];   
  268.     strcpy(pn,temp);   
  269.     return pn;   
  270. }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值