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 ...
随机推荐
- Could not create ServerSocket on address 0.0.0.0/0.0.0.0:9083
遇到这种情况大家都找不到头绪,是因为你开始运行了hive的metastore,可以输入jps 然后出现如下: 红线所示就是hive metastore的进程 为了重新启动,需要把这个进杀掉: kill ...
- 家里网速北京联通100m光纤
http://www.speedtest.net/ http://cs1.bbn.com.cn:8800/gzweb/index.jsp
- BAPI_ACC_DOCUMENT_POST 解决原因代码输入问题-利用BADI
(1) 复制函数SAMPLE_INTERFACE_RWBAPI01为Z SAMPLE_INTERFACE_RWBAPI01 *"---------------------------- ...
- Python基础-常用的内置函数
内置函数filter str = ['a', 'b', 'c', 'd'] def fansik(num): if num != "a": return num ret = fil ...
- Dockerfile学习(一)
FROM指令: 格式为:FROM<image>:<tag>或者FROM<image> Dockerfile的第一条指令必须是FROM,用来指定要制作的镜像继承自哪个 ...
- 方阵行列式并行化计算(OpenMP,MPI),并计算加速比
00][100].在创建方阵时,方阵的阶数N(N<100)由外部输入.然后用两层"for循环"来给方阵 p左上角 N×N个位置赋值.具体实现如下: /* * 定义矩阵阶数N ...
- Spring boot cassandra - nested exception is com.datastax.driver.core.exceptions.NoHostAvailableException
1.在Pom.xml添加spring-boot-starter-data-cassandra依赖: <dependency> <groupId>org.springframew ...
- HackerRank - lonely-integer 【水】
题意 给出一系列数字,输出那个出现次数为奇数次的数字 思路 用MAP标记一下,在输入的时候判断一下 之前有没有输入过,如果有,就抹掉 最后剩下的那个 就是出现次数为奇数的 或者可以用 位运算 AC代码 ...
- javascript;Jquery;获取JSON对象,无刷新分页,异步加载,异步删除,实例。
AjaxNewsList: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> < ...
- Objective-C系列总结之基础知识
//第一个程序示例 #import <Foundation/Foundation.h> int main(int argc,const char * argv[]) { @autorele ...