描述 要求根据输入的板卡信息,过滤满足要求的数据,并按照格式输出。 输入描述 1. 输入为一行字符串,由多组版本信息拼接而成,每组版本信息使用英文分号分割 2. 输入的每组板卡信息格式为:序号,ip,温度,连接状态(序号为整数且不重复) 3. 分割的总板卡数量<=1000 输出描述 1. 输出板卡状态为"Ready"的板卡信息,使用英文分号;分隔 2. 按照序号升序排序 3. 如果满足要求的板卡信息不超过3条,则全部输出;如果超过3条,则只输出前3条 4. 如果输入为空或者不存在满足要求的信息,则输出空字符串 异常考虑: 1. 单组板卡信息缺失或者不满足格式要求则视为异常(非"Ready"状态),不输出
时间: 2024-04-16 22:28:32 浏览: 23
你可以使用以下的 C 代码来实现根据输入的卡信息进行过滤并按照要求输出:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int number;
char ip[16];
int temperature;
char status[10];
} Card;
int compare(const void* a, const void* b) {
Card* cardA = (Card*)a;
Card* cardB = (Card*)b;
return cardA->number - cardB->number;
}
void filterCards(char* input) {
int count = 0;
Card cards[1000];
// 分割字符串为多组版本信息
char* token = strtok(input, ";");
while (token != NULL) {
// 解析单组板卡信息
char* tokenInner = strtok(token, ",");
Card card;
card.number = atoi(tokenInner);
tokenInner = strtok(NULL, ",");
strcpy(card.ip, tokenInner);
tokenInner = strtok(NULL, ",");
card.temperature = atoi(tokenInner);
tokenInner = strtok(NULL, ",");
strcpy(card.status, tokenInner);
// 过滤满足要求的板卡信息
if (strcmp(card.status, "Ready") == 0) {
cards[count] = card;
count++;
}
token = strtok(NULL, ";");
}
// 按照序号升序排序
qsort(cards, count, sizeof(Card), compare);
// 输出满足要求的板卡信息,最多输出前3条
int outputCount = count > 3 ? 3 : count;
for (int i = 0; i < outputCount; i++) {
printf("%d,%s,%d,%s;", cards[i].number, cards[i].ip, cards[i].temperature, cards[i].status);
}
}
int main() {
char input[2001];
printf("请输入板卡信息:");
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = '\0'; // 去除换行符
filterCards(input);
printf("\n");
return 0;
}
```
你可以输入一行字符串,其中包含多组版本信息,每组版本信息使用英文分号进行分割。每组板卡信息使用逗号进行分割,格式为:序号,ip,温度,连接状态。程序将根据要求进行过滤,并按照序号升序排序输出满足要求的板卡信息,最多输出前3条。如果输入为空或者不存在满足要求的信息,则输出空字符串。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![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)
![](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)