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 第五章:语句 ...
随机推荐
- DKNY_百度百科
DKNY_百度百科 DKNY
- hdu 4598 Difference(奇圈判定+差分约束)
这是通化邀请赛的题,当时比赛的时候还完全没想法呢,看来这几个月的训练还是有效果的... 题意要求(1) |ai| < T for all i (2) (vi, vj) in E <=& ...
- 关于cocos2d安装时编译不成功(个人心得)
在解压cocos2d执行vs2010.sln时错误发生不能成功生成.遇到这样的错误: 1>c:\program files\microsoft sdks\windows\v7.0a\includ ...
- 用ADB(Android Debug Bridge)实时监测Android程序的运行
监控Android设备上程序的运行,需要ADB的配合,具体ADB工具的介绍以及命令选项可见博客: http://blog.csdn.net/mliubing2532/article/details ...
- 初学JSoup
jsoup 是一款 Java 的 HTML 解析器,可直接解析某个 URL 地址.HTML 文本内容.它提供了一套非常省力的 API,可通过 DOM,CSS 以及类似于 jQuery 的操作方法来取出 ...
- ASPから広がり
ASP是动态服务器页面(Active Server Page)外语缩写.[1]是微软公司开发的代替CGI脚本程序的一种应用,它可以与数据库和其它程序进行交互,是一种简单.方便的编程工具.ASP的网页文 ...
- 《JavaScript+DOM编程艺术》的摘要(四)appendChild与insertBefore的区别
基本知识点: // 1.js里面为什么要添加window.onload=function (){} // 保证html文档都加载完了,才开始运行js代码,以防html文档没有加载完,找不到相应的元素 ...
- jQuery实现导航监听事件
导航html如下 <div class="main_nav"> <a class="nav_01 active_01" href=" ...
- iOS开发的准备
一.程序设计语言 上一讲已经说到:要想开发一款软件,首先得学习一些相应的程序设计语言.至于iOS开发,需要学习的语言主要有:C.C++.Objective-C. 二.是否需要计算机专业知识 可能很多人 ...
- html字符实体对照表