第四章 编程练习
2016-11-09 04:09
141 查看
编程练习4.1
编程练习4.2
编程练习4.3 & 4.4
编程练习4.4
编程练习4.6
编程练习4.8
编程练习9 (下面的程序在VS2012上不能正常结束,还请路过的大神指教)
编程练习10
#include <iostream> #include <string> int main() { using namespace std; string firstName; string lastName; char gradeDeserve; int age; cout << "what is your first name? "; getline(cin, firstName); cout << "what is your last name? "; getline(cin, lastName); cout << "What letter grade do you deserve? "; cin >> gradeDeserve; cout << "What is your age? "; cin >> age; cout << "Name: " << lastName << ", " << firstName <<endl; cout << "Grade: " << (char) (gradeDeserve +1) << endl; cout << "Age: " << age <<endl; system("pause"); return 0; }
编程练习4.2
#include <iostream> #include <string> using namespace std; int main() { string name; string dessert; cout << "Enter your name: "; getline(cin, name); cout << "Enter your favorite dessert: "; getline(cin, dessert); cout << "I have some delicious " << dessert << " for your, " << name << endl; system("pause"); return 0; }
编程练习4.3 & 4.4
#include <iostream> #include <string> int main() { using namespace std; char firstName[20]; char lastName[20]; string fullName; cout << "Enter your first name: "; cin.getline(firstName,20); cout << "Enter your last name: "; cin.getline(lastName,20); strcat(lastName,", "); fullName = strcat(lastName, firstName); cout << "Here's the information in a single string: " << fullName << endl; system("pause"); return 0; }
编程练习4.4
#include <iostream> #include <string> using namespace std; struct CandyBar { string brand; float weight; int calorie; }; int main() { CandyBar snack = {"Mocha Munch", 2.3, 350}; cout << "Candy " << snack.brand << "'s weight is " << snack.weight << endl; cout << "Candy " << snack.brand << " has " << snack.calorie << " calorie "<< endl; system("pause"); return 0; }
编程练习4.6
#include <iostream> #include <string> using namespace std; struct CandyBar { string brand; float weight; int calorie; }; int main() { CandyBar candy[3] = { {"Candy 1", 2.3, 350}, {"Candy 2", 3.2, 530}, {"Candy 3", 1.5, 250} }; cout << "Candy " << candy[0].brand << "'s weight is " << candy[0].weight << endl; cout << "Candy " << candy[0].brand << " has " << candy[0].calorie << " calorie "<< endl; cout << "Candy " << candy[1].brand << "'s weight is " << candy[1].weight << endl; cout << "Candy " << candy[1].brand << " has " << candy[1].calorie << " calorie "<< endl; cout << "Candy " << candy[2].brand << "'s weight is " << candy[2].weight << endl; cout << "Candy " << candy[2].brand << " has " << candy[2].calorie << " calorie "<< endl; // 可以用一个show函数来挨个显示每个candy的内容 system("pause"); return 0; }
编程练习4.8
#include <iostream> #include <string> using namespace std; struct Pizza { string brand; float size; float weight; }; int main() { Pizza *pizza = new Pizza; cout << "Please enter the brand of pizza: "; getline(cin, pizza->brand); cout << "Please enter the size of pizza: "; cin >> pizza->size; cout << "Please enter the weight of pizza: "; cin >> pizza->weight; cout << "Pizza's brand: " << pizza->brand << endl; cout << "Pizza's size: " << pizza->size << endl; cout << "Pizza's weight: " << pizza->weight << endl; system("pause"); return 0; }
编程练习9 (下面的程序在VS2012上不能正常结束,还请路过的大神指教)
#include <iostream> #include <string> using namespace std; struct CandyBar { string brand; double weight; int calorie; }; void show(const CandyBar *candy); void main() { CandyBar *candy=new CandyBar[3]; for(int i=0;i<3;i++,candy++) { candy->brand="String"+i; candy->weight=i; candy->calorie=i*10; show(candy); } delete []candy; } void show(const CandyBar *candy) { cout<<"name="<<candy->brand<<" ,weight="<<candy->weight<<" ,calorie="<<candy->calorie<<endl; }
编程练习10
#include <iostream> #include <array> using namespace std; int main() { array <float, 3> run; for (int i=0; i<3; i++) { cout << "Enter the runner " << i << "'s time: "; cin >> run[i]; } float sum = 0; for (int i=0; i<3; i++) { cout << "the runner " << i << "'s time: "; cout << run[i] << " s" << endl; sum = sum+run[i]; } cout << "average time is " << sum/3 << " s" << endl; system("pause"); return 0; }
相关文章推荐
- (六十四)第四章编程练习
- c和指针第四章编程练习代码
- 第四章编程练习
- Python核心编程 练习 第四章
- 第四章.编程练习
- 《c primer plus》编程练习回顾-第四章
- C Primer Plus(第6版)第四章编程练习答案+一些思考
- 模板元编程第四章练习(非答案)
- 书本第四章编程练习
- C++ Primer Plus 学习笔记 第四章 04编程练习
- java编程思想 第四章 练习4
- C++ Primer Plus(第六版)第四章编程练习参考答案
- C++ Primer Plus第六版 第四章 编程练习答案
- C Primer Plus 第四章 编程练习 1-7题
- c++ primer plus(第6版)中文版 第四章编程练习答案
- C和指针第四章编程练习
- C Primer Plus 6 第四章编程练习
- C++ Primer Plus第五版 第四章 编程练习答案
- C primer plus 第四章字符串和格式化输入输出 个人编程练习
- C primer plus(第五版)编程练习第四章