设计一个水果和课本的统计表,如果是水果就记录名字,价格,生产日期,保质期,如果是课本就记录名字,价格,生产日期,版本号。
时间: 2024-03-25 18:36:16 浏览: 23
好的,我可以为您提供一个简单的设计,可以使用结构体来表示水果和课本,如下所示:
```cpp
#include <iostream>
#include <string>
using namespace std;
// 水果结构体
struct Fruit {
string name; // 名字
double price; // 价格
string productionDate; // 生产日期
int shelfLife; // 保质期(天)
};
// 课本结构体
struct Book {
string name; // 名字
double price; // 价格
string productionDate; // 生产日期
string version; // 版本号
};
int main() {
Fruit fruit1 = {"苹果", 3.5, "2022-03-01", 7};
Fruit fruit2 = {"香蕉", 2.5, "2022-03-02", 5};
Book book1 = {"C++程序设计", 49.9, "2021-08-01", "第2版"};
Book book2 = {"数据结构与算法", 59.9, "2021-09-01", "第3版"};
// 输出水果信息
cout << "水果:" << endl;
cout << "名称\t价格\t生产日期\t保质期" << endl;
cout << fruit1.name << "\t" << fruit1.price << "\t" << fruit1.productionDate << "\t" << fruit1.shelfLife << "天" << endl;
cout << fruit2.name << "\t" << fruit2.price << "\t" << fruit2.productionDate << "\t" << fruit2.shelfLife << "天" << endl;
// 输出课本信息
cout << "课本:" << endl;
cout << "名称\t价格\t生产日期\t版本号" << endl;
cout << book1.name << "\t" << book1.price << "\t" << book1.productionDate << "\t" << book1.version << endl;
cout << book2.name << "\t" << book2.price << "\t" << book2.productionDate << "\t" << book2.version << endl;
return 0;
}
```
运行程序后,会输出水果和课本的信息,如下所示:
```
水果:
名称 价格 生产日期 保质期
苹果 3.5 2022-03-01 7天
香蕉 2.5 2022-03-02 5天
课本:
名称 价格 生产日期 版本号
C++程序设计 49.9 2021-08-01 第2版
数据结构与算法 59.9 2021-09-01 第3版
```
希望能够帮到您!