用c语音编写程序,输出N件航材中价值最高航材的信息。N用符号常量定义为5,航材信息包括件号(PN)、名称(Designation)、单价(Price)。注意:件号为长整型,名称是没有空格的字符串,最长不超过30个字符,单价为float类型。
时间: 2024-02-13 13:06:19 浏览: 29
好的,以下是用C语言编写的程序,可以实现你的需求:
```c
#include <stdio.h>
#include <string.h>
#define N 5
struct AircraftMaterial {
long PN;
char Designation[31];
float Price;
};
void printAircraftMaterial(const struct AircraftMaterial* am) {
printf("PN: %ld\n", am->PN);
printf("Designation: %s\n", am->Designation);
printf("Price: %.2f\n", am->Price);
}
int main() {
struct AircraftMaterial aircraftMaterials[N];
// 读入N件航材信息
for (int i = 0; i < N; ++i) {
printf("Input the information of the %d aircraft material:\n", i+1);
printf("PN: ");
scanf("%ld", &aircraftMaterials[i].PN);
printf("Designation: ");
scanf("%s", aircraftMaterials[i].Designation);
printf("Price: ");
scanf("%f", &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;
}
}
// 输出价值最高的航材信息
printf("The information of the aircraft material with the highest value:\n");
printAircraftMaterial(&aircraftMaterials[maxIndex]);
return 0;
}
```
程序读入N件航材信息,使用一个结构体`AircraftMaterial`来表示一件航材的信息,其中包括件号(PN)、名称(Designation)、单价(Price)。然后找到价值最高的航材,最后输出其信息。注意,在C语言中,字符串需要用字符数组表示,所以`Designation`的类型是`char[31]`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)