您的位置:首页 > 编程语言 > C语言/C++

C++学习笔记(一)--整形,std输出,浮点数,数组,字符串,结构,指针,循环

2016-08-31 08:27 961 查看
趁着暑假恶补下C++的基本知识,故而选择了八百页的C++ Primer Plus,从第一页开始一页一页往下刷,就着示例每段写了几个示例程序,每个都按照名字封装成了一个个函数,因为函数名以及各种变量名都是中文,想来对新手入门学习相关知识还是挺亲和的。

 

各种平时忽略的小细节或者有趣的东西以及做错的习题都集成在一段段代码里了,注释也写的很详细,有兴趣可以瞅瞅

 

昨天2016-8-30总计刷了前148页的内容,各位看官如果也有这本书也可以对照着看下咯~

 

总计有以下几个部分:

整形

Std输出

浮点数

数组

字符串

结构

指针

new动态结构

for循环

std的cin相关

循环

 

废话不多说开始粘代码~

代码下载地址:http://download.csdn.net/detail/zmdsjtu/9617460

#include<iostream>
#include<string>
////////////////2016-8-30  C++ Primer Plus 学习笔记//////////////
 
void 整形();//声明
void std输出();
void 浮点数();
void 数组();
void 字符串();
void STring();
void 结构();
//共用体union,枚举enum
void 指针();
void new动态结构();//也是指针里的
void for循环();
void 神奇的cin();
void if语句();
 
int main() {
//整形();
//std输出();
//浮点数();
//数组();
//字符串();
//STring();
//结构();
//指针();
//new动态结构();
//for循环();
//神奇的cin();
if语句();
system("pause");
}
 
void 整形() {//学习long, shor ,int的区别以及联系,每个系统可能都不一样,这里long和short一样
using namespace std;
int n_int = INT_MAX;
short n_short = SHRT_MAX;
long n_long = LONG_MAX;
 
cout << "int is " << sizeof(int)<< " bytes." << "Maximum values : " << n_int<< endl;
cout << "short is " << sizeof(short)<< " bytes." << "Maximum values :" << n_short<< endl;
cout << "long is " << sizeof(long)<< " bytes." << "Maximum values: " << n_long<< endl;
 
}
 
void std输出() {//
std::cout << '&'<<std::endl;//单引号是字符
std::cout << "&&&" << std::endl;//双引号是字符串
std::cout << '&&' <<'\n';
std::cout.put('&&')<<"\n";//单双引号都可以换行
std::cout.put('&') << std::endl;
std::cout << int('Q')<< std::endl;//强制类型转换
char test = 65;
std::cout << test<<std::endl;//结果和'A'一样
}
 
void 浮点数() {//double 64位  long double 80-128位
const double Million = 1.0e6;
 
float a = 2.34E+22f;
float b = a + 1;
std::cout << "b-a=" << b - a<< std::endl;
std::cout <<"Million is "<< Million<< std::endl;
}
 
void 数组() {//是数组不是Vector,Vector在16章
using namespace std;
int yams[3];//因为是int所以sizeof是3*4=12,不是3啊亲,算长度除以sizeof (int);
yams[0] = sizeof(int);//3
yams[1] = 6;
yams[2] = 8;
int things[] = { 3,4,5,7 };
int yamcost[3] = { 20,30,5 };
cout << "Size of yams is:" << sizeof yams<< endl;
cout << "Size of one element is: " << sizeof yams[0]<< endl;
 
}
 
void 字符串() {
using namespace std;
char dog[2] = { 'a','b' };//不是字符串
char cat[3] = { 'a','b','\0' };//是字符串,因为以0结尾!!
 
char bird[2] = "B";//"B"相当于'B'+'\0'故而是两个字符
cout << bird<<endl;
cout << dog << endl;//乱码,因为不是字符串!!
cout << cat << endl;
 
const int ArSize = 20;
char name[ArSize];
cout << "Enter you name:";
cin.getline(name, ArSize);//cin.getline的用法~~~~~!
cout <<"Your name is: "<<name<< endl;//用cin.getline要万分注意,如果上一行有换行符记得加上.get()
}
 
void STring() {
using namespace std;
string str1;//创建一个空的strin
string str2 = "Hello,";//创建一个初始化了的string
string str3 = " world.";
cout << str2+str3<< endl;//string拼接
cout << "Size of str2+str3 is: " << (str2+ str3).size()<< endl;
}
 
void 结构() {  //结构struct的使用方法
 
struct 商品 {
char name[20];
float 数量;
double 价格;
};
 
商品 一 = {"桃子",3.0,1.2 };
商品 二 = {"西瓜",2.1,1.8 };
商品 水果[2];//结构数组
水果[0] = 一;
std::cout << 一.name<< "的价格是" << 一.价格<<"\n";
std::cout << "第一种水果是"<<水果[0].name<< std::endl;
}
 
void 指针() {
using namespace std;
int handsome = 10;
int *points;
points = &handsome;
*points += 1;
cout << handsome << endl;//修改地址对应的变量,本质上就是修改了handsome
cout << "handsome的地址为: " << &handsome<< endl;
 
double *beautiful = new double;//为beautiful开辟空间
*beautiful = 99.9;//相当于给一个int赋值了讲道理
cout << "beautiful的数值为: " << *beautiful<< " 地址为: " << beautiful<<endl;
cout << "size of beautiful=" << sizeof beautiful<< endl;
cout << "size of *beautiful=" << sizeof *beautiful<< endl;
 
//delete points;//这里的points不是new出来的所以不能delete
delete beautiful;   //delete只是删掉了分配的空间,指针本身还是在的
//不要释放已经释放了的内存,但是我想试一下,233
//delete beautiful;//果然还是不行啊,哈哈哈哈
 
//------------------------进军数组----------------------
int *ps = new int [3];
ps[0] = 1;
ps[1] = 890;
ps[2] = 233;
*ps = 2;//默认为数组第一个的地址
*(ps + 1) = 888;//相当于ps[1]=888
cout << "ps[0]=" << ps[0]<< endl;
ps = ps + 1;    //这里对动态数组加一相当于下标加了1
cout << "+1之后ps[0]=" << ps[0]<< endl;//ps0变成了ps1这样,,,
ps = ps - 1;//必须要减回来,不然释放报错!!
delete [] ps;//new和delete是一一对应的
 
 
}
 
void new动态结构() {
struct 水果 {
char name[20];
float 重量;
double 价格;
};//先来一个耳熟能详的结构
using namespace std;
水果 *第一种 =new 水果;
cout << "输入水果名字:";
cin.get(第一种->name, 20);//这里是用箭头->而不是点.是因为name其实是指针,数组本质上就是指针
cout << endl << "请输入重量:";
cin >> (*第一种).重量;
cout << "第一种是:" << (*第一种).name<< " 重量为:" << (*第一种).重量<< endl;
delete 第一种;
//cin和cin.getline的区别是cin读的是一个单词而getline是一整行
 
}
 
void for循环() {
using namespace std;
for (int i = 0; i < 5; i++) cout<< "U R handsome."<<endl;
cout << "Handsome不超过五次。" << endl<< endl<< endl;
//for循环用法以及++运算符,--类似
//对了,for里面是分号啊分号,不是逗号
int i = 0;
while (i < 5) {  //while里面是一个bool值,也就是0或者不是0,只要不是0就继续执行
i++;
cout << "Handsome 六次给你看看" << endl;
}
cout << "特么不还是五次么。。。。然后do-while就不展示了。。估计也帅不过五次" << endl;
 
 
}
 
void 神奇的cin() {
using namespace std;
char ch;
int count = 0;
//int x=(1,024)的意思是x的数值为20,为已经忘了数学了,第一反应是最小公约数,没救,八进制的24大小为20
cout << "先来个开胃菜" << endl;
cout << "(1,024)=?" << endl;
cout << "还是直接告诉你好了,等于20" << endl;
cout << "随意输入,计算字符,按ctrl+z以及enter结束" << endl;
cin.get(ch);  
while (cin.fail() == false) {
count << ch;
++count;
cin.get(ch);
}
cout << endl << count << "个字符读进来了" << endl;
cout << "循环嵌套什么的我就不写进来了,2333,毕竟5是个质数" << endl;
 
}
 
void if语句() {
bool 我很帅 =true;
if (我很帅)std::cout << "我觉得这个Bool变量长得很顺眼!"<<std::endl;
else 我很帅 =true;
}


惯例文末开始不正经:



最后祝大家编程愉快~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐