4.13.1
编写程序,按照输出样例来显示信息
输入名,姓,应得的成绩,年龄
输出姓名,成绩,年龄
程序向下调整成绩
#include <iostream>
#include <string>
using namespace std;
/*
编写程序,要求:
输入名,姓,应得的成绩,年龄
输出姓名,成绩,年龄
*/
int main()
{
string first_name;
string last_name;
char grade;
int age;
cout << "输入您的名字:";
cin >> first_name;
cout << "输入您的姓:";
cin >> last_name;
cout << "输入您应得的成绩:";
cin >> grade;
cout << "输入您的年龄:";
cin >> age;
cout << "姓名:" << first_name << "," << last_name << endl;
cout << (char)(grade + 1) <<endl;
cout << "年龄:" << age << endl;
return 0;
}
4.13.2
修改本书程序4.4,使用string而不是char数组
程序4.4
#include <iostream>
int main() {
using namespace std;
const int ArSize = 20;
char name[ArSize];
char dessert[ArSize];
cout << "Enter your name:\n";
cin.getline(name, ArSize);
cout << "Enter your favorite dessert:\n";
cin.getline(dessert, ArSize);
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
return 0;
}
#include <iostream>
#include <string>
using namespace std;
/*
修改4.4,使用string而不是char数组
*/
int main()
{
string dessert;
string name;
cout << "Enter your name:\n";
cin >> name;
cout << "Enter your favorite dessert:\n";
cin >> dessert;
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
return 0;
}
4.13.3
编写程序,用户输入名,姓,最后输出按照“姓,名”的方式组合起来
使用char数组和cstring的函数
#include <iostream>
#include <cstring>
using namespace std;
/*
编写程序,输入名,姓,使用","组合起来并输出
*/
int main()
{
const int length = 20;
char first_name[length];
char last_name[length];
cout << "输入名:";
cin.getline(first_name, length);
cout << "输入姓:";
cin.getline(last_name, length);
char symbol[length] = ",";
strcat(first_name, symbol);
strcat(first_name, last_name);
cout << first_name << endl;
return 0;
}
4.13.4
修改4.13.3,使用string而不是char数组
#include <iostream>
#include <string>
using namespace std;
int main()
{
string first_name;
string last_name;
cout << "输入名:";
cin >> first_name;
cout << "输入姓:";
cin >> last_name;
cout << first_name << "," << last_name;
return 0;
}
4.13.5
结构体CandyBar包含三个成员:品牌,重量(可以有小数),卡路里含量(整数)
编写程序,声明结构,创建一个名为snack的CandyBar变量,初始化为"Mocha Munch"、2.3和350
最后显示内容
#include <iostream>
#include <string>
using namespace std;
/*
结构体CandyBar包含三个成员:品牌,重量(可以有小数),卡路里含量(整数)
编写程序,声明结构,创建一个名为snack的CandyBar变量,初始化为"Mocha Munch"、2.3和350
最后显示内容
*/
struct CandyBar {
string brand;
double weight;
int calorie;
};
int main()
{
CandyBar snack = {"Mocha Munch", 2.3, 350};
cout << "snack的品牌:" << snack.brand << endl;
cout << "snack的重量:" << snack.weight << endl;
cout << "snack的卡路里含量:" << snack.calorie << endl;
return 0;
}
4.13.6
修改4.13.5的程序,使用包含3个元素的CandyBar数组,初始化为所选择的值,最后显示各值
#include <iostream>
#include <string>
using namespace std;
struct CandyBar {
string brand;
double weight;
int calorie;
};
int main()
{
CandyBar snack[3] = {
{"first", 2.3, 300},
{"second", 2.5, 350},
{"third", 2.1, 295}
};
cout << "第一个的品牌:" << snack[0].brand << endl;
cout << "第一个的重量:" << snack[0].weight << endl;
cout << "第一个的卡路里含量:" << snack[0].calorie << endl;
cout << "第二个的品牌:" << snack[1].brand << endl;
cout << "第二个的重量:" << snack[1].weight << endl;
cout << "第二个的卡路里含量:" << snack[1].calorie << endl;
cout << "第三个的品牌:" << snack[2].brand << endl;
cout << "第三个的重量:" << snack[2].weight << endl;
cout << "第三个的卡路里含量:" << snack[2].calorie << endl;
return 0;
}
4.13.7
公司从事比萨饼的分析服务,对于每个比萨饼都要记录以下信息:
公司名称,比萨饼的直径,比萨饼的重量
设计一个结构,用户可以输入对应的量并输出
#include <iostream>
#include <string>
using namespace std;
/*
公司从事比萨饼的分析服务,对于每个比萨饼都要记录以下信息:
公司名称,比萨饼的直径,比萨饼的重量
设计一个结构,用户可以输入对应的量并输出
*/
struct pizza {
string brand;
double diamer; //直径
double weight;
};
int main()
{
pizza p;
cout << "请输入比萨饼的公司名:";
cin >> p.brand;
cout << "请输入比萨饼的直径:";
cin >> p.diamer;
cout << "请输入比萨饼的重量:";
cin >> p.weight;
cout << "您的比萨饼品牌:" << p.brand <<endl;
cout << "您的比萨饼直径:" << p.diamer <<endl;
cout << "您的比萨饼重量:" << p.weight <<endl;
return 0;
}
4.13.8
修改4.13.7,使用new来动态分配,并且在输入公司名之前先输入直径
#include <iostream>
#include <string>
using namespace std;
struct pizza {
string brand;
double diamer; //直径
double weight;
};
int main()
{
pizza *p = new pizza;
cout << "请输入比萨饼的直径:";
cin >> p->diamer;
cout << "请输入比萨饼的公司名:";
cin >> p->brand;
cout << "请输入比萨饼的重量:";
cin >> p->weight;
cout << "您的比萨饼直径:" << p->diamer <<endl;
cout << "您的比萨饼品牌:" << p->brand <<endl;
cout << "您的比萨饼重量:" << p->weight <<endl;
delete p;
return 0;
}
4.13.9
修改4.13.5,使用new来动态分配
#include <iostream>
#include <string>
using namespace std;
struct CandyBar {
string brand;
double weight;
int calorie;
};
int main()
{
CandyBar *c = new CandyBar [3];
cout << "输入第一个的品牌:";
cin >> c[0].brand;
cout << "输入第一个的重量:";
cin >> c[0].weight;
cout << "输入第一个的卡路里含量:";
cin >> c[0].calorie;
cout << "输入第二个的品牌:";
cin >> c[1].brand;
cout << "输入第二个的重量:";
cin >> c[1].weight;
cout << "输入第二个的卡路里含量:";
cin >> c[1].calorie;
cout << "输入第三个的品牌:";
cin >> c[2].brand;
cout << "输入第三个的重量:";
cin >> c[2].weight;
cout << "输入第三个的卡路里含量:";
cin >> c[2].calorie;
cout << "第一个的品牌:" << c[0].brand << endl;
cout << "第一个的重量:" << c[0].weight << endl;
cout << "第一个的卡路里含量:" << c[0].calorie << endl;
cout << "第二个的品牌:" << c[1].brand << endl;
cout << "第二个的重量:" << c[1].weight << endl;
cout << "第二个的卡路里含量:" << c[1].calorie << endl;
cout << "第三个的品牌:" << c[2].brand << endl;
cout << "第三个的重量:" << c[2].weight << endl;
cout << "第三个的卡路里含量:" << c[2].calorie << endl;
delete [] c;
return 0;
}
4.13.10
编写程序,用户输入3次40m跑的成绩,显示次数和平均成绩,使用array对象来存储数据
#include <iostream>
#include <array>
using namespace std;
/*
编写程序,输入三次40米跑的成绩,
显示次数和平均成绩
使用array对象来来存储数据
*/
int main()
{
array<int, 3> grade;
cout << "输入三次成绩:";
cin >> grade[0] >> grade[1] >> grade[2];
int sum = grade[0] + grade[1] + grade[2];
int times = sizeof(grade) / sizeof(int);
cout << "总共有" << times << "次,平均成绩为:" << sum / times << endl;
return 0;
}