#include #include #include #include #include #include "vital.h" const char * const cppversion="vital 1.02.006 of 30th May 2002"; const char * const vndrline="\n-------------------------------\n"; int CheckSameVersions(void) { int ok=(strcmp(hversion, cppversion)==0); if (ok) return 1; return 0; } /* int YesNoDialog(string message, string title) { struct HWND__ { int unused; }; typedef struct HWND__ *HWND; const int MB_YESNO = 0x00000004L, MB_ICONHAND = 0x00000010L, MB_TASKMODAL = 0x00002000L, MB_SETFOREGROUND = 0x00010000L, MB_TOPMOST = 0x00040000L, IDYES = 6; __declspec(dllimport) int __stdcall MessageBoxA(HWND hWnd, const char * lpText, const char * lpCaption, unsigned int uType); int r=MessageBoxA(NULL, message.getcstr(), title.getcstr(), MB_YESNO | MB_ICONHAND | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST); return r==IDYES; } */ int CheckSameVersionsNoisily(void) { int ok=(strcmp(hversion, cppversion)==0); if (ok) { print(cppversion); print(vndrline); return 1; } print("****************\n"); print("Versions Differ:\n"); print(" .h version is '"); print(hversion); print("'\n"); print(" .cpp version is '"); print(cppversion); print("'\n"); print("****************\n\n"); return 0; } string::string(int mx) { max=mx+1; data=new char[max]; data[0]=0; len=0; } string::string(void) { max=257; data=new char[max]; data[0]=0; len=0; } string::string(const char *s) { len=strlen(s); max=len+100; data=new char[max]; strcpy(data, s); } string::string(const string &s) { max=s.max; len=s.len; data=new char[max]; strcpy(data, s.data); } string::~string(void) { delete[] data; } char *string::getcstr(void) const { return data; } char *string::c_str(void) const { return data; } string &string::operator=(const string &s) { max=s.max; len=s.len; if (data!=s.data) { data=new char[max]; strcpy(data, s.data); } return *this; } string &string::operator=(const char *s) { len=strlen(s); max=len+100; data=new char[max]; strcpy(data, s); return *this; } bool string::operator==(const string &s) const { return strcmp(data, s.data)==0; } bool string::operator!=(const string &s) const { return strcmp(data, s.data)!=0; } bool string::operator<(const string &s) const { return strcmp(data, s.data)<0; } bool string::operator>(const string &s) const { return strcmp(data, s.data)>0; } bool string::operator<=(const string &s) const { return strcmp(data, s.data)<=0; } bool string::operator>=(const string &s) const { return strcmp(data, s.data)>=0; } int string::length(void) const { return len; } int length(const string &s) { return s.length(); } char character(const string &s, int i) { return s[i]; } int string::maximum(void) const { return max; } string &string::append(const char *s, int slen) { int newlen=len+slen; if (newlen>=max-1) { int newmax=max+100; char *temp=new char[newmax]; strcpy(temp, data); delete[] data; data=temp; max=newmax; } strcat(data, s); len=newlen; data[len]=0; return *this; } string &string::append(const char *s) { return append(s, strlen(s)); } string &string::append(const string &s) { return append(s.data, s.len); } string &string::append(char c) { char data[2]; data[0]=c; data[1]=0; return append(data, 1); } string string::operator+(const char *addition) { string s(*this); s.append(addition, strlen(addition)); return s; } string string::operator+(const string &s) { return operator+(s.data); } string string::operator+(char c) { char data[2]; data[0]=c; data[1]=0; return operator+(data); } string string::substring(int begin, int end) { if (end<0) end=len+end; if (end<0) end=0; if (begin<0) begin=len+begin; if (begin<0) begin=0; if (begin>end || begin>=len) return string(""); if (end>=len) end=len-1; int newlen=end+1-begin; string s(newlen<257 ? 257 : newlen); for (int i=begin; i<=end; i+=1) s.data[i-begin]=data[i]; s.data[newlen]=0; s.len=newlen; return s; } int qwqwqwignorethis=1; int string::to_int(int &ok) { int n=len, pos=0, sign=1, value=0; while (pos'9') { ok=0; return 0; } while (pos='0' && data[pos]<='9') { value=value*10+data[pos]-'0'; pos+=1; } while (pos=len) { rubbish=0; return rubbish; } return data[n]; } char &string::operator[](int n) const { static char rubbish=0; if (n<0) { rubbish=0; return rubbish; } if (n>=len) { rubbish=0; return rubbish; } return data[n]; } bool operator==(const char *x, const string &s) { return strcmp(x, s.getcstr())==0; } bool operator!=(const char *x, const string &s) { return strcmp(x, s.getcstr())!=0; } bool operator<(const char *x, const string &s) { return strcmp(x, s.getcstr())<0; } bool operator>(const char *x, const string &s) { return strcmp(x, s.getcstr())>0; } bool operator<=(const char *x, const string &s) { return strcmp(x, s.getcstr())<=0; } bool operator>=(const char *x, const string &s) { return strcmp(x, s.getcstr())>=0; } output_object::output_object(void) { ofile=NULL; } void output_object::print_char(char c) { if (ofile!=NULL) putc(c, ofile); } void output_object::print(int n) { if (ofile!=NULL) fprintf(ofile, "%d", n); } void output_object::print(__int64 n) { if (ofile!=NULL) fprintf(ofile, "%I64d", n); } void output_object::print(double f) { if (ofile!=NULL) fprintf(ofile, "%.14le", f); } void output_object::print(float f) { if (ofile!=NULL) fprintf(ofile, "%.7e", f); } void output_object::print(const char *s) { if (ofile!=NULL) fputs(s, ofile); } void output_object::print(const string &s) { if (ofile!=NULL) fputs(s.getcstr(), ofile); } void output_object::newline(void) { if (ofile!=NULL) putc('\n', ofile); } void output_object::flush(void) { if (ofile!=NULL) fflush(ofile); } void output_object::close(void) { if (ofile!=NULL) fclose(ofile); } screen_object::screen_object(void) { ofile=stdout; int bub = CheckSameVersionsNoisily(); } void screen_object::close(void) { } output_file::output_file(char *filename) { ofile=fopen(filename, "w"); if (ofile==NULL) { fprintf(stderr,"\nCan not open file \"%s\"\n", filename); exit(1); } } output_file::output_file(void) { ofile=NULL; } output_file::output_file(string &filename) { char *fname=filename.getcstr(); ofile=fopen(fname, "w"); if (ofile==NULL) { fprintf(stderr,"\nCan not open file \"%s\"\n", fname); exit(1); } } void input_object::rewind(void) { ::rewind(ifile); } int input_object::nextch(void) { if (ifile==NULL) return EOF; else return fgetc(ifile); } void input_object::backch(int c) { ungetc(c, ifile); } void input_object::next_line(void) { while (1) { int c=nextch(); if (c=='\n' || c==EOF) return; } } char *input_object::read_line(void) { char *s=fgets(line, input_object_maxlen, ifile); if (s==NULL) { line[0]=26; line[1]=0; ok=0; return line; } int n=strlen(line)-1; if (line[n]=='\n') line[n]=0; return line; } char *input_object::read_charstar(void) { int len=0, c, started=0; while (1) { c=nextch(); if (c==EOF || c>' ') break; } while (c!=EOF && c>' ') { if (len=len) c=0; else { c=line[pos]; pos+=1; } } char string_scanner::read_char(void) { char c; read(c); return c; } void string_scanner::read_char(char &c) { read(c); } int string_scanner::read_digit(void) { char c=0; while (pos' ') break; } if (c>='0' && c<='9') return c-'0'; return -1; } void string_scanner::read(int &i) { char *s=NULL; i=strtol(line+pos, &s, 10); ok=s!=line+pos; pos=s-line; } int string_scanner::read_int(void) { int i; read(i); return i; } void string_scanner::read(float &f) { char *s=NULL; double d=strtod(line+pos, &s); f=(float)d; ok=s!=line+pos; pos=s-line; } float string_scanner::read_float(void) { float f; read(f); return f; } void string_scanner::read(double &d) { char *s=NULL; d=strtod(line+pos, &s); ok=s!=line+pos; pos=s-line; } double string_scanner::read_double(void) { double d; read(d); return d; } char *string_scanner::read_charstar(void) { static char temp[300]; int i=pos, j=0; while (1) { char c=line[i]; if (c==0 || c>' ') break; i+=1; } while (1) { char c=line[i]; if (c<=' ') { temp[j]=0; break; } temp[j]=c; i+=1; j+=1; } pos=i; ok=temp[0]!=0; return temp; } string string_scanner::read_string(void) { char *t=read_charstar(); return string(t); } void string_scanner::read(string &s) { char *t=read_charstar(); s=string(t); } void string_scanner::close(void) { } char *string_scanner::read_line(void) { return line+pos; } timer_object::timer_object(void) { look_at_the_clock(); } void timer_object::look_at_the_clock() { struct _timeb tb; _ftime(&tb); theseconds=tb.time; struct tm *t=localtime(&tb.time); thedate=19000100+(t->tm_year*10000)+(t->tm_mon*100)+(t->tm_mday); thetime=(t->tm_hour*10000)+(t->tm_min*100)+(t->tm_sec); themilliseconds=tb.millitm; } int timer_object::integer_date(void) { return thedate; } int timer_object::integer_time(void) { return thetime; } int timer_object::integer_ms(void) { return themilliseconds; } double timer_object::double_time(void) { double s=theseconds; double ms=themilliseconds; return s+ms/1000.0; } screen_object screen; keyboard_object keyboard; timer_object timer; void look_at_the_clock(void) { timer.look_at_the_clock(); } int integer_date(void) { return timer.integer_date(); } int integer_time(void) { return timer.integer_time(); } int integer_ms(void) { return timer.integer_ms(); } double double_time(void) { return timer.double_time(); } void print_char(char c) { screen.print_char(c); } void print(int n) { screen.print(n); } void print(__int64 n) { screen.print(n); } void print(float f) { screen.print(f); } void print(double f) { screen.print(f); } void print(const char *s) { screen.print(s); } void print(const string &s) { screen.print(s); } void newline(void) { screen.newline(); } void flush(void) { screen.flush(); } void read(char &c) { keyboard.read(c); } void read_char(char &c) { keyboard.read_char(c); } void read(int &n) { keyboard.read(n); } void read(__int64 &n) { keyboard.read(n); } void read(float &f) { keyboard.read(f); } void read(double &f) { keyboard.read(f); } void read(string &s) { keyboard.read(s); } int read_int(void) { int i; keyboard.read(i); return i; } __int64 read_longlongint(void) { __int64 i; keyboard.read(i); return i; } float read_float(void) { float f; keyboard.read(f); return f; } double read_double(void) { double d; keyboard.read(d); return d; } char read_char(void) { char c; keyboard.read_char(c); return c; } string read_string(void) { string s=keyboard.read_string(); return s; } string read_line(void) { string s=keyboard.read_line(); return s; } int randomised=0; int random(void) { int a=rand(), b=rand(), c=rand(); int r=(a + (b<<12) + (c<<24)) & 0x7fffffff; return r; } void randomise(void) { randomised=1; struct _timeb tb; _ftime(&tb); struct tm *t=localtime(&tb.time); srand(tb.millitm+t->tm_sec*1000+t->tm_min*60000); } void randomize(void) { randomise(); } void do_not_randomise(void) { randomised=1; } void do_not_randomize(void) { do_not_randomise(); } int random_in_range(int a, int b) { if (!randomised) randomise(); return a+random()%(b-a+1); } float random_in_range(float a, float b) { if (!randomised) randomise(); double r=random()/2147483648.0; return (float)(a+r*(b-a)); } double random_in_range(double a, double b) { if (!randomised) randomise(); double r=random()/2147483648.0; return a+r*(b-a); } // void delay(double t) // { Sleep((int)(t*1000.0+0.5)); }