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 ...
随机推荐
- HDFS权限管理指南(HDFS Permissions Guide)
综述 HDFS实现了一个类似POSIX模型的文件和文件夹权限管理模型.每一个文件盒文件夹都有一个所有者和一个组.文件或者文件夹可以通过权限区分是所有者还是组成员或是其他用户.对文件来说,r标示可以阅读 ...
- gitattributes手册
gitattributes(5) Manual Page 1.gitattributes是什么? gitattributes用于定义每个路径的属性. 其语法是:pattern attr1 attr2 ...
- grep命令详细解析 --非原创 原作者ggjucheng
简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...
- OS X 与传统Unix的一点区别
在传统的Unix系统或者Linux系统中,你是很难在根目录下找到大写开头的文件夹的, 但是看一下OS X: ls / Applications Users etc private var Develo ...
- Spring session共享(使用redis)
SpringBoot+Redis实现HttpSession共享 前提:需要使用redis做session存储 一.效果演练(这里使用SpringBoot工程,Spring同理) 1.一个工程使用两个端 ...
- 树莓派使用DHT11温湿度传感器(C语言)
硬件: 树莓派 2.0 DHT模块 接树莓派5V GND GPIO1 功能:读取传感器数据并打印出来 // //mydht11.c // #include <wiringPi.h> #i ...
- nodejs异步调用async
犹豫nodejs是异步编程模型,有一些在同步编程中很容易做到的事情,现在却变得很麻烦,async的流程控制就是为了简化这些操作var async = require('async'); 1.serie ...
- 对vector,list的操作函数
向量只能接受同一类型的数据:list可以接受不同的数据. 1.添加元素 vector:> b=c(1,2,3) > b=c(b,"four") #直接在后面添加添加 & ...
- list— 把数组中的值赋给一组变量
(PHP 4, PHP 5, PHP 7) list — 把数组中的值赋给一组变量 array list ( mixed $var1 [, mixed $... ] ) 像 array() 一样,这不 ...
- Kubernetes Resource Qoutas
配置参数: spec.containers[].resources.limits.cpu spec.containers[].resources.limits.memory spec.containe ...