第四章 课后题

    技术2026-01-05  11

    Prac 1

    // prac1.cpp #include<iostream> struct aaa { char first_name[20]; char last_name[20]; char grade; int Age; } ; int main() { using namespace std; aaa * b = new aaa; cout << "What is your first name?"; cin.get(b->first_name, 20).get(); cout << "What is your last name?"; cin.get(b->last_name, 20).get(); cout << "What letter grade do you deserve?"; cin >> (*b).grade; cout << "What is your age?"; cin >> (*b).Age; cout << "Name: " << (*b).first_name << ", " << (*b).last_name << endl; cout << "Grade: " << b->grade << endl; cout << "Age: " << b->Age << endl; delete b; return 0; } What is your first name?bing What is your last name?Y What letter grade do you deserve?a What is your age?21 Name: bing, Y Grade: a Age: 21

    Prac 2

    // prac2.cpp #include<iostream> #include<string> int main() { using namespace std; string name1; string dessert2; cout << "Enter your name:\n"; cin >> name1; // reads through newline cout << "Enter your favorite dessert:\n"; cin >> dessert2; cout << "I have some delicious " << dessert2; cout << " for you. " << name1 << ".\n"; return 0; } Enter your name: bing Enter your favorite dessert: cake I have some delicious cake for you. bing.

    Prac 3

    //prac3.cpp #include<iostream> #include<cstring> #include<string> int main() { using namespace std; char first_name[20]; char last_name[20]; cout << "Enter your first name: "; cin.getline(first_name,20); cout << "Enter your last name: "; cin.getline(last_name, 20); string str1 = first_name; string str2 = last_name; string output = str1 + ", " + str2; cout << "Here's the information in a single string: " << output; cout << endl; return 0; } Enter your first name: Bing Enter your last name: Y Here's the information in a single string: Bing, Y

    Prac 4

    //prac4.cpp #include<iostream> #include<cstring> #include<string> int main() { using namespace std; string first_name; string last_name; cout << "Enter your first name: "; cin >> first_name; cout << "Enter your last name: "; cin >> last_name; string output = first_name + ", " + last_name; cout << "Here's the information in a single string: " << output; cout << endl; return 0; } Enter your first name: Bing Enter your last name: Y Here's the information in a single string: Bing, Y

    Prac5

    //Prac5.cpp #include<iostream> struct CandyBar { char Brand[20]; float weight; int calorie; } snack { "Mocha Munch", 2.3, 50 }; int main() { using namespace std; cout << "Brand: " << snack.Brand << endl; cout << "weight: " << snack.weight << endl; cout << "calorie: " << snack.calorie << endl; return 0; } Brand: Mocha Munch weight: 2.3 calorie: 50

    Prac6

    //Prac6.cpp #include<iostream> struct CandyBar { char Brand[20]; float weight; int calorie; } ; int main() { using namespace std; CandyBar snack[3] = { {"aaa", 2.2, 2}, {"bbb", 3.3, 3}, {"ccc", 4.4, 4} }; cout << "Brand: " << snack[0].Brand << " and " << snack[1].Brand << endl; cout << "weight: " << snack[0].weight << " and " << snack[1].weight << endl; cout << "calorie: " << snack[0].calorie << " and " << snack[1].calorie << endl; return 0; }

    Prac7

    Brand: aaa and bbb weight: 2.2 and 3.3 calorie: 2 and 3 //prac7.cpp #include<iostream> struct Pizza { char company[30]; int length; int weight; }; int main() { using namespace std; Pizza struc; cout << "Enter the name of Pizaa Company: "; cin >> struc.company; cout << struc.company << endl; cout << "Enter the length of Pizza: "; cin >> struc.company; cout << "Enter the weight of Pizza: "; cin >> struc.weight; return 0; } Enter the name of Pizaa Company: Bing Bing Enter the length of Pizza: 10 Enter the weight of Pizza: 12

    Prac8

    //prac7.cpp #include<iostream> struct Pizza { char company[30]; int length; int weight; }; int main() { using namespace std; Pizza * struc = new Pizza; cout << "Enter the name of Pizaa Company: "; cin.get(struc->company, 30); cout << "Enter the length of Pizza: "; cin >> (*struc).length; cout << "Enter the weight of Pizza: "; cin >> (*struc).weight; return 0; } Enter the name of Pizaa Company: Bing Enter the length of Pizza: 10 Enter the weight of Pizza: 10

    Prac9

    //Prac9.cpp #include<iostream> #include<string> using namespace std; struct CandyBar { string brand; float weight; int calorie; }; int main() { CandyBar* sugar = new CandyBar[3]; sugar[0].brand ="Mocha Munch"; sugar[0].weight=2.3; sugar[0].calorie = 350; sugar[1].brand ="Fruit Salad"; sugar[1].weight=5.0; sugar[1].calorie =400; sugar[2].brand ="Fried Chicken"; sugar[2].weight=3.5; sugar[2].calorie =200; //sugar[0]={"Mocha Munch",2.3,350}; //sugar[1]={"Fruit Salad",5.0,400}; // 不能这么初始化,编译会报错!!! cout<<"The first brand is "<<sugar[0].brand<<endl; cout<<"The first weight is "<<sugar[0].weight<<endl; cout<<"The first calorie is "<<sugar[0].calorie<<endl; cout<<"The second brand is "<<sugar[1].brand<<endl; cout<<"The second weight is "<<sugar[1].weight<<endl; cout<<"The second calorie is "<<sugar[1].calorie<<endl; cout<<"The third brand is "<<sugar[2].brand<<endl; cout<<"The third weight is "<<sugar[2].weight<<endl; cout<<"The third calorie is "<<sugar[2].calorie<<endl; delete [] sugar; cin.get(); return 0; } The first brand is Mocha Munch The first weight is 2.3 The first calorie is 350 The second brand is Fruit Salad The second weight is 5 The second calorie is 400 The third brand is Fried Chicken The third weight is 3.5 The third calorie is 200

    Prac10

    //Prac10.cpp #include<iostream> #include<array> int main() { using namespace std; array<int, 3> result; cout << "Please Enter the first result : "; cin >> result[0]; cout << "Please Enter the second result : "; cin >> result[1]; cout << "Please Enter the third result : "; cin >> result[2]; cout << "The average result is : " << (result[0] + result[1] + result[2])/3; cout << endl; cout << "The total amount of result is : " << sizeof(result)/sizeof(result[0]); return 0; } Please Enter the first result : 3 Please Enter the second result : 4 Please Enter the third result : 5 The average result is : 4 The total amount of result is : 3
    Processed: 0.020, SQL: 10