Add 3 probs
This commit is contained in:
parent
dc274569c4
commit
0cb114e030
111
semester2/pset3/1-filesOps.c
Normal file
111
semester2/pset3/1-filesOps.c
Normal file
|
@ -0,0 +1,111 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#define FILENAME dict.dic
|
||||
#define BUFLEN 65535
|
||||
|
||||
void countChar(FILE *fp);
|
||||
void countLines(FILE *fp);
|
||||
void countCaps(FILE *fp);
|
||||
|
||||
int main(void) {
|
||||
FILE *fp = fopen("dict.dic", "r");
|
||||
|
||||
int op;
|
||||
scanf("%d", &op);
|
||||
switch (op) {
|
||||
case (1): {
|
||||
countChar(fp);
|
||||
break;
|
||||
}
|
||||
case (2): {
|
||||
countLines(fp);
|
||||
break;
|
||||
}
|
||||
case (3): {
|
||||
countCaps(fp);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void countChar(FILE *fp) {
|
||||
int cap = 0, low = 0, digits = 0, others = 0;
|
||||
char buf;
|
||||
while ((buf = fgetc(fp)) != EOF) {
|
||||
if (buf >= 'A' && buf <= 'Z') {
|
||||
cap++;
|
||||
} else if (buf >= 'a' && buf <= 'z') {
|
||||
low++;
|
||||
} else if (buf >= '0' && buf <= '9') {
|
||||
digits++;
|
||||
} else {
|
||||
others++;
|
||||
}
|
||||
}
|
||||
printf("Task1:\n");
|
||||
printf("capital: %d\n", cap);
|
||||
printf("lowercase: %d\n", low);
|
||||
printf("digit: %d\n", digits);
|
||||
printf("others: %d\n", others);
|
||||
}
|
||||
void countLines(FILE *fp) {
|
||||
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++) {
|
||||
}
|
||||
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]);
|
||||
}
|
||||
}
|
71
semester2/pset3/2-write.c
Normal file
71
semester2/pset3/2-write.c
Normal file
|
@ -0,0 +1,71 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
FILE *fp = fopen("dict.dic", "rb");
|
||||
|
||||
char a,
|
||||
aa[5]; //注意这里数组aa为正常使用的字符串,所以会包含字符'\0',它不需要输出
|
||||
short b, bb[5];
|
||||
int c, cc[5];
|
||||
long d, dd[5];
|
||||
long long e, ee[5];
|
||||
float g, gg[5];
|
||||
double h, hh[5];
|
||||
|
||||
int n;
|
||||
scanf("%d", &n);
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
fread(&a, sizeof(a), 1, fp);
|
||||
fread(&b, sizeof(b), 1, fp);
|
||||
fread(&c, sizeof(c), 1, fp);
|
||||
fread(&d, sizeof(d), 1, fp);
|
||||
fread(&e, sizeof(e), 1, fp);
|
||||
fread(&g, sizeof(g), 1, fp);
|
||||
fread(&h, sizeof(h), 1, fp);
|
||||
fread(aa, sizeof(a), 5, fp);
|
||||
fread(bb, sizeof(b), 5, fp);
|
||||
fread(cc, sizeof(c), 5, fp);
|
||||
fread(dd, sizeof(d), 5, fp);
|
||||
fread(ee, sizeof(e), 5, fp);
|
||||
fread(gg, sizeof(g), 5, fp);
|
||||
fread(hh, sizeof(h), 5, fp);
|
||||
}
|
||||
|
||||
printf("%c\n", a);
|
||||
printf("%i\n", b);
|
||||
printf("%d\n", c);
|
||||
printf("%ld\n", d);
|
||||
printf("%lld\n", e);
|
||||
printf("%f\n", g);
|
||||
printf("%lf\n", h);
|
||||
|
||||
printf("%s\n", aa);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
printf("%i ", bb[i]);
|
||||
}
|
||||
printf("%i\n", bb[4]);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
printf("%i ", cc[i]);
|
||||
}
|
||||
printf("%i\n", cc[4]);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
printf("%li ", dd[i]);
|
||||
}
|
||||
printf("%li\n", dd[4]);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
printf("%lli ", ee[i]);
|
||||
}
|
||||
printf("%lli\n", ee[4]);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
printf("%f ", gg[i]);
|
||||
}
|
||||
printf("%f\n", gg[4]);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
printf("%lf ", hh[i]);
|
||||
}
|
||||
printf("%lf\n", hh[4]);
|
||||
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
26
semester2/pset3/3-readFile.c
Normal file
26
semester2/pset3/3-readFile.c
Normal file
|
@ -0,0 +1,26 @@
|
|||
#include <stdio.h>
|
||||
#include <strings.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(void) {
|
||||
FILE *fp = fopen("dict.dic", "r");
|
||||
|
||||
int a;
|
||||
char b[16]; // bool
|
||||
double c;
|
||||
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);
|
||||
}
|
||||
|
||||
printf("%i\n", a);
|
||||
printf("%s\n", b);
|
||||
printf("%lf\n", c);
|
||||
printf("%s\n", d);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue