以下答案本人在linux vscode中均已亲自测试编译通过,完美运行.
4.1
#include<iostream>
using namespace std;
int main()
{
const int size = 30;
char name1[size]={0};
char name2[size]={0};
char grade;
int Age;
cout << "what is your first name ? ";
cin.getline(name1,size);
cout << "what is your last name ? ";
cin.getline(name2,size);
cout << "what letter grade do you deserve ? ";
cin >> grade;
cout << "what is your Age ? ";
cin >> Age;
cout << "name:" << name1 << ',' << name2 << endl;
cout << "grade:" << grade << endl;
cout << "Age:" << Age << endl;
}
4.2
#include<iostream>
#include<string>
using namespace std;
int main()
{
string name;
string dessert;
cout << "Enter your name :\n ";
getline(cin,name);
cout << "Enter your favorite dessert :\n";
getline(cin,dessert);
cout << "I have some delicious " << dessert << " for you," << name << " .\n";
return 0;
}
4.3
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
const int size = 30;
char name1[size]={0};
char name2[size]={0};
cout << "what is your first name ? ";
cin >> name1;
cout << "what is your last name ? ";
cin >> name2;
cout << "here's the information in a single:" << name1 << ", " << name2 << endl;
}
4.4
#include<iostream>
#include<string>
using namespace std;
int main()
{
string name1, name2;
cout << "what is your first name ? ";
cin >> name1;
cout << "what is your last name ? ";
cin >> name2;
cout << "here's the information in a single:" << name1 << ", " << name2 << endl;
}
4.5
#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
string name; //品牌
double weight; //重量
int calorie; //卡路里
};
int main()
{
CandyBar snack{"Mocha Munch",2.3,350};
cout << "name:" << snack.name << endl;
cout << "weight:" << snack.weight << endl;
cout << "calorice:" << snack.calorie << endl;
}
4.6
#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
string name; //品牌
double weight; //重量
int calorie; //卡路里
};
int main()
{
int i;
CandyBar snack[3]{{"Mocha Munch",2.3,350},{"Wujia sweet",2.8,280},{"lijia beautiful",3.0,190}};
for(i=0;i<3;i++)
{
cout << "name:" << snack[i].name << endl;
cout << "weight:" << snack[i].weight << endl;
cout << "calorice:" << snack[i].calorie << endl;
}
}
4.7
#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
string name; //披萨饼公司名称
double diameter; //披萨饼直径
double weight; //披萨饼重量
};
int main()
{
CandyBar pizza;
cout << "Please enter the brand of pizza company:";
getline(cin,pizza.name);
cout << "Please enter the diameter of the pizza:";
cin >> pizza.diameter;
cout << "Please enter the pizza weight:";
cin >> pizza.weight;
cout << "the brand of pizza company:" << pizza.name << endl;
cout << "the diameter of the pizza:" << pizza.diameter << endl;
cout << "the pizza weight:" << pizza.weight << endl;
}
4.8
#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
string name; //披萨饼公司名称
double diameter; //披萨饼直径
double weight; //披萨饼重量
};
int main()
{
CandyBar * pizza = new CandyBar;
cout << "Please enter the diameter of the pizza:";
cin >> pizza->diameter;
cout << "Please enter the brand of pizza company:";
cin.get(); //由于上一次输入结束后,录入一个换行符,导致下条语句判断为输入结束,直接输出结果,本条语句用来接收那个换行符
getline(cin,pizza->name);
cout << "Please enter the pizza weight:";
cin >> pizza->weight;
cout << "the brand of pizza company:" << pizza->name << endl;
cout << "the diameter of the pizza:" << pizza->diameter << endl;
cout << "the pizza weight:" << pizza->weight << endl;
}
4.9
#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
string name; //品牌
double weight; //重量
int calorie; //卡路里
};
int main()
{
int i;
CandyBar * snack = new CandyBar [3]{{"Mocha Munch",2.3,350},{"Wujia sweet",2.8,280},{"lijia beautiful",3.0,190}};
for(i=0;i<3;i++)
{
cout << "name:" << snack[i].name << endl;
cout << "weight:" << snack[i].weight << endl;
cout << "calorice:" << snack[i].calorie << endl;
}
delete [] snack;
}
4.10
#include<iostream>
#include<array>
using namespace std;
int main()
{
int i;
double ave = 0.0,sum = 0.0;
array<double,3> score;
for(i=0;i<3;i++)
{
cout << "Please enter the result of the 40-meter race:";
cin >> score[i];
sum += score[i];
}
ave = sum / 3;
for(i=0;i<3;i++)
cout << "第 " << i+1 <<" 次40米跑成绩:" << score[i] << endl;
cout << i <<" 次40米跑的平均成绩是:" << ave << endl;
}