#include # include #include # include using namespace std; // 全局引入std命名空間struct Process { string name; // 原為 std::string int arrive; int service; int start; int finish; float turnaround; float weighted_ta;};bool compareArrival(const Process& a, const Process& b) { return a.arrive < b.arrive;}int main() { cout << "先來先服務(wù)調(diào)度算法\n"; // 原為 std::cout cout << "輸入進程數(shù)目:"; int n; cin >> n; // 原為 std::cin vector processes(n); // 原為 std::vector for (int i = 0; i < n; ++i) { cout << "請輸入進程" << (i+1) << "的信息(名稱 到達時間 服務(wù)時間):"; cin >> processes[i].name >> processes[i].arrive >> processes[i].service; } sort(processes.begin(), processes.end(), compareArrival); // 原為 std::sort int current_time = 0; for (vector::iterator it = processes.begin(); it != processes.end(); ++it) { it->start = max(current_time, it->arrive); // 原為 std::max it->finish = it->start + it->service; it->turnaround = it->finish - it->arrive; it->weighted_ta = it->turnaround / static_cast(it->service); current_time = it->finish; } // 輸出運行順序 cout << "\n運行順序:"; for (vector::const_iterator it = processes.begin(); it != processes.end(); ++it) { cout << it->name; if (it + 1 != processes.end()) { cout << " → "; } } // 輸出表格 cout << fixed << setprecision(3); // 原為 std::fixed 和 std::setprecision cout << "\n\n" << left << setw(8) << "進程" // 原為 std::left 和 std::setw << right << setw(12) << "到達時間" << setw(12) << "服務(wù)時間" << setw(12) << "開始時間" << setw(12) << "結(jié)束時間" << setw(12) << "周轉(zhuǎn)時間" << "帶權(quán)周轉(zhuǎn)時間\n"; float total_ta = 0, total_wta = 0; for (vector::const_iterator it = processes.begin(); it != processes.end(); ++it) { cout << left << setw(8) << it->name << right << setw(12) << it->arrive << setw(12) << it->service << setw(12) << it->start << setw(12) << it->finish << setw(12) << it->turnaround << setw(12) << it->weighted_ta << "\n"; total_ta += it->turnaround; total_wta += it->weighted_ta; } cout << "\n平均周轉(zhuǎn)時間: " << total_ta / n << "\n平均帶權(quán)周轉(zhuǎn)時間: " << total_wta / n << endl; return 0;}