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 第五章:语句 ...
随机推荐
- Unix/Linux环境C编程入门教程(9) unbntu CCPP开发环境搭建
1. 首先启动VMware,如果没有安装,请查看前面VMware的安装视频 2 启动虚拟机向导,选择自定义 3 单击下一步 4 选择稍后安装操作系统 5 .选择unbntu 64linux ...
- iOS6 旋转
iOS 6的rotation改变了很多.先来看看官方的描述 http://www.bgr.com/2012/08/06/ios-6-beta-4-change-log-now-available/ ...
- C,C++经典(程序、错误程序)
1,程序 未执行完错误的return 0
- brief InformationTechnology theory of evolution
信息技术进化论简述 无文化流氓帮帮主Ruiy Pk 清华土匪帮帮主YiC 1,按人机交互方式 命令行-->图形界面-->自然交互(语音+手控) Tips:命令行(IBM大型机,小型机+ S ...
- ios webview 加载含有中文的URL网页显示白屏
1. ios中的webview加载的URL不可以含有中文,解决办法说将中文字符转码, 如下: - (NSString *)URLEncodeString { NSCharacterSet *set = ...
- java 中解决中文乱码问题的方法(三法)
1. 重新定义. String str = "中文试试" ; str = new String(u.getBytes("iso-8859-1"),"u ...
- Oracle存储过程返回一张表数据
在oracle数据库中你要在程序里得到一张表的数据就必须先创建游标和SQL Service不一样. --创建游标create or replace package pkg_Dataas type re ...
- 设置从本地copy文件到远程计算机上
1.运行中输入mstsc.exe调出远程连接桌面,点击选项 2.在“本地资源”选项卡点击“详细信息” 3.勾选“智能卡”下的“驱动器” 4.设置好后,远程计算机就可以复制,粘贴了
- collectionView 中cell间距设置建议
应该是调节UICollectionViewFlowLayout的minimumInteritemSpacing属性,这个是调节同一行的cell之间的距离的. 使用-(CGFloat )collecti ...
- C - 字符识别?
C - 字符识别? Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Submit Sta ...