From 781b2a1c0bb3e578ed09ecefb1be692f716f7d08 Mon Sep 17 00:00:00 2001 From: juan Date: Thu, 2 Jun 2022 11:59:54 +0800 Subject: [PATCH] no everything ac --- semester2/pset3/1-filesOps.c | 134 +++++++++++++++++------------------ semester2/pset3/3-readFile.c | 8 +-- 2 files changed, 70 insertions(+), 72 deletions(-) diff --git a/semester2/pset3/1-filesOps.c b/semester2/pset3/1-filesOps.c index 401228b..84a4761 100644 --- a/semester2/pset3/1-filesOps.c +++ b/semester2/pset3/1-filesOps.c @@ -1,8 +1,5 @@ #include -#define FILENAME dict.dic -#define BUFLEN 65535 - void countChar(FILE *fp); void countLines(FILE *fp); void countCaps(FILE *fp); @@ -13,21 +10,21 @@ int main(void) { int op; scanf("%d", &op); switch (op) { - case (1): { - countChar(fp); - break; - } - case (2): { - countLines(fp); - break; - } - case (3): { - countCaps(fp); - break; - } - default: { - break; - } + case (1): { + countChar(fp); + break; + } + case (2): { + countLines(fp); + break; + } + case (3): { + countCaps(fp); + break; + } + default: { + break; + } } fclose(fp); @@ -37,7 +34,7 @@ int main(void) { void countChar(FILE *fp) { int cap = 0, low = 0, digits = 0, others = 0; char buf; - while ((buf = fgetc(fp)) != EOF) { + while ((buf = getc(fp)) != EOF) { if (buf >= 'A' && buf <= 'Z') { cap++; } else if (buf >= 'a' && buf <= 'z') { @@ -54,58 +51,59 @@ void countChar(FILE *fp) { printf("digit: %d\n", digits); printf("others: %d\n", others); } + void countLines(FILE *fp) { + // Scan one line first + char buf; int lines = 0; - int maxlen, minlen; - int i; - char buf[BUFLEN]; - - // Firstly read one line - fgets(buf, BUFLEN * sizeof(buf[0]), fp); - lines++; - for (i = 0; buf[i] != '\n'; i++) { - } - maxlen = i; - minlen = i; - - while (fgets(buf, sizeof(buf), fp) != NULL) { - lines++; - for (i = 0; buf[i] != '\n'; i++) { + int linemax = 0; + int linemin = 0; + int i = 0; + while ((buf = fgetc(fp)) != EOF) { + if (buf != '\n') { + i++; + } else { + if (lines == 0) { + linemax = i; + linemin = i; + } else { + if (i > linemax) { + linemax = i; + } + if (i < linemin) { + linemin = i; + } + } + lines++; + i = 0; + } + } + printf("Task2:\nline: %d\n%d characters in max line.\n%d characters in min " + "line.\n", + lines, linemax, linemin); +} + +void countCaps(FILE *fp) { + int low[26] = {}; + int cap[26] = {}; + + char buf; + while ((buf = fgetc(fp)) != EOF) { + if (buf >= 'a' && buf <= 'z') { + low[buf - 'a']++; + } else if (buf >= 'A' && buf <= 'Z') { + cap[buf - 'A']++; } - if (i > maxlen) { - maxlen = i; - } - if (i < minlen) { - minlen = i; - } } - printf("Task2:\n"); - printf("line: %d\n", lines); - printf("%d characters in max line.\n", maxlen); - printf("%d characters in min line.\n", minlen); -} -void countCaps(FILE *fp) { - char low[26] = {}; - char cap[26] = {}; - - char buf; - while ((buf = fgetc(fp)) != EOF) { - if (buf >= 'a' && buf <= 'z') { - low[buf - 'a']++; - } else if (buf >= 'A' && buf <= 'Z') { - cap[buf - 'A']++; - } - } - - printf("Task3:\n"); - printf("CAPITAL:\n"); - for (int i = 0; i < 26; i++) { - printf("%c:%d\n", i + 'A', cap[i]); - } - - printf("LOWERCASE:\n"); - for (int i = 0; i < 26; i++) { - printf("%c:%d\n", i + 'a', low[i]); - } + printf("Task3:\n"); + printf("CAPITAL:\n"); + for (int i = 0; i < 26; i++) { + printf("%c:%d\n", i + 'A', cap[i]); + } + + printf("LOWERCASE:\n"); + for (int i = 0; i < 26; i++) { + printf("%c:%d\n", i + 'a', low[i]); + } } diff --git a/semester2/pset3/3-readFile.c b/semester2/pset3/3-readFile.c index 0942e1f..ef9901c 100644 --- a/semester2/pset3/3-readFile.c +++ b/semester2/pset3/3-readFile.c @@ -7,20 +7,20 @@ int main(void) { int a; char b[16]; // bool - double c; + char c[4096]; char d[4096]; int n; scanf("%d", &n); for (int i = 0; i < n; i++) { - fscanf(fp, "%i%s%lf%s", &a, b, &c, d); + fscanf(fp, "%i%s%s%s\n", &a, b, c, d); } printf("%i\n", a); printf("%s\n", b); - printf("%lf\n", c); - printf("%s\n", d); + printf("%s\n", c); + printf("%s", d); return 0; }