CPP_Basic_Code_P7.1-PP7.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
//P7.1
#include
void simple();//声明没有返回值的函数
int main()
{
using namespace std;
cout<<"main() will call the function simple(): \n";
simple();
cout<<"main() is finisied with the simple() function.\n";
//cin.get();
return 0;
}
void simple()//具体的函数编写
{
using namespace std;
cout<<"I'm but s simple function.\n";
}
//P7.2
#include
void cheers(int);
double cube(double x);//有返回值的函数
int main()
{
using namespace std;
cheers(5);
cout<<"Give me a number:\n";
double side;
cin>>side;
double volume=cube(side);
cout<<"A "<
using namespace std;
void n_chars(char,int);
int main()
{
int times;
char ch;
cout<<"Enter a character: ";
cin>>ch;
while (ch!='q')//输入'q'则退出程序
{
cout<<"Enter an integer: ";
cin>>times;
n_chars(ch,times);//注意调用方式
cout<<"\nEnter another character or press the q-Key to quit: ";
cin>>ch;
}
cout<<"The value of times is "<0)
cout<
long double probability(unsigned numbers,unsigned picks);
int main()
{
using namespace std;
double total,choices;
cout<<"Enter the number of choices on the game card and\n"
"the number of picks allowed:\n";
while ((cin>>total>>choices)&&choices<=total)
{
cout<<"You have one chance in ";
cout<0;n--,p--)
result=result*n/p;//利用连乘实现计算且避免中间数过大
return result;//返回结果
}
//P7.5
#include
const int ArSize=8;
int sum_arr(int arr[],int n);//声明传递数组的函数,指针和整数
//此处也可使用int* arr来实现数组的声明
int main()
{
using namespace std;
int cookies[ArSize] {1,2,4,8,16,32,64,128};
int sum=sum_arr(cookies,ArSize);//函数调用后赋值
cout<<"Total cookies eaten: "<
const int ArSize=8;
int sum_arr(int arr[],int n);
int main()
{
int cookies[ArSize] {1,2,4,8,16,32,64,128};
std::cout<
const int Max =5;
int fill_array(double ar[],int limit);//填充数组函数,返回输入的个数
void show_array(const double ar[],int n);//打印函数,无返回值
void revalue(double r,double ar[],int n);//修改数组函数,直接修改数组无返回值
int main()
{
using namespace std;
double properties[Max];//定义一个数组记录财产
int size=fill_array(properties,Max);//将实际输出的元素数赋值到size
show_array(properties,size);//打印size个财产
if (size>0)
{
cout<<"Enter revaluation factor: ";
double factor;//输入重新评估因子用于修改数组中的值
while (!(cin>>factor))//输入失败检测模块
{
cin.clear();
while (cin.get()!='\n')
continue;
cout<<"Bad input;please enter a number: ";
}
revalue(factor,properties,size);//调用修改数组函数
show_array(properties,size);//显示修改后的数组函数
}
cout<<"Done.\n";
cin.get();//光标浮动以等待输入
cin.get();
return 0;
}
int fill_array(double ar[],int limit)
{
using namespace std;
double temp;
int i;
for (i=0;i>temp;//读取并写入数组
if (!cin)//输入失败检测模块
{
cin.clear();
while (cin.get()!='\n')
continue;
cout<<"Bad input;input process terminated.\n";
break;
}
else if (temp<0)//输入负数跳出读取
break;
ar[i]=temp;//如果非负则赋值成功
}
return i;
}
void show_array(const double ar[],int n)
{
using namespace std;
for (int i=0;i
const int ArSize=8;
int sum_arr(const int * begin,const int * end);//定义数组区别函数,用指针区间传递参数
int main()
{
using namespace std;
int cookies[ArSize] {1,2,4,8,16,32,64,128};
int sum=sum_arr(cookies,cookies+ArSize);//区间为1~ArSize
cout<<"Total cookies eaten: "<
unsigned int c_in_str(const char* str,char ch);//也可使用const char str[]
int main()
{
using namespace std;
char mmm[15]="minimum";
char* wail="ululate";//显式指针赋值
unsigned int ms=c_in_str(mmm,'m');//指针参数和特征字符参数
unsigned int us=c_in_str(wail,'u');
cout<
char* buildstr(char c,int n);//返回指针的函数声明
int main()
{
using namespace std;
int times;
char ch;
cout<<"Enter a character: ";
cin>>ch;
cout<<"Enter a integer: ";
cin>>times;
char* ps=buildstr(ch,times);
cout<0)//n减到0
pstr[n]=c;//将字符c的值赋值给数组中各元素位置
return pstr;//返回字符串指针,new出来的地址
}
//P7.11
#include
struct travel_time
{
int hours;
int mins;
};
const int Mins_per_hr=60;//1小时=60分
travel_time sum(travel_time t1,travel_time t2);//返回结构的函数原型声明,接收2个结构参数
void show_time(travel_time t);//显示结构,接收1个结构参数
int main()
{
using namespace std;
travel_time day1={5,45};//注意结构不用()用{}
travel_time day2={4,55};
travel_time trip=sum(day1,day2);//结构赋值
cout<<"Two_day total: ";
show_time(trip);
travel_time day3={4,32};
cout<<"Three-day total: ";
show_time(sum(trip,day3));
return 0;
}
travel_time sum(travel_time t1,travel_time t2)
{
travel_time total;
total.mins=(t1.mins+t2.mins)%Mins_per_hr;//取模获得剩余分钟数
total.hours=t1.hours+t2.hours+(t1.mins+t2.mins)/Mins_per_hr;//除法获得小时数
return total;
}
void show_time(travel_time t)
{
using namespace std;
cout<
#include
struct polar//极坐标结构
{
double distance;
double angle;
};
struct rect//直角坐标结构
{
double x;
double y;
};
polar rect_to_polar(rect xypos);//直角转为极坐标
void show_polar(polar dapos);//显示极坐标
int main()
{
using namespace std;
rect rplace;//声明两个结构以存储输入的数据
polar pplace;
cout<<"Enter the x and y value: ";
while (cin>>rplace.x>>rplace.y)//连续使用cin可行,且忽略空格等
{
pplace=rect_to_polar(rplace);//结果赋值给第二个结构
show_polar(pplace);//显示
cout<<"Next two numbers (q to quiit): ";
}
cout<<"Done.\n";
return 0;
}
polar rect_to_polar(rect xypos)
{
using namespace std;
polar answer;
answer.distance=sqrt(xypos.x*xypos.x+xypos.y*xypos.y);
answer.angle=atan2(xypos.y,xypos.x);//y/x计算arctan
return answer;//返回结构
}
void show_polar(polar dapos)
{
using namespace std;
const double Rad_to_deg=57.29577951;//弧度转换为度数的常数因子
cout<<"Distance = "<
#include
struct polar//极坐标结构
{
double distance;
double angle;
};
struct rect//直角坐标结构
{
double x;
double y;
};
void rect_to_polar(const rect* pxy,polar* pda);//传递指针,第二个需要修改故不const
void show_polar(const polar* pda);//显示极坐标
int main()
{
using namespace std;
rect rplace;//声明两个结构以存储输入的数据
polar pplace;
cout<<"Enter the x and y value: ";
while (cin>>rplace.x>>rplace.y)//连续使用cin可行,且忽略空格等
{
rect_to_polar(&rplace,&pplace);//结果赋值给第二个结构
show_polar(&pplace);//显示
cout<<"Next two numbers (q to quiit): ";
}
cout<<"Done.\n";
return 0;
}
void rect_to_polar(const rect* pxy,polar* pda)
{
using namespace std;
pda->distance=sqrt(pxy->x*pxy->x+pxy->y*pxy->y);//指针使用->来控制成员
pda->angle=atan2(pxy->y,pxy->x);
}
void show_polar(const polar* pda)
{
using namespace std;
const double Rad_to_deg=57.29577951;//弧度转换为度数的常数因子
cout<<"Distance = "<distance;
cout<<",angle = "<angle*Rad_to_deg;
cout<<" degrees\n";
}
//P7.14
#include
#include
using namespace std;
const int SIZE=5;
void display(const string sa[],int n);//可写为string* sa
int main()
{
string list[SIZE];
cout<<"Enter your "<
#include
#include
const int Seasons=4;
const std::array Snames=
{"Spring","Summer","Fall","Winter"};
void fill(std::array *pa);
void show(std::array da);
int main()
{
std::array expenses;
fill(&expenses);//传递地址指针效率高但是看起来复杂
show(expenses);//传递实体效率低下
return 0;
}
void fill(std::array *pa)
{
using namespace std;
for (int i=0;i>(*pa)[i];//此处括号必不可少
}
}
void show(std::array da)
{
using namespace std;
double total=0.0;
cout<<"\nEXPENSES\n";
for (int i=0;i
void countdown(int n);
int main()
{
countdown(4);
return 0;
}
void countdown(int n)
{
using namespace std;
cout<<"Counting down……"<0)
countdown(n-1);//递归递减
cout<
const int Len=66;
const int Divs=6;
void subdivide(char ar[],int low,int high,int level);
int main()
{
char ruler[Len];
int i;
for (i=1;i
double betsy(int);
double pam(int);
void estimate(int lines,double (*pf)(int));//声明了函数指针
//(*pf)等同函数名效果,两者用法一致;且因历史原因,pf等价于(*pf)
int main()
{
using namespace std;
int code;
cout<<"How many lines of code do you need? ";
cin>>code;
cout<<"Here's Betsy's estimate:\n";
estimate(code,betsy);//调用函数指针betsy,函数名即地址,可传递
cout<<"Here's Pam's estimate:\n";
estimate(code,pam);
return 0;
}
void estimate(int lines,double (*pf)(int))
{
using namespace std;
cout<
const double* f1(const double ar[],int n);
const double* f2(const double [],int n);
const double* f3(const double *,int n);
//以上3种函数声明是完全相同的效果
int main()
{
using namespace std;
double av[3] {1112.3,1542.6,2227.9};
const double *(*p1) (const double *,int)=f1;//定义函数指针p1,初始化指向f1
auto p2=f2;//自动推导来自定义初始化p2,指向f2
cout<<"Using pointers to functions:\n";
cout<<"Address Value\n";
cout<<(*p1)(av,3)<<": "<<*(*p1)(av,3)<
void show();
double thpjs(double x,double y);
double n1,n2;
int main()
{
using namespace std;
show();//调用函数不加前面的返回值类型
while (n1!=0&&n2!=0)
{
double result=thpjs(n1,n2);
cout<<"Result of your input: "<>n1;
cout<<"Please enter another number: ";
cin>>n2;
}
//PP7.13.2
#include
int score_input(double *,int);
double score_average(const double *,int);
void score_show(const double *,int,double);//务必注意const必须全部到位!
const int ArSize=10;
int main()
{
using namespace std;
double score_list[ArSize] {0};//初始化成绩数组
int realsize=score_input(score_list,ArSize);//获取数据同时返回实际输入的值
double averagex=score_average(score_list,realsize);//调用函数获取数组平均值
score_show(score_list,realsize,averagex);//显示函数
return 0;
}
int score_input(double arr[],int limit)
{
using namespace std;
double temp=0;//输入安全检测临时变量
int i;
for (i=0;i>temp;
if (!cin)//异常处理模块
{
cin.clear();
while (cin.get()!='\n')//注意此处是cin.get()
continue;
cout<<"Bad input,over.\n";
break;
}
if (temp<0)//输入无效跳出
break;
arr[i]=temp;//有效则赋值
}
return i;//返回实际输入的个数
}
double score_average(const double arr[],int limit)//注意const
{
double sum=0;
for (int i=0;i
const int ArSize=40;
struct box
{
char maker[ArSize];
float height;
float width;
float length;
float volume;
};
box input_real_value(box rv);//按实值传递读取输入
box show_real_value(box rv);//按实值传递显示
void input_pointer(box* spr);//按指针传递读取输入
void show_pointer(const box* spr);//按指针传递显示
int main()
{
using namespace std;
box xv1;
xv1=input_real_value(xv1);//读取数据到xv1
show_real_value(xv1);//显示读入的数据
box* xv2=new box;//指针指向new出的box结构
input_pointer(xv2);//读取输入到指针指向的结构
show_pointer(xv2);
delete xv2;//勿忘
return 0;
}
box input_real_value(box rv)
{
using namespace std;
cout<<"Please enter the maker:";
cin>>rv.maker;
cout<<"Please enter the height,width,length: ";
cin>>rv.height>>rv.width>>rv.length;//数据可以连续读取
rv.volume=rv.height*rv.width*rv.length;//体积计算
return rv;
}
box show_real_value(box rv)
{
using namespace std;
cout<<"Maker: "<>spr->maker;
cout<<"Please enter the height,width,length: ";
cin>>spr->height>>spr->width>>spr->length;//指针式连续读取
spr->volume=spr->height*spr->width*spr->length;
}
void show_pointer(const box* spr)
{
using namespace std;
cout<<"Maker: "<maker<height
<<" "<width<<" "<length<<" "<volume<
long double probability(unsigned field_numbers,unsigned picks,double extr);
int main()
{
using namespace std;
double total,choices,extra;
long double pro;
cout<<"Enter field_numbers,the number of picks allowed and extra_number:\n";
cout<<"Of course,'q' to quit."<>total>>choices>>extra)&&choices<=total)
//同时读取三个参数
{
pro=probability(total,choices,extra);
cout<<"Input is ok.\n";
break;
}
cout<<"Probability to win top-Money is: "<0;n--,p--)
result=result*n/p;//利用连乘实现计算且避免中间数过大
result=result*(1/extr);
return result;//返回结果
}
//PP7.13.5
#include
long long int n_multiplication(int n);
int main()
{
using namespace std;
cout<<"Enter the number: (q to quit.)";
int n_number;
while (cin>>n_number)
{
long long int result=n_multiplication(n_number);
cout<0)
RST=n*n_multiplication(n-1);//递归关键核心
else
RST=1;
return RST;
}
//PP7.13.6
#include
int Fill_array(double arr[],int limit);
void show_array(const double [],int limit);
void reverse_array(double *,int limit);
const int ArSize=10;
int main()
{
using namespace std;
double z_array[ArSize];
int realvalue=Fill_array(z_array,ArSize);
show_array(z_array,realvalue);
reverse_array(z_array,realvalue);
show_array(z_array,realvalue);
reverse_array(z_array+1,realvalue-2);//指针右移一个元素以及长度缩短两个元素
show_array(z_array,realvalue);
return 0;
}
int Fill_array(double arr[],int limit)
{
using namespace std;
double temp;
int i;
for (i=0;i>temp)
arr[i]=temp;
else
{
cin.clear();
while (cin.get()!='\n')
continue;
cout<<"Bad input!\n";
break;
}
}
return i;
}
void show_array(const double arr[],int limit)
{
using namespace std;
cout<<"Numbers: ";
for (int i=0;ii;i++)//关键!交换不会反转的核心
{
swap=arr[limit];//经典交换模型
arr[limit]=arr[i];
arr[i]=swap;
}
}
//PP7.13.7
#include
const int Max =5;
double* fill_array(double ar[],int limit);//填充数组函数,返回实际数组末尾指针
void show_array(const double *,const double *);
void revalue(double r,double *,double *);
int main()
{
using namespace std;
double properties[Max];//定义一个数组记录财产
double* size=fill_array(properties,Max);//获取返回的数组指针
show_array(properties,size);
if (size>0)
{
cout<<"\n\nEnter revaluation factor:";
double factor;//输入重新评估因子用于修改数组中的值
while (!(cin>>factor))//输入失败检测模块
{
cin.clear();
while (cin.get()!='\n')
continue;
cout<<"Bad input;please enter a number: ";
}
revalue(factor,properties,size);//调用修改数组函数
show_array(properties,size);//显示修改后的数组函数
}
cout<<"\n\nDone.\n";
cin.get();//光标浮动以等待输入
return 0;
}
double* fill_array(double ar[],int limit)
{
using namespace std;
double temp;
int i;
for (i=0;i>temp;//读取并写入数组
if (!cin)//输入失败检测模块
{
cin.clear();
while (cin.get()!='\n')
continue;
cout<<"Bad input;input process terminated.\n";
break;
}
else if (temp<0)//输入负数跳出读取
break;
ar[i]=temp;//如果非负则赋值成功
}
double* ip=&ar[i];
return ip;
}
void show_array(const double* a,const double* b)
{
using namespace std;
cout<<"\nProperty: $";
for (a;a!=b;a++)//注意指针的条件判断方式
{
cout<<*a<<" ";
}
}
void revalue(double r,double* a,double* b)
{
for (a;a!=b;a++)
(*a)*=r;
}
//PP7.13.8.a
#include
const int Seasons=4;
const char* Sname[Seasons] {"Spring","Summer","Fall","Winter"};
void fill(double*);
void show(double*);
int main()
{
double expenses[Seasons] {0};
fill(expenses);//传递指针
show(expenses);
return 0;
}
void fill(double arr[])
{
using namespace std;
for (int i=0;i>arr[i];//此处括号必不可少
}
}
void show(double arr[])
{
using namespace std;
double total=0.0;
cout<<"\nEXPENSES\n";
for (int i=0;i//细心再细心啊!!
const int Seasons=4;
struct expense_s
{
double expense_arr[Seasons] {0};
};//结构定义在函数原型前,避免函数原型声明无效
const char* Sname[Seasons] {"Spring","Summer","Fall","Winter"};
expense_s fill(expense_s t);
void show(const expense_s* t1);
int main()
{
expense_s sear;
sear=fill(sear);//传递结构
std::cout<>t1.expense_arr[i];//结构式存储
}
return t1;//传递结构记得返回!!!
}
void show(const expense_s* t1)
{
using namespace std;
double total=0.0;
cout<<"\nEXPENSES\n";
for (int i=0;iexpense_arr[i]<expense_arr[i];//求和
}
cout<<"Total Expenses: $"<
using namespace std;
const int SLEN=30;
struct student
{
char fullname[SLEN];
char hobby[SLEN];
int opplevel;
};
int getinfo(student pa[],int n);
void display1(student st);
void display2(const student* ps);
void display3(const student pa[],int n);
int main()
{
cout<<"Enter calss size: ";
int class_size;
cin>>class_size;
while (cin.get()!='\n')
continue;
student* ptr_stu=new student[class_size];
int entered=getinfo(ptr_stu,class_size);
for (int i=0;i>pa->fullname;
cout<<"Enter #"<>pa->hobby;
cout<<"Enter #"<>pa->opplevel;
while (!cin)
{
cin.clear();
--i;//输入错误重新返回i值
cout<<"Bad input.\n";
while (cin.get()!='\n')
continue;
}
}
return i;
}
void display1(student st)
{
using namespace std;
cout<<"Student name: "<fullname<hobby<opplevel<fullname<hobby<opplevel<
const int ArSize=3;
double calculate(double n1,double n2,double (*ptr)(double, double));
double add(double,double);
double minus_n(double,double);
double change(double,double);
void show_arr(double (*pf[ArSize])(double,double),const char**,double,double);
int main()
{
using namespace std;
double q=calculate(2.5,10.4,add);
cout<<"Test: q= "<>n1>>n2)
{
show_arr(pf,npa,n1,n2);
}
cout<<"Done.\n";
return 0;
}
double calculate(double n1,double n2,double (*ptr)(double, double))
{
return (*ptr)(n1,n2);//此处ptr也可写为(*ptr)
}
double add(double x,double y)
{
return x+y;
}
double minus_n(double x,double y)
{
return x-y;
}
double change(double x,double y)
{
return x*y*0.8;
}
void show_arr(double (*pf[ArSize])(double,double),const char** pn,double t1,double t2)
{
using namespace std;
for (int i=0;i