1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
#include <cctype>
#ifndef _simpio_h
#define _simpio_h
#ifndef _genlib_h
#define _genlib_h
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdarg.h>
#include <stdbool.h>
#define FALSE false
#define TRUE true
#define ErrorExitStatus 1
typedef char *string;
typedef FILE *stream;
#define UNDEFINED ((void *) undefined_object)
extern char undefined_object[];
char undefined_object[] = "UNDEFINED";
void *GetBlock(size_t nbytes);
void *GetBlock(size_t nbytes)
{
void *result;
result = malloc(nbytes);
//if (result == NULL) Error("No memory available");
return result;
}
void FreeBlock(void *ptr);
void FreeBlock(void *ptr)
{
free(ptr);
}
#define New(type) ((type) GetBlock(sizeof *((type) NULL)))
#define NewArray(n, type) ((type *) GetBlock((n) * sizeof (type)))
void Error(string msg, ...);
void Error(string msg, ...)
{
va_list args;
va_start(args,msg);
fprintf(stderr, "Error: ");
vfprintf(stderr, msg, args);
fprintf(stderr, "\n");
va_end(args);
exit(ErrorExitStatus);
}
#define repeat for (;;)
#if defined( __BORLANDC__ )
# if !defined ( __LARGE__ )
# error The genlib.h library requires the LARGE memory model.
# endif
# if defined( _Windows )
# define main void main
# else
# define main() void Main(void); \
main() \
{ \
Main(); \
(void) getchar(); \
return (0); \
} \
void Main(void)
# endif
#endif
#endif
#define InitialBufferSize 120
string ReadLine(FILE *infile)
{
string line, nline;
int n, ch, size;
n = 0;
size = InitialBufferSize;
line = (char *) GetBlock(size + 1);
while ((ch = getc(infile)) != '\n' && ch != EOF)
{
if (n == size)
{
size *= 2;
nline = (string) GetBlock(size + 1);
strncpy(nline,line,n);
FreeBlock(line);
line = nline;
}
line[n++] = ch;
}
if (n == 0 && ch == EOF)
{
FreeBlock(line);
return (NULL);
}
line[n] = '\0';
nline = (string) GetBlock(n + 1);
strcpy(nline , line);
FreeBlock(line);
return (nline);
}
string GetLine(void)
{
return (ReadLine(stdin));
}
int GetInteger(void)
{
string line;
int value;
char termch;
while (1)
{
line = GetLine();
switch (sscanf(line, " %d %c", &value, &termch))
{
case 1:
FreeBlock(line);
return value;
case 2:
printf("Unexpected character: '%c'\n", termch);
break;
default:
printf("Please enter an integer\n");
break;
}
FreeBlock(line);
printf("Retry: ");
}
}
long GetLong(void)
{
string line;
long value;
char termch;
while (1)
{
line = (char *) GetLine();
switch (sscanf(line, " %ld %c", &value, &termch))
{
case 1:
FreeBlock(line);
return value;
case 2:
printf("Unexpected character: '%c'\n", termch);
break;
default:
printf("Please enter an integer\n");
break;
}
FreeBlock(line);
printf("Retry: ");
}
}
double GetReal(void)
{
string line;
double value;
char termch;
while (1)
{
line = GetLine();
switch (sscanf(line, " %lf %c", &value, &termch))
{
case 1:
FreeBlock(line);
return value;
case 2:
printf("Unexpected character: '%c'\n", termch);
break;
default:
printf("Please enter an integer\n");
break;
}
FreeBlock(line);
printf("Retry: ");
}
}
#endif
|