c++primer 第四章编程练习答案
4.13.1
#include<iostream>
struct students {
char firstname[];
char lastname[];
char grade;
int age;
};
int main() {
using namespace std;
students student1;
cout << "What is your fistname? ";
cin.get(student1.firstname, ).get();
cout << "What is your lastname? ";
cin.getline(student1.lastname, );
cout << "What latter grade do you deserve? ";
cin >> student1.grade;
cout << "What is your age? ";
cin >> student1.age;
cin.get();
cout << endl;
cout << "name: " << student1.firstname << " , " << student1.lastname << endl;
cout << "grade: " << char (student1.grade + ) << endl;
cout << "age: " << student1.age;
cin.get();
}
4.13.2
#include<iostream>
struct students {
char firstname[];
char lastname[];
char grade;
int age;
};
int main() {
using namespace std;
students student1;
cout << "What is your fistname? ";
cin.get(student1.firstname, ).get();
cout << "What is your lastname? ";
cin.getline(student1.lastname, );
cout << "What latter grade do you deserve? ";
cin >> student1.grade;
cout << "What is your age? ";
cin >> student1.age;
cin.get();
cout << endl;
cout << "name: " << student1.firstname << " , " << student1.lastname << endl;
cout << "grade: " << char (student1.grade + ) << endl;
cout << "age: " << student1.age;
cin.get();
}
4.13.3
#include <iostream>
#include <cstring> const int Strlen = ; int main()
{
using namespace std; char first_name[Strlen];
char last_name[Strlen];
char full_name[ * Strlen]; cout << "Enter your first name: ";
cin.get(first_name, Strlen).get();
cout << "Enter your last name: ";
cin.get(last_name, Strlen).get(); strcpy_s(full_name, last_name);//strcpy经常发生缓存区溢出,被视为是不安全的
strcat_s(full_name, ", ");
strcat_s(full_name, first_name); cout << "Here's the information in a single string: " << full_name;
cin.get();
}
4.13.4
#include<iostream>
#include<string> int main() {
using namespace std;
string firstname;
string lastname, fullname;
cout << "Enter your first name: ";
getline(cin, firstname);
cout << "Enter your last name: ";
cin >> lastname;
cin.get();
fullname = firstname + ',' + lastname;
cout << "Here's the information in a single string: " << fullname;
cin.get();
}
4.13.5
#include<iostream>
#include<string> struct CandyBar
{
std::string brand;
double weight;
int calories;
};
S
int main() {
using namespace std;
CandyBar snack = {
"Mocha Munch",
2.3, };
cout << snack.brand << endl << snack.weight << endl << snack.calories;
cin.get();
}
4.13.6
#include<iostream>
#include<string> struct CandyBar
{
std::string brand;
double weight;
int calories;
}; int main() {
using namespace std;
int i;
CandyBar candy[]{
{"haha",12.3,},
{"jude",11.2,},
{"zc",25.2,}
};
for (i = ; i < ; i++) {
cout << "candybar " << i << endl
<< candy[i].brand << endl
<< candy[i].weight << endl
<< candy[i].calories << endl;
}
cin.get();
}
4.13.7
#include<iostream>
#include<string> struct PizzaBar
{
std::string name;
float diameter;
float weight;
};
int main() {
using namespace std;
PizzaBar pizza;
cout << "Please Enter your pizza name: ";
cin >> pizza.name;
cout << "Please Enter your pizza diameter: ";
cin >> pizza.diameter;
cout << "Please Enter your pizza weight: ";
cin >> pizza.weight;
cin.get();
cout << "your pizza well be ordered ,please confirm!" << endl;
cout << pizza.name << endl << pizza.diameter << endl << pizza.weight;
cin.get();
}
4.13.8
#include <iostream>
#include<string> int main() {
using namespace std;
char *name = new char[];
double *diameter = new double;
double *weight = new double;
cout << "pizza name: ";
cin >> name;
cout << "pizza weight: ";
cin >> *weight;
cout << "pizza diameter: ";
cin >> *diameter;
cin.get();
cout << "name: " << name << endl;
cout << "weight:" << *weight << endl;
cout << "diameter: " << *diameter << endl;
cin.get();
}
4.13.9
#include<iostream>
#include<string> using namespace std;
struct CandyBar
{
string brand;
double weight;
int calories;
}; int main() {
CandyBar *candy = new CandyBar[];
int i;
candy->brand = "dad";
candy->weight = 12.16;
candy->calories = ;
(candy + )->brand = "wqe";
(candy + )->weight = 45.6;
(candy + )->calories = ;
(candy + )->brand = "zxc";
(candy + )->weight = 78.9;
(candy + )->calories = ;
for (i = ; i < ; i++) {
cout << "display " << i+ << " CandyBar\n";
cout << "brand: " << candy[i].brand << endl;
cout << "weight: " << candy[i].weight << endl;
cout << "calories: " << candy[i].calories << endl;
cout << endl;
}
delete [] candy;
cin.get();
}
4.13.10
#include<iostream>
#include<array> int main() {
using namespace std;
array<double, > score;
double sum = ;
int i;
for (i = ; i < ; i++) {
cout << "input " << i+ << " score: ";
cin >> score[i];
sum += score[i];
cout << "display " << i + << " average score is " << sum / (i + ) << endl;
cin.get();
}
cin.get();
}
c++primer 第四章编程练习答案的更多相关文章
- c++primer 第五章编程练习答案
5.9.1 #include<iostream> int main() { using namespace std; ; cout << "input first i ...
- c++primer 第三章编程练习答案
3.7.1 #include<iostream> int main() { using namespace std; ; int height,inch,foot; cout <&l ...
- c++ primer plus 第四章 课后题答案
#include<iostream> #include<string> using namespace std; int main() { string first_name; ...
- c++primer 第l六章编程练习答案
6.11.1 #include<iostream> #include<cctype> int main() { using namespace std; char ch; ci ...
- c++primer 第二章编程练习答案
2.7.1 #include<iostream> int main() { using namespace std; ]; ]; cout << "input nam ...
- <<C++ Primer>> 第四章 表达式
术语表 第 4 章 表达式 算术转换(arithmetic conversion): 从一种算术类型转换成另一种算术类型.在二元运算符的上下文中,为了保留精度,算术转换通常把较小的类型转换成较大的类型 ...
- C++Primer 第四章
//1.当我们对运算符进行重载的时候,其包括运算对象的类型和返回值的类型都是由该运算符定义的,但是运算对象的个数和优先级,结合律都是不能改变的 //2.当一个对象被用作右值的时候,用的是对象的值(内容 ...
- c++ primer plus 第二章 课后题答案
#include<iostream> using namespace std; int main() { cout << "My name is Jiantong C ...
- java第四章编程题(初学篇)
代码: /* test.java */ package test; public class test { public static void main(String args[] ) { CPU ...
随机推荐
- Linux中的特殊字符
单引号: 在单引号中所有的特殊字符都没有特殊含义 双引号: 在双引号中 $ ` \ 三个字符表示,调用变量的值.引用命令.转义,其他特殊字符均没有特殊含义 反引号: 用反引号括起来的内容被当作系统命令 ...
- hbase中清空整张表的数据
hbase(main):005:0> truncate 'fr:test' Truncating 'FaceBase' table (it may take a while): - Disabl ...
- tfboys——tensorflow模块学习(二)
tf.contrib模块 tf.contrib 模块是一个比较复杂的模块. contrib细节: tf.contrib.bayesflow.entropy 香农信息论 tf.contrib.baye ...
- adb通过TCP/IP连接提示 unable to connect to *, Connection refused的解决方法
通过串口连接板子进入命令行,然后执行: su setprop service.adb.tcp.port 5555 stop adbd start adbd
- FTP主动连接与被动连接
FTP(File Transfer Protocol, FTP)是TCP/IP网络上两台计算机传送文件的协议,应用层的协议,它基于传输层, FTP是一个8位的客户端-服务器协议,能操作任何类型的文件而 ...
- LDA主题模型三连击-入门/理论/代码
目录 概况 为什么需要 LDA是什么 LDA的应用 gensim应用 数学原理 预备知识 抽取模型 样本生成 代码编写 本文将从三个方面介绍LDA主题模型--整体概况.数学推导.动手实现. 关于LDA ...
- 02 Spring框架 简单配置和三种bean的创建方式
整理了一下之前学习Spring框架时候的一点笔记.如有错误欢迎指正,不喜勿喷. 上一节学习了如何搭建SpringIOC的环境,下一步我们就来讨论一下如何利用ioc来管理对象和维护对象关系. <? ...
- HAProxy详解
HAProxy概述与配置 一.HAProxy概述 HAProxy是由 WillyTarreau开发的一款具备高可用性.负载均及基于 TCP和 HTTP的应用代理开源软件,基于HAProxy的负载均衡架 ...
- 前端之 Ajax(补)
概述 对于WEB应用程序:用户浏览器发送请求,服务器接收并处理请求,然后返回结果,往往返回就是字符串(HTML),浏览器将字符串(HTML)渲染并显示浏览器上. 1.传统的Web应用 一个简单操作需要 ...
- smarty变量调节器与函数
smarty自带了一些变量调节器与内置函数,都在libs/plugins目录下,变量调节器以modifier开头,函数以function开头,而且我们可以自定义变量调节器与函数,熟练运用之后会极大地提 ...