第四章 编程练习答案
1.
#include<iostream>
int main()
{
using namespace std;
cout << "What is your first name? ";
char firstname[20];
cin.getline(firstname, 20);//输入字符串
cout << "What is your last name? ";
char lastname[20];
cin.getline(lastname, 20);
cout << "What letter grade do you deserve? ";
char grade;
cin >> grade;
grade = grade + 1;
cout << "What is your age? ";
int age;
cin >> age;
cout << "Name: " << lastname << ", " << firstname << endl;
cout << "Grade: " << grade << endl;
cout << "Age: " << age << endl;
return 0;
}
2.
#include<iostream>
#include<string>//string类的函数库
int main()
{
using namespace std;
string name;
string dessert;
cout << "Enter your name:\n";
getline(cin, name);//输入string类
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>
int main()
{
using namespace std;
char firstname[20];
string lastname;
cout << "Enter your first name: ";
cin.getline(firstname, 20);//输入字符串
cout << "Enter your last name: ";
getline(cin, lastname);//输入string类
cout << "Here's the information in a single string: "
<< lastname << ", " << firstname << endl;
return 0;
}
4.
题目3和4的区别在于char数组和string的区别,其他一致,就不放了。
5.
#include<iostream>
struct CandyBar{
char brand[20];
float weight;
int calories;
};
int main()
{
using namespace std;
CandyBar snack = {"Mocha Munch", 2.3, 350};
cout << "Brand: " << snack.brand << endl;
cout << "Weight: " << snack.weight << endl;
cout << "calories: " << snack.calories << endl;
return 0;
}
6.
#include<iostream>
struct CandyBar{
char brand[20];
float weight;
int calories;
};
int main()
{
using namespace std;
CandyBar snack[3] = {
{"Mocha Munch1", 2.3, 350},
{"Mocha Munch2", 1.1, 150},
{"Mocha Munch3", 1.3, 450},
};
for(int i=0;i<3;i++)
{
cout << "Brand: " << snack[i].brand << endl;//这里直接用成员运算符(.)
cout << "Weight: " << snack[i].weight << endl;
cout << "calories: " << snack[i].calories << endl;
}
return 0;
}
7.
#include<iostream>
struct pizza{
char brand[20];
float length;
float weight;
};
int main()
{
using namespace std;
int n;
cout << "Please enter the number of the pizza: ";
cin >> n;
cin.get();//注意这里数字末尾的换行符
pizza AAA[n];
for(int i = 0;i < n ; i++)
{
cout << "Brand: " << endl;
cin.getline(AAA[i].brand, 20);
cout << "Weight: " << endl;
cin >> AAA[i].weight;
cout << "length: " << endl;
cin >> AAA[i].length;
cin.get();
}
cout << "\nHere is the information of the pizza: " << endl;
for(int i = 0;i < n ; i++)
{
cout << "pizza #" << i+1 << ": " << endl;
cout << "length: " << AAA[i].length << endl;
cout << "Brand: " << AAA[i].brand << endl;
cout << "Weight: " << AAA[i].weight << endl;
}
return 0;
}
8.
#include<iostream>
struct pizza{
char brand[20];
float length;
float weight;
};
int main()
{
using namespace std;
int n;
cout << "Please enter the number of the pizza: ";
cin >> n;
pizza * AAA = new pizza[n];//动态分配内存
for(int i = 0;i < n ; i++)
{
cout << "pizza #" << i+1 << ": " << endl;
cout << "length: " << endl;
cin >> AAA[i].length;
cin.get();//注意这里数字末尾的换行符
cout << "Brand: " << endl;
cin.getline(AAA[i].brand, 20);
cout << "Weight: " << endl;
cin >> AAA[i].weight;
}
cout << "Here is the information of the pizza: " << endl;
for(int i = 0;i < n ; i++)
{
cout << "pizza #" << i+1 << ": " << endl;
cout << "length: " << AAA[i].length << endl;
cout << "Brand: " << AAA[i].brand << endl;
cout << "Weight: " << AAA[i].weight << endl;
}
return 0;
}
9.
#include<iostream>
#include<string>
using namespace std;
struct CandyBar{
string brand;
float weight;
int calories;
};
int main()
{
CandyBar * snack = new CandyBar[3];//动态分配数组
snack[0] = {"Mocha Munch1", 2.3, 350};
snack[1] = {"Mocha Munch2", 1.1, 150};
snack[2] = {"Mocha Munch3", 1.3, 450};
for(int i=0;i<3;i++)
{
cout << "Brand: " << snack[i].brand << endl;
cout << "Weight: " << snack[i].weight << endl;
cout << "calories: " << snack[i].calories << endl;
}
return 0;
}
10.
#include<iostream>
int main()
{
using namespace std;
double runtime[3], average;
int i;
for(i=0;i<3;i++){
cout << "输入第" << i+1 << "次时间:" << endl;
cin >> runtime[i];
}
average = (runtime[0] + runtime[1] + runtime[2]) / 3;
cout << "平均时间:" << average << endl;
return 0;
}