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 <iostream>
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<<yams[0]+yams[1]+yams[2]<<endl;
cout<<"The package with "<<yams[1]<<"yams costs ";
cout<<yamcosts[1]<<" cents per yam.\n";
int total=yams[0]*yamcosts[0]+yams[1]*yamcosts[1];
total=total+yams[2]*yamcosts[2];
cout<<"The total yam expense is "<<total<<" cents.\n";
cout<<"\nSize of yams array = "<<sizeof yams;//計(jì)算數(shù)組字節(jié)數(shù)
cout<<" bytes.\n";
cout<<"Size of one element = "<<sizeof yams[0];//計(jì)算數(shù)組單個(gè)成員字節(jié)數(shù)
cout<<" bytes.\n";
return 0;
}
//P4.2
#include <iostream>
int main()
{
using namespace std;
const int Size=15;
char name1[Size];
char name2[Size]="C++owboy";
cout<<"Howdy! I'm "<<name2;
cout<<"! What's yuour name?\n";
cin>>name1;//這種方式的讀取不允許姓名間有空格一類
cout<<"Well, "<<name1<<", your name has ";
cout<<strlen(name1)<<" letters and is stored\n";//提取字符串長(zhǎng)度
cout<<"in an array of "<<sizeof(name1)<<" bytes.\n";//數(shù)組所占字節(jié)數(shù)
cout<<"Your initial is "<<name1[0]<<".\n";//第一個(gè)字母即第一個(gè)數(shù)組元素
name2[3]='\0';//將數(shù)組name2的第四個(gè)元素置為"\0"
cout<<"Here are the first 3 characters of my name: ";
cout<<name2<<endl;//此時(shí)將讀取前三個(gè)元素后即碰到\0辟犀,故只輸出前三個(gè)
return 0;
}
//P4.3
#include <iostream>
int main()
{
using namespace std;
const int Arsize=20;
char name[Arsize];
char dessert[Arsize];
cout<<"Enter your name:\n";
cin>>name;//由于此處不支持空格吊圾,會(huì)導(dǎo)致last name進(jìn)入后面的cin隊(duì)列,從而沒有機(jī)會(huì)輸入dessert
cout<<"Enter your favorite dessert:\n";
cin>>dessert;
cout<<"I have some delicious "<<dessert;
cout<<" for you, "<<name<<".\n";
return 0;
}
//P4.4
#include <iostream>
int main()
{
using namespace std;
const int Arsize=20;//修改數(shù)組容量可以儲(chǔ)存更多的字符串
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 "<<dessert;
cout<<" for you, "<<name<<".\n";
return 0;
}
//P4.5
#include <iostream>
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();//這種方式會(huì)調(diào)用讀取下一個(gè)字符(這里也就是回車)
cout<<"Enter your favorite dessert:\n";
cin.get(dessert,Arsize).get();//其實(shí)是cin.get(name,value)和cin.get()的合成縮寫
cout<<"I have some delicious "<<dessert;
cout<<" for you, "<<name<<".\n";
return 0;
}
//P4.6
#include <iostream>
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: "<<year<<endl;
cout<<"Address: "<<address<<endl;
cout<<"Done!\n";
return 0;
}
//P4,7
#include <iostream>
#include <string>
int main()
{
using namespace std;
char charr1[20];
char charr2[20]="jaguar";//數(shù)組式初始化
string str1;
string str2="panther";//C風(fēng)格字符串的初始化
cout<<"Enter a kind of feline: ";
cin>>charr1;//可使用cin注入數(shù)組
cout<<"Enter another kind of feline: ";
cin>>str1;//可使用cin注入string對(duì)象
cout<<"Here are some felines: \n";
cout<<charr1<<" "<<charr2<<" "<<str1<<" "<<str2<<" "<<endl;
cout<<"The third letter in "<<charr2<<" is "<< charr2[2]<<endl;//數(shù)組法引用
cout<<"The third letter in "<<str2<<" is "<<str2[2]<<endl;//數(shù)組法引用string對(duì)象中的元素
return 0;
}
//P4.8
#include <iostream>
#include <string>
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: "<<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 string:s3=s1+s2\n";
s3=s1+s2;//直接合并兩個(gè)字符串
cout<<"s3:"<<s3<<endl;
cout<<"You can append strings.\n";
s1+=s2;//等價(jià)于s1=s1+s2,是添加運(yùn)算符+=的效果
cout<<"s1+=s2 yield s1 = "<<s1<<endl;
s2+=" for a day!";//在結(jié)尾添加字符串
cout<<"s2+= \" for a day!\" yield s2 = "<<s2<<endl;//使用string對(duì)象無需考慮字符串長(zhǎng)度超出數(shù)組問題
return 0;
}
//P4.9
#include <iostream>
#include <string>
#include <cstring>//C風(fēng)格字符串
int main()
{
using namespace std;
char charr1[20];
char charr2[20]="jaguar";
string str1;
string str2="panther";
str1=str2;
strcpy(charr1,charr2);//C風(fēng)格字符串復(fù)制傳遞
str1+=" paste";
strcat(charr1," juice");//C風(fēng)格字符串結(jié)尾添加字符
int len1=str1.size();//子函數(shù)字符長(zhǎng)度計(jì)算
int len2=strlen(charr1);//C風(fēng)格字符長(zhǎng)度計(jì)算
cout<<"The string "<<str1<<" contains "<<len1<<" characters.\n";
cout<<"The string "<<charr1<<" contains "<<len2<<" charaters.\n";
return 0;
}
//P4.10
#include <iostream>
#include <string>
#include <cstring>
int main()
{
using namespace std;
char charr[20];
string str;
cout<<"Length of string in charr before input: "
<<strlen(charr)<<endl;//此時(shí)因未初始化,故字符內(nèi)容不確定
cout<<"Length of strinf in str before input: "
<<str.size()<<endl;//因?yàn)槭莝tring對(duì)象,未初始化前內(nèi)容自動(dòng)為0
cout<<"Enter a line of text:\n";
cin.getline(charr,20);//經(jīng)典C數(shù)組風(fēng)格讀取輸入
cout<<"You entered: "<<charr<<endl;
cout<<"Enter another line of text: \n";
getline(cin,str);//C++ string 對(duì)象讀取骗露,長(zhǎng)度自適應(yīng)
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;
return 0;
}
//P4.11
#include <iostream>
struct inflatable
{
char name[20];
float volume;
double price;
};//注意結(jié)構(gòu)體聲明需要;結(jié)尾
int main()
{
using namespace std;
inflatable guest={"Glorious Gloria",1.88,29.99};//初始化結(jié)構(gòu)成員guest
inflatable pal={"Audacious Arthur",3.12,32.99};//初始化結(jié)構(gòu)成員pal
cout<<"Expand your guest list with "<<guest.name;
cout<<" and "<<pal.name<<"!\n";//pal.name是一個(gè)char數(shù)組
cout<<"You can have both for $";
cout<<guest.price+pal.price<<"血巍!\n";//guest.price是一個(gè)double變量
return 0;
}
//P4.12
#include <iostream>
struct inflatable//結(jié)構(gòu)化聲明提倡使用外部式
{
char name[20];
float volume;
double price;
};
int main()
{
using namespace std;
inflatable bouquet={"Sunflowers",0.20,12.49};
inflatable choice;//未被初始化
cout<<"bouquet: "<<bouquet.name<<" for $";
cout<<bouquet.price<<endl;
choice=bouquet;//結(jié)構(gòu)體可以直接賦值萧锉,哪怕子結(jié)構(gòu)中有數(shù)組
cout<<"bouquet: "<<choice.name<<" for $";
cout<<choice.price<<endl;
return 0;
}
//P4.13
#include <iostream>
struct inflatable
{
char name[20];
float volume;
double price;
};
int main()
{
using namespace std;
inflatable guest[2]=//此處[2]表明有兩個(gè)結(jié)構(gòu)
{
{"Bambi",0.5,21.99},
{"Godzilla",2000,565.99}
};//結(jié)構(gòu)初始化之后必須加;
cout<<"The guest "<<guest[0].name<<" and "<<guest[1].name
<<"\nhave a combined volume of "
<<guest[0].volume+guest[1].volume<<" cubic feet.\n";
return 0;
}
//P4.14
#include <iostream>
int main()
{
using namespace std;
int donuts=6;
double cups=4.5;
cout<<"donuts value = "<<donuts;
cout<<" and donuts address = "<<&donuts<<endl;//使用取址運(yùn)算符&
cout<<"cups avalue = "<<cups;
cout<<" and cups address = "<<&cups<<endl;
return 0;
}
//P4.15
#include <iostream>
int main()
{
using namespace std;
int updates=6;
int* p_updates;//此處定義指針
p_updates=&updates;//指針必須被賦值初始化
cout<<"Value: updates = "<<updates;
cout<<" , *p_updates = "<<*p_updates<<endl;//此處為所指向地址里的值
cout<<"Address: &updates = "<<&updates;
cout<<" , p_updates = "<<p_updates<<endl;//指針本身是一個(gè)地址
++*p_updates;//*p_updates可以像使用int變量一樣操作
cout<<"Now updates = "<<updates<<endl;
return 0;
}
//P4.16
#include <iostream>
int main()
{
using namespace std;
int higgens=5;
int* pt=&higgens;
cout<<"Value of higgens = "<<higgens
<<"; Address of higgens = "<<&higgens<<endl;
cout<<"Value of *pt = "<<*pt//帶*的指針即為對(duì)應(yīng)的值
<<"; Value of pt = "<<pt<<endl;//不帶*即為指針本身藻茂,即地址
return 0;
}
//P4.17
#include <iostream>
int main()
{
using namespace std;
int nights=1001;
int* pt=new int;//申請(qǐng)了一塊數(shù)據(jù)類型為int的新內(nèi)存驹暑,且指針也指向int
*pt=1001;
cout<<"nights value = ";
cout<<nights<<": location "<<&nights<<endl;//nights的地址
cout<<"int ";
cout<<"value = "<<*pt<<": location = "<<pt<<endl;
double* pd=new double;//申請(qǐng)了一塊數(shù)據(jù)類型為double的新內(nèi)存且指向double
*pd=10000001.0;
cout<<"double ";
cout<<"value = "<<*pd<<": location = "<<pd<<endl;//此處只能用pd來訪問新申請(qǐng)的內(nèi)存
cout<<"location of pointer pd: "<<&pd<<endl;
cout<<"size of pt = "<<sizeof(pt);//這里計(jì)算指針的字節(jié)數(shù)
cout<<": size of *pt = "<<sizeof(*pt)<<endl;//這里是指針?biāo)笇?duì)象的值的字節(jié)數(shù)
cout<<"size of pd = "<<sizeof(pd);//這里計(jì)算指針的字節(jié)數(shù)
cout<<": size of *pd = "<<sizeof(*pd)<<endl;//這里是指針?biāo)笇?duì)象的值的字節(jié)數(shù)
delete pt;
delete pd;
return 0;
}
//P4.18
#include <iostream>
int main()
{
using namespace std;
double* p3=new double[3];//new了一個(gè)3個(gè)元素的double數(shù)組
p3[0]=0.2;
p3[1]=0.5;
p3[2]=0.8;
cout<<"p3[1] is "<<p3[1]<<".\n";
++p3;//指針右移1個(gè)元素
cout<<"Now p3[0] is "<<p3[0]<<" and ";
cout<<"p3[1] is "<<p3[1]<<".\n";
--p3;//指針移回原位玫恳,以備之后釋放內(nèi)存
delete [] p3;//delete了之前new的內(nèi)存
return 0;
}
//P4.19
#include <iostream>
int main()
{
using namespace std;
double wages[3]={10000.0,20000.0,30000.0};
short stacks[3]={3,2,1};
double* pw=wages;//數(shù)組名為數(shù)組第一個(gè)元素的地址
short* ps=&stacks[0];//這是上一句顯式的寫法
cout<<"pw= "<<pw<<", *pw= "<<*pw<<endl;
++pw;//右移一個(gè)元素辨赐,double實(shí)際上是8字節(jié)
cout<<"add 1 to the pw pointer:\n";
cout<<"pw= "<<pw<<", *pw= "<<*pw<<"\n\n";
cout<<"ps= "<<ps<<", *ps= "<<*ps<<endl;
++ps;//右移一個(gè)元素,short實(shí)際上是2字節(jié)
cout<<"add 1 to ps pointer:\n";
cout<<"ps= "<<ps<<", *ps= "<<*ps<<"\n\n";
cout<<"access two elements with array notation\n";
cout<<"stacks[0]= "<<stacks[0]<<",stacks[1]= "<<stacks[1]<<endl;
//以上是直接訪問數(shù)組元素的方式
cout<<"access two elements with pointer notaion\n";
cout<<"*stacks= "<<*stacks<<",*(stacks+1)= "<<*(stacks+1)<<endl;
//以上是將數(shù)組名當(dāng)做指針使用京办,移動(dòng)提取元素的方式
cout<<sizeof(wages)<<" = size of wages array\n";//計(jì)算整個(gè)數(shù)組的字節(jié)數(shù)
cout<<sizeof(pw)<<" = size of pw pointer\n";//僅計(jì)算pw指針的字節(jié)數(shù)
return 0;
}
//P4.20
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
char animal[20]="bear";
const char* bird="wren";//使用const可防止bird修改值wren
char* ps;
cout<<animal<<" and "<<bird<<endl;//cout對(duì)char類型默認(rèn)為字符串輸出
cout<<"Enter a kind of animal: ";
cin>>animal;
ps=animal;
cout<<ps<<"!\n";
cout<<"before using strcpy():\n";
cout<<animal<<" at "<<(int*)animal<<endl;//使用(int*)強(qiáng)制類型轉(zhuǎn)換顯示地址
cout<<ps<<" at "<<(int*)ps<<endl;
ps=new char[strlen(animal)+1];//申請(qǐng)新內(nèi)存掀序,且容量以animal為準(zhǔn)再加1,以便存儲(chǔ)空字符\0到結(jié)尾
strcpy(ps,animal);
cout<<"after using strcpy():\n";
cout<<animal<<" at "<<(int*)animal<<endl;
cout<<ps<<" at "<<(int*)ps<<endl;
delete [] ps;
return 0;
}
//P4.21
#include <iostream>
struct inflatable
{
char name[20];
float volume;
double price;
};
int main()
{
using namespace std;
inflatable * ps=new inflatable;//新申請(qǐng)一個(gè)動(dòng)態(tài)結(jié)構(gòu)指針
cout<<"Enter name of inflatable item: ";
cin.get(ps->name,20);//指針法讀取輸入
cout<<"Enter volume in cubic feet: ";
cin>>(*ps).volume;//數(shù)組名引用法讀取輸入
cout<<"Enter price: $";
cin>>ps->price;
cout<<"Name: "<<(*ps).name<<endl;
cout<<"Volume: "<<ps->volume<<" cubic feet\n";
cout<<"Price: $"<<ps->price<<endl;
delete ps;//釋放new出的內(nèi)存
return 0;
}
//P4.22
#include <iostream>
#include <cstring>
using namespace std;
char* getname(void);//函數(shù)聲明惭婿,返回值為char不恭,不接受參數(shù)
int main()
{
char* name;
name=getname();//第一次調(diào)用子函數(shù)
cout<<name<<" at "<<(int*)name<<"\n";
delete [] name;
name=getname();//第二次調(diào)用子函數(shù)
cout<<name<<" at "<<(int*)name<<"\n";
delete [] name;//釋放內(nèi)存
return 0;
}
char* getname()//子函數(shù)開始
{
char temp[80];//建立臨時(shí)字符串?dāng)?shù)組
cout<<"Enter last name: ";
cin>>temp;//讀取字符串到數(shù)組temp
char* pn=new char[strlen(temp)+1];//新定義恰到好處的指針和所需內(nèi)存
strcpy(pn,temp);//復(fù)制數(shù)據(jù)到新的指針地址
return pn;//返回指針地址(char類型叶雹,將被識(shí)別為字符串本身)
}
//P4.23
#include <iostream>;
struct antarctica_years_end//結(jié)構(gòu)定義
{
int year;
};
int main()
{
antarctica_years_end s01, s02, s03;//定義三個(gè)結(jié)構(gòu)變量
s01.year = 1998;//結(jié)構(gòu)式賦值
antarctica_years_end *pa = &s02;//定義結(jié)構(gòu)指針pa指向s02
pa->year = 1999;//指針式賦值
antarctica_years_end trio[3];//創(chuàng)建三個(gè)元素的結(jié)構(gòu)數(shù)組
trio[0].year = 2003;//結(jié)構(gòu)數(shù)組訪問成員
std::cout << trio->year << std::endl;
//輸出結(jié)構(gòu)數(shù)組第一個(gè)元素結(jié)構(gòu)的year成員 2003
const antarctica_years_end *arp[3] = {&s01, &s02, &s03};//定義含3指針的指針數(shù)組
//指針式訪問第二個(gè)元素,即&s02指向結(jié)構(gòu)變量的year成員1999
const antarctica_years_end **ppa = arp;
//創(chuàng)建指向上一個(gè)指針數(shù)組的的指針
//或者使用const antarctica_years_end **ppb=arp
std::cout<<(*ppa)->year<<std::endl;
//ppa解除引用后换吧,元素依然為指針(畢竟為指針數(shù)組第一個(gè)元素)1998
//ppb和ppa一樣折晦,故+1后指針右移一個(gè)元素為&s02,1999
return 0;
}
//P4.24
#include <iostream>
#include <vector>
#include <array>
int main()
{
using namespace std;
double a1[4]={1.2,2.4,3.6,4.8};//傳統(tǒng)C語言寫法初始化四元素?cái)?shù)組
vector<double> a2(4);//C++98方法初始化,動(dòng)態(tài)數(shù)組替代品
a2[0]=1.0/3.0;//初始化各個(gè)元素沾瓦,C++98中沒有捷徑初始化
a2[1]=1.0/5.0;
a2[2]=1.0/7.0;
a2[3]=1.0/9.0;
array<double,4> a3={3.14,2.72,1.62,1.41};//靜態(tài)內(nèi)存分配满着,常規(guī)數(shù)組替代品
array<double,4> a4;//被默認(rèn)初始化為0
a4=a3;
cout<<"a1[2]: "<<a1[2]<<" at "<<&a1[2]<<endl;
cout<<"a2[2]: "<<a2[2]<<" at "<<&a2[2]<<endl;
cout<<"a3[2]: "<<a3[2]<<" at "<<&a3[2]<<endl;
cout<<"a4[2]: "<<a4[2]<<" at "<<&a4[2]<<endl;
a1[-2]=20.2;//即*(a1-2),a1數(shù)組前移兩個(gè)元素贯莺,已位于數(shù)組外部风喇!可使用如:a2.at(1)=2.3
cout<<"a1[-2]: "<<a1[-2]<<" at "<<&a1[-2]<<endl;
cout<<"a3[2]: "<<a3[2]<<" at "<<&a3[2]<<endl;
cout<<"a4[2]: "<<a4[2]<<" at "<<&a4[2]<<endl;
return 0;
}
//PP4.13.1
#include <iostream>
//#include <cstring>
struct person
{
char name1[20];
char name2[20];
char grade;//定義字符型變量
int age;
};
int main()
{
using namespace std;
person* xv=new person;//new一個(gè)xv指向結(jié)構(gòu)person
cout<<"What is your first name? "<<endl;
cin.getline(xv->name1,20);//使用getline以避免姓名中出現(xiàn)空格,造成\n被讀取
cout<<"What is your last name? "<<endl;
cin.getline(xv->name2,20);
cout<<"what letter grade do you deserve? "<<endl;
cin>>(*xv).grade;//讀入輸入的成績(jī)值
cout<<"What is your age? "<<endl;
cin>>xv->age;
cout<<"Name: "<<xv->name2<<", "<<xv->name1<<endl;
cout<<"Grade: "<<++(*xv).grade<<endl;//此處因?yàn)槭亲址吐铺剑恍?+即可輸出下一檔次成績(jī)值
cout<<"Age: "<<xv->age<<endl;
delete xv;//釋放new出來的內(nèi)存
return 0;
}
//PP4.13.2
#include <iostream>
#include <string>
int main()
{
using namespace std;
string name;
string dessert;
cout<<"Enter your name:\n";
getline(cin,name);//注意和數(shù)組的語法區(qū)別:cin.getline(name,30)
cout<<"Enter your favorite dessert:\n";
cin>>dessert;
cout<<"I have some delicious "<<dessert;
cout<<" for you, "<<name<<".\n";
return 0;
}
//PP4.13.3
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
char name1[20];
char name2[20];
cout<<"Enter your first name: "<<endl;
cin.getline(name1,20);
cout<<"Enter your last name: "<<endl;
cin.getline(name2,20);
strcat(name2,",");//C風(fēng)格字符串的添加使用strcat()函數(shù)
strcat(name2,name1);
cout<<"Here's the information in single string: "<<endl
<<name2;
return 0;
}
//PP4.13.4
#include <iostream>
#include <string>
int main()
{
using namespace std;
string name1,name2,name;
cout<<"Enter your first name: "<<endl;
getline(cin,name1);//使用string對(duì)象的處理方式
cout<<"Enter your last name: "<<endl;
getline(cin,name2);
name=name2+","+name1;//string類支持直接加合多個(gè)字符串且不用考慮長(zhǎng)度問題
cout<<"Here's the information in single string: "<<endl
<<name;
return 0;
}
//PP4.13.5
#include <iostream>
#include <string>
struct CandyBar
{
std::string brand="Mocha Munch";
float weight=2.3;
int calorie=350;
}snack;//定義結(jié)構(gòu)時(shí)即初始化變量snack
int main()
{
std::cout<<"Brand: "<<snack.brand<<std::endl
<<"Weight: "<<snack.weight<<std::endl
<<"Calorie: "<<snack.calorie<<std::endl;
return 0;
}
//PP4.13.6
#include <iostream>
#include <string>
struct CandyBar
{
std::string brand;
float weight;
int calorie;
};
int main()
{
CandyBar species[3];//初始化一個(gè)三元素的結(jié)構(gòu)數(shù)組species
species[0].brand="傻子";//數(shù)組法初始化
species[0].weight=25.25;
species[0].calorie=250;
(species+1)->brand="聰明者";//數(shù)組名即為指針魂莫,故也可用指針法給第二個(gè)成員結(jié)構(gòu)的元素賦值
(species+1)->weight=36.66;
(species+1)->calorie=360;
CandyBar* xv[3]{&species[0],&species[1],&species[2]};
//上一行開頭處不用const可通過編譯,新建指針數(shù)組爹耗,分別指向三個(gè)結(jié)構(gòu)
xv[2]->brand="普通人";//使用指針數(shù)組給第三個(gè)成員結(jié)構(gòu)的元素賦值
xv[2]->weight=22.33;
xv[2]->calorie=290;
std::cout<<"Brand1: "<<species[0].brand<<std::endl//輸出第一個(gè)成員結(jié)構(gòu)的品牌名
<<"Weight2: "<<(species+1)->weight<<std::endl//輸出第二個(gè)成員結(jié)構(gòu)的產(chǎn)品重量
<<"Calorie3: "<<xv[2]->calorie<<std::endl;//輸出第三個(gè)成員結(jié)構(gòu)的產(chǎn)品熱量
return 0;
}
//PP4.13.7&//PP4.13.8
#include <iostream>
#include <string>
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: "<<endl;
getline(cin,xv->company_name);//注意語法細(xì)節(jié)耙考,括號(hào)前面沒有"."!!!
cout<<"Enter the diameter of pizza: "<<endl;
cin>>xv->pizza_diameter;
cout<<"Enter the weight of pizza: "<<endl;
cin>>xv->pizza_weight;
cout<<"So here's the information you have input: "<<endl;
cout<<"公司名稱: "<<xv->company_name<<endl
<<"披薩尺寸: "<<xv->pizza_diameter<<"寸"<<endl
<<"披薩重量: "<<xv->pizza_weight<<"磅";
delete xv;//注意釋放New出的內(nèi)存
return 0;
}
//PP4.13.9
#include <iostream>
#include <string>
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<<"品牌名: "<<xv->brand<<std::endl
<<"重量: "<<xv->weight<<std::endl
<<"熱量: "<<xv->calorie<<std::endl;
delete xv;
return 0;
}
//PP4.13.10
#include <iostream>
#include <array>
int main()
{
using namespace std;
cout.setf(ios_base::fixed,ios_base::floatfield);//修復(fù)cout輸出的小數(shù)點(diǎn)潭兽,防止整數(shù)時(shí)丟尾
array<float,3> run_score;//注意array的初始化和聲明方式琳骡,和數(shù)組區(qū)別
enum run_times{zero,first,second,third};//試試枚舉變量而已,另類的const
cout<<"Enter your first score of 100meter: "<<endl;
cin>>run_score[0];
cout<<"Enter your second score of 100meter: "<<endl;
cin>>run_score[1];
cout<<"Enter your third score of 100meter: "<<endl;
cin>>run_score[2];
float mean_value=run_score[0]+run_score[1]+run_score[2];
mean_value=(mean_value)/3;
cout<<"Your NO."<<first<<" score of 100 meter: "<<run_score[0]<<"s"<<endl;
cout<<"Your NO."<<second<<" score of 100 meter: "<<run_score[1]<<"s"<<endl;
cout<<"Your NO."<<third<<" score of 100 meter: "<<run_score[2]<<"s"<<endl;
cout<<"So your mean value score is: "<<mean_value<<"s"<<endl;
return 0;
}