| Код | #include <stdio.h> #include <fstream.h> #include <string.h> #include <conio.h> #include <iostream.h> //////////////////////////////////////////////////// void SaveText() { FILE *f = fopen("text.dat","w"); fprintf( f, "int main () { clrscr () ; SaveText () ;\n" ); fprintf( f, "char text [100][20] ; char slovo [20] ; ifstream\n" ); fprintf( f, "if ( ! fin ) { cout << Error open file ! << endl ;\n" ); fprintf( f, "return 1 ; } int kolvo = out ( fin , text ) ;\n" ); fprintf( f, "for ( int i = 0 ; i < kolvo ; i ++ ) { cout << text [i] ;}\n" ); fprintf( f, "cout << Vvedite slovo dla poiska ( 'end' -exit) > ; cin\n" ); fprintf( f, "if ( ! strcmp ( slovo , endl ) ) return 0 ;\n" ); fprintf( f, "char name [] = ERROR Otsutstvuet ! \n" ); fprintf( f, "for ( int i = 0; i < kolvo ; i ++ )\n" ); fprintf( f, "int c = strcmp ( text [i], slovo ) ; if ( !c )\n" ); fclose( f ); }; ///////////////////////////////////////////////////////// int out(ifstream &f, char text[][20]) { int i = 0; char c[20]; while( f>>c ) { memcpy(text[i],c,20); i++; } return i; }; /////////////////////////////////////////////////// void cop( char *c1, char *c2 ) { char *ch=" "; // 20 strcpy( ch, c1 ); strcpy( c1, c2 ); strcpy( c2, ch ); }; ///////////////////////////////////////////////////// int IF ( char *c1,int kolvo, char text[][20] ) { for ( int i=0; i<kolvo; i++ ) { if ( !strcmp(text[i], c1)) return i ; } return -1; }; /////////////////////////////////////////////////////// int main() { clrscr(); SaveText(); char text[200][20]; char slovo1[20], slovo2[20]; ifstream fin("text.dat"); if (!fin) { cout << "Error open file! " <<endl; return 1; } int kolvo = out( fin, text ); fin.close(); int i,ixs,iys; char *name = " "; // 20 while (1) { clrscr(); for ( i=0; i<kolvo; i++ ) cout << text[i]<<" "; cout<<endl<<endl<<name; cout <<endl<<endl<< "Vvedite 2 slova dla poiska ('end'-exit)"; cout<<endl<<"> "; cin >> slovo1 ; if ( !strcmp(slovo1,"end") ) break; cin>>slovo2; strcpy(name, "ERROR Otsutstvuet! ") ; ixs = IF(slovo1,kolvo,text); iys = IF(slovo2,kolvo,text); if ( ixs != -1 && iys != -1 ) { strcpy(name, "OK. Prisutstvuet! "); cop(text[ixs], text[iys]); } cout<<endl; } return 0; }
|
Только выводид программа текст не так как он в файле записан - страками, а сплошняком, все слова подряд. Для выхода из программы введи 'end'. |