【C++】学习笔记十四——C++ primer plus第六版第4章编程练习
来源:互联网 发布:软件版本发布说明 编辑:程序博客网 时间:2024/06/10 16:55
- 1.
//4.13编程练习 1#include<iostream>#include<string>int main(){ using namespace std; enum grade {A, B, C, D}; cout << "What is your first name? "; string first_name; getline(cin, first_name); cout << "What is your last name? "; string last_name; cin >> last_name; cout << "What letter grade do you deserve? "; char grade; cin >> grade; cout << "What is your age? "; int age; cin >> age; cout << "Name: " << last_name << ", " << first_name << endl; cout << "Grade: " << char(grade + 1) << endl; cout << "Age: " << age; cin.get(); cin.get(); return 0;}
2.
#include<iostream>#include<string>int main(){ using namespace std; string name; string dessert; cout << "Enter your name:\n"; getline(cin,name); //读取一行 cout << "Enter your favorite dessert:\n"; getline(cin,dessert); cout << "I have some delicious " << dessert; cout << " for you, " << name << ".\n"; cin.get(); cin.get(); return 0;}
3.
#include<iostream>#include<cstring>int main(){ using namespace std; cout << "Enter your first name: "; char firstname[20]; cin >> firstname; cout << "Enter your last name: "; char lastname[20]; cin >> lastname; char name[40]; strcpy(name, lastname); strcat(name, ", "); strcat(name, firstname); cout << "Here's the information in a single string: " << name; cin.get(); cin.get(); return 0;}
4.
#include<iostream>#include<string>int main(){ using namespace std; cout << "Enter your first name: "; string firstname; cin >> firstname; cout << "Enter your last name: "; string lastname; cin >> lastname; string name = lastname + ", " + firstname; cout << "Here's the information in a single string: " << name; cin.get(); cin.get(); return 0;}
5.
#include<iostream>#include<string>using namespace std;struct CandyBar{ string brand; double weight; int calorie;};int main(){ CandyBar snack { "Mocha Munch", 2.3, 350 }; cout << "snack: " << endl; cout << "brand: " << snack.brand << endl; cout << "weight: " << snack.weight << endl; cout << "calorie: " << snack.calorie << endl; cin.get(); return 0;}
6.
#include<iostream>#include<string>using namespace std;struct CandyBar{ string brand; double weight; int calorie;};int main(){ CandyBar snack[3] { {"Mocha Munch", 2.3, 350}, {"Dabaitu", 5.6, 520}, {"Wowo", 3.6, 450} }; int i; for (i = 0; i < 3; i++) { cout << "Candy " << i << endl; cout << "\tbrand: " << snack[i].brand << endl; cout << "\tweight: " << snack[i].weight << endl; cout << "\tcalorie: " << snack[i].calorie << endl; } cin.get(); return 0;}
7.
#include<iostream>#include<string>using namespace std;struct William{ string company_name; double pizza_diameter; double pizza_weight;};int main(){ William pizza; cout << "Enter the name of the company: "; getline(cin, pizza.company_name); cout << "Enter the diameter of the pizza: "; cin >> pizza.pizza_diameter; cout << "Enter the weight of the pizza:"; cin >> pizza.pizza_weight; cout << "pizza: "; cout << "\tCompany: " << pizza.company_name << endl << "\tdiameter: " << pizza.pizza_diameter << endl << "\tweight: " << pizza.pizza_weight << endl; cin.get(); cin.get(); return 0;}
8.
#include<iostream>#include<string>using namespace std;struct William{ string company_name; double pizza_diameter; double pizza_weight;};int main(){ William * pizza = new William; cout << "Enter the diameter of the pizza: "; cin >> pizza->pizza_diameter; cout << "Enter the name of the company: "; cin.get(); //输入直径后,输入队列中还有换行符 getline(cin, pizza->company_name); cout << "Enter the weight of the pizza:"; cin >> pizza->pizza_weight; cout << "pizza: "; cout << "\tCompany: " << pizza->company_name << endl << "\tdiameter: " << pizza->pizza_diameter << endl << "\tweight: " << pizza->pizza_weight << endl; delete pizza; cin.get(); cin.get(); return 0;}
9.
#include<iostream>#include<string>using namespace std;struct CandyBar{ string brand; double weight; int calorie;};int main(){ CandyBar * snack = new CandyBar[3]; snack[0] = { "Mocha Munch", 2.3, 350 }; snack[1] = { "Dabaitu", 5.6, 520 }; snack[2] = { "Wowo", 3.6, 450 }; int i; for (i = 0; i < 3; i++) { cout << "Candy " << i << endl; cout << "\tbrand: " << snack[i].brand << endl; cout << "\tweight: " << snack[i].weight << endl; cout << "\tcalorie: " << snack[i].calorie << endl; } delete [] snack; //不要忘记delete,不要忘记[] cin.get(); return 0;}
10.
#include<iostream>#include<array>int main(){ using namespace std; array<double, 3> grade; cout << "Enter your first grade: "; cin >> grade[0]; cout << "Enter your second grade: "; cin >> grade[1]; cout << "Enter your third grade: "; cin >> grade[2]; double grade_avr = (grade[0] + grade[1] + grade[2]) / 3; cout << "The average grade of your 3 times grades is " << grade_avr; cin.get(); cin.get(); return 0;}
0 0
- 【C++】学习笔记十四——C++ primer plus第六版第4章编程练习
- C.Primer.Plus(第六版)第10章 编程练习
- C.Primer.Plus(第六版)第11章 编程练习
- C.Primer.Plus(第六版)第12章 编程练习
- C.Primer.Plus(第六版)第13章 编程练习
- C.Primer.Plus(第六版)第14章 编程练习
- C.Primer.Plus(第六版)第15章 编程练习
- C.Primer.Plus(第六版)第16章 编程练习
- C.Primer.Plus(第六版)第17章 编程练习
- C.Primer.Plus(第六版)第六章编程练习
- C ++ Primer Plus 第六版 第六章编程练习答案
- C++primer plus第六章编程练习
- 【学习C++】C++ Primer Plus (第六版)第九章编程练习1-4
- 【学习C++】C++ Primer Plus (第六版)第十三章编程练习1-4
- C primer plus(第五版)编程练习第六章
- c++.primer.plus第五版第六章编程练习答案
- C.Primer.Plus(第六版)第五章编程练习
- C.Primer.Plus(第六版)第七章 编程练习
- 正则表达式规则
- Laravel 分割 routes.php 路由文件的最佳方式
- jdbc 获取 自增主键值
- iWebShop如何替换短信接口
- 《中国民间传说》
- 【C++】学习笔记十四——C++ primer plus第六版第4章编程练习
- 万维网发布服务(w3svc)以停止。除非万维网发布服务(w3svc)
- github(1)
- C语言/C++文件的读取与写入操作
- OpenFileDialog 类型名和文件名冲突,类无法使用
- 如何用Python写spark
- 【未解决】安卓版的游戏崩溃了,在bugly上看不到详细堆栈
- 轻松自动化---selenium-webdriver(python) (五)---层级定位
- maven 开发环境 、测试环境、生产环境打包