从.txt文件中读取,计算并输出到txt文件 [英] Read from a .txt file, calculate and output to a txt file

查看:81
本文介绍了从.txt文件中读取,计算并输出到txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,该程序要求用户输入输入infile和outfile文件名。如果无法打开文件,则会出错。如果它可以打开infile,它会读取第一个值并将其视为数组大小。之后,它继续其他数字并存储它们。然后使用插入排序对它们进行排序,并获得均值,中位数和标准偏差。最后,它会在输出文件中打印已排序的数字以及计算值。

我想要做的是删除插入排序,并使用我自己的函数。我喜欢称它为顺序插入,我想使用三个函数。我称之为(find_index),(insert_at)和(find_index)。因此,按顺序插入的方式如下:

按顺序插入意味着您将添加一个名为find_index的函数,该函数将在数组中找到应插入新值的位置。然后它将调用insert_at,它将移动数组的相应现有元素,以便可以将新元素插入其正确的索引中。所以insert_at将由find_index调用,它将由insert_in_order调用。 Insert_in_order读入每个新值并使用它调用find_index,如上所述设置一个链,该链以在右索引处插入的每个元素结束,以便按顺序组装数组。

这是我的意思的一个例子(虽然语法可能不是100%正确)。









Right now, this program asks the user to enter the input infile and outfile file names. Then it gives an error if it can't open the file. If it can open infile, it reads the first value and consider that as the array size. After that it continues with other numbers and stores them. Then it sorts them using insert sort, and get the mean, median, and standard deviation. Finally it prints the sorted numbers along with calculated values in the output file.
What I want to do is to remove that insert sort, and use my own functions. I like to call it insert in order, I want to use three functions for that. I call them (find_index), (insert_at), and (find_index). So, Insert in order works like this:
Insert in order means that you will add a function called find_index that will locate the place in the array that a new value should be inserted. It will then call insert_at, which will move the appropriate, existing elements of the array so that the new element may be inserted in its proper index. So insert_at will be called by find_index, which will be called by insert_in_order. Insert_in_order reads in each new value and calls find_index with it, setting off a chain as described above that ends with each element inserted at the right index so that the array is assembled in order.
Here is an example of what I mean, (the syntax might not be 100% correct though).




While (infile) insertinorder(&infile);
    
    void insertinorder (&infile) {
    
    double nextval;
    
    infile>>nextval;
    
    findindex (nextval);
    
    }
    
    void findindex (nextval) {
    
    int i = 0;
    
    if (lenth ==0) list[0]=maxval; lenth++;
    
    else
    
    while (nextval<list(i)) i++
    
    insertat (nextval, i);
    
    }
    
    void insertat(nextval, i){
    
    int j=0;
    
    if (lenth<max){
    
    for (j=lenth; j>i; j--) list[j]= list (j-1);
    
    list[i]=nextval;
    
    lenth++;
    
    }
    
    }



请记住,这只是一个想法。但如果做得正确,我相信它有效。为什么不使用其他方法?因为我还在学习c ++,但对指针或模板一无所知。另一方面,如果你不想使用poitners和那种东西,你必须提到数组大小。我还不想使用类。所以,你可以简单地阅读(infile)。一旦你到达文件的末尾,它将停止阅读。实际上,那将是按顺序插入(infile)。按顺序插入然后调用查询索引,调用insert at。每次插入值时,以增量插入数组的长度。正如你所看到的那样,没有必要告诉程序这个数组有多大。

虽然不是绝对必要,但我确实喜欢程序给出错误并停止,如果它到达数组200,因此infile不应包含超过200个数字。同样,这不如使用三个函数对数字进行排序那么重要。现在,这是我的问题,我相信这一定必须有效,但无论我做什么,我都无法使其与我的程序一起工作(列出的错误太多)。毫无疑问,我犯了一些错误。任何想法我怎么能使这个东西工作?

这是我的源代码100%工作。现在它仍然使用插入排序算法,并期望第一个数字infile是数组大小。所以,88 66 10,应该是3 88 66 10,这就是我想要改变的。




















Keep in mind that this is just an idea. But if done correctly I am sure it works. Why not use other methods? Because I am still learning c++ and don't no nothing about pointers or templates yet. On the other hand, you'd have to mention the array size, if you don't want to use poitners and that kind of stuff. I don't want to use classes either, yet. So, You can simply read while (infile). Once you get to the end of file it will stop reading. Actually, that would be doing insert in order while (infile). Insert in order then calls find index which calls insert at. Insert at increments the length of the array every time a value is inserted. There is no need for telling the program how large the array is, then, as you can see.
Although not absolutely required, I do like the program to give an error and cease, if it reaches array 200th, so the infile should not contain more than 200 numbers. Again that is not as important as using three functions to sort the numbers. Now, here is my problem, I am sure this must work, but no matter what I do I cannot make this to work with my program(There are too many errors to list). There is no doubt that I am making some mistakes. Any ideas how I can make this thing work?
Here is my source code that works 100%. Right now it is still using insert sort algorithm, and expects the first number infile to be the array size. so, 88 66 10, should be 3 88 66 10, which is what I want changed.









#include <cmath>
      #include <iostream>
      #include <fstream>
      #include <iomanip>
      #include <string>


      using namespace std;

      const int Max_Size = 200;

      void print (ofstream& outdata, double list[], int lenth);
      void insertionSort (ofstream& outdata, double list[], int lenth);
      void median (ofstream& outdata, double list[], int lenth);
      void mean (ofstream& outdata, double list[], int lenth);
      void standard_deviation(ofstream& outdata, double list[], int lenth);
      void readdata(ifstream& indata, double list[], int& lenth, bool& lenthok);



      int main()
      {
          double dataarray[Max_Size];
          int datalenth;
          bool lenthisok;

          ifstream indata;
          ofstream outdata;

          string inputfile;
          string outputfile;

          cout<<"Please enter the input file name:"<<endl;
          cin>>inputfile;

          indata.open(inputfile.c_str());
          if(!indata)
          {
              cout << "\n\nCannot open the input file: " << inputfile <<
                      "\n\n** Please make sure the input file path and name are correct and try again." << endl;

              return 1;
          }

          cout<<"Please enter the output file name: "<<endl;
          cin>>outputfile;

          outdata.open(outputfile);


          {

      readdata(indata, dataarray, datalenth, lenthisok);

      if (lenthisok)
      insertionSort(outdata, dataarray, datalenth);


      else
          cout<<"Lenth of the secret code must be <="<<Max_Size<<endl;

          print (outdata, dataarray, datalenth);
          median(outdata, dataarray, datalenth);
          mean(outdata, dataarray, datalenth);
          standard_deviation(outdata, dataarray, datalenth);

      cout<<"\n\nThe input data has been read from: " << inputfile << "\nand the output data was written to: " << outputfile <<endl;


          }



        indata.close();
        outdata.close();

          return 0;
      }




      void readdata(ifstream& indata, double list[], int& lenth, bool& lenthok)
      {
          int i;
          lenthok = true;

          indata>>lenth;
          if (lenth>Max_Size)
          {
              lenthok = false;
              return;
          }

          for (i=0; i<lenth; i++)
              indata>>list[i];
      }



      void insertionSort(ofstream& outdata, double list[], int lenth)
      {
          int firstoutoforder;
          int location;
          double temp;


          for (firstoutoforder=1; firstoutoforder < lenth; firstoutoforder++)

              if (list[firstoutoforder] < list[firstoutoforder-1])
              {
                  temp = list[firstoutoforder];
                  location = firstoutoforder;

                  do
                  {
                      list[location] = list[location-1];
                      location--;
                  }
                  while (location > 0 && list[location-1] > temp);

                  list[location] = temp;

      }
      }



      void print (ofstream& outdata, double list[], int lenth)
      {
          int i;
          outdata<<"Using insertion sort algorithm, the elements are:"<<endl;
          for (i=0; i<lenth; i++)
              outdata<<list[i]<<" ";
      }



      void median (ofstream& outdata, double list[], int lenth)
      {
          int middle;
          double median;

          middle = lenth / 2;

          if (lenth % 2==0)
          {
              median = (list[middle-1] + list[middle]) / 2;
          }
          else
              median = (list[middle]);

          outdata<<"\nThe median for the elements entered is:"<<median<<endl;

      }



      void mean (ofstream& outdata, double list[], int lenth)
      {
          double sum = 0;
          double average = 0;
          int i;

          for (i=0; i<lenth; i++)
              sum = sum+list[i];
          average = sum/lenth;

          outdata<<"\nThe Mean is:"<<average<<endl;
      }




      void standard_deviation(ofstream& outdata, double list[], int lenth)
      {

          double sum = 0;
          double average = 0;
          double sq_diff_sum = 0;
          double variance = 0;
          double diff = 0;
          int i;
          double deviation = 0;

          for (i=0; i<lenth; i++)
              sum = sum+list[i];
          average = sum/lenth;

          for (i=0; i<lenth; i++)
          {

          diff = list[i] - average;
          sq_diff_sum += diff * diff;
          }

          variance = sq_diff_sum/lenth;
          deviation = sqrt(variance);
          outdata<<"\nThe Standard Deviation is:"<<deviation<<endl;



      }

      //End of code.

推荐答案

以下显示如何使用数组。



The following shows how to use arrays.

//	Shuffle 2 Piles
void CLayoutView::Shuffle2()
{
	CCardDeck tmpArray;  UINT i, j, k;
	tmpArray.SetSize(78,-1);
	while ((UINT)m_cardPile1.GetSize() && (UINT)m_cardPile2.GetSize())
	{
		for (j = 0; j < 78; j++)
		{
			srand(((UINT)time(NULL)* (UINT)time(NULL))/(rand() +1));
			i = ((UINT)rand() * (UINT)m_cardPile1.GetSize())/RAND_MAX;
				tmpArray.SetAt(j, m_cardPile1.GetAt(i));
				tmpArray.SetAt(j+1, m_cardPile2.GetAt(i));
				j++;
			m_cardPile1.RemoveAt(i,1);
			m_cardPile2.RemoveAt(i,1);
		}
	}
	
	m_pTarotCardDeck->RemoveAll();
	m_pTarotCardDeck->SetSize(0,-1);
	m_pTarotCardDeck->SetSize(78,-1);

	for (k = 0; k <78; k++)
	{
		m_pTarotCardDeck->SetAt(k, tmpArray.GetAt(k));
	}
}





你也可以在文本文件中尝试计算分隔符。



You could also try counting delimiters with in the text file.


这篇关于从.txt文件中读取,计算并输出到txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆