编写程序,输出N件航材中价值最高航材的信息。N用符号常量定义为5,航材信息包括件号(PN)、名称(Designation)、单价(Price)。注意:件号为长整型,名称是没有空格的字符串,最长不超过30个字符,单价为float类型。
时间: 2024-02-13 13:06:18 浏览: 74
好的,以下是用C++编写的程序,可以实现你的需求:
```c++
#include <iostream>
#include <string>
using namespace std;
const int N = 5;
struct AircraftMaterial {
long PN;
string Designation;
float Price;
};
void printAircraftMaterial(const AircraftMaterial& am) {
cout << "PN: " << am.PN << endl;
cout << "Designation: " << am.Designation << endl;
cout << "Price: " << am.Price << endl;
}
int main() {
AircraftMaterial aircraftMaterials[N];
// 读入N件航材信息
for (int i = 0; i < N; ++i) {
cout << "Input the information of the " << i+1 << " aircraft material:" << endl;
cout << "PN: ";
cin >> aircraftMaterials[i].PN;
cout << "Designation: ";
cin >> aircraftMaterials[i].Designation;
cout << "Price: ";
cin >> aircraftMaterials[i].Price;
}
// 找到价值最高的航材
int maxIndex = 0;
float maxPrice = aircraftMaterials[0].Price;
for (int i = 1; i < N; ++i) {
if (aircraftMaterials[i].Price > maxPrice) {
maxIndex = i;
maxPrice = aircraftMaterials[i].Price;
}
}
// 输出价值最高的航材信息
cout << "The information of the aircraft material with the highest value:" << endl;
printAircraftMaterial(aircraftMaterials[maxIndex]);
return 0;
}
```
程序读入N件航材信息,使用一个结构体`AircraftMaterial`来表示一件航材的信息,其中包括件号(PN)、名称(Designation)、单价(Price)。然后找到价值最高的航材,最后输出其信息。
阅读全文