C++primer plus 第四章程式設計練習
阿新 • • 發佈:2019-01-13
本人用code::block 編寫,如需參考,善用Ctrl+shift+C 和 Ctrl + shift + X 快捷鍵
如有任何錯誤或疑問,歡迎留言
//4.1 //#include <iostream> //#include <vector> //#include <string> //int main() //{ // using namespace std; // char firstname[20]; // char lastname[20]; // int age = 0; // char Rank = 0; // cout << "What is your first name? "; // cin.getline(firstname, 19); // cout << "What is your last name? "; // cin.get(lastname, 20); // cout << "What letter grade dou you deserve? "; // cin >> Rank; // cout << "What is your age? "; // cin >> age; // cout << "Name: " << firstname << ", " << lastname << endl; // cout << "Grade: " << char(Rank + 1) << endl; // cout << "Age: " << age << endl; // return 0; //} //4.2 //#include <iostream> //#include <string> // //int main() //{ // using namespace std; // const int ArSize(20); // string name; // string dessert; // // cout << "Enter your name:\n"; // getline(cin, name); // new usage; // cout << "Enter your favorite dessert:\n" ; // getline(cin, dessert); // cout << "I have some delicious " << dessert; // cout << " for you, " << name << endl; // return 0; //} //4.3 //#include <iostream> //#include <cstring> //int main() //{ // using namespace std; // char firstname[20]; // char lastname[20]; // char name[40]; // cout << "Enter your first name: "; // cin.getline(firstname, 20); // cout << "Enter your last name: "; // cin.getline(lastname, 20); // strncpy(name,firstname,39); // strcat(name, ", "); // strcat(name, lastname); // // cout << "Here's the information in a single string: " << name; // // return 0; //} //4.4 //#include <iostream> //#include <string> //int main() //{ // using namespace std; // string firstname; // string lastname; // // string name; // cout << "Enter your first name: "; // getline(cin, firstname); // cout << "Enter your last name: "; // getline(cin, lastname); // name = firstname + ", " + lastname; // // // cout << "Here's the information in a single string: " << name; // // return 0; //} //4.5 //#include <iostream> //#include <string> //using namespace std; //struct CandyBar //{ // string brand; // double weight; // int calories; //}; //int main() //{ // // CandyBar snack = {"Mocha Munch", 2.3, 50}; // // cout << "Here's a kind of snack, its information as follow:\nbrand: " // << snack.brand << endl; // cout << "weights: " << snack.weight << endl; // cout << "calories: " << snack.calories << endl; // // return 0; //} //4.6 //#include <iostream> //#include <string> //using namespace std; //struct CandyBar //{ // string brand; // double weight; // int calories; //}; //int main() //{ // // CandyBar snacks[3]; // int i = 0; // for (; i<3; i++) // { // cout << "Enter the information of the snacks " << i+1 << endl; // getline(cin, (snacks+i)->brand); // // (cin >> (snacks+i)->weight >> (snacks+i)->calories).get(); // } // cout << endl << "Here is the information of the three snacks: " << endl << endl; // // for (i=0; i<3; i++) // { // cout << "snacks " << i+1 << ": "; // cout << "brand: "<< snacks[i].brand << endl; // cout << "weights: " << snacks[i].weight << endl; // cout << "calories: " << snacks[i].calories << endl; // } // return 0; //} //4.7 //#include <iostream> //#include <string> //using namespace std; //struct Pizza //{ // string company; // double diameter; // double weight; //}; //int main() //{ // Pizza lili; // cout << "Enter yout pizzza's name, diameter, weight:" << endl; // getline(cin, lili.company); // cin >> lili.diameter >> lili.weight; // cout << "Thie is your pizza's information: " << endl; // cout << "name: " << lili.company << endl; // cout << "diameter: " << lili.diameter << endl; // cout << "weight: " << lili.weight << endl; // return 0; //} //4.8 //#include <iostream> //#include <string> //using namespace std; //struct Pizza //{ // string company; // double diameter; // double weight; //}; //int main() //{ // Pizza* lili = new Pizza; // cout << "Enter yout pizzza's diameter, weight, name:" << endl; // (cin >> lili->diameter >> lili->weight).get(); // getline(cin, lili->company); // cout << "Thie is your pizza's information: " << endl; // cout << "name: " << lili->company << endl; // cout << "diameter: " << lili->diameter << endl; // cout << "weight: " << lili->weight << endl; // return 0; //} //4.9 //#include <iostream> //#include <string> //using namespace std; //struct CandyBar //{ // string brand; // double weight; // int calories; //}; //int main() //{ // // CandyBar* snacks = new CandyBar[3]; // int i = 0; // for (; i<3; i++) // { // cout << "Enter the information of the snacks " << i+1 << endl; // getline(cin, (snacks+i)->brand); // // (cin >> (snacks+i)->weight >> (snacks+i)->calories).get(); // } // cout << endl << "Here is the information of the three snacks: " << endl << endl; // // for (i=0; i<3; i++) // { // cout << "snacks " << i+1 << ": "; // cout << "brand: "<< snacks[i].brand << endl; // cout << "weights: " << snacks[i].weight << endl; // cout << "calories: " << snacks[i].calories << endl; // } // return 0; //} //4.10 #include <iostream> #include <array> using namespace std; int main() { array<double, 3> score; int i = 0; cout << "Enter your scores in 40 yards rushing:" << endl; for (; i<3; i++) { cout << "score " << i+1 << ":" << endl; cin >> score[i]; } double ave = 0.0; ave = (score[0]+score[1]+score[2])/i; cout << "You have run " << i << " times" << endl; cout << "Your score in average is: " << ave << endl; return 0; }