CPP_Basic_Code_P4.1-PP4.13.10
// The Notes Created by Z-Tech on 2017/2/17.
// All Codes Boot on 《C++ Primer Plus》V6.0
// OS:MacOS 10.12.4
// Translater:clang/llvm8.0.0 &g++4.2.1
// Editer:iTerm 2&Sublime text 3
// IDE: Xcode8.2.1&Clion2017.1
//P4.1
#include
int main()
{
using namespace std;
int yams[3];
yams[0]=7;
yams[1]=8;
yams[2]=6;
int yamcosts[3]={20,30,5};
cout<<"Total yams = ";
cout<
int main()
{
using namespace std;
const int Size=15;
char name1[Size];
char name2[Size]="C++owboy";
cout<<"Howdy! I'm "<>name1;//这种方式的读取不允许姓名间有空格一类
cout<<"Well, "<
int main()
{
using namespace std;
const int Arsize=20;
char name[Arsize];
char dessert[Arsize];
cout<<"Enter your name:\n";
cin>>name;//由于此处不支持空格,会导致last name进入后面的cin队列,从而没有机会输入dessert
cout<<"Enter your favorite dessert:\n";
cin>>dessert;
cout<<"I have some delicious "<
int main()
{
using namespace std;
const int Arsize=20;//修改数组容量可以储存更多的字符串
char name[Arsize];
char dessert[Arsize];
cout<<"Enter your name:\n";
cin.getline(name,Arsize);//此处使用cin.getline面向行处理,成功克服空格问题
cout<<"Enter your favorite dessert:\n";
cin.getline(dessert,Arsize);//再次使用行处理
cout<<"I have some delicious "<
int main()
{
using namespace std;
const int Arsize=20;
char name[Arsize];
char dessert[Arsize];
cout<<"Enter your name:\n";
cin.get(name,Arsize).get();//这种方式会调用读取下一个字符(这里也就是回车)
cout<<"Enter your favorite dessert:\n";
cin.get(dessert,Arsize).get();//其实是cin.get(name,value)和cin.get()的合成缩写
cout<<"I have some delicious "<
int main()
{
using namespace std;
cout<<"What year was your house built?\n";
int year;
(cin>>year).get();//解决回车按键的问题
cout<<"What's its street address?\n";
char address[80];
cin.getline(address,80);
cout<<"Year built: "<
#include
int main()
{
using namespace std;
char charr1[20];
char charr2[20]="jaguar";//数组式初始化
string str1;
string str2="panther";//C风格字符串的初始化
cout<<"Enter a kind of feline: ";
cin>>charr1;//可使用cin注入数组
cout<<"Enter another kind of feline: ";
cin>>str1;//可使用cin注入string对象
cout<<"Here are some felines: \n";
cout<
#include
int main()
{
using namespace std;
string s1="penguin";
string s2,s3;
cout<<"You can assign one string object to another:s2=s1\n";
s2=s1;
cout<<"s1: "<
#include
#include
int main()
{
using namespace std;
char charr[20];
string str;
cout<<"Length of string in charr before input: "
<
struct inflatable
{
char name[20];
float volume;
double price;
};//注意结构体声明需要;结尾
int main()
{
using namespace std;
inflatable guest={"Glorious Gloria",1.88,29.99};//初始化结构成员guest
inflatable pal={"Audacious Arthur",3.12,32.99};//初始化结构成员pal
cout<<"Expand your guest list with "<
struct inflatable//结构化声明提倡使用外部式
{
char name[20];
float volume;
double price;
};
int main()
{
using namespace std;
inflatable bouquet={"Sunflowers",0.20,12.49};
inflatable choice;//未被初始化
cout<<"bouquet: "<
struct inflatable
{
char name[20];
float volume;
double price;
};
int main()
{
using namespace std;
inflatable guest[2]=//此处[2]表明有两个结构
{
{"Bambi",0.5,21.99},
{"Godzilla",2000,565.99}
};//结构初始化之后必须加;
cout<<"The guest "<
int main()
{
using namespace std;
int donuts=6;
double cups=4.5;
cout<<"donuts value = "<
int main()
{
using namespace std;
double* p3=new double[3];//new了一个3个元素的double数组
p3[0]=0.2;
p3[1]=0.5;
p3[2]=0.8;
cout<<"p3[1] is "<
int main()
{
using namespace std;
double wages[3]={10000.0,20000.0,30000.0};
short stacks[3]={3,2,1};
double* pw=wages;//数组名为数组第一个元素的地址
short* ps=&stacks[0];//这是上一句显式的写法
cout<<"pw= "<
#include
int main()
{
using namespace std;
char animal[20]="bear";
const char* bird="wren";//使用const可防止bird修改值wren
char* ps;
cout<>animal;
ps=animal;
cout<
struct inflatable
{
char name[20];
float volume;
double price;
};
int main()
{
using namespace std;
inflatable * ps=new inflatable;//新申请一个动态结构指针
cout<<"Enter name of inflatable item: ";
cin.get(ps->name,20);//指针法读取输入
cout<<"Enter volume in cubic feet: ";
cin>>(*ps).volume;//数组名引用法读取输入
cout<<"Enter price: $";
cin>>ps->price;
cout<<"Name: "<<(*ps).name<volume<<" cubic feet\n";
cout<<"Price: $"<price<
#include
using namespace std;
char* getname(void);//函数声明,返回值为char,不接受参数
int main()
{
char* name;
name=getname();//第一次调用子函数
cout<>temp;//读取字符串到数组temp
char* pn=new char[strlen(temp)+1];//新定义恰到好处的指针和所需内存
strcpy(pn,temp);//复制数据到新的指针地址
return pn;//返回指针地址(char类型,将被识别为字符串本身)
}
//P4.23
#include ;
struct antarctica_years_end//结构定义
{
int year;
};
int main()
{
antarctica_years_end s01, s02, s03;//定义三个结构变量
s01.year = 1998;//结构式赋值
antarctica_years_end *pa = &s02;//定义结构指针pa指向s02
pa->year = 1999;//指针式赋值
antarctica_years_end trio[3];//创建三个元素的结构数组
trio[0].year = 2003;//结构数组访问成员
std::cout << trio->year << std::endl;
//输出结构数组第一个元素结构的year成员 2003
const antarctica_years_end *arp[3] = {&s01, &s02, &s03};//定义含3指针的指针数组
//指针式访问第二个元素,即&s02指向结构变量的year成员1999
const antarctica_years_end **ppa = arp;
//创建指向上一个指针数组的的指针
//或者使用const antarctica_years_end **ppb=arp
std::cout<<(*ppa)->year<
#include
#include
int main()
{
using namespace std;
double a1[4]={1.2,2.4,3.6,4.8};//传统C语言写法初始化四元素数组
vector a2(4);//C++98方法初始化,动态数组替代品
a2[0]=1.0/3.0;//初始化各个元素,C++98中没有捷径初始化
a2[1]=1.0/5.0;
a2[2]=1.0/7.0;
a2[3]=1.0/9.0;
array a3={3.14,2.72,1.62,1.41};//静态内存分配,常规数组替代品
array a4;//被默认初始化为0
a4=a3;
cout<<"a1[2]: "<
//#include
struct person
{
char name1[20];
char name2[20];
char grade;//定义字符型变量
int age;
};
int main()
{
using namespace std;
person* xv=new person;//new一个xv指向结构person
cout<<"What is your first name? "<name1,20);//使用getline以避免姓名中出现空格,造成\n被读取
cout<<"What is your last name? "<name2,20);
cout<<"what letter grade do you deserve? "<>(*xv).grade;//读入输入的成绩值
cout<<"What is your age? "<>xv->age;
cout<<"Name: "<name2<<", "<name1<age<
#include
int main()
{
using namespace std;
string name;
string dessert;
cout<<"Enter your name:\n";
getline(cin,name);//注意和数组的语法区别:cin.getline(name,30)
cout<<"Enter your favorite dessert:\n";
cin>>dessert;
cout<<"I have some delicious "<
#include
int main()
{
using namespace std;
char name1[20];
char name2[20];
cout<<"Enter your first name: "<
#include
int main()
{
using namespace std;
string name1,name2,name;
cout<<"Enter your first name: "<
#include
struct CandyBar
{
std::string brand="Mocha Munch";
float weight=2.3;
int calorie=350;
}snack;//定义结构时即初始化变量snack
int main()
{
std::cout<<"Brand: "<
#include
struct CandyBar
{
std::string brand;
float weight;
int calorie;
};
int main()
{
CandyBar species[3];//初始化一个三元素的结构数组species
species[0].brand="傻子";//数组法初始化
species[0].weight=25.25;
species[0].calorie=250;
(species+1)->brand="聪明者";//数组名即为指针,故也可用指针法给第二个成员结构的元素赋值
(species+1)->weight=36.66;
(species+1)->calorie=360;
CandyBar* xv[3]{&species[0],&species[1],&species[2]};
//上一行开头处不用const可通过编译,新建指针数组,分别指向三个结构
xv[2]->brand="普通人";//使用指针数组给第三个成员结构的元素赋值
xv[2]->weight=22.33;
xv[2]->calorie=290;
std::cout<<"Brand1: "<weight<calorie<
#include
using namespace std;
struct pizza
{
string company_name;
float pizza_diameter;
float pizza_weight;
};
int main()
{
pizza* xv=new pizza;
cout<<"Enter the name of pizza company: "<company_name);//注意语法细节,括号前面没有"."!!!
cout<<"Enter the diameter of pizza: "<>xv->pizza_diameter;
cout<<"Enter the weight of pizza: "<>xv->pizza_weight;
cout<<"So here's the information you have input: "<company_name<pizza_diameter<<"寸"<pizza_weight<<"磅";
delete xv;//注意释放New出的内存
return 0;
}
//PP4.13.9
#include
#include
struct CandyBar
{
std::string brand;
float weight;
int calorie;
};
int main()
{
CandyBar* xv=new CandyBar;//一旦new了切记使用delete!
xv->brand="傻瓜牌";
xv->weight=250.55;
xv->calorie=750;
std::cout<<"品牌名: "<brand<weight<calorie<
#include
int main()
{
using namespace std;
cout.setf(ios_base::fixed,ios_base::floatfield);//修复cout输出的小数点,防止整数时丢尾
array run_score;//注意array的初始化和声明方式,和数组区别
enum run_times{zero,first,second,third};//试试枚举变量而已,另类的const
cout<<"Enter your first score of 100meter: "<>run_score[0];
cout<<"Enter your second score of 100meter: "<>run_score[1];
cout<<"Enter your third score of 100meter: "<>run_score[2];
float mean_value=run_score[0]+run_score[1]+run_score[2];
mean_value=(mean_value)/3;
cout<<"Your NO."<