c++ primer plus 习题答案(2)
p221.8
- #include<iostream>
- #include<cstdlib>
- #include<cstring>
- using namespace std;
- const int SLEN = ;
- 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);
- 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);
- for (int i = ; i < entered; i++){
- display1(ptr_stu[i]);
- display2(&ptr_stu[i]);
- }
- display3(ptr_stu, entered);
- delete[]ptr_stu;
- cout << "done\n";
- system("pause");
- return ;
- }
- int getinfo(student pa[], int n){
- int i = , count = ;
- while (i < n && *pa[i].fullname != '\0'){
- cin.getline(pa[i].fullname, SLEN);
- cin.getline(pa[i].hobby, SLEN);
- cin >> pa[i].ooplevel;
- cin.get();
- count++;
- i++;
- }
- return count;
- }
- void display1(student st){
- cout << "st.fullname" << st.fullname <<
- endl << "st.hobby" << st.hobby <<
- endl << "st.ooplevel" << st.ooplevel <<
- endl;
- }
- void display2(const student *ps){
- cout << "ps->fullname" << ps->fullname <<
- endl << "ps->hobby" << ps->hobby <<
- endl << "ps->ooplevel" << ps->ooplevel <<
- endl;
- }
- void display3(const student pa[], int n){
- for (int i = ; i < n; i++){
- cout << "pa[i].fullname" << pa[i].fullname
- << endl << "pa[i].hobby" << pa[i].hobby
- << endl << "pa[i].ooplevel" << pa[i].ooplevel
- << endl;
- }
- }
p259.2
- #include<iostream>
- #include<cstdlib>
- #include<cstring>
- using namespace std;
- struct candybar {
- char brand[];
- float weight;
- int calory;
- };
- void assign(candybar &, char br[]="millennium munch", float w=2.85, int c=);
- void show_menu(candybar&);
- int main(){
- candybar tasty;
- assign(tasty);
- show_menu(tasty);
- system("pause");
- return ;
- }
- void assign(candybar &delicious, char br[], float w , int c){
- strcpy(delicious.brand, br);
- delicious.weight = w;
- delicious.calory = c;
- }
- void show_menu(candybar&delicious){
- cout << delicious.brand << endl;
- cout << delicious.weight << endl;
- cout << delicious.calory << endl;
- }
p259.4
- #include<iostream>
- #include<cstdlib>
- #include<cctype>
- using namespace std;
- struct stringy{
- char* str;
- int ct;
- };
- void set(stringy &, const char*);
- void show(const stringy &, int times = );
- void show(const char*, int times = );
- int main(){
- stringy beany;
- char testing[] = "reality isn't what it used to be.";
- set(beany, testing);
- show(beany);
- show(beany, );
- delete beany.str;
- testing[] = 'D';
- testing[] = 'u';
- show(testing);
- show(testing, );
- show("Done!");
- system("pause");
- return ;
- }
- void set(stringy &beany, const char* testing){
- int num = strlen(testing);
- beany.str = new char[num + ];
- strcpy(beany.str, testing);
- beany.ct = num;
- }
- void show(const stringy &beany, int times){
- for (int i = ; i < times; i++)
- cout << beany.str << endl
- << beany.ct << endl;
- }
- void show(const char* testing, int times){
- for (int i = ; i < times; i++)
- cout << testing << endl;
- }
p260.6
- #include<iostream>
- #include<cstdlib>
- #include<cstring>
- using namespace std;
- template<class T>
- T maxn(T ar[], int num);
- template<> char *maxn(char *pt[], int num);
- int main(){
- int num1[];
- double num2[];
- cout << "enter the member of two array\n";
- for (int i = ; i < ; i++)
- cin >> num1[i];
- for (int i = ; i < ; i++)
- cin >> num2[i];
- int imax = maxn(num1, );
- double dmax = maxn(num2, );
- cout << "imax is" << imax << endl
- << "dmax is" << dmax << endl;
- char *str[];
- str[] = "his eyes came round";
- str[] = "it all seems very unchanged";
- str[] = "which contain of old the pipes";
- str[] = "yes, billy, i know";
- str[] = "will you be please to dine";
- char *address = maxn(str, );
- cout << (int*)address << " " <<
- address << endl;
- system("pause");
- return ;
- }
- template<class T>
- T maxn(T ar[], int num){
- T max=ar[];
- for (int i = ; i < num; i++)
- if (max < ar[i])
- max = ar[i];
- return max;
- }
- template<> char *maxn(char *pt[], int num){
- int length[], i;
- for (i = ; i < ; i++)
- length[i] = strlen(pt[i]);
- int max = length[];
- for (i = ; i < ; i++)
- if (max < length[i])
- max = length[i];
- for (i = ; i < ; i++)
- if (max == length[i])
- break;
- return pt[i];
- }
c++ primer plus 习题答案(2)的更多相关文章
- c++ primer plus 习题答案(1)
c++ primer plus 习题答案用的是第五版,IDE仍然是vs2013.我只标注了题号,具体的题目找下书上对应内容吧. p110.8 #include<iostream> #inc ...
- c++ primer plus 习题答案(8)
p475.2 //头文件: class Cd{ private: char *performers; char *label; int selections; double playtime; pub ...
- c++ primer plus 习题答案(7)
p427.4 //头文件: #include<iostream> #ifndef STACK_H_ #define STACK_H_ typedef unsigned long Item; ...
- c++ primer plus 习题答案(6)
p425.1 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...
- c++ primer plus 习题答案(5)
p333.7 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...
- c++ primer plus 习题答案(4)
p333.3 #include<iostream> #include<cstdlib> #include<cstring> #include<string&g ...
- c++ primer plus 习题答案(3)
p296.3 #include<iostream> #include<cstdlib> #include<string> #include<cstring&g ...
- C++Primer第五版——习题答案目录
目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS ...
- 《C++Primer》第五版习题答案--第五章【学习笔记】
<C++Primer>第五版习题答案--第五章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/15 第五章:语句 ...
随机推荐
- [LeetCode][Python]Container With Most Water
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/container-with-most-water/ Given n non-neg ...
- ubuntu14.04 Markdown编辑器推荐之Remarkable
如今已经习惯了用Markdown编辑器写博文的习惯,那么ubuntu以下有什么好用的呢?搜索中发现了这个叫Remarkable的免费Markdown编辑器.为什么推荐这个呢?说说它的特点: 实时预览 ...
- 深入理解Linux网络技术内幕——中断与网络驱动程序
接收到帧时通知驱动程序 在网络环境中.设备(网卡)接收到一个数据帧时,须要通知驱动程序进行处理. 有一下几种通知机制: 轮询: 内核不断检查设备是否有话要说.(比較耗资源,但在一些情况 ...
- ASPxGridView-单元格合并
<dx:ASPxGridView ID="gridView" runat="server" ClientInstanceName="gvResu ...
- 保护眼睛,绿豆沙颜色的RGB值和HSL值
现在的人尤其是职场中人,每天都得花很长时间对着电脑,对眼睛的伤害很大,其实我们可以对电脑进行一个简单的设置,把窗口背景设置成绿豆沙颜色的,对眼睛的保护很有帮助的. 下面是绿豆沙颜色的RGB值和HSL值 ...
- ubuntu10.04 安装NVIDIA GT 420M驱动
安装ubuntu已经好几天了,由于显卡驱动没装,屏幕在600X800下的效果很难看,于是就想办法,查阅资料终于安装成功了,下面将我的安装方法记录下来以供大家参考. 借鉴:ubuntu12.04下安装N ...
- spring schema自定义
今天看了一下分布式服务框架的那本书,于是里面提到了spring schema的自定义,于是去简单的了解了一下 参考资源:spring schema扩展: http://www.yihaomen.com ...
- HelloWorld——Cocos2d-x学习历程(二)
HelloWorld分析: 1."resource"文件夹 该文件夹主要用于存放游戏中需要的图片.音频和配置等资源文件. 2."include"和"s ...
- nginx install lua module
#install luajit #http://luajit.org/download.html .tar.gz cd LuaJIT- make install PREFIX=/home/allen. ...
- Spring学习之常用注解(转)
使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base-package ...