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 第五章:语句 ...
随机推荐
- 未能从文本"Template"创建 "System.Windows.DependencyProperty"
XXXXProperty = DependencyProperty.Register("XXXX", typeof(double), typeof(MyClass), new Pr ...
- Oracle - index (索引)
索引: 一种独立于表的模式对象, 可以存储在与表不同的磁盘或表空间中 @ 索引被删除或损坏, 不会对表产生影响, 其影响的只是查询的速度 @ 索引一旦建立, Oracle 管理系统会对其进行自 ...
- SDOTOJ2088 refresh的停车场(栈和队列)
refresh的停车场 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit S ...
- 1.对于.NET的初步理解和介绍
好久没写博客了,最近心情比较low,不知道为什么.很流行的一个问题叫做:如果你明天就挂了,那么你最后悔的事情将会是什么.我想了两个月,答案是不知道,无所谓.这样不好,那这个问题先放一边吧,我们开始这一 ...
- Javascript基础篇小结
转载请声明出处 博客原文 随手翻阅以前的学习笔记,顺便整理一下放在这里,方便自己复习,也希望你有也有帮助吧 第一课时 入门基础 知识点: 操作系统就是个应用程序 只要是应用程序都要占用物理内存 浏览器 ...
- <转>ASP.NET学习笔记之MVC 3 数据验证 Model Validation 详解
MVC 3 数据验证 Model Validation 详解 再附加一些比较好的验证详解:(以下均为引用) 1.asp.net mvc3 的数据验证(一) - zhangkai2237 - 博客园 ...
- 基于Nodejs开发的web即时聊天工具
由于公司需要开发web即时聊天的功能,开始时我们主要的实施方法是用jquery的ajax定时(10秒)轮询向服务器请求,由于是轮询请求,对 服务器的压力比较大.我们网站上线的时间不长,访问量不是很大, ...
- C#Base64加密
using System;using System.Collections.Generic;using System.Text;using System.Security.Cryptography;u ...
- apk混淆打包注意事项
混淆打包搞了好几天才初步了解,其中碰到很多Debug正常,Release的apk不能用,基本都是第三方的jar的问题,所以要排除混淆. 1. Json解析对象出错 用到fastJson或者GJson的 ...
- php mysql 数据库写入与读取取文件
近期的项目由于特殊原因,需要将文件存到数据库中.今天特地测试,首先在php网站上传文件,将文件读取出来——再存入到MySQL数据库中. 一.首先创建php 代码如下(网上找了段代码进行过修改):源代码 ...