diff --git a/SS2301-01/ss2301_01.cpp b/SS2301-01/ss2301_01.cpp index fb56355..e0e2c0b 100644 --- a/SS2301-01/ss2301_01.cpp +++ b/SS2301-01/ss2301_01.cpp @@ -1,20 +1,52 @@ #include +#include +#include +#include +#include -// ベースケース: 単一の値を返す template -T minimum(T t) { - return t; +T minimum(const std::vector& values) { + T minVal = std::numeric_limits::max(); + for (const auto& val : values) { + if (val < minVal) minVal = val; + } + return minVal; } -// 再帰的に最小値を取得する -template -T minimum(T first, Args... args) { - return std::min(first, minimum(args...)); -} +int main(int argc, char* argv[]) { + std::vector data; -int main() { - std::cout << "Minimum value is: " << minimum(5, 3, 9, 1, 8) << std::endl; // 1が出力される - std::cout << "Minimum value is: " << minimum(10, 20, 5, 15) << std::endl; // 5が出力される - std::cout << "Minimum value is: " << minimum(4.5, 3.5, 5.6, 7.8) << std::endl; // 3.5が出力される + 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; }