#偷懒系列#用计算机辅助做概统作业

来源:互联网 发布:淘宝店铺所在地 编辑:程序博客网 时间:2024/04/30 01:46

缘起:今天概统作业有好多道题,然而一道题100个多数据,算平均数,方差,画图表。我在默默地想,人肉做要做到地老天荒的节奏啊……于是,机智的程序员就写了个小程序。并有了以下偷懒算法:

Step1:把数据文字化

用手机拍照,传到电脑:
这里写图片描述
然后,下载个 汉王OCR(点击跳入下载页面), 把文字转成数据。然后,手动在数据的头部插入数据的总数,如:129
这里写图片描述

Step2:执行文件获得结果

将Step1获得的数据与myComputer.exe(源代码附在后面,程序员们可自行编译)放在同一个目录,然后运行:这里写图片描述

然后就获得了,方差,平均数,及其排序后的数据。
然后机智的程序员画茎叶图的时候就不用一个一个点了……
代码在下面,做题遇到什么新的麻烦东西在更新:)

#include <iostream>#include <fstream>using namespace std;int main(){    ifstream file("in.txt");    cout << "----------probability and statics----------" << endl;    cout << endl;    int how_much;    file >> how_much;    cout << "the amount of data: " << how_much << endl;    double* data = new double[how_much];    double sum = 0;    for (int i = 0; i < how_much; i++) {        file >> data[i];        sum += data[i];    }    // to check whether read in correctly    /*    cout << "the data read in: " << endl;    for (int i = 0; i < how_much; i++) {        cout << data[i] << ' ';    }    */    double mean = sum / how_much;    cout << "the mean of datas is: " << mean << endl;    double variance = 0;    for (int i = 0; i < how_much; i++) {        variance += (data[i] - mean) * (data[i] - mean);    }    variance /= how_much;    cout << "the variance of datas is: " << variance << endl;    double hold;    for (int i = 0; i < how_much - 1; i++) {        for (int j = how_much - 1; j > i; j--) {            if (data[j] < data[j-1]) {                hold = data[j];                data[j] = data[j-1];                data[j-1] = hold;            }        }    }    cout << "the sorted data is:" << endl;    for (int i = 0; i < how_much; i++) {        cout << data[i] << ' ';    }    delete[] data;    return 0;}
3 0
原创粉丝点击