C++實現詞典管理程序

一、想法來源:(源於百度之星程序設計大賽)

      題目描述:請編寫程序,根據指定的對應關係,把一個文本中的字符串替換成另外的字符串。

      輸入數據:程序讀入已被命名爲text.txt和dict.txt的兩個輸入數據文本文件,text.txt爲一個包含大量字符串(含中文)的文 本,以whitespace爲分隔符;dict.txt爲表示字符串(s1)與字符串(s2)的對應關係的另一個文本(含中文),大約在1萬行左右,每行 兩個字符串(即s1和s2),用一個\t或空格分隔。dict.txt中各行的s1沒有排序,並有可能有重複,這時以最後出現的那次s1所對應的s2爲 準。text.txt和dict.txt中的每個字符串都可能包含除whitespace之外的任何字符。text.txt中的字符串必須和 dict.txt中的某s1完全匹配才能被替換。(爲便於調試,您可下載測試text.txt和dict.txt文件,實際運行時我們會使用不同內容的輸 入文件。)  

二、  主要功能:

             1、翻譯文檔;

           2、添加單詞;

           3、查找單詞;

           4、修改詞典

三、思路:使用C++容器,map。Map是一類平衡二叉樹,是一種關聯容器。在插入、刪除等具有較小的時間複雜度。

四、Map的用法

  1、包含的頭文件:

#include<map>
  2、 創建map容器:

map<string,string> ditcmap;   /*表示創建一個map類的容器,名稱爲dictmap。同時兩個string表示的是一個對象所包含的兩個屬性是string類型。若是定義的類型爲map<int,int>dictmap;則是表示爲兩個屬性是int類型。*/
  3、 插入一條記錄

dictmap.insert(make_pair(str1, str2)) ;//插入一條記錄,包含兩個屬性爲str1和str2 。

  4、刪除記錄

dictmap.erase(str);//刪除與str有關的記錄,str表示的是屬性1的關鍵字。
  5、輸出所有記錄

 map<string, string>::iterator it;   //定義一個遍歷器
	for (it=dictmap.begin();it!=dictmap.end();it++)
	{
		cout<<it->first.c_str()<<"\t"<<it->second.c_str()<<endl;
	}
 //it=dictmap.begin(),表示從第一條記錄開始;it!=dictmap.end(),如果沒有結束,則是繼續進行循環;it++,遍歷器增加。
  6、刪除一條記錄

map<string,string>::iterator mapIter;
     if ((mapIter=dictmap.find(str))!=dictmap.end())
     {
         cout<<mapIter->first.c_str()<<"\t"<<mapIter->second.c_str()<<endl;
     }
/*定義遍歷器mapIter,dictmap.find(str),返回的是str所在的地址,賦值給mapIter,mapIter判斷是不是在結尾,如果不是的話輸出查找的內容。*/

五、總體框架

   1、需要的幾個變量

 struct data 
{
	string EN;
	string CN;
};
data DT;    //存儲字典記錄。

int choice;  
bool t=false;
string str1, str2,str3;
map<string, string> dictmap;
string str; 
const char *p;
   2、包含幾個主要的函數

int Read();  //從文件中讀取字典數據
void Menu();  //主界面菜單
void Do();  //選擇菜單的操作
void Translate(); //翻譯文件中的內容
void Add();  //在字典中增加記錄
void Search();  //在字典中查找
void Change();  //在字典中替換記錄
void Save(const char *p);  //把字典保存在文件中

六、源代碼

#pragma warning(disable : 4786)
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <ctime>
#include <map>
#include <fstream>
#include <string>
using namespace std;

struct data 
{
	string EN;
	string CN;
};
data DT;

int choice;
bool t=false;
string str1, str2,str3;
map<string, string> dictmap;
string str; 
const char *p;

int Read();
void Menu();
void Do();
int main();
void Translate();
void Add();
void Search();
void Change();
void Save(const char *p);

int Read()
{
	cout<<"請輸入字典的的存儲路徑:"<<endl;
    cin>>str;
    p=str.c_str();
	FILE *fp;
	fp=fopen(p,"r");
	char Line[1024];
	char *ppos = NULL;
	while ( fgets(Line, 1024, fp) != NULL )
	{
		
		if ( (ppos = strchr(Line, ' ')) == NULL 
			&& (ppos = strchr(Line, '\t')) == NULL )
		{
			continue;
		}
		*ppos++ = '\0';
		
		if ( ppos[strlen(ppos) - 1] = '\n' )
		{
			ppos[strlen(ppos) - 1] = '\0';
		}
		DT.EN = Line;
		DT.CN = ppos;
		map<string, string>::iterator it;
		if (( it=dictmap.find(DT.EN)) != dictmap.end() )
        {
			dictmap.erase(DT.EN);
        }
		dictmap.insert(make_pair(DT.EN, DT.CN));
     }
	
	map<string, string>::iterator it;
	for (it=dictmap.begin();it!=dictmap.end();it++)
	{
		cout<<it->first.c_str()<<"****"<<it->second.c_str()<<endl;
	}
	system("pause");
	fclose(fp);
	return 0;
}

void Save(const char *p)
{
	ofstream fout(p);
	map<string,string>::iterator it;
	for (it=dictmap.begin();it!=dictmap.end();it++)
	{
		fout<<it->first.c_str()<<"                       "<<it->second.c_str()<<endl;

	}
	fout.close();
	cout<<"保存成功 !"<<endl;
}

void Add()
{
     cout<<"請輸入兩個字符串,以空格分隔:"<<endl;
     cin>>str1>>str2;
     map<string,string>::iterator mapIter;
     if ((mapIter=dictmap.find(str1))!=dictmap.end())
     {
         cout<<"此詞已經存在!"<<endl;
     }
     else
     {
		 dictmap.insert(make_pair(str1,str2));
		 t=true;
     }
}

void Search()
{
     cout<<"請輸入需要查找的單詞:"<<endl;
     cin>>str1;
     map<string,string>::iterator mapIter;
     if ((mapIter=dictmap.find(str1))!=dictmap.end())
     {
         cout<<"\""<<str1<<"\""<<"  在字典中翻譯爲: "<<"\""<<mapIter->second.c_str()<<"\""<<endl;
     }
     else
     {
         cout<<"此詞不存在字典中!"<<endl;
     }
}

void Menu()
{
	system("cls");
    cout<<"\t================================================"<<endl;
    cout<<"\t*                                              *"<<endl;
    cout<<"\t*              1、翻譯文檔                     *"<<endl;
    cout<<"\t*              2、添加單詞                     *"<<endl;
    cout<<"\t*              3、查找單詞                     *"<<endl;
    cout<<"\t*              4、修改字典                     *"<<endl;
    cout<<"\t*              0、退    出                     *"<<endl;
    cout<<"\t*                                              *"<<endl;
    cout<<"\t================================================"<<endl;
    cout<<"請輸入您的選項:";
    cin>>choice;
    while(choice<-1||choice>4)
    {
        cout<<"輸入錯誤!"<<endl<<"請重新輸入:"<<endl;
        cin>>choice;
    }
    Do();
}

void Do()
{
	char a;
    switch(choice)
    {
        case 1:
        system("cls");
        Translate();
        system("pause");
        system("cls");
        Menu();
        break;
        case 2:
        system("cls");
        Add();
        system("pause");
        system("cls");
        Menu();
        break;
        case 3:
        system("cls");
        Search();
        system("pause");
        system("cls");
        Menu();
        break;
        case 4:
        system("cls");
        Change();
        system("pause");
        system("cls");
        Menu();
        break;
        case 0:
			if (t)
			{
				cout<<"是否保存 (Y/N)?"<<endl;
				cin>>a;
				if(a=='Y')
				{
					Save(p);
				}
			}
			exit(0);
			break;
    }
}

void Translate()
{
	string str_1,str_2;
	const char *p1,*p2;
	cout<<"請輸入翻譯原文路徑:"<<endl;
	cin>>str_1;
    p1=str_1.c_str();
	cout<<"請輸入譯文存儲路徑:"<<endl;
	cin>>str_2;
	p2=str_2.c_str();
	ifstream fin2(p1);
	ofstream fout1(p2);
	if (!fin2)
	{
		cout<<"打開翻譯源文件失敗 !"<<endl;
	}
	if (!fout1)
	{
		cout<<"創建譯文文件失敗 !"<<endl;
	}
     map<string, string>::iterator mapIter;
     string strText;
	 while (fin2)
	 {
		 fin2>>strText;
		 if ( (mapIter = dictmap.find(strText)) != dictmap.end() )
		 {
			 fout1<<mapIter->second.c_str()<<"\t";
		 }
		 else
		 {
			 fout1<<strText.c_str()<<"\t";
		 }
	 }
	 cout<<"翻譯成功 !"<<endl;
}

void Change()
{
	string chgf,chgt;
	cout<<"請輸入您要替換的單詞:"<<endl;
	cin>>chgf;
	map<string,string>::iterator mapIter;
	if ((mapIter=dictmap.find(chgf))!=dictmap.end())
	{
		cout<<"\""<<chgf<<"\""<<"  在字典中翻譯爲: "<<"\""<<mapIter->second.c_str()<<"\""<<endl;
		cout<<"請輸入您所要翻譯成的意思:";
		cin>>chgt;
		dictmap.erase(chgf);
		dictmap.insert(make_pair(chgf, chgt));
		cout<<"修改成功!"<<endl;
		cout<<"\""<<chgf<<"\""<<"  在字典中翻譯成爲:  "<<"\""<<chgt<<"\""<<endl;
		t=true;
	}
	else
	{
		cout<<"此詞不存在字典中!"<<endl;
		return;
    }
}

int main()
{
    Read();
    Menu();
    return 1;
}


以上成果是小組的共同努力!


附:程序源文件

發佈了27 篇原創文章 · 獲贊 13 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章