第7章

7.13-1调和平均数

//7.13-1 excise.cpp 调和平均数
#include <iostream>

double calculate(double a,double b);

using namespace std;

main()
{
double x,y,z;
cout<<"please input x and y value: ";
cin>>x>>y;

while(x!=0&&y!=0)
{
z=calculate(x,y);
cout<<"调和平均数是"<<z<<endl;
cout<<"please input x and y value again: ";
cin>>x>>y;
}
cout<<"输入的值不符合要求,退出";
return 0;
}

double calculate(double a,double b)
{
double z;
z=2.0*a*b/(a+b);
return z;
}

 

7.13-2.cpp    高尔夫 数组求值

//7.13-2 excise golf
#include <iostream>


int set(double * pa,int size);
void show(const double * pa ,int real_size);
void average(const double * pa ,int real_size);

using namespace std;

int main()
{

int size=10;
double score[10]={0};
int real_size;
real_size=set(score,size);
cout<<"数组设置已经完成!"<<endl;
cout<<real_size;
show(score,real_size);
cout<<endl;
cout<<"以上数组的平均值是: "<<endl;
average(score,real_size);
cout<<endl;

return 0;
}
int set(double * pa ,int size)
{
int i;
cout<<"please golf score:";
for(i=0;i<size;i++)
{
cin>>pa[i];

if(!cin)
break;
}
return i;
}

void show(const double * pa,int real_size)
{
for(int i=0;i<real_size;i++)
{
cout<<"数组score["<<i<<"]= "<<pa[i]<<";";
cout<<endl;}
}

void average(const double * pa,int real_size)
{
double sum=0.0;
double ave;
for(int i=0;i<real_size;i++)
{
sum+=pa[i];
}
ave=sum/real_size;

cout<<"数组的平均值是: "<<ave;
}

 

 

7.13-3excise cpp   结构体计算体积

//7.13-3 结构

#include <iostream>

struct box{
char maker[40];
float height;
float width;
float length;
float volume;
};

using namespace std;

//prototype
void showa(struct box box1);
void set_volume(struct box * box2);


int main()
{
struct box boxtemp;
int a;
cout<<"please input struct's members maker:";
cin>>boxtemp.maker;
cout<<"please input struct's members height:";
cin>>boxtemp.height;
cout<<"please input struct's members length:";
cin>>boxtemp.length;
cout<<"please input struct's members width:";
cin>>boxtemp.width;
cout<<"please input struct's members volume:";
cin>>boxtemp.volume;
cout<<"输入已经完成"<<endl;
showa(boxtemp);

set_volume(&boxtemp);
cout<<"再次显示结构成员"<<endl;
showa(boxtemp);
return 0;
}

void showa(struct box box1)
{
cout<<box1.maker<<endl;
cout<<box1.height<<endl;
cout<<box1.length<<endl;
cout<<box1.width<<endl;
cout<<box1.volume<<endl;

}

void set_volume(struct box * box2)
{
box2->volume=(box2->height)*(box2->width)*(box2->length);
}

 

//7.13-5 excise 递归算阶乘
#include <iostream>

int jiecheng(int n);

using namespace std;

int main()
{
int a;

cout<<"please input value: ";
cin>>a;
while(a)
{

if(a==0)
cout<<a<<"的阶乘1";
else
//cout<<"adf";
cout<<a<<"的阶乘为"<<jiecheng(a)<<endl;
cin>>a;
}
return 0;
}

int jiecheng(int n)
{
int m;
if(n>0)
m=n*jiecheng(n-1);
else return 1;

return m;
}

 

 

 

//7.13-6 excise cpp  填写数组 显示并反转数组
#include <iostream>
using namespace std;
int fill_array(double (* pa),int size);
void show_array(double (* pa),int size);
void reverse_array(double (* pa),int size);

main()
{
int size=10;
double array[size];
int count=0;

count=fill_array(array,size);
show_array(array,count);

reverse_array(array,count);
cout<<"反转之后的数组为:"<<endl;
show_array(array,count);

return 0;

}

int fill_array(double (* pa),int size)
{
double a;
int count=0;
for(int i=0;i<size;i++)
{
cout<<"请输入数组的第"<<i+1<<"个元素: ";
cin>>a;
if(cin)
{
pa[i]=a;
count++;
}
else break;
}
return count;
}

void show_array(double (* pa),int size)
{
for(int i=0;i<size;i++)
{
cout<<"数组中的第"<<i+1<<"值为:"<<pa[i]<<endl;
}

}


void reverse_array(double (* pa),int size)
{
double m;
int i=0;
int j=size-1;
for(;i<=j/2;i++,j--)
{
m=pa[i];
pa[i]=pa[j];
pa[j]=m;
}

}

 

//7.13.6.cpp   指针表示区间的数组设置值,显示值,重新赋值
#include <iostream>
using namespace std;
const int Max=10;

double * fill_array(double * begin,double * end);//原型 fill_array
void show_array(const double * begin,const double * end);//原型 show_array
void revalue(double * begin,double * end,double r);//原型 revalue

int main(){
double properties[Max]={0};
double * pa;
double r=3.5;
int i,j;
cout<<"请输入数组开始位置:";
cin>>i;
cout<<"请输入数组结束位置:";
cin>>j;
if(0<=i<j<=Max)
{
cout<<sizeof(double)<<endl;
cout<<& properties[i]<<":"<<& properties[j]<<endl;
pa=fill_array(& properties[i],& properties[j]);//调用填充函数 注意参数
cout<<pa;
show_array(properties,pa);//调用显示函数
cout<<"重新赋值之后的数组为:"<<endl;
revalue(& properties[i],& properties[j],r);
show_array(properties,pa);
}
else return 1;
return 0;
}

double * fill_array(double * begin,double * end)//填充数组的实现
{
double temp;
cout<<end-begin+1<<endl;

for(;begin<=end;begin++)
{
cout<<"please input a double value: ";
cin>>temp;
if(!cin)
{
cin.clear();
while(cin.get()!='\n')
continue;
cout<<"bad input;input process terminated .\n";
break;
}
else if(temp<0)//输入的是负值则跳出循环
break;
*begin=temp;//数组赋值
}
return --begin;
}

void show_array(const double * begin,const double * end)//显示数组中各个元素的值 这里的const 用来保证不会修改原始的数组;
{
int i=0;
for(begin;begin<=end;begin++)
{
cout<<"protery #"<<i++<<"="<<*begin<<endl;
}
}

void revalue(double * begin,double * end,double r)//重新赋值
{
for(begin;begin<=end;begin++)
*begin*=r;
}

 

 

//arrobj.cpp 7.13.8-a excise    const char * 类型四季字符存储及double数组的写入和读出
#include <iostream>


using namespace std;

void fill(double * pa);
void show(const double * pa);
const char arr[4][6]={{'s','p','r','i','n','g'},{'s','u','m','m','e','r'},{'f','a','l','l'},{'w','i','n','t','e','r'}};
const char (*p)[6]=arr;
main()
{

double expenses[4];
fill(expenses);
show(expenses);
return 0;
}
void fill(double * pa)
{
for(int i=0;i<4;i++)
{
cout<<"please enter ";
for(int j=0;j<6;j++)
cout<<p[i][j];
cout<<" expenses: ";
cin>>pa[i];
}
}

void show(const double * pa)
{
double total=0.0;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
cout<<p[i][j];
cout<<" :$ "<<pa[i]<<endl;
total+=pa[i];
}
cout<<"total expenses: $ " <<total<<endl;
}

 

 

//arrobj.cpp 7.13.8-a excise const char * 类型四季字符存储及结构体成员的写入和读出
#include <iostream>

using namespace std;

struct array{
double expenses[4];
};

void fill(array * pa);//prototype
void show(const array * pa);
const char arr[4][6]={{'s','p','r','i','n','g'},{'s','u','m','m','e','r'},{'f','a','l','l'},{'w','i','n','t','e','r'}};
const char (*p)[6]=arr;
main()
{
struct array output;

fill(&output);
show(&output);

return 0;
}
void fill(array * pa)
{
for(int i=0;i<4;i++)
{
cout<<"please enter ";
for(int j=0;j<6;j++)
cout<<p[i][j];
cout<<" expenses: ";
cin>>pa->expenses[i];
}
}

void show(const array * pa)
{
double total=0.0;
for(int i=0;i<4;i++)
{
for(int j=0;j<6;j++)
cout<<p[i][j];
cout<<" :$ "<<pa->expenses[i]<<endl;
total+=pa->expenses[i];
}
cout<<"total expenses: $ " <<total<<endl;
}

 

//7.13.9 excise 框架
#include <iostream>

using namespace std;
const int SLEN=30;
struct student{
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
int getinfo(student pa[],int n);
/*
void display1(student st);
void display2(const student * ps);
void display3(const student pa[],int n);
*/
void show(student pa[],int n);
int main()
{
cout<<"enter class 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);
show(ptr_stu,entered);
/*
for(int i=0;i<entered;i++)
{
display1(ptr_stu[i]);
display2(&ptr_tu[i]);
}
display3(ptr_stu,entered);
*/
delete [] ptr_stu;
cout<<"Done\n";
return 0;
}

int getinfo(student pa[],int n)
{
int count=0;
for(int i=0;i<n;i++)
{
cout<<"enter student fullname: ";
cin>>pa[i].fullname;
if(!cin) break;
cout<<"enter student hobby: ";
cin>>pa[i].hobby;
if(!cin) break;
cout<<"enter student ooplevel: ";
cin>>pa[i].ooplevel;
if(!cin) break;
count++;
}
return count;
}

void show(student pa[],int n)
{
for(int i=0;i<n;i++)
{
cout<<pa[i].fullname<<pa[i].hobby<<pa[i].ooplevel<<endl;
}
}

 

 

第8章

8.8.2   结构体

//8.8.2 excise cpp
#include <iostream>

using namespace std;
struct candybar{
char name[15];
double weight;
int fat;
};

//prototype
int set_candybar(candybar & pc,const char * pn,double pw,int pf);
void show_candybar(const candybar & pc);


int main()
{
candybar c1;
set_candybar(c1,"MACOS PC DONG",2.85,350);
cout<<"设置成功!"<<endl;
show_candybar(c1);
return 0;
}


int set_candybar(candybar & pc, const char * pn,double pw,int pf)
{
const char * temp=pn;
if(!pn)
{
cout<<"pn=null";
return -1;
}
int i=0;
while((*pn)!='\0')
{
pn++;
i++;
}
if(i>32)
{
cout<<"超出name元素的最大长度"<<endl;
return -2;
}
pn=temp;
for(int j=0;j<i;j++)
{
pc.name[j]=*pn;
pn++;
}
pc.weight=pw;
pc.fat=pf;

}

void show_candybar(const candybar & pc)
{
cout<<pc.name<<endl;
cout<<pc.weight<<endl;
cout<<pc.fat<<endl;
}

 

8.8.3

 

//8.8.3 excise string

#include <iostream>
#include <cctype>
#include <string>
using namespace std;


void str_to_upper(string & str);

int main()
{
string str1;
cout<<"enter a string :";
while(getline(cin,str1)&&str1!="q"&&str1!="Q")
{
str_to_upper(str1);
cout<<str1<<endl;
cout<<"next enter string: ";
}
cout<<"bye!"<<endl;
return 0;
}
void str_to_upper(string & str)
{
int n=str.size();
for(int i=0;i<n;i++)
{
str[i]=toupper(str[i]);
}
}

 

8.8.4 (一)  程序框架

 

//8.8.4 excise .cpp
#include <iostream>

using namespace std;
#include <cstring> //for strlen(), strcpy()

struct stringy
{
char *str;
int ct;
};

//prototypes for set(),show(),and show() go here
void show(const stringy & ps,int i=1);//prototype
void show(const char * str,int i=1);
void set(stringy &ps,const char * str);


int main()
{
stringy beany;
char testing[]="reality isn't what it used to be.";

set(beany,testing);
cout<<beany.str<<endl;
show(beany);
show(beany,2);
testing[0]='D';
testing[1]='u';
show(testing);
show(testing,3);
show("Done!");

return 0;

}

void show(const stringy & ps,int i)//结构体
{ if(i=1)
{
cout<<ps.str<<":";
cout<<ps.ct<<endl;}
else{

for(int j=0;j<i;j++)
{
cout<<ps.str<<":";
cout<<ps.ct<<endl;
}
}
}

void show(const char * str,int i)//字符串
{

if(i=1)
{
cout<<str<<endl;
}

else
{
for(int j=0;j<i;j++)
{
cout<<str<<endl;
}
}
}


void set(stringy & ps,const char * str)
{

ps.ct=strlen(str);
ps.str=new char[ps.ct+1];
strcpy(ps.str,str);
}

 

 

//8.8.5 excise cpp
#include <iostream>
using namespace std;
template <typename T>
T max5(T ar[]);

int main(void)
{
int array1[5]={0};
double array2[5]={0};
for(int i=0;i<5;i++)
{
cout<<"please enter list "<<i+1<<" int value:"<<endl;
cin>>array1[i];
}
int maxint=max5(array1);
cout<<"maxint: "<<maxint<<endl;
for(int i=0;i<5;i++)
{
cout<<"please enter list "<<i+1<<" double value:"<<endl;
cin>>array2[i];
}
double maxdouble=max5(array2);
cout<<"maxdouble:"<<maxdouble<<endl;
return 0;
}

template <typename T>
T max5(T ar[])
{
T temp;
temp=ar[0];
for(int i=0;i<5;i++)
{

if(temp<ar[i+1])
temp=ar[i+1];

}
return temp;
}

 

 

//8.8.6 excise cpp
#include <iostream>
#include <cstring>
#include <string>

using namespace std;

template <typename T>
T maxn(T ar[],int n);

template <> const char * maxn(const char * ar[],int n);

int main(void)
{
int array1[6];
double array2[4];
const char * array3[5]={
"asdf","asdfasdf","asdfasdfasdfasdf","adsfdf","aa"
};

for(int i=0;i<6;i++)
{
cout<<"int value: "<<i+1<<"=";
cin>> array1[i];
}
int a1=maxn(array1,6);
cout<<a1<<endl;
for(int i=0;i<4;i++)
{
cout<<"double value: "<<i+1<<"=";
cin>> array2[i];
}
double a2=maxn(array2,6);
cout<<a2<<endl;

const char * a3=maxn(array3,5);
cout<<&a3<<endl;
return 0;
}

template <typename T>
T maxn(T ar[],int n)
{
T temp;
temp=ar[0];
for(int i=0;i<n;i++)
{
if(temp<ar[i])
temp=ar[i];
}
return temp;
}

template <> const char * maxn(const char * ar[],int n)
{
const char * temp=ar[0];

for(int i=0;i<n;i++)
{
if(strlen(temp)<strlen(ar[i]))
temp=ar[i];
}


return temp;
}

posted on 2018-07-24 13:25  学习记录园  阅读(218)  评论(0编辑  收藏  举报