c++ primer plus(第6版)中文版 第四章编程练习答案
来源:互联网 发布:c语言n位水仙花数程序 编辑:程序博客网 时间:2024/05/16 06:52
第四章编程练习答案
4.1接收姓名,成绩等级和年龄,输出时成绩等级下调
//4.1接收姓名,成绩等级和年龄,输出时成绩等级下调#include <iostream>#include <string>using namespace std;int main () { string Firstname,Lastname;cout << "What is your first name? ";getline(cin, Firstname);cout << "What is your last name? ";cin >> Lastname;charchgrade;cout << "What letter grade do you deserve? ";cin >> chgrade;unsignedint Age;cout << "What is your age? ";cin >> Age;cout << "Name: " << Lastname << " " << Firstname << endl;cout << "Grade: " << char(chgrade + 1) << endl;cout << "Age: " << Age << endl;}
4.2用c++ string类改写程序清单4.4
//4.2用c++ string类改写程序清单4.4#include <iostream>#include <string>using namespace std;int main () { string Name;cout << "Enter your name: ";cin >> Name;string Dessert;cout << "Enter your favorite dessert: ";cin >> Dessert;cout << "I have some delicious " << Dessert;cout << " for you, " << Name << endl;}
4.3输入名和姓,输出“名, 姓”(用cstring和char数组)
//4.3输入名和姓,输出“名, 姓”(用cstring和char数组)#include <iostream>#include <cstring>using namespace std;int main () { charfirstname[20],lastname[20],name[40];cout << "Enter your first name: ";cin.getline(firstname,20);cout << "Enter your last name: ";cin.getline(lastname, 20);strcpy(name, lastname);strcat(name, ", ");strcat(name, firstname);cout << "Here’s the information in a single string: " << name << endl;}
4.4输入名和姓,输出“名, 姓”(用string类和函数)
//4.4输入名和姓,输出“名, 姓”(用string类和函数)#include <iostream>#include <string>using namespace std;int main () { string firstname,lastname,name;cout << "Enter your first name: ";cin >> firstname;cout << "Enter your last name: ";cin >> lastname;name += firstname + ',' + lastname;cout << "Here’s the information in a single string: " << name << endl;}
4.5建立结构体CandyBar,包括品牌,重量,卡路里3个成员,初始化并输出
//4.5建立结构体CandyBar,包括品牌,重量,卡路里3个成员,初始化并输出#include <iostream>#include <string>using namespace std;struct CandyBar{string name;doubleweight;doublecalories;};int main () { CandyBarsnack = {"Mocha Munch", 2.3, 350};cout << snack.name << "\n" << snack.weight << "\n" << snack.calories << endl; }
4.6结构体同题4.5,初始化为3个元素并输出
//4.6结构体同题4.5,初始化为3个元素并输出#include <iostream>#include <string>using namespace std;struct CandyBar{string name;doubleweight;doublecalories;};int main (void) { CandyBarsnack[] ={{"A", 1, 10},{"B", 2, 20},{"C", 3, 30}};cout << snack[0].name << "\t" << snack[0].weight << "\t" << snack[0].calories << endl; cout << snack[1].name << "\t" << snack[1].weight << "\t" << snack[1].calories << endl; cout << snack[2].name << "\t" << snack[2].weight << "\t" << snack[2].calories << endl; }
4.7Pizza有三个记录值(公司名称,直径,重量),主函数可输入可输出
//4.7Pizza有三个记录值(公司名称,直径,重量),主函数可输入可输出#include <iostream>#include <string>using namespace std;struct Pizza{string company_name;doublediameter;doubleweight;};int main () { Pizzapizza;cout << "输入披萨名:";getline(cin, pizza.company_name);cout << "输入披萨直径(厘米):";cin >> pizza.diameter;cout << "输入披萨重量(克):";cin >> pizza.weight;cout << pizza.company_name << "公司出品直径为" << pizza.diameter << "厘米重量为" << pizza.weight << "克的披萨" << endl;}
4.8对4.7的结构体用new来分配内存
//4.8对4.7的结构体用new来分配内存#include <iostream>#include <string>using namespace std;structPizza{stringcompany_name;doublediameter;doubleweight;};int main () { Pizza*pizza = new Pizza;cout << "输入披萨直径(厘米):";cin >> pizza->diameter;// 回车符仍保留在输入流中cin.get(); //取先前的回车符cout << "输入披萨名:";getline(cin, pizza->company_name);cout << "输入披萨重量(克):";cin >> pizza->weight;cout << pizza->company_name << "公司出品直径为" << pizza->diameter << "厘米重量为" << pizza->weight << "克的披萨" << endl;delete pizza;pizza = NULL;}
4.9对4.6使用new分配内存
//4.9对4.6使用new分配内存#include <iostream>#include <string>using namespace std;struct CandyBar{stringname;doubleweight;doublecalories;};int main () { CandyBar* candybar_list = new CandyBar [3];candybar_list[0].name = "A";candybar_list[0].weight = 1;candybar_list[0].calories = 10;candybar_list[1].name = "B";candybar_list[1].weight = 2;candybar_list[1].calories = 20;candybar_list[2].name = "C";candybar_list[2].weight = 3;candybar_list[2].calories = 20;cout << candybar_list[0].name << "\t" << candybar_list[0].weight << "\t" << candybar_list[0].calories << endl; cout << candybar_list[1].name << "\t" << candybar_list[1].weight << "\t" << candybar_list[1].calories << endl; cout << candybar_list[2].name << "\t" << candybar_list[2].weight << "\t" << candybar_list[2].calories << endl; delete [] candybar_list;}
4.10请用户输入3次成绩,显示次数和平均成绩
//4.10请用户输入3次成绩,显示次数和平均成绩(array为c++11新加标准,vector可通用)#include <iostream>//#include <vector>#include <array>using namespace std;int main () {//vector<double> dash(3);array<double,3> dash; for(int i=0;i<3;i++) { cout<<"Enter the "<<i+1<<" time :"; cin>>dash[i]; } cout << "共计输入3次短跑成绩,平均分为" << (dash[0] + dash[1] + dash[2]) / 3 << endl;}
0 0
- c++ primer plus(第6版)中文版 第四章编程练习答案
- c++ primer plus(第6版)中文版 第二章编程练习答案
- c++ primer plus(第6版)中文版 第三章编程练习答案
- c++ primer plus(第6版)中文版 第五章编程练习答案
- c++ primer plus(第6版)中文版 第六章编程练习答案
- c++ primer plus(第6版)中文版 第七章编程练习答案
- c++ primer plus(第6版)中文版 第八章编程练习答案
- c++ primer plus(第6版)中文版 第九章编程练习答案
- c++ primer plus(第6版)中文版 第十章编程练习答案
- c++ primer plus(第6版)中文版 第十一章编程练习答案
- c++ primer plus(第6版)中文版 第十二章编程练习答案
- c++ primer plus(第6版)中文版 第十三章编程练习答案
- C++Primer Plus第6版 4.13编程练习答案
- 《C Primer Plus(第5版)中文版》第6章编程练习第1题
- 《C Primer Plus(第5版)中文版》第6章编程练习第14题
- 《C Primer Plus(第5版)中文版》第6章编程练习第15题
- 《C Primer Plus(第5版)中文版》第6章编程练习第16题
- 《C Primer Plus(第5版)中文版》第7章编程练习第6题
- Ring3下实现进程保护,不用hook
- Android Google Map API 开发基础知识
- dd命令和cp命令的区别
- python 添加路由
- sec7113:css 因Mine 类型不匹配而被忽略
- c++ primer plus(第6版)中文版 第四章编程练习答案
- 【C/C++】Linux下使用system()函数一定要谨慎
- template模版引擎的使用(underscore.js)
- CFormView
- 【SkinUI实例】仿QQ界面设计第二十八课
- C++ 容器的使用
- c++开方
- Makefile中override 指示符
- 微信爱帮公交查询之公交线路查询