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

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

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

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

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

Ответ
 
Опции темы Поиск в этой теме
Старый 02.04.2012, 00:18   #1
MartinSeptim
 
Регистрация: 18.02.2012
Сообщений: 9
Восклицание Перевод каретки при чтении строк из файла

Проблема моей программы (смотри ниже) в том что она постоянно использует значения одной и той же строки. Например файл входа
Код HTML:
10.200 666.111
125125215125.1251251 221512515.125125125
1215125125125.12512515512 122161651216612621.1252156126512
однако в файле выхода
Код HTML:
660,22200
660,22200
660,22200
хотя должно выдавать разные значения.
Что в моей программе не так?

Основное тело:
Код:
#include "StdAfx.h"
#include "conio.h"
#include <iostream>
#include <vector>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include "readlongs.h"
using namespace std;
typedef std::vector <int> type;

 
int _tmain() {


    ifstream file("in.txt");
    string str, tmp;
    int count=0;
    if(file.is_open()){
        while(getline(file, str)){
            count++; 
			cout << str << '\n';
			
			 }
		
    } 
    cout << "\n Count = " << count;
	cout<<"\n";ofstream out( "out.txt");
	for (int k=0;k<count; k++){
type a, b, c, d, rez, rez1; //разные rez нужны для не засорения
	readlong (a); 
    readlong1 (b);
	readlong2 (c); 
    readlong3 (d);
	ofstream out( "out.txt", ios::app );
    mult (a, b, rez); //использование умножения
      cout << rez.back (); out<<rez.back();
    for (int i = rez.size () - 2; i >= 0; i--)
	{ cout << rez[i]; out<<rez[i];}
	 
	printf(","); out<<",";
/*==================================================*/
	mult (c, d, rez1); //использование умножения

   cout << rez1.back (); out<<rez1.back();
    for (int i = rez1.size () - 2; i >= 0; i--)
	{cout << rez1[i]; out<<rez1[i];}
	cout<<"\n";
	out<<"\r\n";
	out.close();  } 

 getch();
    return 0;
}
MartinSeptim вне форума Ответить с цитированием
Старый 02.04.2012, 00:19   #2
MartinSeptim
 
Регистрация: 18.02.2012
Сообщений: 9
По умолчанию

Header-файл readlongs.h
Код:
#include "StdAfx.h"
#include "conio.h"
#include <iostream>
#include <vector>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <ctime>
#define BASE 10
#define LEN 1
const int N=51000;
using namespace std;
typedef std::vector <int> type;

void readlong (type &vec)
{
    string str, str1, str2, str4;
   char str3[N];
   ifstream fin("in.txt");
  fin.getline(str3, 2*N+2);
str2=str3;
int v;
int len1=str2.length();
for(int i=0;i<len1;i++)
		if(str2[i]==' ')  v=i; 
	char buf2[2*N+2];
   memset( buf2, '\0', 2*N+2 );
   str2.copy( buf2, v );
    str4 = (const char*) buf2; // конвертация буфера в строку
     str=str4;
//==================================================
  fin.close(); 
  int d;
	int len=str.length(); // длина основной строки
	for(int i=0;i<len;i++)
		if(str[i]=='.')  d=i; 
	char buf[N];
   memset( buf, '\0', N );
   str.copy( buf, d );
    str1 = (const char*) buf; // конвертация буфера в строку
     str=str1;

    for (int i = str.size (); i > 0; i--)
        vec.push_back (atoi (str.substr (i - LEN, LEN).c_str()));
}

void readlong1 (type &vec)
{
    string str, str1, str2, str4;
   char str3[N];
   ifstream fin("in.txt");
  fin.getline(str3, 2*N+2);
str2=str3;
int v, f, k;
int len1=str2.length();
if(str2[0]=='-')  k=1; //проверка первого числа на -
//==========================
for(int i=0;i<len1;i++)
if(str2[i]==' ')  {v=i; f=len1-v; } 
	char buf2[2*N+2];
   memset( buf2, '\0', 2*N+2 );
   str2.copy( buf2, v, f );
    str4 = (const char*) buf2; // конвертация буфера в строку
     str=str4;
	//==============================
  fin.close(); 
	int len=str.length(); // длина основной строки
	int d, c;
	if(str[0]=='-')  k=0; //проверка второго числа на -
	if (k==1) {cout<<"-"; }
	for(int i=0;i<len;i++)
		if(str[i]=='.')  d=i; 
	char buf[50];
   memset( buf, '\0', N );
   str.copy( buf, d );
    str1 = (const char*) buf; // конвертация буфера в строку
     str=str1;
    for (int i = str.size (); i > 0; i--)
        vec.push_back (atoi (str.substr (i - LEN, LEN).c_str()));
}
 void readlong2 (type &vec)
{string str, str1, str2, str4;
   char str3[N];
   ifstream fin("in.txt");
  fin.getline(str3, 2*N+2);
str2=str3;
int v;
int len1=str2.length();
for(int i=0;i<len1;i++)
		if(str2[i]==' ')  v=i; 
	char buf2[2*N+2];
   memset( buf2, '\0', 2*N+2 );
   str2.copy( buf2, v );
    str4 = (const char*) buf2; // конвертация буфера в строку
     str=str4;
    //=====================
  fin.close(); 

	int len=str.length(); // длина основной строки
	int d, c;
	for(int i=0;i<len;i++)
		if(str[i]=='.')  {d=i; c=len-d;}
	char buf1[N];
 
memset( buf1, '\0', N );
   str.copy( buf1, c, d+1 );
   str2 = (const char*) buf1; // конвертация буфера в строку
     str=str2;
    for (int i = str.size (); i > 0; i--)
        vec.push_back (atoi (str.substr (i - LEN, LEN).c_str()));
}

 void readlong3 (type &vec)
{ string str, str1, str2, str4;
   char str3[N];
   ifstream fin("in.txt");
  fin.getline(str3, 2*N+2);
str2=str3;
int v, f;
int len1=str2.length();
for(int i=0;i<len1;i++)
if(str2[i]==' ')  {v=i; f=len1-v; } 
	char buf2[2*N+2];
   memset( buf2, '\0', 2*N+2 );
   str2.copy( buf2, v, f );
    str4 = (const char*) buf2; // конвертация буфера в строку
     str=str4;

    //=====================
  fin.close(); 

	int len=str.length(); // длина основной строки
	int d, c;
	for(int i=0;i<len;i++)
		if(str[i]=='.')  {d=i; c=len-d;}
	char buf1[N];
 
memset( buf1, '\0', N );
   str.copy( buf1, c, d+1 );
   str2 = (const char*) buf1; // конвертация буфера в строку
     str=str2;
    for (int i = str.size (); i > 0; i--)
        vec.push_back (atoi (str.substr (i - LEN, LEN).c_str()));
}

void mult (type &a, type &b, type &rez)
{  
    rez.resize (a.size() + b.size());
    for (int i = 0; i < a.size(); ++i)
        for (int j = 0, carry = 0; j < b.size() || carry; ++j) //сarry вес разряда
        {
            long long cur = rez[i+j] + a[i] * (j < b.size() ? b[j] : 0) + carry;
            rez[i+j] = cur % BASE;
            carry = cur / BASE;
        }
		while (rez.size() > 1 && rez.back() == 0)
        rez.pop_back();
}
MartinSeptim вне форума Ответить с цитированием
Ответ


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



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Ошибка при чтении из файла BEL9ILLI Общие вопросы C/C++ 3 13.01.2012 10:12
Кодировка при чтении из файла _-Re@l-_ Общие вопросы .NET 2 21.11.2010 20:12
Ошибка при чтении из файла alex_alpha Общие вопросы C/C++ 5 08.08.2010 20:28
ошибка при чтении файла Alik-Soldier Общие вопросы C/C++ 1 31.05.2009 20:44
Проблема при чтении из файла Satorin Общие вопросы Delphi 2 17.12.2008 11:32