酷代码 AI
菜单
服务商

C语言图书借阅统计程序:输入处理、逻辑纠错与时间精度全解析

```c #include <stdio.h> #include <stdlib.h> typedef struct { int bookId; int startHour; int startMinute; int endHour; int endMinute; int isBorrowed; } BookRecord; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { BookRecord records[1000]; int recordCount = 0; int borrowCount = 0; int totalReadTime = 0; while (1) { int bookId; char key; int hour, minute; scanf("%d %c %d:%d", &bookId, &key, &hour, &minute); if (bookId == 0) { break; } if (key =='s') { records[recordCount].bookId = bookId; records[recordCount].startHour = hour; records[recordCount].startMinute = minute; records[recordCount].isBorrowed = 1; recordCount++; } else if (key == 'e') { int found = 0; for (int j = 0; j < recordCount; j++) { if (records[j].bookId == bookId && records[j].isBorrowed == 1) { records[j].endHour = hour; records[j].endMinute = minute; records[j].isBorrowed = 0; int startMinuteTotal = records[j].startHour * 60 + records[j].startMinute; int endMinuteTotal = records[j].endHour * 60 + records[j].endMinute; totalReadTime += endMinuteTotal - startMinuteTotal; borrowCount++; found = 1; break; } } if (!found) { continue; } } } printf("%d %d\n", borrowCount, totalReadTime / borrowCount); } return 0; } ``` 额外需要注意的逻辑点: 1. 对于输入的时间格式要进行严格的检查和处理,确保其符合要求。 2. 在处理借书和还书的匹配时,要注意可能存在的错误情况,比如重复的借书或还书记录。 3. 计算平均阅读时间时要注意处理整除可能导致的精度问题。 ######[AI写代码神器 | 590点数解答 | 2024-11-30 10:47:49]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]