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中权限管理之sudo权限
1.suodo的操作对象是系统命令 2.root把本来只能是超级用户执行的命令赋予普通用户执行 3.设置sudo权限 命令:visudo 找到: ## Allow root to run any co ...
- boost之正确性和测试
BOOST_ASSERT在debug模式下有效. #include <iostream> #include <boost/assert.hpp> using namespace ...
- zabbix-2.4.8-1添加tcp状态监控
1.安装zabbix-agentyum -y install zabbix-2.4.8-1.el6.x86_64.rpm zabbix-agent-2.4.8-1.el6.x86_64.rpm2.编辑 ...
- Ip-san 配置过程
1:SAN的定义 SAN是storage area network(存储区域网络)的简写,早期的san采用的是光纤通道技术,后期当iscsi协议出现以后,为了区分两者,就划分了IP SAN和FC SA ...
- IP地址处理模块IPy
IP地址规划是网络设计中非常重要的一个环节,规划的好坏会直接影响路由协议算法的效率,包括网络性能.可扩展性等方面. 在这个过程中,免不了要计算大量的IP地址,包括网段.网络掩码.广播地址.子网数.IP ...
- 在C#中动态编译T4模板代码
转: http://www.wxzzz.com/1438.html 资料: https://cnsmartcodegenerator.codeplex.com/SourceControl/latest ...
- php异常处理类
<?php header('content-type:text/html;charset=UTF-8'); // 创建email异常处理类 class emailException extend ...
- CentOS7在VMWare12中安装后不能上网解决办法
首先要保证你的VMWare Workstation12 在安装号CentOS7后没改动什么关于网络相关的. 1.我的电脑一开始用的是VMWare WorkStations10,发现VMnet8根本不通 ...
- mysql利于cte进行分组统计并计算占比
CTE定义:一个公共表表达式(common table expression)是一个命名的临时结果集,它在一条单独的语句中有效,可以在语句中被引用多次. CTE基本语法: WITH cte1 [(co ...
- PMON使用手册
转:http://www.docin.com/p-1949877603.html