⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program_6_8.cpp

📁 c++程序设计讲义 cppprograming 含源码
💻 CPP
字号:
// program 6.8: Sorting numbers read from a file
#include <iostream>
#include <fstream>
using namespace std;
// swap two values
void Swap(int &x, int &y) {
    int tmp = x;
    x = y;
    y = tmp;
    return;
}
// sort three numbers into nondecreasing order
void Sort3(int &a, int &b, int &c) {
    if (a > b)
        Swap(a, b);
    if (a > c)
        Swap(a, c);
    if (b > c)
        Swap(b, c);
    return;
}
// read values from file
bool ReadValues(istream &in, int &v1, int &v2, int &v3) {
    if(in >> v1 >> v2 >> v3)
        return true;
    else
        return false;
}
// read three numbers and output them in sorted order
int main() {
    // Open the input file
    ifstream fin("test.dat");
    if (!fin) {
        cerr << "Could not open test.dat" << endl;
        return 1;
    }
    int Input1;
    int Input2;
    int Input3;
    if (!ReadValues(fin, Input1, Input2, Input3)) {
        cerr << "Could not read three values" << endl;
        return 1;
    }
    int Output1 = Input1;
    int Output2 = Input2;
    int Output3 = Input3;
    // Sort the three numbers
    Sort3(Output1, Output2, Output3);
    // Output the sorted numbers
    cout << Input1 << " " << Input2 << " " << Input3
     << " in sorted order is "
     << Output1 << " " << Output2 << " " << Output3 << endl;
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -