Newer
Older
Skill_semi_sample / SS2301-01 / ss2301_01.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <limits>

template <typename T>
T minimum(const std::vector<T>& values) {
    T minVal = std::numeric_limits<T>::max();
    for (const auto& val : values) {
        if (val < minVal) minVal = val;
    }
    return minVal;
}

int main(int argc, char* argv[]) {
    std::vector<double> data;

    if (argc > 2) {
        std::cerr << "Too many arguments. Please provide only a single file path or no arguments to use standard input." << std::endl;
        return 1;
    }

    if (argc == 2) { // テキストファイルからの読み込み
        std::ifstream infile(argv[1]);
        if (!infile) {
            std::cerr << "Cannot open file: " << argv[1] << std::endl;
            return 1;
        }
        double value;
        while (infile >> value) {
            data.push_back(value);
        }
    } else if (argc == 1) { // 標準入力からの読み込み
        std::cout << "Please enter your data (separated by spaces) and press Enter: ";
        std::string line;
        std::getline(std::cin, line);
        std::istringstream iss(line);
        double value;
        while (iss >> value) {
            data.push_back(value);
        }
    }

    if (data.empty()) {
        std::cerr << "No data provided." << std::endl;
        return 1;
    }

    std::cout << "Minimum value is: " << minimum(data) << std::endl;
    return 0;
}