String i short

0

Witam

Problem jest następujący: chcę pobrać łańcuch znaków (będą to same cyfry) z wejścia, po czym zapisać każdą cyfrę w osobnym elemencie dynamicznie tworzonej tablicy. Próbowałem najpierw zapisać do stringa i po kolejnych indeksach kopiować do tejże utworzonej tablicy. Wszystko się sypie gdy przychodzi czas na konwersję.

Pozdrawiam

0
string s;
vector<short> liczby;
cin >> s;
for( string::iterator i = s.begin(); i!=s.end(); i++ )
     liczby.push_back( *i - '0' );
0

Coś się dalej sypie, oto kod, będę wdzięczny za jakieś porady. Program ma póki co dodawać dwie duże liczby.

BigNumber.h

#ifndef BIGNUMBER_H
#define BIGNUMBER_H
#include <iostream>
#include <vector>

class BNum
{
private:
	int size;
public:
	short* number;

	BNum(int sz);
	BNum(const BNum&);
	~BNum();

	friend BNum const operator + (const BNum& left, const BNum& right);
};
#endif // BIGNUMBER_H 

BigNumber.cpp

#include "stdafx.h"
#include "BigNumber.h"

using namespace std;

BNum::BNum(int sz) : size(sz)
{
	number = new short int [size];
	printf("Utworzono nowy obiekt\n");
}
BNum::BNum(const BNum& bn) : number(bn.number)
{
	number = bn.number;
}
BNum::~BNum()
{
	delete [] number;
	printf("Usunieto obiekt\n");
}
const BNum operator + (const BNum& left, const BNum& right)
{
	vector<short> v;
	short temp;
	int sum, max;
	
	if((sizeof(left.number)/sizeof(short)) <= (sizeof(right.number)/sizeof(short)))
		max = sizeof(right.number)/sizeof(short);
	else
		max = sizeof(left.number)/sizeof(short);

	v.push_back(0);

	for(int i = 0; i < max; i++)
	{
		sum = left.number[i] + right.number[i];
		v[i] += sum;
		temp = static_cast<short>(v[i]/10);
		v[i] = v[i] % 10;
		v.push_back(temp);
	}

	BNum result(v.size());

	for(int i = 0; i < v.size(); i++)
	{
		cout << v[i];
		result.number[i] = v[i];
	}

	return result;
}

BigNum.cpp

#include "stdafx.h"
#include "BigNumber.h"
#include <string>
#include <cstdlib>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	string p, q;
	vector<short> ps;
	vector<short> qs;
	cout << "Podaj pierwsza liczbe: " << endl;
	cin >> p;
	for( string::iterator i = p.begin(); i!=p.end(); i++ )
		ps.push_back( *i - '0' );

	cout << "druga liczbe liczbe: " << endl;
	cin >> q;
	for( string::iterator i = q.begin(); i!=q.end(); i++ )
		qs.push_back( *i - '0' );

	BNum pbn(p.size());
	BNum qbn(q.size());

	system("PAUSE");

	for(int i = 0; i < p.size(); i++)
	{
		pbn.number[p.size() - i - 1] = p[i];
		cout << pbn.number[p.size() - i - 1];
	}

	cout << endl;

	for(int i = 0; i < q.size(); i++)
	{	
		qbn.number[p.size() - i - 1] = q[i];
		cout << pbn.number[p.size() - i - 1];
	}

	cout << endl;

	system("PAUSE");

	pbn = pbn + qbn;

	system("PAUSE");

	return 0;
}
0

skorzystaj z wektora, a będzie łatwiej zmienić rozmiar.

0

Nie wiem czy o to chodzi co podam. Ciekaw jestem czy to ałaściwa odpowiedź.

#include<iostream>
#include<vector>
#include<conio.h>
using namespace std;

int main()
{
 vector<char>chr;   
 char ch;
 string liczba;
 char *tab;  
 cout<<"I wersja"<<endl;
  cout<<"podaj liczbe w postaci stringu "<<endl;
  cin>>liczba;
  int dl=liczba.length();
 tab=new char(dl);
    for(int i=0;i<dl;i++)
    tab[i]=liczba[i];
 delete []tab;
 cout<<endl<<endl<<"II wersja,wpisanie litery to koniec wpisywania"<<endl<<endl;
 vector<char>::iterator iter;   
   while(1)
    {
     cout<<"podaj cyfre ";
     cin>>ch;
     if(!isdigit(ch))break;
     chr.push_back(ch); 
    }  
  int size=chr.size();  
  tab=new char(size);  
  cout<<"tablica dynamiczna"<<endl;
    for( iter = chr.begin(); iter != chr.end(); iter++ ) 
     {
      int i=0;    
      tab[i]=*iter;   
      cout<<tab[i]<<" ";
      i++;
    }
  
  delete []tab;
getch();
return 0;
}
0

if((sizeof(left.number)/sizeof(short)) <= (sizeof(right.number)/sizeof(short)))
max = sizeof(right.number)/sizeof(short);
else
max = sizeof(left.number)/sizeof(short);
A to co? Tak nie sprawdzisz długości dynamicznej tablicy. Użyj vector.

0

Dzięki za odpowiedzi. Już działa. Teraz problem przeniósł się na destruktor. Podczas jego wywołania wyrzuca błąd.

0

Jaki błąd?

0

Już nieaktualne. Dziękuje wszystkim za pomoc. Opłacało się przesiąść na vector :-)

1 użytkowników online, w tym zalogowanych: 0, gości: 1