在看《c++ Primer Plus 》的学习笔记,和大家分享,都是些很简单的东西,但是很少能看得那么仔细,大家一起进步~~~
- #include<iostream>
- #include<climits> //该头文件中定义了很多数据类型的大小,长度的宏
- #include<string> //string类头文件
- #include<cstring> //c语言中的string库
- using namespace std;
- char * getname(void);
- struct inflatable //该结构体的申明可以在main函数中申明,
- { //但是就只能被main使用,如果有其他函数,不能使用该结构体
- char name[20];
- float volume;
- double price;
- };
- int main()
- {
- int n_int = INT_MAX;
- short n_short = SHRT_MAX;
- long n_long = LONG_MAX;
- cout << "int is " << sizeof (int) << " bytes." << endl;
- cout << "short is " << sizeof n_short << " bytes." << endl;
- cout << "long is " << sizeof n_long << " bytes." << endl << endl;
- cout << "Maxinum values:" << endl;
- cout << "int: " << n_int << endl;
- cout << "short: " << n_short << endl;
- cout << "long: " << n_long << endl << endl;
- cout << "Mininum int value = " << INT_MIN << endl;
- cout << "Bits per byte = " << CHAR_BIT << endl;
- int R(321); //c++中的另一种赋值方式
- cout << "R" << " = " << R << endl << endl;
- int chest = 42;
- int waist = 42;
- int inseam = 42;
- cout << "Monsieur cuts a striking figure!" << endl;
- cout << "chest = " << chest << " (decimal)" << endl;
- cout << hex;
- cout << "waist = " << waist << " (hexadecimal)" << endl;
- // cout << oct;
- cout << oct << "inseam = " << inseam << " (octal)" << endl << endl;
- cout << dec << 8 << endl;
- 字符验证
- char ch = 'M';
- int i = ch;
- cout << "The ASCII code for " << ch << " is " << i << endl;
- cout << "Add one to the character code: "<< endl;
- ch = ch + 1;
- i = ch;
- cout << "The ASCII code for " << ch << " is " << i << endl;
- cout << "Displaying char ch using cout.put(ch): ";
- cout.put(ch);
- cout.put('!');
- cout << endl << "Done" << endl;
- char alarm = '/a';
- cout << alarm << "Don't do that again!/a/n";
- cout << "Ben /"Buggsie/" Hacker/nwas here!/n";
- wchar_t bob = L'P';
- wcout << L"tall" <<endl; //用两个字节来存储一个字符
- cout << 3.15E+3 << endl;
- cout << sizeof (float) << " : " << sizeof (double) << " : " << sizeof (long double) << endl;
- float a = 2.34E22f;
- float b = a + 1.0f; //这个例子验证浮点数的精度问题
- cout << endl << "b - a = " << b - a << endl;
- cout << endl << int (3.9) << ":" << int (3.4) << endl;
- int modee = 13 % 2 * 4;
- cout << "modee = " << modee << endl;
- int cards[4]={1,2,3,4};
- cout << "the size of cards : " << sizeof (cards) << endl;
- cout << "the size of the element of cards : " << sizeof (cards[0]) << endl;
- ///
- char dog[5] = {'a','p','p','l','e'};
- char cat[5] = {'k','i','t','y','/0'};
- cout << "the name of dog is : " << dog <<endl; //会打印无效的字符,直到遇到空字符为止
- cout << "the name of cat is : " << cat << endl;
- ///get() 和 getline()的用法
- const int ArSize = 20;
- char name[ArSize];
- char dessert[ArSize];
- cout << "/nEnter your name :/n";
- // cin.getline(name,ArSize);
- cin.get(name,ArSize);
- cin.get(); //必须要有这一句,不然dessert储存的便是换行符
- cout << "Enter your favorite dessert :/n";
- // cin.getline(dessert,ArSize);
- cin.get(dessert,ArSize);
- cin.get(); //刚开始没有加这个,导致后面的name里面放的换行符
- cout << "I have some delicious " << dessert << " for you, " << name << "./n";
- cout << endl << "Try again in another way " << endl;
- cin.getline(name,ArSize).getline(dessert,ArSize);
- cout << "I have some delicious " << dessert << " for you, " << name << "./n";
- // get()可以用来检查错误,通过查看最后一个字符是不是‘/0’
- cin.clear() 的用法 读取空行时候面对的问题 ///
- char blank[ArSize];
- cout <<endl << "Enter /"enter/" key derectly ,in order to create a blank line" << endl;
- cin.get(blank,ArSize).get();
- // cin.get(blank,ArSize); //这里不管用这两种方式中哪一种,都会阻断后面name的输入,
- cin.clear(); //解决这种问题的方法只能在这里加入一个cin.clear();
- //但是这里加了这个也不管作用,貌似只要输入空行就会阻断后面的输入,
- // 大家能不能帮忙看看这个
- cout << "Enter your name: " ;
- cin.get(name,ArSize).get();
- cout << "blank is : " << blank << endl;
- cout << "name is : " << name << endl;
- // string 的用法 ///
- string s1 = "penguin";
- string s2,s3;
- cout << endl << "you can assign one string object to another : s2 = s1/n";
- s2 = s1;
- cout << "s1: " << s1 << ", s2: " << s2 << endl;
- cout << "You can assign a C-style string to a string object./n";
- cout << "s2 =/"buzzard/"/n";
- s2 = "buzzard";
- cout << "s2: " << s2 << endl;
- cout << "You can concatenate strings: s3 = s1 + s2/n";
- s3 = s1 + s2;
- cout << "s3: " << s3 << endl;
- cout << "you can append strings./n";
- s1 += s2;
- cout << "s1 += s2 yields s1 = " << s1 << endl;
- s2 += " for a day";
- cout << "s2 += /" for a day/" yields s2 = " << s2 << endl;
- /// string类 I/O 的用法 /
- char charr[20];
- string str;
- cout << endl;
- cout << "Length of string in charr before input: "
- << strlen(charr) << endl; //这里输出的结果很可能不是20,
- cout << "Length of string in str before input: " //因为未初始化的数组的内容是未定义的
- << str.size() << endl;
- cout << "Enter a line of text: /n";
- cin.getline(charr,20);
- cout << "You entered: " << charr << endl;
- cout << "Enter another line of text: /n";
- getline(cin,str); //这里只算第一行输入的字符串
- cout << "You entered: " << str << endl;
- cout << "Length of string in charr after input: "
- << strlen(charr) << endl;
- cout << "length of string in str after input: "
- << str.size() << endl;
- cin >> str; // 注意,这种方法只能输入一个词,不能输入一个句子
- cout << "str : " << str << endl; //这里还有一个问题,如果在上一个getline(cin,str)的输入中,
- //你输入了两行,那么后一行会排在输入队列中,作为这里的str输入,
- //这样str直接输出结果,不会给你输入的机会
- /// C++ 中结构体的用法 /
- inflatable guest =
- {
- "Glorious Gloria",
- 1.88f,
- 29.99
- };
- inflatable pal =
- {
- "Audacious Arthur",
- 3.12f,
- 32.99
- };
- cout << endl;
- cout << "Expand your guest list with "<< guest.name;
- cout << " and " << pal.name << endl;
- cout << "You can have both for $";
- cout << guest.price + pal.price << "!/n";
- // 枚举的使用方法 ///
- enum spectrum {red, orange, yellow, green, blue, biolet, indigo, ultraviolet};
- spectrum band;
- band = blue;
- // band = 2000; // 这种赋值是非法的
- cout << endl;
- cout << "band = blue" << endl;
- cout << "band = " << band << endl;
- band = orange;
- // ++band; // 这种操作是非法的
- // band = orange + red; // 非法操作
- int color = blue;
- // band = 3; // 非法,不能将3赋值给一个枚举变量
- color = 3 + red;
- cout << endl;
- cout << "color = 3 + red" << endl;
- cout << "color = " << color << endl;
- band = spectrum(3); //强制转换类型 c++通过强制转换类型,增加了可赋给枚举变量的合法值
- cout << endl;
- cout << "band = spectrum(3)" << endl;
- cout << "band = " << band << endl;
- // 指针的使用方法 /
- // long * fellow; // 这种用法不正确,一定要在将fellow指定一个具体的地址
- // *fellow = 223323 ;
- int *pt;
- pt= (int *) 0xB8000000; // 可以给pt指定地址
- cout << endl;
- cout << "location of pt is : " << pt << endl;
- new 和 delete的用法 ///
- double *p3 = new double [3];
- p3[0] = 0.2;
- p3[1] = 0.5;
- p3[2] = 0.8;
- cout << endl;
- cout << "p3[1] is " << p3[1] << endl;
- p3 = p3 + 1; // 这里将指针当数组名使用,如果定义的时候不是指针,
- cout << "now p3[0] is " << p3[0] << endl; // 是数组名形式,那么不可以加1,但是这里可以加1
- cout << "p3[1] is" << p3[1] << "./n";
- p3 = p3 - 1;
- delete [] p3;
- 一个使用new 和 delete的例子
- char * sname; // 这种方法是根据需要分配内存空间
- sname = getname(); // 同时要注意的是 new 和 delete 在不同的函数中,
- cout << sname << " at " << (int *)sname << "/n"; // delete很容易忘记,但是这样是可以的
- delete [] sname;
- sname = getname();
- cout << sname << " at " << (int *)sname << "/n";
- delete [] sname;
- return 0;
- }
- char *getname()
- {
- char temp[80];
- cout << "Enter last name: ";
- cin >> temp;
- char *pn = new char[strlen(temp) + 1];
- strcpy(pn,temp);
- return pn;
- }