c++ primer 6 练习题 (非复习题)
第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;
}
c++ primer 6 练习题 (非复习题)的更多相关文章
- C primer plus 练习题 第七章
1. #include <stdio.h> #define SPACE ' ' #define NEWLINE '\n' int main() { int spaces,newlines, ...
- C primer plus 练习题 第六章
16. #include <stdio.h> int main() { double remain = 100.0; ; ) { remain = remain * 0.08 + rema ...
- C primer plus 练习题 第五章
1. #include <stdio.h> #define MINU 60 int main() { int minute, hour, m; printf("请输入分钟:&qu ...
- C primer plus 练习题 第三章
5. #include <stdio.h> int main() { float you_sec; printf("请输入你的年龄:"); scanf("%f ...
- C primer plus 练习题 第二章
6. #include <stdio.h> void echo(); int main() { /* echo(); echo(); echo(); printf("\n&quo ...
- C primer plus 练习题 第一章
1. #include <stdio.h> int main() { //将英寸转换为厘米 1英寸=2.54厘米 int inch; printf("请输入英寸:"); ...
- const 使用一二
Primer C++ 练习题4.20: int i = -1; const int ic = i; 对于这个,一开始认为,ic 作为const 类型变量,定义时应该给其赋常值,而此处给的是变量i,因此 ...
- 值得Python小白学习的书 简单推荐几本吧
于我个人而言,我很喜欢Python,当然我也有很多的理由推荐你去学python.我只说两点.一是简单,二是写python薪资高.我觉得这俩理由就够了,对不对.买本书,装上pycharm,把书上面的例子 ...
- 《C Primer Plus(第6版)中文版》一1.12 复习题
本节书摘来自异步社区<C Primer Plus(第6版)中文版>一书中的第1章,第1.12节,作者 傅道坤,更多章节内容可以访问云栖社区"异步社区"公众号查看. 1. ...
随机推荐
- HDU1195 双向BFS(或BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1195 , 双向BFS或者直接BFS也可以过. 其实这道题只是单向BFS就可以过的,但是为了练算法,所以 ...
- linux 命令——58 ss(转)
telnet 命令通常用来远程登录.telnet程序是基于TELNET协议的远程登录客户端程序.Telnet协议是TCP/IP协议族中的一员,是 Internet远程登陆服务的标准协议和主要方式.它为 ...
- raspberrypi&linux
Raspberrypi&linux 2018-01-23 19:54:01 Let's go!
- [转载]AngularJS入门教程01:静态模板
为了说明angularJS如何增强了标准HTML,我们先将创建一个静态HTML页面模板,然后把这个静态HTML页面模板转换成能动态显示的AngularJS模板. 在本步骤中,我们往HTML页面中添加两 ...
- An incomplete guide to LaTex
LATEX入门与提高.陈志杰数理学院喜闻乐见的电子书.这本电子书由于是图片版本,所以无法使用搜索功能,幸亏目录详细. LaTeX Beginner's Guide.latex使用者都是从模版开始学习, ...
- SqlServer 学习笔记
随机函数 select rand() declare @age int set @age = rand()*100 select @age 数据类型转换 declare @birthday datat ...
- nodejs fs路径
引用:https://www.cnblogs.com/guangzhou11/p/7705257.html require()别的js文件的时候,将执行那个js文件. 注意: require()中的路 ...
- 【转】 bind1st bind2nd的使用
以前在使用stl的过程中发现bind1st和bind2nd这两个函数,当时不太理解什么意思,今天在网上查了一下相关资料发现竟然很简单,下面我就具体解释一下他们的用法. bind1st和bind2nd函 ...
- linux运维、架构之路-MySQL多实例
一.MySQL多实例介绍 一台服务器上开启多个不同的服务端口(3306,3307,3308),运行多个MySQL服务进程,共用一套MySQL安装程序,多实例MySQL在逻辑上看是 ...
- 八、MySQL 数据类型
MySQL 数据类型 MySQL中定义数据字段的类型对你数据库的优化是非常重要的. MySQL支持多种类型,大致可以分为三类:数值.日期/时间和字符串(字符)类型. 数值类型 MySQL支持所有标准S ...