Re: I'm a newbie. Is this code ugly?
"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4b57ff58$0$1135$4fafbaef@reader1.news.tin.it...
what about use of the template?
yes i write horrible code, but not all the sheepe are white;
Is it possible use the assembly language for write template functions?
i think yes it is enought to pass the sizeof(T) like argument
and all should goes well
if yes template could be all good
how i can use cpm1 in a.sort() function?
do you think there are memory leak? (i don't have here my functions)
thank you
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define R return
using namespace std;
char* faiMemCopia(char* v)
{int i, n;
char *p;
if(v==0) R 0;
n=strlen(v);
if(n<0) R 0;
p=(char*) malloc(n+1);
if(p==0) R 0;
for(i=0; i<n; ++i)
p[i]=v[i];
p[i]=0;
R p;
}
class Person{
public:
char *num_, *surname_;
char *key_;
Person(){num_=0; surname_=0; key_=0;}
int InitializePointer(char *num, char *key, char *surname)
{num_=0; surname_=0; key_=0;
num_=faiMemCopia(num);
if(num_==0) return 0;
surname_=faiMemCopia(surname);
if(surname==0)
{la0:
free(num_); num_=0;
R 0;
}
key_=faiMemCopia(key);
if(key_==0){free(surname_); surname_=0;
goto la0;
}
R 1;
}
Person(char *num, char *key, char *surname)
{if( this->InitializePointer(num, key, surname) ==0)
{cerr << "Need more memory\n"; exit(0);}
}
Person(Person& a)
{if( this->InitializePointer(a.num_, a.key_, a.surname_) ==0)
{cerr << "Need more memory\n"; exit(0);}
}
void FreePointer(void)
{free(num_); free(key_); free(surname_);
num_=0; surname_=0; key_=0;
}
~Person(){this->FreePointer();}
Person& operator=(Person& r)
{if(&r!=this)
{free(num_); free(key_); free(surname_);
if(this->InitializePointer(r.num_, r.key_, r.surname_)==0)
{cerr << "Need more memory\n"; exit(0);}
}
R *this;
}
int cmp1(Person& a, Person& b){R strcmp(a.key_,b.key_);}
};
template <class T> class RosList{
public:
T** v;
int n;
int sz;
RosList(){v=0; n=0; sz=0;}
int add(T& a)
{if(sz<=n){T **p;
p=(T**)realloc(v, (n+128)*sizeof(T*));
if(p==0) R 0;
sz=n+128;
v =p;
}
v[n]=(T*) malloc(sizeof(T));
if(v[n]==0) R 0;
*(v[n])=a; ++n;
R 1;
}
void sort(int (*cmp)(T& a, T& b))
{int i, hit=1, len=n;
T *temp;
while(len>1&&hit)
{len--;
hit=0;
for(i=0; i<len; ++i)
if(cmp( *(v[i]), *(v[i+1]) )>0)
{temp=v[i]; v[i]=v[i+1]; v[i+1]=temp; hit=1;}
}
}
~RosList(){int i=n;
for(--i; i>=0; --i)
{v[i]->FreePointer();
free(v[i]);
}
free(v);
}
T& operator[](int i)
{static T no;
if(i<0||i>=n)
{cout << "\n\aIndice fuori dei limiti\n"; R no;}
R *(v[i]);
}
};
int cmp(Person& a, Person& b){R strcmp(a.key_,b.key_);}
int main(void)
{int i;
RosList<Person> a;
i=1;
i*=a.add(Person("a","Mann","Thomas"));
i*=a.add(Person("b","Satie","Erik"));
i*=a.add(Person("c","Goldfarb","Sarah"));
i*=a.add(Person("d","Ravel","Maurice"));
i*=a.add(Person("e","Hideyuki","Tanaka"));
i*=a.add(Person("f","Twain","Mark"));
if(i==0) {cout << "Memory error\n"; R 0;}
a.sort(cmp);
for(i=0; i<a.n; ++i)
cout <<a.v[i]->surname_<<"\t"<<a.v[i]->key_<<"\n";
for(i=0; i<a.n; ++i)
cout <<a[i].surname_<<"\t"<<a[i].key_<<"\n";
R 0;
}