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. ...
随机推荐
- 559. N 叉树的最大深度
给定一个 N 叉树,找到其最大深度. 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数. 例如,给定一个 3叉树 : 我们应返回其最大深度,3. 说明: 树的深度不会超过 1000.树的节点总 ...
- Hyper-V 2016 配置管理系列(部署篇)
Hyper主机前提准备以后,我们开始Hyper-V Cluster 群集配置 准备验证Cluster 群集 : 1)打开群集管理器,点击"validate Configuration&quo ...
- 写在Github被微软收购之际 - Github的那些另类用法
这几天朋友圈被微软75亿美元收购Github的新闻刷屏了.Jerry也来贡献一篇和Github相关的文章. 这篇文章包含了Jerry平时对于Github的一些另类用法.目录如下: 1. 部署HTML应 ...
- [VC]strcpy memcpy memset区别与简介
strcpy 原型:extern char *strcpy(char *dest,char *src); 用法:#include <string.h> 功能:把src所指由NULL结束的字 ...
- 解决mysql8小时无连接自动断掉机制
windows下打开my.ini,增加: interactive_timeout=28800000 wait_timeout=28800000 MySQL是一个小型关系型数据库管理系统,由于MySQL ...
- z-index、absolute、marquee滚动条的问题
1.z-index 层次叠加 ,元素叠加,谁的权重大谁就在上面 1).父级出现position:relation:的时候,失效: 2).层叠元素出现float的时候失效: 3).层次元素也得设置pos ...
- 2019 ACM-ICPC全国邀请赛(西安) M.Travel 二分+判联通
https://nanti.jisuanke.com/t/39280 讲道理这题写bfs求最大边权限制下从1到n的最短步数,然后二分判一下就行了. 然鹅我还是直接套了dij,一开始纠结dij能不能过, ...
- 用JavaScript实现CheckBox的全选取消反选,及遮罩层中添加内容
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- DevOps - CI/CD - Jenkins
Jenkins 是一款流行的开源持续集成(Continuous Integration)工具,广泛用于项目开发,具有自动化构建.测试和部署等功能.本文以 CentOS7 环境为例,总结了 Jenkin ...
- php订单号的生成
来自ECSHOP订单号生成函数:/includes/lib_order.php文件中的get_order_sn() /** * 得到新订单号 * @return string */ function ...