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

📄 maxval1.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(ifstream& 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()
{  
   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;
   }

   double max = read_data(infile);
   cout << "The maximum value is " << max << "\n";

   infile.close();
   return 0;
}

⌨️ 快捷键说明

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