//主要算法,二叉排序树的查找
//以出现的各字符构成一棵二叉排序树,针对每一个字符在二叉树中查找,如果找到了就增加计数,否则就插入#include<iostream>#include<string>#include<fstream>using namespace std;typedef char KeyType;struct BstNode { KeyType data; int num; BstNode *lch,*rch;};class BstTree{private: BstNode *root; BstNode* insert(BstNode *t,BstNode *s); void inorder(ofstream& outfile,BstNode *p); void deletenode(BstNode *p);public: BstTree(){root=NULL;} ~BstTree(){delete(root);root=NULL;} void read_creat(ifstream& infile); void print_inorder(ofstream& outfile) {inorder(outfile,root);}};BstNode* BstTree::insert(BstNode *t,BstNode *s){ if(t==NULL){t=s;t->num=1;} else if(t->data==s->data){delete(s);s=NULL;t->num++;} else { if(t->data>s->data)t->lch=insert(t->lch,s); else t->rch=insert(t->rch,s); } return t;}void BstTree::read_creat(ifstream& infile){ char ch; infile.get(ch); while(infile) { BstNode *s;s=new BstNode;s->data=ch;s->num=1;s->lch=s->rch=NULL; root=insert(root,s); infile.get(ch); }}void BstTree::deletenode(BstNode *p){ if(p!=NULL) { deletenode(p->lch);deletenode(p->rch); delete p; }}void BstTree::inorder(ofstream& outfile,BstNode *p){ if(p!=NULL) { inorder(outfile,p->lch); outfile<<p->data<<" "<<p->num<<endl; inorder(outfile,p->rch); }}void main(){ ifstream infile;ofstream outfile;infile.open("E:\c++.txt"); if(!infile){cout<<"open file failure!n";return;} outfile.open("E:\b++.txt"); BstTree tree; tree.read_creat(infile); tree.print_inorder(outfile); infile.close();outfile.close();}
读取文本中的字符并计算出各字母出现的次数
//读取文本中的字符并计算出各字母出现的次数#include<iostream>#include<fstream>#include<cctype>using namespace std;void initialize(int& lc,int list[]);void copyText(ifstream& intext,ofstream& outtext,char& ch,int list[]);void characterCount(char ch,int list[]);void writeTotal(ofstream& outtext,int lc,int list[]);int main(){ int lineCount; int listLetter[26]; char ch; ifstream infile; ofstream outfile; infile.open("C:\c++.txt"); if(!infile) { cout<<"Cannot open input file."<<endl; return 1; } outfile.open("C:\b++.txt"); initialize(lineCount,listLetter); infile.get(ch); while(infile) { copyText(infile,outfile,ch,listLetter); lineCount++; infile.get(ch); } writeTotal(outfile,lineCount,listLetter); infile.close(); outfile.close(); return 0;}void initialize(int& lc,int list[]){ int j; lc=0; for(j=0;j<26;j++) list[j]=0;}void copyText(ifstream& intext,ofstream& outtext,char& ch,int list[]){ while(ch!='n') { outtext<<ch; //输出原文本的信息 characterCount(ch,list); intext.get(ch); } outtext<<ch; //原文本断行时新文本也断行}void characterCount(char ch,int list[]){ int index; ch=toupper(ch); index=static_cast<int>(ch)-65; if(0<=index && index<26) list[index]++;}void writeTotal(ofstream& outtext,int lc,int list[]){ int index; outtext<<endl<<endl; outtext<<"The number of lines="<<lc<<endl; for(index=0;index<26;index++) outtext<<static_cast<char>(index+65)<<" count = "<<list[index]<<endl;}/*sampleC:\c++.txt:fwwwewefqtgqtggrqgrgqeerghhwhwewhtfwqfqgqrewgqegqrgergqegrqcwoifuqiwofjeiopjwesvjxvzvmxnbdmeioejgeorigjqioadf;klsajfpoiqfew;fjwf54651844fwgqrwegergqwereaC:\b++.txt:fwwwewefqtgqtggrqgrgqeerghhwhwewhtfwqfqgqrewgqegqrgergqegrqcwoifuqiwofjeiopjwesvjxvzvmxnbdmeioejgeorigjqioadf;klsajfpoiqfew;fjwf54651844fwgqrwegergqwereaThe number of lines=6A count = 3B count = 1C count = 1D count = 2E count = 19F count = 12G count = 17H count = 4I count = 7J count = 7K count = 1L count = 1M count = 2N count = 1O count = 7P count = 2Q count = 16R count = 11S count = 2T count = 3U count = 1V count = 3W count = 17X count = 2Y count = 0Z count = 1*/
原文链接: https://www.cnblogs.com/mjc467621163/archive/2011/08/23/2151014.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/31068
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!