编程练习6链接:
https://blog.csdn.net/qq_34847850/article/details/119509313?spm=1001.2014.3001.5501
根据上面内容做题:
//4.11.9.cpp -- 使用new和数组、结构体
# include <iostream>
# include <string>
using namespace std;
struct CandyBar
{
string kind;
double weight;
int calorie;
};
int main()
{
CandyBar * snack = new CandyBar[3];
snack[0].kind="Mocha Munch1";
snack[0].weight=2.3;
snack[0].calorie=350;
snack[1].kind="Mocha Munch2";
snack[1].weight=2.6;
snack[1].calorie=300;
snack[2].kind="Mocha Munch3";
snack[2].weight=2.7;
snack[2].calorie=450;
cout<<"糖块的品牌1: "<<snack[0].kind<<"\n";
cout<<"糖块的重量1: "<<snack[0].weight<<"\n";
cout<<"糖块的卡路里1: "<<snack[0].calorie<<"\n";
cout<<"糖块的品牌2: "<<snack[1].kind<<"\n";
cout<<"糖块的重量2: "<<snack[1].weight<<"\n";
cout<<"糖块的卡路里2: "<<snack[1].calorie<<"\n";
cout<<"糖块的品牌3: "<<snack[2].kind<<"\n";
cout<<"糖块的重量3: "<<snack[2].weight<<"\n";
cout<<"糖块的卡路里3: "<<snack[2].calorie<<"\n";
delete [] snack;
return 0;
}
/*
输出结果:
糖块的品牌1: Mocha Munch1
糖块的重量1: 2.3
糖块的卡路里1: 350
糖块的品牌2: Mocha Munch2
糖块的重量2: 2.6
糖块的卡路里2: 300
糖块的品牌3: Mocha Munch3
糖块的重量3: 2.7
糖块的卡路里3: 450
Press any key to continue
*/