添加数组的功能

时间:2013-12-13 19:55:46

标签: c++

我刚刚完成,但我很难想到如何从文件中添加数组中的数字。我希望它作为用户定义的函数。我是通过预定义的方法完成的,但我想知道如何创建一个完成这项工作的函数。

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <stdlib.h>
using namespace std;

const int OCCUPATION_MAX = 10;
void sort(string occupation[], double salary[]);
void sum(double salary[], double sum, int index);
double calc(double avgSalary, double medSalary);

int main()
{   
  string occupation[OCCUPATION_MAX];    //array of type string for the occupation names
  double salary[OCCUPATION_MAX];
  ifstream input;
  ofstream output;
  int index = 0;
  double sum = 0;
  string fname;
  double average;
  double median;

  //Intro
  cout << "Welcome to this program.\n";
  cout << "Please enter text file you want to use.\n";
  cin >> fname;
  //opening file of correct
  input.open(fname.c_str());

  if(input.fail())
  {
    cout << "Not a valid filename\n";
    exit(0);
  }

  //If input file exists, read all records from file
  while (input >> occupation[index])
  {
    input >> salary[index];
    sum = sum + salary[index];
    index++;
  }
  //Close input file
  input.close();

  //Call function to sort arrays in descending order by salary
  sort(occupation, salary);

  //Call function to calculate average and median of salaries
  double calc(double sum, double salary[], double& average, double& median);

  //Open output file, remember to look at output.txt for the result)
  output.open("output.txt");

  //Write file to output
  output << setw(15) << "OCCUPATION" << setw(15)<< "SALARY" << endl;
  output << "------------------------------------" << endl;
  for (index = 0; index<OCCUPATION_MAX; index++)
  {
    output << setw(15) << occupation[index] << setw(10) <<setprecision(2) << fixed << "$ " << salary[index] << endl;
  }
  output << endl << setw(15) << "Average Salary" << setw(10) <<setprecision(2) << fixed << "$ " << average << endl;
  output << setw(15) << "Median Salary" << setw(10) <<setprecision(2) << fixed << "$ " << median << endl;

  //Close output file
  output.close();

  return 0;
}

//Function to sort both arrays in descending order by salary
void sort(string occupation[], double salary[])
{
  for (int i=0; i<OCCUPATION_MAX; i++)
  {
    for (int j=i+1; j<OCCUPATION_MAX; j++)
    {
      if (salary[i] < salary[j])
      {
        //Swap salary
        double temp = salary[i];
        salary[i] = salary[j];
        salary[j] = temp;

        //Swap occupations
        string t = occupation[i];
        occupation[i] = occupation[j];
        occupation[j] = t;
      }
    }
  }
}
void sum(double salary[], double sum, index=0)
double salary;
double sum=0;
int index=0;
{
 while(!eof)
 {
    output >> salary[index];
    sum = sum + salary[index];
    index++;
  }
}
//Function to calculate average and median
double calc(double sum, double salary[], double& average, double& median)
{
  //Calculate average of occupations
  average = sum / salary[OCCUPATION_MAX];

  //Calculate median of occupations
  median = (salary[OCCUPATION_MAX/2] + salary[(OCCUPATION_MAX-1)/2])/2;
}

到目前为止,我被告知要取消引用我的指示。

void sum(double salary[], double sum, index=0)
    double salary;
    double sum=0;
    int index=0;
    {
     while(!eof)
     {
        output >> salary[index];
        sum = sum + salary[index];
        index++;
      }
    }

1 个答案:

答案 0 :(得分:1)

抱歉,我无法处理您sum功能中的错误:
避免旧的函数声明并使用现代声明:

void sum(std::istream&        input_file,
         std::vector<double>& salary,
         double &             sum,
         unsigned int&        values_read);

正确阅读:

{
  double salary_read = 0.0;
  sum = 0.0;
  while (input_file >> salary_read)
  {
      salary.push_back(salary_read);
      sum += salary_read;
      ++values_read;
  }
}

功能应该只有一个目的
输入和求和是两个独立的功能 您可以在输入数据后求和 分离功能允许您使用一个功能输入数据,并将数据用于其他目的,而无需每次计算总和。

查看输入迭代器
输入迭代器将允许您告诉编译器生成更有效的代码以从输入流中读取文件。

std::vector为您提供元素数量
您不必计算读入的元素数量 如果您使用std::vector,则可以使用size方法获取值的数量。

简化功能 是的,使用<algorithm>中的工具可以更加紧凑地编写函数,但这些函数说明了基本概念。

std::istream& input_salaries(std::vector<double>& salaries,
                            std::istream& input_file)
{
  double value_from_file = 0.0;
  while (input_file >> value_from_file)
  {
    salaries.push_back(value_from_file);
  }
  return input_file;  // So this function can be "chained"
}

double sum(const std::vector<double>& salaries)
{
  double sum = 0.0;
  const unsigned int number_of_salaries = salaries.size();
  for (unsigned int i = 0; i < number_of_salaries; ++i)
  {
    sum += salaries[i];
  }
  return sum;
}