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的基本配置(修改主机名修改ip地址安装JDK/Tomcat/MySQL等等)
(一)基本操作修改 修改主机名 vi /etc/sysconfig/network NETWORKING=yes HOSTNAME=server1.itcast.cn 修改ip地址 vi /etc/s ...
- JDK版本更改,修改环境变量不生效解决办法
问题: 当使用安装版本JDK后,想要更改系统环境变量时,直接更改JAVA_HOME无效. 原因: 当使用安装版本的JDK程序时(一般是1.7版本以上),在安装结束后安装程序会自动将java.exe.j ...
- springboot整合Ehcache
首先引入maven包: <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...
- random模块(随机数库)
random random.random random.random()用于生成一个0到1的随机浮点数: 0 <= n < 1.0 random.uniform random.unifor ...
- Django——admin组件
Django提供了基于web的管理工具. Django自动管理工具是django.contrib的一部分.你可以在项目的settings.py中的INSTALLED_APPS看到它: # Applic ...
- CSS素材+特效
1.字体:https://www.zhihu.com/question/19680724 2.loading特效:http://www.cnblogs.com/lhb25/archive/2013/1 ...
- 乐思启慧教学系列—Bootstrap布局规则
1外层变化,内层相应变化规则 col-md-6 col-md-4 外层6变成12,扩大了2倍,里面就得缩小2倍(除以2), 只有这样才能保持外部变化了,内部依然对齐 col-md-12 col-md- ...
- Cookie用法简介
java操作Cookie---javax.servlet.http.Cookie 1.增加一个Cookie Cookie cookie = new Cookie("username" ...
- mongodb简单用法
修改器: $inc: 增加已有的键值,如果键值不存在就创建一个 数据库中存在这样的数据:{ , "url": "www.example.com", }db.fz ...
- jni 编译错误error: unknown type name '__va_list'
platforms\android-9\arch-arm\usr\include\stdio.h:257:37: error: unknown type name '__va_list' 解 ...