Консультация № 72920
27.01.2007, 21:19
0.00 руб.
0 2 2
Здравствуйте! Помогите пожалуйста, написать программу на С++ к следующей работе! Заранее СПАСИБО! Работа со строками. Текстовые файлы.
Задание. Составить программу, выполняющую преобразования над текстовыми файлами согласно заданному варианту. Исходный текстовый файл создается при помощи редактора текста и может содержать произвольную последовательность различных символов произвольной длины.
Вариант3: Скопировать исходный файл в другой, удалив палиндромы в промежутке между введенными начальной и конечной цепочками. На экран вывести количество скопированных и удаленных слов.
С уважением, Галя.

Обсуждение

Неизвестный
29.01.2007, 05:44
общий
это ответ
Здравствуйте, Галя!
Вот вариант на C++. Из исходного текста удаляются слова-палиндромы.

Приложение:
#include <iostream>#include <fstream>#include <string>#include <algorithm>using namespace std;int main(int argc, char* argv[]) { const string in_file_name = "in.txt"; const string out_file_name = "out.txt"; ifstream in(in_file_name.c_str(), ios_base::in); ofstream out(out_file_name.c_str(), ios_base::out); string line; string word; string reverse_word; const string separators = " \n\t.,:;"; int copy_count = 0; int remove_count = 0; string::size_type beg_index; string::size_type end_index; while(getline(in, line)) { beg_index = str.find_first_not_of(separators); while(beg_index != string::npos) { end_index = str.find_first_of(separators, beg_index); if(end_index == string::npos) end_index = line.length(); word = line.substr(beg_index, end_index - beg_index); reverse_word = word; reverse(reverse_word.begin(), reverse_word.end()); if(word == reverse_word) { remove_count++; out << line.at(end_index); } else { copy_count++; out << word << line.at(end_index); } beg_index = line.find_first_not_of(separators, end_index); } } cout << "Count of words is copied: " << copy_count << endl; cout << "Count of words is removed: " << remove_count << endl; in.close(); out.close(); return 0;}
давно
Специалист
399094
201
30.01.2007, 22:54
общий
это ответ
Здравствуйте, Галя!
Вот ещё вариант
Если будут вопросы пишите

Приложение:
#include <stdio.h>#include <conio.h>#include <string.h>union slova{char s[255];};slova SL1[1000],SL2[1000];int n,delslov,copyslov,start,end;void main(){delslov=0;copyslov=0;FILE *in;FILE *out;in=fopen("in.txt","r");out=fopen("out.txt","w");n=1;while(!feof(in)){fscanf(in,"%s",&SL1[n].s);strcpy(SL1[n].s,strrev(SL1[n].s));n++;}fclose(in);in=fopen("in.txt","r");n=1;while(!feof(in)){fscanf(in,"%s",&SL2[n].s);n++;}fclose(in);printf("\nVvedite s kakogo slova nachinat poisk(type integer)\n");scanf("%d",&start);printf("\nVvedite end(type integer)\n");scanf("%d",&end);for(int i=start;i<=end;i++){if (strcmp(SL1[i].s,SL2[i].s)==0) {delslov++;}else{fprintf(out,"%s\n",&SL2[i].s);copyslov++;}}printf("Kol-vo udalennix slov = %d\n",delslov);printf("Kol-vo skopirovannix slov = %d\n",copyslov);printf("\n\nPress any key to exit please\n");fclose(out);getch();}
Форма ответа