如何计算读取文件时的百分比?

时间:2013-11-24 23:02:04

标签: c++ c

我想知道在从文件流中读取数据时如何计算百分比。我试过这种方式,我得到的只是零

typedef struct Counter{
    int range1,range2,range3,range4;
    double preset1 ,preset2 ,preset3 ,preset4;
}  countType;

    void analysis_range(double d, countType &ctp) {

        // from 0 to 1.00 KM
        if (d >= 0.00 && d <= 1.00) {
            ctp.range1 += 1;
            ctp.preset1 = ((ctp.range1 / (ctp.range1 + ctp.range2 + ctp.range3 +
                                          ctp.range4)));
        }
        // from 1.00 to 2.00 KM
        else if (d > 1.00 && d <= 2.00) {
            ctp.range2 += 1;
            ctp.preset2 = ((ctp.range2 / (ctp.range1 + ctp.range2 + ctp.range3 +
                                          ctp.range4))) *
                          10;
        }
        // from 1.00 to 2.00 KM
        else if (d > 2.00 && d <= 5.00) {
            ctp.range3 += 1;
            ctp.preset3 = ((ctp.range3 / (ctp.range1 + ctp.range2 + ctp.range3 +
                                          ctp.range4))) *
                          10;
        }
        // grater than 5.00 KM
        else if (d > 5.00) {
            ctp.range4 += 1;
            ctp.preset4 = ((ctp.range4 / (ctp.range1 + ctp.range2 + ctp.range3 +
                                          ctp.range4))) *
                          10;
        }
    }

    void ProcesData(int rank, int numProcs) {

        static countType count;
        MPI_Datatype recType = createRecType();
        // read file and populate the vectors
        ifstream foodbankFile("foodbanks.dat");
        ifstream residenceFile("residences.dat");

        // populate datavector
        std::vector<Foodbank> foodbankData(
            (std::istream_iterator<Foodbank>(foodbankFile)),
            std::istream_iterator<Foodbank>());

        Residence res;
        int numLines = 0;

        while (!residenceFile.eof()) {
            residenceFile >> res.x >> res.y;

            if (numLines % numProcs == rank) {
                // call the  process
                // populate_distancesVector(res,foodbankData);
                analysis_range(populate_distancesVector(res, foodbankData),
                               count);
            }
            ++numLines;
        }

        std::cout << "for Rank" << rank << ",from 0 to 1.00 KM:" << count.range1
                  << ",%" << count.preset1
                  << ",from 1.00 to 2.00 KM:" << count.range2 << ",%"
                  << count.preset2 << ",from 2.00 to 5.00 KM:" << count.range3
                  << ",%" << count.preset3
                  << ",grater than 5.00 KM:" << count.range4 << ",%"
                  << count.preset3 << std::endl;
    }

2 个答案:

答案 0 :(得分:2)

您可能得到零,因为ctp.range1到ctp.range4不是浮点值。如果将整数除以整数,结果也会变为整数。将其中一个操作数转换为float或double,如下所示:

ctp.preset1 = (((float) ctp.range1/(ctp.range1+ctp.range2+ctp.range3+ctp.range4)));

答案 1 :(得分:1)

所以,据我所知,ctp.preset1假定根据ctp.range1中的计数保持范围1的百分比。

如果您使用的是整数,那么你要归零的原因是:

(ctp.range1/(ctp.range1+ctp.range2+ctp.range3+ctp.range4))

对于有意义的范围计数,这将始终评估为零,因为整数数学会截断。将其更改为:

(ctp.range1/double(ctp.range1+ctp.range2+ctp.range3+ctp.range4))

一切都应该没问题。