C++学习-Day-19

  • 一、编程练习

一、编程练习

  1. p_9.6.1
#ifndef EXERCISE27_H_H_INCLUDED
#define EXERCISE27_H_H_INCLUDED
//golf.h
const int len=40;
struct golf
{char fullname[len];int handicap;
};//non-interactive version;
//function sets golf structure to provided name,handicap
//using values passed as arguments to the function
void setgolf(golf &g,const char * name,int hc);//interactive version:
//function solicits name and handicap from user
//and sets the members of g to the value entered
//returns 1 if name is entered,0 if name is empty string
int setgolf(golf &g);//function resets handicap to new value
void handicap(golf &g,int hc);//function display contents of golf structure
void showgolf(const golf &g);#endif // EXERCISE27_H_H_INCLUDED
//exercise27.cpp
#include<iostream>
#include"exercise27_h.h"
#include<cstring>
void setgolf(golf &g,const char * name,int hc)
{for(int i=0;i<len;i++)g.fullname[i]=name[i];g.handicap=hc;
}int setgolf(golf &g)
{std::cout<<"Please enter the name "<<std::endl;int i=0;std::cin.getline(g.fullname,len);std::cout<<"Please enter the handicap "<<std::endl;std::cin>>g.handicap;if(strlen(g.fullname)==0)return 0;elsereturn 1;
}void showgolf(const golf &g)
{std::cout<<"name: "<<g.fullname<<std::endl;std::cout<<"handicap: "<<g.handicap<<std::endl;
}void handicap(golf &g,int hc)
{g.handicap=hc;
}int main()
{golf g[3];int n = 0;std::cout<< "Enter the information of golf player: " <<std::endl;while ((n < 3) && (setgolf(g[n]))){n++;std::cout<< "Next golf player: " <<std::endl;std::cin.ignore(1);}std::cout << "Show all golf player information: " <<std::endl;for (int i = 0; i < 3; i++){showgolf(g[i]);}std::cout<<"display a player's information\n";golf p;const char na[5]="stan";setgolf(p,na,9);showgolf(p);handicap(p,10);showgolf(p);
}2. 使用string读取输入的字符串,构造一个函数计算输入字符串的字符数(不包含空格),并输出每次字符串的字符数和累计字符数。```c
#include<iostream>
#include<string>
void strcount(const char *str);
using namespace std;
int main()
{string input;const char *p=input.c_str();cout<<"Enter a line('q'to quit):\n";while(getline(cin,input)){strcount(p);cout<<"Enter a next line:\n";}
}
void strcount(const char *str)
{static int total=0;int nu,n=0;nu=string(str).length();for(int i=0;i<nu;i++){if(str[i]!=32)n++;}total+=n;cout<<str<<" has "<<n<<" characters\n";cout<<"Total characters are "<<total<<endl;
}
  1. 下面是一个结构声明:
struct chaff
{char dross[20];int slag;
};

使用定位new运算符将包含两个这种结构的数组分配缓冲区,然后给成员赋值并显示。

#include<iostream>
#include<cstring>
#include<new>
const int BUF=512;
using namespace std;
struct chaff
{char dross[20];int slag;
};
int main()
{chaff *ch[2];char buffer1[BUF];char buffer2[BUF];ch[0]=new (buffer1) chaff;ch[1]=new (buffer2) chaff;for(int i=0;i<2;i++){char dr[20];cout<<"Please input the name of dross\n";cin>>dr;strcpy(ch[i]->dross,dr);cout<<"Please input the number of dross\n";cin>>ch[i]->slag;}for(int i=0;i<2;i++){cout<<"dross of chaff "<<i+1<<": "<<ch[i]->dross<<endl;cout<<"slag of chaff "<<i+1<<": "<<ch[i]->slag<<endl;}
}
  1. p_9.6.4
//exercise30_h.h
#ifndef EXERCISE30_H_H_INCLUDED
#define EXERCISE30_H_H_INCLUDED
#include <iostream>
namespace SALES
{const int QUARTERS = 4;struct Sales{double sales[QUARTERS];double average;double max;double min;};//copies the lesser of 4 or n items from the array ar//to the sales member of s and computes and stores the//average, maximum, and minimum values of the entered items;//remaining elements of sales, if any, set to 0void setSales(Sales & s, const double ar[], int n);//gathers sales for 4 quarters interactively, stores them//in the sales member of s and computes and stores the//average, maximum, and minimum valuesvoid setSales(Sales & s);//display all information in structurevoid showSales(const Sales & s);
}
#endif // EXERCISE30_H_H_INCLUDED
//exercise30.cpp
#include <iostream>
#include"exercise30_h.h"
using std::cout;
using std::cin;
using std::endl;
namespace SALES
{//copies the lesser of 4 or n items from the array ar//to the sales member of s and computes and stores the//average, maximum, and minimum values of the entered items;//remaining elements of sales, if any, set to 0
void setSales(Sales & s, const double ar[], int n)
{double total=0;for(int i=0;i<n;i++){s.sales[i]=ar[i];total+=ar[i];}s.average=total/n;double ma=s.sales[0];double mi=s.sales[0];for(int i=1;i<n;i++){ma<s.sales[i]?s.sales[i]:ma;mi>s.sales[i]?s.sales[i]:mi;}s.max=ma;s.min=mi;
}
//gathers sales for 4 quarters interactively, stores them
//in the sales member of s and computes and stores the
//average, maximum, and minimum values
void setSales(Sales & s)
{cout<<"Please input "<<QUARTERS<<" double digits\n";double total=0;for(int i=0;i<QUARTERS;i++){cin>> s.sales[i];total+=s.sales[i];}s.average=total/QUARTERS;double ma=s.sales[0];double mi=s.sales[0];for(int i=1;i<QUARTERS;i++){ma<s.sales[i]?s.sales[i]:ma;mi>s.sales[i]?s.sales[i]:mi;}s.max=ma;s.min=mi;
}
//display all information in structure
void showSales(const Sales & s)
{cout<<"The "<<QUARTERS<<" quarters sales are \n";for(int i=0;i<QUARTERS;i++){cout<<s.sales[i]<<endl;}cout<<"Average sales are: "<<s.average<<endl;cout<<"Maximum sales are: "<<s.max<<endl;cout<<"Minimum sales are: "<<s.min<<endl;
}
Sales s1,s2;
int main()
{int n=6;const double ar[n]={43.6,42,46.1,32.6,44.2,35};setSales(s1,ar,n);showSales(s1);setSales(s2);showSales(s2);
}}

C++学习-Day-19相关推荐

  1. Ext.Net学习笔记19:Ext.Net FormPanel 简单用法

    Ext.Net学习笔记19:Ext.Net FormPanel 简单用法 FormPanel是一个常用的控件,Ext.Net中的FormPanel控件同样具有非常丰富的功能,在接下来的笔记中我们将一起 ...

  2. Caffe学习系列(19): 绘制loss和accuracy曲线

    转载自: Caffe学习系列(19): 绘制loss和accuracy曲线 - denny402 - 博客园 http://www.cnblogs.com/denny402/p/5110204.htm ...

  3. springmvc学习笔记(19)-RESTful支持

    springmvc学习笔记(19)-RESTful支持 标签: springmvc springmvc学习笔记19-RESTful支持 概念 REST的样例 controller REST方法的前端控 ...

  4. 深度学习(19)神经网络与全连接层二: 测试(张量)实战

    深度学习(19)神经网络与全连接层二: 测试(张量)实战 1. 传入测试集数据 2. 数据类型转换 3. 创建test_db 4. test/evluation 5. 创建神经网络 6. 输出 7. ...

  5. Android 8.0系统学习(19)--- SystemUI启动流程

    Android 8.0系统学习(19)--- SystemUI启动流程 systemui属于系统级应用,在开机过程中就会启动.具体来讲是在SystemServer进程中startOtherServic ...

  6. 区块链学习笔记19——ETH难度调整

    区块链学习笔记19--ETH难度调整 学习视频:北京大学肖臻老师<区块链技术与应用> 笔记参考:北京大学肖臻老师<区块链技术与应用>公开课系列笔记--目录导航页 前面学过,比特 ...

  7. Python学习笔记19:列表 III

    Python学习笔记19:列表 III 其实这篇笔记标题应该是列表扩展,从列表开始,将涵盖Python中的序列容器. 关于列表的基础知识,可以看我的前两篇文章: Python学习笔记1:列表. Pyt ...

  8. Linux 学习笔记19 信号

    Linux 学习笔记19 信号 信号 信号概述 为什么要是使用信号--为了实现进程的有序退出 信号是进程运行过程中,由自身产生或者由进程外部发来的消息.信号是硬件中断的软件模拟(软中断) signal ...

  9. c语言图片透明度混合,【PS CC 2018 学习连载 19】如何让图层与图层之间融合的更好?不透明度和混合模式详细讲解...

    原标题:[PS CC 2018 学习连载 19]如何让图层与图层之间融合的更好?不透明度和混合模式详细讲解 说起图层,根据之前的连载,已经学习了不少内容,比如: 但以上内容,都只是对图层的理解以及对指 ...

  10. 狂神学习系列19:ElasticSearch

    狂神学习系列19:ElasticSearch 声明: 本文章是基于狂神的课程所编写,本人才疏学浅,内容仅作参考 文章目录 狂神学习系列19:ElasticSearch 1. ElasticSearch ...

最新文章

  1. SQL Server:OA权限管理设计的实现 下
  2. POJ-2251 Dungeon Master bfs搜索
  3. 案例:图书管理——补充知识(数组相关API)||补充知识(数组响应式变化)
  4. SCSF 系列:Smart Client Software Factory 中 MVP 模式最佳实践
  5. C#修改文件或文件夹的权限,为指定用户、用户组添加完全控制权限
  6. 浅谈html的语义化和一些简单优化,html标签语义化
  7. Oracle 数据库的子查询(关联子查询)
  8. 【LeetCode笔记】240. 搜索二维矩阵II 剑指 Offer 04 二维数组中的查找(Java、指针)
  9. UITableView 系列二 :资料的设定方式 (Navigation Controller切换视图) (实例)
  10. 01-Eureka是什么?
  11. Openstack M版快速配置(二)--刷数据库
  12. 教你前端面试技巧,教你如何涨薪!
  13. 六部工坊ros启智机器人定点导航技术_【展品抢鲜看】程天科技外骨骼机器人亮相峰会,让每个人享受机器人的服务!...
  14. 强连通分量[trajan]
  15. jquery进度条组件
  16. 三种方法求解Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1,当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少。
  17. arduino环境esp32跑freertos系统实现触摸检测及wifi控制
  18. 6.1 matlab数值微分与数值积分
  19. 比较详细的HC-SR04超声波传感器数据及机器人避障的应用方法
  20. 双软企业的税收优惠政策

热门文章

  1. Win10系统截图新工具的快捷键
  2. css圣杯布局与双飞翼布局_CSS布局研讨会已于2018年更新
  3. python爬虫豆瓣top250_Python 爬取豆瓣TOP250实战
  4. 测试岗位面试前复习之【测试基础知识篇】
  5. iOS 开发:『Runtime』详解(二)Method Swizzling
  6. N MOSFET VGS(th)和管子导通的关系
  7. 293、Java中级10 -【多线程】 2020.03.31
  8. DNN与推荐两大门派,一念神魔,功不唐捐
  9. 教你把竖屏视频剪辑为横屏播放的方法
  10. AnyTrans for Mac(ios数据传输工具)