Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > C/C++ программирование > Общие вопросы C/C++
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 07.10.2009, 22:49   #1
hen
Пользователь
 
Регистрация: 31.03.2009
Сообщений: 96
По умолчанию Слияние двух файлов в один

Задание таково:
"Написать программу на языке C++ чересстрочного слияния (т.е. первая строка в выходном файле - это первая строка из первого файла, вторая строка в выходном файле - это первая строка из второго файла, третья строка в выходном файле - вторая строка из первого файла и т.д.) двух текстовых файлов с удалением в каждой строке слов после которых стоит любой знак препинания."

вот моя прога
Код:
#include <stdio.h>
#include <iostream.h>
#include <ctype.h>
#include <stdlib.h>

char* rez_strok_dubl;
int ch_str_1= 0;
int ch_str_2= 0;
int lenf1;
int lenf2;

void func (char* uk, int* ch_str, char* stroka, int len) {
  while ((*uk!=10)&&(*ch_str<len-1)) {
   uk++;
   (*ch_str)++;
  }
  if (!ispunct(*(uk-1))) {
     do {
	uk--;
	(*ch_str)--;
   }
   while (!ispunct(*uk)&&(*uk!=10)&&(uk!=stroka));
   if (uk!=stroka) {
    uk++;
    (* ch_str)++;
   }
   while ((*uk!= 10)&&(*ch_str<len- 1)) {
    *rez_strok_dubl++=*uk++;
    (*ch_str)++;
   }
   *rez_strok_dubl++=*uk++;
  }
  uk++;
  (*ch_str)++;
 }

int main(){
 FILE *f1, *f2;
 if (!(f1= fopen ("D:\\work\\file_1.txt", "rb"))) {
  cout<<"file_1_ne_otkrit\n";
  system ("PAUSE");
  exit (1);
 }
 if (!(f2= fopen ("D:\\work\\file_2.txt", "rb"))) {
  cout<< "file_2_ne_otkrit\n";
  system ("PAUSE");
  exit (1);
 }
 fseek (f1, 0, SEEK_END);
 lenf1=ftell(f1);
 fseek (f2, 0, SEEK_END);
 lenf2=ftell(f2);
 char* stroka_1= (char*) malloc (lenf1) ;
 char* stroka_2= (char*) malloc (lenf2) ;
 fseek (f1, 0, SEEK_SET);
 fseek (f2, 0, SEEK_SET);
 fread (stroka_1, lenf1, 1, f1);
 fread (stroka_2, lenf2, 1, f2);
 fclose (f1);
 fclose (f2);
 char* rez_strok= (char*) malloc (lenf1+lenf2) ;
 rez_strok_dubl= rez_strok;
 while (ch_str_2<lenf2) {
  func (&stroka_1[ch_str_1], &ch_str_1, stroka_1, lenf1);
  func (&stroka_2[ch_str_2], &ch_str_2, stroka_2, lenf2);
 }

 FILE* f3;
 if (!(f3= fopen ("D:\\work\\B.txt", "wb"))) {
  cout<< "file_3_ne_otkrit\n";
  system ("PAUSE");
  exit (1);
 }

 int i= 0;
 while (&rez_strok [i]!= rez_strok_dubl)
  fputc ((unsigned char)rez_strok [i++], f3);
 fclose (f3);
 return 0;
}
file_1.txt
Код:
First codex was used for accounting, but 
with the development of parchment in the III century BC. e., gradually began to displace the papyri. 
This is already happening in the Christian world. The reasons for the adoption of the manuscript as the main type of books 
several: it is economical because you can use both sides of the paper, it is easy to hide; 
It was convenient and accessible. Perhaps the Christian writers used the original purpose, that they were not 
similar to the pagan texts, which are usually written in the form of scrolls.
file_2.txt
Код:
At the beginning of XIV century in Western Europe appeared woodcutting (it was developed 
long before the East). In the matrix of woodcuts depicting Pages 
cut from a piece of wood. It can be dipped in ink and 
used to make multiple copies of a page. Books, as well as playing 
cards and religious pictures, began to produce woodcuts. But the creation of the book was 
laborious process, since each page had to do his carving. Besides,
First codex was used for accounting, but 
with the development of parchment in the III century BC. e., gradually began to displace the papyri. 
This is already happening in the Christian world. The reasons for the adoption of the manuscript as the main type of books 
several: it is economical because you can use both sides of the paper, it is easy to hide; 
It was convenient and accessible. Perhaps the Christian writers used the original purpose, that they were not 
similar to the pagan texts, which are usually written in the form of scrolls.
B.txt(получившийся файл)
Код:
 but 
it was developed 
 
 In the matrix of woodcuts depicting Pages 
 The reasons for the adoption of the manuscript as the main type of books 
 It can be dipped in ink and 
 
 as well as playing 
 that they were not 
 But the creation of the book was 
 which are usually written in the form of scrolls.
 but 
¦ 
¦ The reasons for the adoption of the manuscript as the main type of books 
¦ 
¦ that they were not 
¦ which are usually written in the form of scrolls.

Последний раз редактировалось hen; 07.10.2009 в 22:59.
hen вне форума Ответить с цитированием
Старый 07.10.2009, 22:59   #2
hen
Пользователь
 
Регистрация: 31.03.2009
Сообщений: 96
По умолчанию

Проблемы:
1) У меня не получилось организовать слияние строк... В моём случае в первом файле 6 строк, а во втором - 12 строк... При черезстрочном слиянии получается, что когда в первом файле обработалось 6 строк и во втором файле 6 строк, в первом файле строки закончились, но во втором и еще 6.. И они копируются оставляя перед собой то какой-то символ, то просто пустую строчку...
Как бы сделать, чтобы когда одинаковое кол-во строк в файлах закончилось, и если в каком-то файле еще остались строки, то чтобы они просто скопировались в исходный файл (без пропусков строки и всяких посторонних символов)??

2) В моем варианте при встрече знака препинания удаляются все предшествующие ему слова...
Как бы сделать, чтобы удалялось только то слово после которого стоит знак препинания??
Например, чтобы получилось вот так (это вторая строка первого файла...)
Код:
with the development of parchment in the III century gradually began to displace the
hen вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Слияние двух массивов в Delphi sauron99 Помощь студентам 17 03.04.2016 09:37
Чересстрочное слияние двух файлов в один hen Общие вопросы C/C++ 5 01.10.2009 15:15
Слияние нескольких wav файлов lacost Мультимедиа в Delphi 1 30.09.2008 19:25
Копирование двух файлов в один JRcoker Общие вопросы Delphi 5 18.07.2008 21:37
Слияние двух текстовых файлов nataly_ukr Общие вопросы Delphi 5 26.12.2007 16:45