C ++数据结构程序可按不同字段搜索记录 [英] c++ data structures program to search records by different fields
本文介绍了C ++数据结构程序可按不同字段搜索记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Books.txt文件,看起来像这样.
Book name---------------Author Lord of the rings---------J. R R Tolkiens Tom sawyer--------------Mark twain Just after sunset---------Stephen king The shining--------------Stephen king Oliver twist------------Charles Dickens Huck Finn---------------Marktwain Children on Hurin------J R R Tolkiens Silmarilions------------J R R tolkiens
我该如何使我的C ++程序读取此文件,将记录读取为数组,对其进行排序以及按书名或自动搜索来进行搜索.如果我编写Tolkiends,我如何使其输出JRR tolkiens编写的所有书籍?
解决方案
您可以将文件读入hash_multimap [ // **************** *************************************************** // 项目中使用的头文件 // ************************ ******************************************** #include >< > #include < conio.h > #include < stdio.h > #include < process.h > #include < string.h > #include < iomanip > #include < iostream > 使用 命名空间 std; 课程预订 { 公共: 字符 ID [ 6 ]; 字符内容[ 50 ]; 字符 AUTHOR [ 20 ]; 字符图书[ 20 ]; 无效 create_book()// 创建新的书 { cout<< " ; cout<< " ; gets(ID); cout<< " ;; gets(内容); cout<< " ; gets(作者); cout<< " ; 得到(书); cout<< " ; getch(); } 无效 report() {cout<< ID<< setw( 10 )<<<< setw( 30 )<< AUTHOR< setw( 20 )<<<<< endl;} // setw->设置宽度 void 复制(书本高清){// 此函数复制整个记录 strcpy(ID,(hd.ID)); // 复制书本ID strcpy(AUTHOR,(hd.AUTHOR)); // 复制BOOK ka AUTHOR strcpy(Book,(hd.Book)); strcpy(Content,(hd.Content)); } }; // 课程到此结束 无效 SwapBOOK(图书&hd1,图书&hd2){// // BOOK1 ko temp主要副本kiya hd1.Copy(hd2); // BOOK2 ko BOOK1主要副本kiya(现在BOOK2已移至BOOK1的位置) hd2.Copy(Temp); // BOOK1 ko finalyy BOOK2 ki jaga per copy ker diya } // ************************ ******************************************* // 流对象(对象)的全局声明 // ************************ ******************************************** fstream fp; // fstream提供了一个接口,可以从文件中读取和写入数据作为输入/输出流. 的ofof; // ofstream提供了一个接口,可将数据作为输出流写入文件中 图书hd [ 1000 ]; // 1000个图书的数组 int countBOOK = 0 ; // 程序中最初的aBOOK为零 void ReportAll(){// 此函数输出 系统(" ); for ( int i = 0 ; i< ; countbook; i ++){> hd [i] .report(); } getche(); } 无效 writeAllBOOK() { 字符 ch; ofp.open(" ,ios :: trunc); // purani文件(BOOK.dat)在新文件中覆盖ker的内容 // 如果为输出操作而打开的文件之前已经存在,则其先前的内容将被删除并由新的内容替换. 系统(" ); for ( int i = 0 ; i < countBOOK; i ++){ ofp.write(( char *)&(hd [i]), sizeof (BOOK)) ; // hd [i] ko char samaj k write ker do BOOK } ofp.flush(); // 从头开始写ker do ...缓冲区初始值kero ofp.close(); // 关闭文件 } 预定温度 无效 LoadAllBOOK() { 系统(" ); fp.open(" ,ios :: in ); // 读取文件 如果(!fp)// 如果文件未打开 { cout<< " ; getch(); // 字符lo countBOOK = 0 ; 返回; // 或waapis peechhay walay菜单主要挑战 } int i = 0 ; 同时(fp.read(( char *)& temp,// // 将BOOK复制到主数组 } countBOOK = i; // jitni dafa ye kaam ho'count BOOK'utni dafa增量日做 fp.close(); // 关闭文件 getch(); } // ************************ ******************************************* // 按作者的排序和搜索(START) // ************************ ******************************************** 无效 SortByAUTHOR()// 气泡排序 { for ( int i = 0 ; i< ; countBOOK- 1 ; i ++){ for ( int j = 0 ; j < countbook- 1 ;> if (strcmp(hd [j] .AUTHOR,hd [j +1].作者)> 0) SwapBOOK(hd [j],hd [j + 1]); // 如果满足上述条件,则调用我们定义的'swapBOOK'函数 } } } /// /////////////////////////////////////////////////////////////// 无效 SearchBetweenIndex( int IndexA, int IndexB,字符 *作者) { 如果(索引A>索引B){ int temp; temp = IndexA; 索引A =索引B; IndexB = temp; } for ( int i = IndexA; i< = IndexB; i ++){ 如果(strcmp(hd [i] .AUTHOR,AUTHOR)== 0 ) hd [i] .report(); } getch(); } /// //////////////////////////////////////////////////////////////////// 无效 ListByAUTHOR(字符 *作者){// 按作者搜索 int PreviousIndex = 0 ; // 首先 int StartIndex = 0 ,EndIndex = countBOOK- 1 跨度>; int i = 2 ; 同时( 1 == < 1 ){ int CurrentIndex =(EndIndex + StartIndex)/2; // 从中间位置开始搜索 如果(strcmp(hd [CurrentIndex] .AUTHOR,AUTHOR)> 0 ){ PreviousIndex = EndIndex; EndIndex = CurrentIndex; } 其他 if (strcmp(hd [CurrentIndex] .AUTHOR,AUTHOR)< 其他 { SearchBetweenIndex(StartIndex,EndIndex,AUTHOR); break ; } 如果(当前索引==上一个索引) break ; } } /// //////////////////////////////////////////////////////////////////// 无效 SortAndSearchByAUTHOR(){// 输入作者搜索标准 系统(" ); 字符 str [ 50 ]; cout<< " ; 得到(str); SortByAUTHOR(); // 调用此功能 ListByAUTHOR(str); // 调用此功能 } // ************************ ******************************************* // 按作者身份排序和搜索 // ************************ ******************************************** 无效 main() { 字符 ch; LoadAllBOOK(); 做 { 系统(" ); cout<< " ; cout<< " ; cout<< " ; cout<< " " ; cout<< " ; ch = getche(); 开关(ch) { 案例 ' 1':system(" ); hd [countBOOK] .create_book(); countBOOK ++; break ; 案例 ' 2': SortAndSearchByAUTHOR(); break ; 案例 ' 3': ReportAll(); break ; 案例 ' 4': writeAllBOOK(); exit( 0 ); 默认:cout<< " ; } } 同时(ch!= ' 4'); } </iostream>/iomanip>/string.h>/process.h>/stdio.h</conio.h</fstream>>
I have a Books.txt file which looks like this.
Book name---------------Author Lord of the rings---------J. R R Tolkiens Tom sawyer--------------Mark twain Just after sunset---------Stephen king The shining--------------Stephen king Oliver twist------------Charles Dickens Huck Finn---------------Marktwain Children on Hurin------J R R Tolkiens Silmarilions------------J R R tolkiens
How can i make my c++ program to read this file, read records as arrays, sort them, and search either by book name or by the auhtor. If i write Tolkiends, how can i make it output all the books written by J R R tolkiens?解决方案You could read the file into a hash_multimap[^] and then do a lookup based on the author name as key, but this will be limited by available memory. You may be better advised to use a database of some sort such that you can add, delete and update records as books are moved around. However, given your statement that you are not familiar with C++ you may be even better advised to pass this to an experienced programmer, or buy a ready made library management package.
Alright, now this is what i have. I have used the approach of bubble sorting then binary searching. what i want to do now is to search a particular BOOK from someone without using the bubble sorting or binary searching method and instead search all the records one at a time, from start till the end. i think this will make the coding a lot shorter than it is now
//*************************************************************** // HEADER FILE USED IN PROJECT //**************************************************************** #include<fstream> #include<conio.h> #include<stdio.h> #include<process.h> #include<string.h> #include<iomanip> #include<iostream> using namespace std; class BOOK { public: char ID[6]; char Content[50]; char AUTHOR[20]; char Book[20]; void create_book()// create new BOOK { cout<<"\nNEW BOOK ENTRY...\n"; cout<<"\nEnter The BOOK ID."; gets(ID); cout<<"\n\nEnter BOOK NAME "; gets(Content); cout<<"\n\nEnter The AUTHOR's Name "; gets(AUTHOR); cout<<"\n\nEnter The Source Book "; gets(Book); cout<<"\n\n\nBOOK Created.."; getch(); } void report() {cout<<ID<<setw(10)<<Content<<setw(30)<<AUTHOR<<setw(20)<<Book<<endl;} //setw--> set width void Copy(BOOK hd){ // this function copies whole record strcpy(ID,(hd.ID)); //copy BOOK ki id strcpy(AUTHOR,(hd.AUTHOR)); // copy BOOK ka AUTHOR strcpy(Book,(hd.Book)); strcpy(Content,(hd.Content)); } }; //class ends here void SwapBOOK(BOOK &hd1, BOOK &hd2){ //swaps position of BOOK1 with BOOK2 BOOK Temp; Temp.Copy(hd1); //BOOK1 ko temp main copy kiya hd1.Copy(hd2); //BOOK2 ko BOOK1 main copy kiya (now BOOK2 has moved to the place of BOOK1) hd2.Copy(Temp); //BOOK1 ko finalyy BOOK2 ki jaga per copy ker diya } //*************************************************************** // global declaration for stream object, object //**************************************************************** fstream fp; // fstream provides an interface to read and write data from files as input/output streams. ofstream ofp; // ofstream provides an interface to write data to files as output streams BOOK hd[1000]; //array of 1000 BOOK int countBOOK=0; // initially there are zero aBOOK in the program void ReportAll(){ // this function outputs a system("cls"); for(int i=0;i<countbook;i++){> hd[i].report(); } getche(); } void writeAllBOOK() { char ch; ofp.open("BOOK.dat",ios::trunc); // purani file (BOOK.dat) del ker k poora naya content overwrite ker do in new file //If the file opened for output operations already existed before, its previous content is deleted and replaced by the new one. system("cls"); for(int i=0; i < countBOOK;i++){ ofp.write((char*)&(hd[i]),sizeof(BOOK)); // hd[i] ko char samaj k write ker do BOOK } ofp.flush(); // abhi write ker do ... buffer istimal mat kero ofp.close();// close file } BOOK temp; void LoadAllBOOK() { system("cls"); fp.open("BOOK.dat",ios::in);// read file if(!fp) // if file doesnt opens { cout<<"ERROR!!! FILE COULD NOT BE OPEN "; getch(); // character lo countBOOK =0; return; // or waapis peechhay walay menu main chal do } int i=0; while(fp.read((char*)&temp,sizeof(BOOK))) // file say BOOK k siz ko as a string read ker raha hai and loading to memory { hd[i++].Copy( temp); // copying BOOK to main array } countBOOK=i; // jitni dafa ye kaam ho 'count BOOK' ko utni dafa increment day do fp.close(); // close file getch(); } //*************************************************************** // SORT AND SEARCH BY AUTHOR (START) //**************************************************************** void SortByAUTHOR() // bubble sort { for(int i=0; i< countBOOK-1; i++){ for(int j=0; j <countbook-1;> if(strcmp(hd[j].AUTHOR,hd[j+1].AUTHOR)>0) SwapBOOK(hd[j],hd[j+1]); // if above condition is satisfied, then call 'swapBOOK' function which is defined by us } } } ////////////////////////////////////////////////////////////////// void SearchBetweenIndex(int IndexA, int IndexB, char* AUTHOR) { if(IndexA > IndexB){ int temp; temp = IndexA; IndexA = IndexB; IndexB= temp; } for(int i = IndexA; i <= IndexB;i++){ if(strcmp(hd[i].AUTHOR,AUTHOR)==0) hd[i].report(); } getch(); } ///////////////////////////////////////////////////////////////////// void ListByAUTHOR(char* AUTHOR){ //search by AUTHOR int PreviousIndex = 0; // first int StartIndex =0 , EndIndex = countBOOK-1; int i=2; while(1==1){ int CurrentIndex=(EndIndex+StartIndex)/2; //start searching from the mid position if(strcmp(hd[CurrentIndex].AUTHOR, AUTHOR) > 0){ PreviousIndex = EndIndex; EndIndex = CurrentIndex; }else if(strcmp(hd[CurrentIndex].AUTHOR, AUTHOR) < 0){ PreviousIndex = StartIndex; StartIndex = CurrentIndex; }else { SearchBetweenIndex(StartIndex, EndIndex, AUTHOR); break; } if(CurrentIndex == PreviousIndex) break; } } ///////////////////////////////////////////////////////////////////// void SortAndSearchByAUTHOR(){ // INPUT AUTHOR SEARCH CRITERIA system("cls"); char str[50]; cout << "Enter the Search Criteria for AUTHOR "; gets(str); SortByAUTHOR(); // CALL THIS FUNCTION ListByAUTHOR(str); // CALLL THIS FUNCTION } //*************************************************************** // SORT AND SEARCH BY AUTHOR ENDS //**************************************************************** void main() { char ch; LoadAllBOOK(); do { system("cls"); cout<<"\n\n\n\tMAIN MENU"; cout<<"\n\n\t01. Create BOOK"; cout<<"\n\n\t02. SORT And Search By AUTHOR"; cout<<"\n\n\t03. lIST ALL ABOOK"; cout<<"\n\n\t04. EXIT"; cout<<"\n\n\tPlease Select Your Option (1-4) "; ch=getche(); switch(ch) { case '1':system("cls"); hd[countBOOK].create_book(); countBOOK++; break; case '2': SortAndSearchByAUTHOR(); break; case '3': ReportAll(); break; case '4': writeAllBOOK(); exit(0); default :cout<<"\a"; } }while(ch!='4'); } </iostream></iomanip></string.h></process.h></stdio.h></conio.h></fstream>
这篇关于C ++数据结构程序可按不同字段搜索记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文