-1

我有一个 C++ 程序,可以计算平均值、方差、标准偏差和标记的变异系数,但它只从键盘读取值。我正在寻求一个 c++ 程序的帮助,该程序计算以上所有内容但从计算机读取值(即从计算机读取具有多列的文件和/或表,并像足球联赛表一样独立计算所有列的上述所有内容)是我想要修改的草稿程序以满足上述所有要求吗

#include<iostream.h>
#include<cmath>
#define MAXSIZE 1000
#include<fstream>
using namespace std;

void sd(void);
void average(void);
void variance(void);
void coeff(void);
void all(void);
float x[MAXSIZE];
int i=0, n, choice, c=0;
float avrg=0,var,stdv,cv, sum1=0, sum=0;

main()
{
      cout<<"Enter the numbers (press -200 when finished)\n";      
      int hasrun=0;                       
      for(int i=0; i<MAXSIZE; i++)                        ///INTERCHANGE n WiTH c
                {
                if (hasrun == 0)
                   {
                    cin>>x[i];
                    if ( x[i] == -200 )
                       {
                       hasrun =1;
                       }
                    c++;
                   }
                   if(x[i]<0|x[i]>100)
                        {
                        break;
                        cout<<"\nthe entered value is invalid\n";
                        hasrun =1;
                        }
                }
 for(;;)
       {            
          cout<<"\n\nEnter Your Choice\n"
          "1.Calculate average\n"
          "2.Calculate variance\n"
          "3.Calculate standard deviation\n"
          "4.Calculate the coefficient of variation\n"
          "5.Display all\n"
          "6.end\n\n";          

           cin >> choice;
           int exit = 0;
         switch (choice)
                {
                 case 1: 
                      average();
                      break;
                 case 2:
                      variance();
                      break;
                 case 3:
                      sd();
                      break;
                 case 4:
                      coeff();
                      break;
                 case 5:
                      all();
                      break;
                 case 6: 
                      exit=1;
                      break;
                 default:
                         cout<<"invalid choice"<<endl;
                 }
         if (exit==1){
            break;}
       }     
}

 /* Compute the sum of all elements */
void average(void)
{ 
 sum = 0;    
 cout<<"\tMEAN OF STUDENTS MARKS:\n\t_____________________\n";    
 for(int j=0; j<(c-1); j++)
 {
  sum = sum + x[j];
 }
 avrg = sum /(float)(c-1);

 cout<<"\t\t";
 cout<<avrg;
 cout<<"\n\n";
//system("PAUSE");
}

void variance()
{
sum1=0;
     cout<<"\tVARIANCE OF THE STUDENTS MARKS:\n\t_______________________________\n";
for(i=0; i<(c-1); i++)
                 {
                  sum1 = sum1 + pow((x[i] - avrg),2);
                 }
                 var = (sum1 /(float)(c-1) );

                 cout<<"\t\t\t";
                 cout<<var;
                 cout<<"\n\n";
}

void sd()
{
     cout<<"\tSTANDARD DEVIATION OF THE STUDENTS MARKS:\n\t________________________________\n";
     stdv=sqrt(var);
 cout<<"\t\t\t";
 cout<<stdv;
 cout<<"\n\n";
}
void coeff()
{
    cout<<"\tCOEFFICIENT OF VARIATION:\n\t__________________________\n";
    cv=stdv/avrg;
    cout<<"\t\t\t";
    cout<<cv;
    cout<<"\n\n";
}
void all()
{
    cout<<"\tDISPLAY ALL:\n\t********************\n";
 cout<<"average=";
 cout<<avrg;
 cout<<"\n\n";
     cout<<"variance=";
     cout<<var;`enter code here`
     cout<<"\n\n";
         cout<<"standard deviation=";
         cout<<stdv;
         cout<<"\n\n";
             cout<<"coefficient of variation=";
             cout<<cv;
             cout<<"\n\n";
}

这是我自己的工作,我只是一个喜欢尝试新事物的学生,我自己教了上述大部分功能。帮助............

4

2 回答 2

0

C++ 流库的美妙之处在于,无论数据来自何处,它们的行为都是相同的。在您的情况下,您只需要阅读fstream库,并从文件而不是标准输入中读取数据。

如果 youf 文件是基于行的,我将为您节省一个进一步的问题,并告诉您您可能也想阅读有关std::getline()函数的信息。

于 2013-07-17T22:45:24.593 回答
0

使用标准库中的文件流类。您需要包括<fstream>. 然后创建一个输入文件流以从您的文件中读取:

ifstream fin( "myfile.txt" );
if( !fin ) {
     cerr << "Failed to open file\n";
}

使用文件流的方式与使用标准输入流 ( ) 完全相同cin

于 2013-07-17T22:45:31.840 回答