dice_analyzer.cpp
上传用户:jtjnyq9001
上传日期:2014-11-21
资源大小:3974k
文件大小:3k
源码类别:

3G开发

开发平台:

Visual C++

  1. //
  2. //  File = dice_analyzer.cpp
  3. //
  4. #include <stdlib.h>
  5. #include <fstream>
  6. #include "parmfile.h"
  7. #include "dice_analyzer.h"
  8. #include "typedefs.h"
  9. #include "model_graph.h"
  10. #include "syst_graph.h"
  11. extern ParmFile *ParmInput;
  12. extern ofstream *DebugFile;
  13. extern SystemGraph CommSystemGraph;
  14. extern int PassNumber;
  15. extern int MaxPassNumber;
  16. //======================================================
  17. DiceAnalyzer::DiceAnalyzer( char* instance_name,
  18.                         PracSimModel* outer_model,
  19.                         Signal<byte_t>* symb_seq )
  20.           :PracSimModel(instance_name,
  21.                         outer_model)
  22. {
  23.   MODEL_NAME(DiceAnalyzer);
  24.   In_Sig = symb_seq;
  25.   OPEN_PARM_BLOCK;
  26.   GET_INT_PARM(Report_Intvl_In_Blocks);
  27.   GET_INT_PARM(Faces_Per_Die);
  28.   MAKE_INPUT(In_Sig);
  29.   Symb_Count = new int[Faces_Per_Die];
  30.   Double_Count = new int[Faces_Per_Die*Faces_Per_Die];
  31. }
  32. //======================================================
  33. DiceAnalyzer::~DiceAnalyzer( void ){ };
  34. //======================================================
  35. void DiceAnalyzer::Initialize(void)
  36. {
  37.    int i;
  38.    Block_Size = In_Sig->GetBlockSize();
  39.    for(i=0; i<Faces_Per_Die; i++)
  40.       Symb_Count[i] = 0;
  41.    for(i=0; i<Faces_Per_Die*Faces_Per_Die; i++)
  42.       Double_Count[i] = 0;
  43. };
  44. //======================================================
  45. int DiceAnalyzer::Execute()
  46. {
  47.    byte_t *in_byte_ptr, in_byte;
  48.    byte_t old_byte;
  49.    int is, i,j;
  50.    in_byte_ptr = GET_INPUT_PTR( In_Sig );
  51.    for( is=0; is < Block_Size; is++)
  52.    {
  53.       in_byte = *in_byte_ptr++;
  54.       Symb_Count[in_byte-1]++;
  55.       if(is%2)
  56.       {
  57.          Double_Count[old_byte*Faces_Per_Die + in_byte-1]++;
  58.       }
  59.       else
  60.       {
  61.          old_byte = in_byte-1;
  62.       }
  63.      
  64.    }
  65.    if( (PassNumber % Report_Intvl_In_Blocks) == 0)
  66.    {
  67.       float total_count = float(Symb_Count[0]);
  68.       for( i=1; i<Faces_Per_Die; i++)
  69.          total_count += Symb_Count[i];
  70.       BasicResults << Instance_Name << ": "
  71.               << PassNumber << "  symbol %'s: "
  72.               << (Symb_Count[0]/total_count);
  73.       for( i=1; i<Faces_Per_Die; i++)
  74.       {
  75.          BasicResults << ", " << (Symb_Count[i]/total_count);
  76.       }
  77.       BasicResults << "n" << endl;
  78.       total_count = float(Double_Count[0]);
  79.       for( i=1; i<Faces_Per_Die*Faces_Per_Die; i++)
  80.          total_count += Double_Count[i];
  81.       for( i=0; i<Faces_Per_Die; i++)
  82.       {
  83.          BasicResults << (Double_Count[i*Faces_Per_Die]/total_count);
  84.          for( j=1; j<Faces_Per_Die; j++)
  85.          {
  86.             BasicResults << ", " << (Double_Count[i*Faces_Per_Die+j]/total_count);
  87.          }
  88.          BasicResults << "n" << endl;
  89.       }
  90.    }
  91.    return(_MES_AOK);
  92. }