📄 statistics.cpp
字号:
#include <fstream.h>
ifstream finUpdate("update.txt");
ifstream finLookup("lookup.txt");
ifstream finDiscRatio("DiscRatio.txt");
ifstream finSucRatio("SucRatio.txt");
ofstream fout("Statistics.txt");
struct Node
{
int num;
Node* nextNode;
};
void calculate(ifstream &fin, int grain, bool needAverage);
void main()
{
fout<<"update ram access times:"<<endl;
calculate(finUpdate, 1, true);
fout<<"lookup ram access times:"<<endl;
calculate(finLookup, 1, true);
fout<<"lookup success ratio:"<<endl;
calculate(finSucRatio, 10, false);
fout<<"lookup disc ratio:"<<endl;
calculate(finDiscRatio, 10, false);
}
void calculate(ifstream &fin, int grain, bool needAverage)
{
int num, maxNum, sumLevel;
int index;
Node* headNode, *newNode, *curNode;
unsigned int total, globalSum;
unsigned int* sum;
fin>>num;
headNode = new Node;
headNode -> num = num;
headNode -> nextNode = NULL;
newNode = headNode;
maxNum = num;
while(true)
{
num = -1;
fin>>num;
if(num == -1)
break;
else
{
newNode -> nextNode = new Node;
newNode = newNode -> nextNode;
newNode -> num = num;
newNode -> nextNode = NULL;
if(num > maxNum)
maxNum = num;
}
}
total = globalSum = 0;
sumLevel = maxNum / grain + 1;
sum = new unsigned int[sumLevel];
for(index = 0; index < sumLevel; index++)
sum[index] = 0;
curNode = headNode;
while(curNode)
{
sum[curNode -> num / grain]++;
total ++;
globalSum += curNode -> num;
curNode = curNode -> nextNode;
}
for(index = 0; index < sumLevel; index++)
{
if(grain > 1)
fout<<"from "<<index*grain<<" to "<<(index+1)*grain-1<<":\t"
<<sum[index]<<"\t"<<((double)100*sum[index])/total<<"%"<<endl;
else
fout<<index*grain<<":\t"<<sum[index]<<"\t"<<((double)100*sum[index])/total<<"%"<<endl;
}
fout<<"total:"<<total<<endl;
if(needAverage)
fout<<"average:"<<(double)globalSum/total<<endl;
fout<<endl;
curNode = headNode -> nextNode;
while(curNode)
{
headNode -> nextNode = curNode -> nextNode;
delete curNode;
curNode = headNode -> nextNode;
}
delete headNode;
delete sum;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -