登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

秒大刀 博客

好好学习 天天向上

 
 
 

日志

 
 
 
 

5.9-11  

2006-03-28 00:15:06|  分类: C/C++ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

//问题描述:  Read a sequence of words from input. Use Quit as a word
// that terminates the input. Print the words in the order they were
// entered. Don't print a word twice. Modify the program to sort the
// words before printing them.
//备注:     
//作者:      杨明哲
//完成日期:  2005-03-25
//BUG报告;  dearymz@163.com


#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

namespace ymz
{
 struct Pair
 {
  string word;//要统计的单词
  int counter;//该单词出现的次数
 };
 typedef vector<Pair>::const_iterator icPair;
 typedef vector<Pair>::iterator iPair;
 const int REDUNDANCE = -1;//冗余标志

 //Pair 结构体的比较函数
 int PairCompare(const Pair& p1, const Pair& p2)
 {
  if(p1.word == p2.word)
   return 0;
  else if(p1.word > p2.word)
   return 1;
  else
   return -1;
 }
 //Pair 结构体的比较函数
 bool operator==(const Pair& p1, const Pair& p2)
 {
  return PairCompare(p1, p2) == 0;
 }
 //Pair 结构的比较函数
 bool operator<(const Pair& p1, const Pair& p2)
 {
  return PairCompare(p1, p2) < 0;
 }
 //对words中的单词进行个数统计。重复的用“REDUNDANCE”标识
 void Count(vector<ymz::Pair>& words)//个数统计
 {
  bool findFlag;
  for(unsigned int i = 0; i < words.size(); i++)
  {
   findFlag = false;
   for(unsigned int j = 0; j < i; j++)
   {
    if(words[j].counter != REDUNDANCE && words[i] == words[j])//以前有重复的
    {
     findFlag = true;
     words[j].counter++;//前面的计数器增加
     words[i].counter = REDUNDANCE;//设置删除标志,以后可以将这个对象删除的
    }
    else//以前没有重复的
    {
     words[i].counter = 1;//这应该是第一次出现的单词
    }
   }
   if(findFlag)
    words[i].counter = REDUNDANCE;
  }
 
 }
 //将words中用“REDUNDANCE”标识的对象去除
 void Unrepeatable(vector<ymz::Pair>& words) //去除重复
 {
  for(int i = static_cast<int>(words.size()) - 1; i >=0; i--)
  {
   if(words[i].counter == REDUNDANCE)
   {
    words.erase(words.begin() + i);
   }
  }
  //for(iPair it = words.)
 }
 //将pair对象格式化输出到流
 ostream& operator<<(ostream& ostr, const Pair& pair)
 {
  if(pair.counter == REDUNDANCE)
   ostr<<"[重复]";
  else
   ostr<<pair.counter;
  ostr<<"\t"<<pair.word;
  return ostr;
 }
 //对words进行按word属性的字母表顺序进行排序
 void Sort(vector<ymz::Pair>& words)
 {
  sort(words.begin(), words.end(), ymz::operator<);
 }
}

const char TERMINATE[] = "Quit";
const int bufSize = 64;
void inputWords(vector<ymz::Pair>& words)
{
 ymz::Pair p;
 p.counter = 1;

 cout<<"请输入单词,每行一个。(键入"<<TERMINATE<<"退出)"<<endl;
 char buf[bufSize + 1];//输入缓冲区
 buf[bufSize] = '\0';
 while(true)
 {
  cin.getline(buf, bufSize);
  if(strcmp(buf, TERMINATE) == 0)
   break;
  p.word = buf;
  words.push_back(p);
 }
}

//
////插入单词的宏定义
//#define ADD(a) \
// p.word = a;\
// pairs.push_back(p)

void main()
{
 //初始化要查找的对象
 vector<ymz::Pair> pairs;
 /*ymz::Pair p;
 ADD("A");
 ADD("B");
 ADD("A");
 ADD("A");
 ADD("C");
 ADD("B");*/
 inputWords(pairs);
 for(ymz::iPair it = pairs.begin(); it != pairs.end(); it++)
  it->counter = 1;

 //开始处理
 cout<<"要处理的单词为:"<<endl;
 for(it = pairs.begin(); it != pairs.end(); it++)
  cout<<*it<<endl;

 ymz::Sort(pairs);
 cout<<"排序后的单词为:"<<endl;
 for(it = pairs.begin(); it != pairs.end(); it++)
  cout<<*it<<endl;

 ymz::Count(pairs);
 cout<<"计数后的单词为:"<<endl;
 for(it = pairs.begin(); it != pairs.end(); it++)
  cout<<*it<<endl;

 ymz::Unrepeatable(pairs);
 cout<<"去掉重复后的单词为:"<<endl;
 for(it = pairs.begin(); it != pairs.end(); it++)
  cout<<*it<<endl;

 cin.get(); 
}

  评论这张
 
阅读(843)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018