⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hist.cpp

📁 各种算术算法
💻 CPP
字号:
///////////////////////////////////////////
///    This file is edited by hustxdp   ///
///       Time:  2007/10/27   19:13     ///
///       Contact:                      ///
///            qq:249798245             ///
///            E-mail:hustxdp@gmail.com ///
///////////////////////////////////////////

/****************************************************
 *hist-----------生成一個數值數組的直方圖           *
 *                                                  *
 *用法                                              *
 *      hist <file>                                 *
 *其中  file是使用的文件的名字                      *
 ****************************************************/
#include "ia.h"

#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <assert.h>

/*
 *下麵定義了直方圖
 */

const int NUMBER_OF_LINES = 50;  //結果中的行數
const int LOW_BOUND       = 0;   //記錄的最低值
const int HIGH_BOUND      = 99;  //記錄的最高值

/*
 *如果輸出NUMBER_OF_LINES數據,
 *那麽每一項都必須使用下麵的因子
 */
const int FACTOR = ((HIGH_BOUND-LOW_BOUND+1)/NUMBER_OF_LINES);

//製作直方圖的字符值寬度
const int WIDTH = 60;
//用于存儲數據的數組
static infinite_array data_array;
//如果該項在數組中就計數
static int data_items;

void read_data(const char *const name);

int main(int argc, char **argv)
{
	void read_data(const char *const name); //將數據放入數組中
	void print_histogram();                 //輸出數據

	if (argc!=2)
	{
		cerr<<"Error:Wrong number of arguments\n";
		cerr<<"Usage is:\n";
		cerr<<" hist <data_file>\n";
		exit(8);
	}
	data_items = 0;
	read_data(argv[1]);
	print_histogram();
	return 0;

}
/***************************************************
 *read_data  ---- 將數據輸入文件讀入到data_array中 *
 *                                                 *
 *參數                                             *
 *     name  ----要讀取的文件的名字                *
 ***************************************************/
void read_data(const char *const name)
{
  ifstream in_file(name); //輸入文件
  int data;
  if (in_file.bad())
  {
	  cerr<<"Error:Unable to open "<<name<<'\n';
	  exit(8);
  }
  while (!in_file.eof())
  {
     in_file>>data;

	 //如果得到一個EOF.那麽這就是最後一次讀數,數據讀完了
	 if(in_file.eof())
		 break;

	 //此處不需要斷言,因爲data_array是一個ia
	 data_array[data_items] = data;
	 ++data_items;
  }
}
/***************************************************
 *print_histogram()    打印直方圖輸出結果          *
 ***************************************************/
void print_histogram()
{
	//打印輸出的上邊界
	int counters[NUMBER_OF_LINES];
	int low;                 //打印輸出的下邊界
	int out_of_range = 0;    //超過邊界的項數
	int max_count = 0;       //最大的計數器
	float scale;             //輸出點的比例
	int index;               //data的下標

	memset(counters,'\0',sizeof(counters));
	for(index = 0; index<data_items; ++index)
	{
		int data;            //這個點的數據
		data = data_array[index];

		if((data<LOW_BOUND)||(data>HIGH_BOUND))
			++out_of_range;
		else
		{
			//counters數組的下標
			int count_index;
			count_index = static_cast<int>(static_cast<float>(data - LOW_BOUND)/FACTOR);

			assert(count_index>=0);
			assert(count_index<sizeof(counters)/sizeof(counters[0]));
			++counters[count_index];
			if(counters[count_index]>max_count)
				max_count = counters[count_index];	
		}
	}
	scale = float(max_count)/float(WIDTH);

	low = LOW_BOUND;

	for (index = 0; index<NUMBER_OF_LINES; ++index)
	{
		//輸出點時使用的下標
		int char_index;
		int number_of_dots;  //輸出*字符的個數

		cout<<setw(2)<<index<<' '<<
			setw(3)<<low<<"-"<<
			setw(3)<<(low + FACTOR -1)<<"("<<
			setw(4)<<counters[index]<<"):";

		number_of_dots = int(float(counters[index])/scale);
		for(char_index = 0; char_index<number_of_dots; ++char_index)
			cout<<"*";
		low += FACTOR;
	}
	cout<<out_of_range<<" items out of range\n";
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -