练习1 #include <iostream> #include <cstring> using namespace std; int main() { char fname[20],lname[20],grade; int age; cout << "What is your first name? "; cin.getline(fname,20); cout << "What is your last name? "; cin.getline(lname,20); cout << "What letter grade do you deserve? "; cin.get(grade).get(); cout << "What is your age? "; cin >> age; grade++; cout << "Name: " << lname << ", " << fname << endl; cout << "Grade: " << grade << endl; cout << "Age: " << age << endl; return 0; } 练习2 #include <iostream> #include <cstring> int main() { using namespace std; string name,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"; return 0; } 练习3 #include <iostream> #include <cstring> using namespace std; int main() { char full_name[20],last_name[20]; cout << "Enter your first name: "; cin.getline(full_name,20); cout << "Enter your last name: "; cin.getline(last_name,20); strcat(full_name, ", "); strcat(full_name, last_name); cout << "Here's the infomation in a single string: " << full_name << endl; return 0; } 练习4 #include <iostream> #include <string> using namespace std; int main() { string full_name,last_name; cout << "Enter your first name: "; getline(cin,full_name); cout << "Enter your last name: "; getline(cin,last_name); full_name+=", " + last_name; cout << "Here's the infomation in a single string: " << full_name << endl; return 0; } 练习5 #include <iostream> struct CandyBar{ std::string brand; float weight; unsigned int calorie; }; int main() { using namespace std; CandyBar snack={"Mocha Munch",2.3,350}; cout << snack.brand << " 's candybar has " << snack.calorie << " calories for weight " << snack.weight; cout << endl; return 0; } 练习6 #include <iostream> struct CandyBar{ std::string brand; float weight; unsigned int calorie; }; int main() { using namespace std; CandyBar candys[]={ {"Mocha Munch",2.3,350}, {"Ohca Lily",2.5,360}, {"Fula Rich",3.1,400} }; cout << candys[0].brand << " 's candybar has " << candys[0].calorie << " calories for weight " << candys[0].weight << endl; cout << candys[1].brand << " 's candybar has " << candys[1].calorie << " calories for weight " << candys[1].weight << endl; cout << candys[2].brand << " 's candybar has " << candys[2].calorie << " calories for weight " << candys[2].weight << endl; return 0; } 练习7 #include <iostream> struct Pisa{ std::string corporation; float diameter; float weight; }; using namespace std; int main() { Pisa Newpisa; cout << "Enter manufactory of the pisa: "; getline(cin,Newpisa.corporation); cout << "Enter the diameter of the pisa: "; cin >> Newpisa.diameter; cout << "Enter the weight of the pisa: "; cin >> Newpisa.weight; cout << "Here're infomations about the pisa being input right now/n"; cout << "Manufactory: " << Newpisa.corporation << endl; cout << "Diameter: " << Newpisa.diameter << endl; cout << "Weight: " << Newpisa.weight << endl; return 0; } 练习8 #include <iostream> struct Pisa{ std::string corporation; float diameter; float weight; }; using namespace std; int main() { Pisa *pisa = new Pisa; cout << "Enter the diameter of the pisa: "; (cin >> pisa->diameter).get(); cout << "Enter manufactory of the pisa: "; getline(cin,pisa->corporation); cout << "Enter the weight of the pisa: "; cin >> pisa->weight; cout << "Here're infomations about the pisa being input right now/n"; cout << "Manufactory: " << pisa->corporation << endl; cout << "Diameter: " << pisa->diameter << endl; cout << "Weight: " << pisa->weight << endl; return 0; } 练习9 #include <iostream> struct CandyBar{ std::string brand; float weight; unsigned int calorie; }; int main() { using namespace std; CandyBar *candys=new CandyBar[3]; candys[0].brand="Mocha Munch"; candys[0].weight=2.3; candys[0].calorie=350; candys[1].brand="Ohca Lily"; candys[1].weight=2.5; candys[1].calorie=360; candys[2].brand="Fula Rich"; candys[2].weight=3.1; candys[2].calorie=400; cout << candys[0].brand << " 's candybar has " << candys[0].calorie << " calories for weight " << candys[0].weight << endl; cout << candys[1].brand << " 's candybar has " << candys[1].calorie << " calories for weight " << candys[1].weight << endl; cout << candys[2].brand << " 's candybar has " << candys[2].calorie << " calories for weight " << candys[2].weight << endl; delete [] candys; return 0; }