改成正确的,可以使用的
时间: 2024-12-19 22:17:26 浏览: 3
为了使提供的代码能够正确编译和运行,需要对代码进行一些格式上的调整,并且确保所有必要的部分都完整。以下是修正后的代码:
```cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;
// 磁盘请求结构体
struct DiskRequest {
int id; // 磁盘访问ID
int arrivalTime; // 到达时间
int track; // 访问磁道
int accessTime; // 读写时间
};
// 磁盘调度结果结构体
struct DiskSchedule {
int id; // 磁盘访问ID
int arrivalTime; // 到达时间
int finishTime; // 完成时间
int track; // 访问磁道
int seekDistance; // 寻道距离
int seekTime; // 寻道时间
int dataTime; // 读写时间
int accessTime; // 访问时间
};
// 读取磁盘请求
vector<DiskRequest> readDiskRequests(const string& filename) {
ifstream file(filename);
int n;
file >> n;
vector<DiskRequest> requests(n);
for (int i = 0; i < n; i++) {
file >> requests[i].id >> requests[i].arrivalTime >> requests[i].track >> requests[i].accessTime;
}
return requests;
}
// FCFS调度算法
void FCFS(const vector<DiskRequest>& requests, vector<DiskSchedule>& schedules) {
int currentTrack = 0; // 初始磁头位置
int currentTime = 0; // 当前时间
for (const auto& request : requests) {
// 计算寻道距离和时间
int seekDistance = abs(request.track - currentTrack);
int seekTime = seekDistance * 2; // 假设寻道时间与距离成正比
int finishTime = max(currentTime, request.arrivalTime) + seekTime + request.accessTime;
schedules.push_back({ request.id, request.arrivalTime, finishTime, request.track, seekDistance, seekTime, request.accessTime, request.accessTime + seekTime });
currentTrack = request.track;
currentTime = finishTime;
}
}
// SSTF调度算法
void SSTF(const vector<DiskRequest>& requests, vector<DiskSchedule>& schedules) {
vector<DiskRequest> remainingRequests = requests;
int currentTrack = 0;
int currentTime = 0;
while (!remainingRequests.empty()) {
// 找到离当前磁头最近的请求
auto closestRequest = min_element(remainingRequests.begin(), remainingRequests.end(),
[currentTrack](const DiskRequest& a, const DiskRequest& b) {
return abs(a.track - currentTrack) < abs(b.track - currentTrack);
});
int seekDistance = abs(closestRequest->track - currentTrack);
int seekTime = seekDistance * 2;
int finishTime = max(currentTime, closestRequest->arrivalTime) + seekTime + closestRequest->accessTime;
schedules.push_back({ closestRequest->id, closestRequest->arrivalTime, finishTime, closestRequest->track, seekDistance, seekTime, closestRequest->accessTime, closestRequest->accessTime + seekTime });
currentTrack = closestRequest->track;
currentTime = finishTime;
remainingRequests.erase(closestRequest);
}
}
// EA调度算法 (电梯算法)
void EA(const vector<DiskRequest>& requests, vector<DiskSchedule>& schedules) {
vector<DiskRequest> remainingRequests = requests;
int currentTrack = 0;
int currentTime = 0;
bool movingUp = true;
while (!remainingRequests.empty()) {
// 根据方向选择请求
vector<DiskRequest> validRequests;
for (const auto& request : remainingRequests) {
if ((movingUp && request.track >= currentTrack) || (!movingUp && request.track < currentTrack)) {
validRequests.push_back(request);
}
}
// 处理有效请求
if (!validRequests.empty()) {
auto closestRequest = *min_element(validRequests.begin(), validRequests.end(),
[currentTrack](const DiskRequest& a, const DiskRequest& b) {
return a.track < b.track;
});
int seekDistance = abs(closestRequest.track - currentTrack);
int seekTime = seekDistance * 2;
int finishTime = max(currentTime, closestRequest.arrivalTime) + seekTime + closestRequest.accessTime;
schedules.push_back({ closestRequest.id, closestRequest.arrivalTime, finishTime, closestRequest.track, seekDistance, seekTime, closestRequest.accessTime, closestRequest.accessTime + seekTime });
currentTrack = closestRequest.track;
currentTime = finishTime;
remainingRequests.erase(remove(remainingRequests.begin(), remainingRequests.end(), closestRequest), remainingRequests.end());
} else {
movingUp = !movingUp; // 改变方向
}
}
}
// 计算并输出性能指标
void calculateAverages(const vector<DiskSchedule>& schedules) {
int totalSeekDistance = 0;
int totalSeekTime = 0;
int totalDataTime = 0;
int totalAccessTime = 0;
for (const auto& schedule : schedules) {
totalSeekDistance += schedule.seekDistance;
totalSeekTime += schedule.seekTime;
totalDataTime += schedule.dataTime;
totalAccessTime += schedule.accessTime;
}
int n = schedules.size();
cout << "Average Seek Distance: " << fixed << setprecision(2) << (double)totalSeekDistance / n << endl;
cout << "Average Seek Time: " << fixed << setprecision(2) << (double)totalSeekTime / n << endl;
cout << "Average Data Time: " << fixed << setprecision(2) << (double)totalDataTime / n << endl;
cout << "Average Access Time: " << fixed << setprecision(2) << (double)totalAccessTime / n << endl;
}
// 主函数
int main() {
// 读取磁盘请求数据
string inputFile = "DISK.txt"; // 假设输入文件名为 DISK.txt
vector<DiskRequest> requests = readDiskRequests(inputFile);
// FCFS调度
vector<DiskSchedule> fcfsSchedules;
FCFS(requests, fcfsSchedules);
cout << "FCFS Scheduling Results:" << endl;
calculateAverages(fcfsSchedules);
cout << endl;
// SSTF调度
vector<DiskSchedule> sstfSchedules;
SSTF(requests, sstfSchedules);
cout << "SSTF Scheduling Results:" << endl;
calculateAverages(sstfSchedules);
cout << endl;
// EA调度
vector<DiskSchedule> eaSchedules;
EA(requests, eaSchedules);
cout << "EA Scheduling Results:" << endl;
calculateAverages(eaSchedules);
cout << endl;
return 0;
}
```
### 说明
1. **格式调整**:将代码中的每一行进行了适当的换行和缩进,使其更易阅读。
2. **完整性检查**:确保所有的结构体定义、函数声明和实现都是完整的。
3. **注释保留**:保留了原有的注释,以便理解代码的功能。
现在这段代码应该可以在支持C++的编译器中正确编译和运行。确保输入文件 `DISK.txt` 存在并且格式正确。
阅读全文