第四章编程练习
#include<iostream> #include<climits> #include<string> #include<cstring> #include<array> using namespace std; struct CandyBar { string brand; double weight; int carl; }; struct Pizza { string companyName; float diameter; float weight; }; int main() { //第四章编程练习4.1 /*char firstName[20]; char lastName[20]; char grade; int age; cout << "what is your first name?"; cin.getline(firstName, 20); cout << "what is your last name?"; cin.getline(lastName, 20); cout << "what letter grade do you deserve?"; cin >> grade; cout << "what is your age?"; cin >> age; cout << "name:" << firstName << "," << lastName << endl; cout << "grade:" << grade << endl; cout << "age:" << age << endl;*/ //4.2 /*string name; string dessert; cout << "enter your name:\n"; getline(cin, name); cout << "enter your favorite dessert:\n"; getline(cin, dessert); cout << "I have delicious " << dessert << " for you, " << name << endl;*/ //4.3 /*char firstName[20]; char lastName[20]; cout << "Enter your first name:"; cin.getline(firstName, 20); cout << "Enter your last name:"; cin.getline(lastName, 20); char name[40]; strcpy_s(name, lastName); strcat_s(lastName, ","); strcat_s(lastName, firstName); cout << "here some information in a single string:" << lastName << endl;*/ //4.4 /*string firstname; string lastname; string name; cout << "Enter your first name:"; getline(cin, firstname); cout << "Enter your last name:"; getline(cin,lastname); name = lastname + "," + firstname; cout << "here some information in a single string:" << name << endl;*/ //4.5 /*CandyBar snack{ "Mocha Munch",2.3,350 }; cout << "snack's brand is:" << snack.brand << endl; cout << "snack's weight is:" << snack.weight << endl; cout << "snack's carl is:" << snack.carl << endl;*/ //4.6 //CandyBar candy[3] = { { "Mocha Munch",2.3,350 },{ "Mocha",1.3,150 } ,{ "DEFU",2,300 } }; //4.7 /*Pizza p; cout << "Enter pizza's company name:"; getline(cin, p.companyName); cout << "Enter pizza's diameter:"; cin >> p.diameter; cout << "Enter pizza's weight:"; cin >> p.weight; cout << "This pizza's information is:\n"; cout << "Company name = " << p.companyName << endl; cout << "diameter = " << p.diameter << endl; cout << "weight= " << p.weight << endl;*/ //4.8 /*Pizza *p = new Pizza; cout << "Enter pizza's company name:"; getline(cin, p->companyName); cout << "Enter pizza's diameter:"; cin >> p->diameter; cout << "Enter pizza's weight:"; cin >> p->weight; cout << "This pizza's information is:\n"; cout << "Company name = " << p->companyName << endl; cout << "diameter = " << p->diameter << endl; cout << "weight= " << p->weight << endl; delete p;*/ //4.9 /*CandyBar *cand = new CandyBar[3]; cand[0] = { "Mocha Munch",2.3,350 }; cand[1] = { "Mocha",1.3,150 }; cand[2] = { "DEFU",2,300 }; cout << "cand[0]'s brand is " << cand[0].brand << endl; cout << "cand[0]'s weight is " << cand[0].weight << endl; cout << "cand[0]'s carl is " << cand[0].carl << endl; cout << "cand[1]'s brand is " << cand[1].brand << endl; cout << "cand[1]'s weight is " << cand[1].weight << endl; cout << "cand[1]'s carl is " << cand[1].carl << endl; cout << "cand[2]'s brand is " << cand[2].brand << endl; cout << "cand[2]'s weight is " << cand[2].weight << endl; cout << "cand[2]'s carl is " << cand[2].carl << endl; delete[] cand;*/ //4.10 array<double, 3> grade; cin >> grade[0]; cin >> grade[1]; cin >> grade[2]; double aver = (grade[0] + grade[1] + grade[2]) / 3.0; cout << "The average grade is " << aver << endl; return 0; }