2019-10-03 in class
This commit is contained in:
parent
89ac20cdf0
commit
93c8c5f0d8
3 changed files with 73 additions and 0 deletions
50
myscanf.c
Normal file
50
myscanf.c
Normal file
|
@ -0,0 +1,50 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <ctype.h>
|
||||
#include <limits.h>
|
||||
|
||||
char next_sign_char();
|
||||
|
||||
inline char next_sign_char() {
|
||||
char c;
|
||||
while (isspace(c = getchar()));
|
||||
return c;
|
||||
}
|
||||
|
||||
int read_int(int* p) {
|
||||
int i = 0;
|
||||
bool pos = true;
|
||||
|
||||
char c = next_sign_char();
|
||||
|
||||
if (c == '-' || c == '+') {
|
||||
pos = c == '+';
|
||||
} else if (!isdigit(c)) {
|
||||
return 0;
|
||||
} else {
|
||||
i = c - '0';
|
||||
}
|
||||
|
||||
while ((c = getchar()) != EOF && c != '\n' && c != ' ') {
|
||||
if (!isdigit(c)) return 0;
|
||||
int digit = c - '0';
|
||||
if ((pos && i > (MAX_INT - digit) / 10) ||
|
||||
(!pos && i > (-1 * MIN_INT - digit) / 10)) {
|
||||
break;
|
||||
}
|
||||
i *= 10;
|
||||
i += c - '0';
|
||||
}
|
||||
|
||||
*p = pos ? i : -i;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
int a;
|
||||
read_int(&a);
|
||||
printf("%d", a);
|
||||
}
|
23
revargs.c
Normal file
23
revargs.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
// vim: set ts=4 sw=4 et tw=80:
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
bool compare_reverse(char* s1, char* s2) {
|
||||
const size_t len = strlen(s1);
|
||||
if (len != strlen(s2))
|
||||
return false;
|
||||
for (size_t i = 0; i < len; i++)
|
||||
if (s1[i] != s2[len - 1 - i])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
int main (const int argc, char** argv) {
|
||||
for (int i = 1; i < argc - 1; i++)
|
||||
for (int j = i + 1; j < argc; j++)
|
||||
if (compare_reverse(argv[i], argv[j]))
|
||||
printf("%s %s\n", argv[i], argv[j]);
|
||||
}
|
Reference in a new issue