您的位置:首页 > 其它

一个简单的文件处理--16进制数据统计分析

2011-03-03 23:21 751 查看
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const char * str1="offset_lr < offset: ";
const char * str2="offset_lr > offset: ";
#define BUFFERSIZE 128
char StrBuffer[BUFFERSIZE];
#define SIZE 49
int SMALL_COUNT[SIZE];
int LARGE_COUNT[SIZE];
    double average=0.0;
    double SMALL_SUM=0;
    double LARGE_SUM=0;
    int totalCount=0;
        int strSize=20;
int findAndCount(const char * searchStr,int arr[SIZE] ,bool Large)
{
        char * str;
    char ch;
    int tmp=0;
    FILE * f=fopen("test.txt","rb");
    if(f==NULL)
    {
        return (-1);
    }
    else
    {
        while(fread(StrBuffer,1,BUFFERSIZE,f)==BUFFERSIZE)
        {
            str=StrBuffer;
        if ((str=strstr(str,searchStr))!=NULL)
        {
            bool exitFlag=false;
            for(int i=2;i<8;i++)
            {
                ch=*(str+strSize+i);
                printf("%c",ch);
                if(ch=='/0')
                {
                    exitFlag=true;
                    break;
                }
            }
            if(exitFlag)
                continue;
                ch=*(str+strSize+8);
                if(ch=='/0')
                    continue;
                printf("%c",ch);
                if(ch<='9')
                    tmp=(ch-'0')*16;       
                else
                    tmp=(ch-'A'+10)*16;
                ch=*(str+strSize+9);
                if(ch=='/0')
                    continue;
                printf("%c",ch);
                if(ch<='9')
                    tmp+=(ch-'0');       
                else
                    tmp+=(ch-'A'+10);
                if(tmp<SIZE)
                {    arr[tmp]++;
                    printf("/n%d",arr[tmp]);
                }
                printf("/n");
            }
        }
        for(int i=0;i<SIZE;i++)
        {   
            totalCount+=arr[i];
            if(Large)
                LARGE_SUM+=(arr[i]*i);
            else
                SMALL_SUM+=(-arr[i])*i;
            printf("%d:%d/n",i,arr[i]);
        }
        fclose(f);
    }
}
int _tmain(int argc, _TCHAR* argv[])
{
    for(int i=0;i<SIZE;i++)
    {
        SMALL_COUNT[i]=0;
        LARGE_COUNT[i]=0;
    }
    printf("%d/n",strSize);
    findAndCount(str2,LARGE_COUNT,true);
    printf("LARGE_SUM:%lf,SMALL_SUM:%lf/n",LARGE_SUM,SMALL_SUM);
    system("pause");
    findAndCount(str1,SMALL_COUNT,false);   
    printf("LARGE_SUM:%lf,SMALL_SUM:%lf/n",LARGE_SUM,SMALL_SUM);
    average=(LARGE_SUM+SMALL_SUM)/totalCount;
    printf("Total Count:%d/nAverage:%lf/n",totalCount,average);
    system("pause");
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐