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

📄 maxval2.cpp

📁 BigC++的源码
💻 CPP
字号:
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

/**
   Reads numbers from a file and finds the maximum value
   @param in the input stream to read from
   @return the maximum value or 0 if the file has no numbers
*/            
double read_data(istream& in)
{  
   double highest;
   double next;
   if (in >> next)
      highest = next;
   else
      return 0;

   while (in >> next)
   {  
      if (next > highest)
         highest = next;
   }

   return highest;
}

int main()
{  
   double max;

   string input;
   cout << "Do you want to read from a file? (y/n) ";
   cin >> input;

   if (input == "y")
   {
      string filename;
      cout << "Please enter the data file name: ";
      cin >> filename;

      ifstream infile;
      infile.open(filename.c_str());

      if (infile.fail()) 
      {
         cout << "Error opening " << filename << "\n";
         return 1;
      }

      max = read_data(infile);
      infile.close();
   }
   else
      max = read_data(cin);

   cout << "The maximum value is " << max << "\n";

   return 0;
}

⌨️ 快捷键说明

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