第四章编程练习(6-10)
4.13.6
#include<iostream>
using namespace std;
struct CandyBar {
char flavor[20];
float price;
double weight;
};
int main() {
CandyBar candy{
"Sweet",
5.21,
500
};
cout << candy.flavor << endl
<< candy.price << "元" << endl
<< candy.weight << " g." << endl;
return 0;
}
4.13.7
#include<iostream>
#include<string>
using namespace std;
struct william {
string name;
float calibre;
double weight;
};
int main() {
william wingate{};
cout << "Enter the name of the pizza : ";
getline(cin,wingate.name);
cout << "Enter the calibre of the pizza : ";
cin >> wingate.calibre;
cout << "Enter the weight of the pizza : ";
cin >> wingate.weight;
cout << "The pizza's name is " << wingate.name << endl;
cout << "The pizza's calibre is " << wingate.calibre << endl;
cout << "The pizza's weight is " << wingate.weight << endl;
return 0;
}
4.13.8
#include<iostream>
#include<string>
using namespace std;
struct william {
string name;
float calibre;
double weight;
};
int main() {
william* pizza = new william;
cout << "Enter the calibre of the pizza : ";
(cin >> pizza->calibre).get(); //.get(),以获取并返回输入流中的下一个字符
cout << "Enter the name of the pizza : ";
getline(cin, pizza->name);
cout << "Enter the weight of the pizza : ";
cin >> pizza->weight;
cout << "The pizza's name is " << pizza->name << endl;
cout << "The pizza's calibre is " << pizza->calibre << endl;
cout << "The pizza's weight is " << pizza->weight << endl;
delete pizza;
return 0;
}
4.13.9
//4.13.9
#include<iostream>
#include<string>
using namespace std;
struct CandyBar {
string name;
float price;
double weight;
};
int main() {
CandyBar* candy = new CandyBar;
cout << "Enter the name of candybar : ";
getline(cin, candy->name);
cout << "Enter the price of candybar : ";
cin>>candy->price;
cout << "Enter the weight of candybar : ";
cin >> candy->weight;
cout << "The name of candybar is : " << candy->name << endl;
cout << "The price of candybar is : " << candy->price << endl;
cout << "The weight of candybar is : " << candy->weight << endl;
delete candy;
return 0;
}
4.13.10
#include<iostream>
#include<string>
#include<array>
using namespace std;
int main() {
array<double,3>scores;
cout << "Enter your first scores of 40m: ";
cin >> scores[0];
cout << "Enter your second scores of 40m: ";
cin >> scores[1];
cout << "Enter your third scores of 40m: ";
cin >> scores[2];
cout << "You's the first score is :" << scores[0] << endl;
cout << "You's the second score is :" << scores[1] << endl;
cout << "You's the third score is :" << scores[2] << endl;
cout << "You's the average score is :" << (scores[0] + scores[1] + scores[2]) / 3 << endl;
return 0;
}