Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
昌润 叶
Lesson1task
Commits
5df87d79
Commit
5df87d79
authored
May 15, 2023
by
昌润 叶
Browse files
task1
parent
0df2cf64
Changes
22
Hide whitespace changes
Inline
Side-by-side
src/simpio.c
0 → 100644
View file @
5df87d79
/*
* File: simpio.c
* Version: 3.0
* Last modified on Tue Oct 4 11:24:40 1994 by eroberts
* -----------------------------------------------------
* This file implements the simpio.h interface.
*/
#include
<stdio.h>
#include
<string.h>
#include
"genlib.h"
#include
"strlib.h"
#include
"simpio.h"
/*
* Constants:
* ----------
* InitialBufferSize -- Initial buffer size for ReadLine
*/
#define InitialBufferSize 120
/* Exported entries */
/*
* Functions: GetInteger, GetLong, GetReal
* ---------------------------------------
* These functions first read a line and then call sscanf to
* translate the number. Reading an entire line is essential to
* good error recovery, because the characters after the point of
* error would otherwise remain in the input buffer and confuse
* subsequent input operations. The sscanf line allows white space
* before and after the number but no other extraneous characters.
*/
int
GetInteger
(
void
)
{
string
line
;
int
value
;
char
termch
;
while
(
TRUE
)
{
line
=
GetLine
();
if
(
line
==
NULL
)
Error
(
"GetInteger: unexpected end of file"
);
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
(
TRUE
)
{
line
=
GetLine
();
if
(
line
==
NULL
)
Error
(
"GetLong: unexpected end of file"
);
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
(
TRUE
)
{
line
=
GetLine
();
if
(
line
==
NULL
)
Error
(
"GetReal: unexpected end of file"
);
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 a real number
\n
"
);
break
;
}
FreeBlock
(
line
);
printf
(
"Retry: "
);
}
}
/*
* Function: GetLine
* -----------------
* This function is a simple wrapper; all the work is done by
* ReadLine.
*/
string
GetLine
(
void
)
{
return
(
ReadLine
(
stdin
));
}
/*
* Function: ReadLine
* ------------------
* This function operates by reading characters from the file
* into a dynamically allocated buffer. If the buffer becomes
* full before the end of the line is reached, a new buffer
* twice the size of the previous one is allocated.
*/
string
ReadLine
(
FILE
*
infile
)
{
string
line
,
nline
;
int
n
,
ch
,
size
;
n
=
0
;
size
=
InitialBufferSize
;
line
=
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
);
}
src/strlib.c
0 → 100644
View file @
5df87d79
/*
* File: strlib.c
* Version: 1.0
* Last modified on Fri Jul 15 14:10:41 1994 by eroberts
* -----------------------------------------------------
* This file implements the strlib.h interface.
*/
/*
* General implementation notes:
* -----------------------------
* This module implements the strlib library by mapping all
* functions into the appropriate calls to the ANSI <string.h>
* interface. The implementations of the individual functions
* are all quite simple and do not require individual comments.
* For descriptions of the behavior of each function, see the
* interface.
*/
#include
<stdio.h>
#include
<string.h>
#include
<ctype.h>
#include
"genlib.h"
#include
"strlib.h"
/*
* Constant: MaxDigits
* -------------------
* This constant must be larger than the maximum
* number of digits that can appear in a number.
*/
#define MaxDigits 30
/* Private function prototypes */
static
string
CreateString
(
int
len
);
/* Section 1 -- Basic string operations */
string
Concat
(
string
s1
,
string
s2
)
{
string
s
;
int
len1
,
len2
;
if
(
s1
==
NULL
||
s2
==
NULL
)
{
Error
(
"NULL string passed to Concat"
);
}
len1
=
strlen
(
s1
);
len2
=
strlen
(
s2
);
s
=
CreateString
(
len1
+
len2
);
strcpy
(
s
,
s1
);
strcpy
(
s
+
len1
,
s2
);
return
(
s
);
}
char
IthChar
(
string
s
,
int
i
)
{
int
len
;
if
(
s
==
NULL
)
Error
(
"NULL string passed to IthChar"
);
len
=
strlen
(
s
);
if
(
i
<
0
||
i
>
len
)
{
Error
(
"Index outside of string range in IthChar"
);
}
return
(
s
[
i
]);
}
string
SubString
(
string
s
,
int
p1
,
int
p2
)
{
int
len
;
string
result
;
if
(
s
==
NULL
)
Error
(
"NULL string passed to SubString"
);
len
=
strlen
(
s
);
if
(
p1
<
0
)
p1
=
0
;
if
(
p2
>=
len
)
p2
=
len
-
1
;
len
=
p2
-
p1
+
1
;
if
(
len
<
0
)
len
=
0
;
result
=
CreateString
(
len
);
strncpy
(
result
,
s
+
p1
,
len
);
result
[
len
]
=
'\0'
;
return
(
result
);
}
string
CharToString
(
char
ch
)
{
string
result
;
result
=
CreateString
(
1
);
result
[
0
]
=
ch
;
result
[
1
]
=
'\0'
;
return
(
result
);
}
int
StringLength
(
string
s
)
{
if
(
s
==
NULL
)
Error
(
"NULL string passed to StringLength"
);
return
(
strlen
(
s
));
}
string
CopyString
(
string
s
)
{
string
newstr
;
if
(
s
==
NULL
)
Error
(
"NULL string passed to CopyString"
);
newstr
=
CreateString
(
strlen
(
s
));
strcpy
(
newstr
,
s
);
return
(
newstr
);
}
/* Section 2 -- String comparison functions */
bool
StringEqual
(
string
s1
,
string
s2
)
{
if
(
s1
==
NULL
||
s2
==
NULL
)
{
Error
(
"NULL string passed to StringEqual"
);
}
return
(
strcmp
(
s1
,
s2
)
==
0
);
}
int
StringCompare
(
string
s1
,
string
s2
)
{
if
(
s1
==
NULL
||
s2
==
NULL
)
{
Error
(
"NULL string passed to StringCompare"
);
}
return
(
strcmp
(
s1
,
s2
));
}
/* Section 3 -- Search functions */
int
FindChar
(
char
ch
,
string
text
,
int
start
)
{
char
*
cptr
;
if
(
text
==
NULL
)
Error
(
"NULL string passed to FindChar"
);
if
(
start
<
0
)
start
=
0
;
if
(
start
>
strlen
(
text
))
return
(
-
1
);
cptr
=
strchr
(
text
+
start
,
ch
);
if
(
cptr
==
NULL
)
return
(
-
1
);
return
((
int
)
(
cptr
-
text
));
}
int
FindString
(
string
str
,
string
text
,
int
start
)
{
char
*
cptr
;
if
(
str
==
NULL
)
Error
(
"NULL pattern string in FindString"
);
if
(
text
==
NULL
)
Error
(
"NULL text string in FindString"
);
if
(
start
<
0
)
start
=
0
;
if
(
start
>
strlen
(
text
))
return
(
-
1
);
cptr
=
strstr
(
text
+
start
,
str
);
if
(
cptr
==
NULL
)
return
(
-
1
);
return
((
int
)
(
cptr
-
text
));
}
/* Section 4 -- Case-conversion functions */
string
ConvertToLowerCase
(
string
s
)
{
string
result
;
int
i
;
if
(
s
==
NULL
)
{
Error
(
"NULL string passed to ConvertToLowerCase"
);
}
result
=
CreateString
(
strlen
(
s
));
for
(
i
=
0
;
s
[
i
]
!=
'\0'
;
i
++
)
result
[
i
]
=
tolower
(
s
[
i
]);
result
[
i
]
=
'\0'
;
return
(
result
);
}
string
ConvertToUpperCase
(
string
s
)
{
string
result
;
int
i
;
if
(
s
==
NULL
)
{
Error
(
"NULL string passed to ConvertToUpperCase"
);
}
result
=
CreateString
(
strlen
(
s
));
for
(
i
=
0
;
s
[
i
]
!=
'\0'
;
i
++
)
result
[
i
]
=
toupper
(
s
[
i
]);
result
[
i
]
=
'\0'
;
return
(
result
);
}
/* Section 5 -- Functions for converting numbers to strings */
string
IntegerToString
(
int
n
)
{
char
buffer
[
MaxDigits
];
sprintf
(
buffer
,
"%d"
,
n
);
return
(
CopyString
(
buffer
));
}
int
StringToInteger
(
string
s
)
{
int
result
;
char
dummy
;
if
(
s
==
NULL
)
{
Error
(
"NULL string passed to StringToInteger"
);
}
if
(
sscanf
(
s
,
" %d %c"
,
&
result
,
&
dummy
)
!=
1
)
{
Error
(
"StringToInteger called on illegal number %s"
,
s
);
}
return
(
result
);
}
string
RealToString
(
double
d
)
{
char
buffer
[
MaxDigits
];
sprintf
(
buffer
,
"%G"
,
d
);
return
(
CopyString
(
buffer
));
}
double
StringToReal
(
string
s
)
{
double
result
;
char
dummy
;
if
(
s
==
NULL
)
Error
(
"NULL string passed to StringToReal"
);
if
(
sscanf
(
s
,
" %lg %c"
,
&
result
,
&
dummy
)
!=
1
)
{
Error
(
"StringToReal called on illegal number %s"
,
s
);
}
return
(
result
);
}
/* Private functions */
/*
* Function: CreateString
* Usage: s = CreateString(len);
* -----------------------------
* This function dynamically allocates space for a string of
* len characters, leaving room for the null character at the
* end.
*/
static
string
CreateString
(
int
len
)
{
return
((
string
)
GetBlock
(
len
+
1
));
}
Prev
1
2
Next
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment