读取文件中的数据,并且计算其平均值和标准偏差
2010-12-21 18:59
471 查看
#include <fstream> #include <string> #include <assert.h> #include <iostream> #include <vector> #include <math.h> using namespace std; int main(int argc, char* argv[]) { double data; vector<double> Vdata; // file contains an undermined number of integer values ifstream fin; // declare stream variable name fin.open(argv[1]/*"D://test//1.txt/0"*/,ios::in); // open file assert (!fin.fail()); fin >> data; // get first number from the file (priming the input statement) // You must attempt to read info prior to an eof( ) test. while (!fin.eof( )) //if not at end of file, continue reading numbers { Vdata.push_back(data); cout<<data<<endl; //print numbers to screen fin >> data; //get next number from file } fin.close( ); //close file double sum=0; int count=0; for (vector<double>::iterator itr=Vdata.begin();itr!=Vdata.end();++itr) { ++count; sum+=*itr; } double avg=sum/count; //cout<<avg<<endl; double stdDev2=0; double stdDev=0; for (vector<double>::iterator itr=Vdata.begin();itr!=Vdata.end();++itr) { stdDev2+=pow((*itr-avg),2); } stdDev=sqrt(stdDev2/(count-1)); cout<<"Average="<<avg<<" "<<"Standard Deviation="<<stdDev<<endl; cout<<"Relative Standard Deviation="<<stdDev/avg*100<<"%"<<endl; return 0; }
相关文章推荐
- 从一个磁盘文件中读取数据计算平均值,最后把计算结果放到另一个磁盘文件中(fscanf(),fprintf()函数应用)
- 很棒的!从标准输入文件流读取多行数据并且存放到文件---两种实现;标准输入流到文件再到标准输出流
- 计算数据的平均值、方差和标准差
- 读TXT文件中的数据并且计算期望方差
- C语言之文件操作07——读取文件数据并计算均值方差标准差
- java 数据与文本文件存储读取,文件大小计算。
- 简洁的从txt文件中读取数据并且修改数据重新存入
- 将文件a.txt 中的数据读出来并且计算出来 将结果保存到文件b.txt
- python3将csv文件中的两列数据读取出来,并且按行写入txt文本之中
- JAVA写个东西读取TXT中的数据 且要计算出平均值和总值 最后还要按总值排序
- 计算一组离散数据的标准偏差和中位数。函数的构建
- 从本地目录读取指定.txt文件,并且对数据进行解析。
- 设计一个学生类,其中包含学号、姓名、成绩等数据成员,创建学生对象并且倒入到文件file.txt,然后由文件读取到另一个学生对象并输出,试编程实现。
- 从标准输入流中读取数据,并存储到文件中
- shell脚本从文件中按行读取数据,并且赋值到数组中的几种方法
- Java 实现按行读取文件并且将行中的重复数据删除
- java 标准IO、文件中读取数据(上)
- Mean and Standard Deviation-从文件中读取数据计算其平均数和标准差
- c++输入文件处理按行读取并且将实现对一行数据分别处理
- 读取含有json格式数据的txt文件 并且把数据保存到数据库