|
75樓 巨大八爪鱼
2016-1-16 20:15
【9-3】 #include <stdio.h> #include <stdlib.h>
void main() { int num, pn = 0, nn = 0; FILE *fp; if ((fp = fopen("test.txt", "r")) == NULL) { printf("文件打不開\n"); exit(0); } while (!feof(fp)) { fscanf(fp, "%d", &num); if (num > 0) pn++; else if (num < 0) nn++; } printf("positive=%d\nnegative=%d\n", pn, nn); }
|
|
76樓 巨大八爪鱼
2016-1-16 20:15
剛才Arslanbar突然不能訪問了。。。。
|
|
77樓 巨大八爪鱼
2016-1-16 20:33
【9-4】 #include <stdio.h> #include <stdlib.h>
void main() { FILE *fp1, *fp2; char ch; if ((fp1 = fopen("file1.txt", "r")) == NULL || (fp2 = fopen("file2.txt", "w")) == NULL) { printf("無法打開文件\n"); exit(0); } while (ch = fgetc(fp1), !feof(fp1)) { putchar(ch); fputc(ch, fp2); } fclose(fp1); fclose(fp2); }
|
|
78樓 巨大八爪鱼
2016-1-16 20:37
【9-5】 #include <stdio.h> #include <stdlib.h>
void main() { FILE *fp; char ch; int a = 0, b = 0; if ((fp = fopen("3.c", "r")) == NULL) { printf("文件打不開\n"); exit(0); } while (ch = fgetc(fp), !feof(fp)) { if (ch == '{') a++; else if (ch == '}') b++; } if (a == b) printf("OK!\n"); else printf("ERROR!\n"); }
|
|
79樓 巨大八爪鱼
2016-1-16 20:46
【9-6】 #include <stdio.h> #include <stdlib.h>
void main() { char filename[100]; FILE *fp; int count[26] = {0}; // 數組一定要初始化後再使用! char ch; int i; gets(filename); if ((fp = fopen(filename, "r")) == NULL) { printf("文件打不開\n"); exit(0); } while (ch = fgetc(fp), !feof(fp)) { if (ch >= 'A' && ch <= 'Z') count[ch - 'A']++; else if (ch >= 'a' && ch <= 'z') count[ch - 'a']++; } for (i = 0; i < 26; i++) { printf("%c(%c)=%4d ", 'A' + i, 'a' + i, count[i]); if ((i + 1) % 5 == 0) putchar('\n'); } putchar('\n'); fclose(fp); }
|
|
80樓 巨大八爪鱼
2016-1-16 21:21
【9-7】 輸入若干個字符串排序。。。。這個挺複雜的 #include <stdio.h> #include <stdlib.h> #include <string.h>
void main() { FILE *fp; int i, j, n, min_j; char *temp; char *spaceHead; char **strlist; if ((fp = fopen("string.txt", "w")) == NULL) { printf("文件打不開\n"); exit(0); } printf("Enter number:\n"); scanf("%d", &n); strlist = (char **)calloc(n, sizeof(char *)); spaceHead = (char *)calloc(n, 100 * sizeof(char)); printf("Enter strings:\n"); gets(spaceHead); // remove the \n that follows the just input number for (i = 0; i < n; i++) { strlist[i] = spaceHead + 100 * i; gets(strlist[i]); } for (i = 0; i < n - 1; i++) { min_j = i + 1; for (j = i + 2; j < n; j++) { if (strcmp(strlist[j], strlist[min_j]) < 0) min_j = j; } if (strcmp(strlist[i], strlist[min_j]) > 0) { temp = strlist[i]; strlist[i] = strlist[min_j]; strlist[min_j] = temp; } } printf("\nThe new sequence:\n"); for (i = 0; i < n; i++) { puts(strlist[i]); fputs(strlist[i], fp); } free(spaceHead); free(strlist); fclose(fp); }
運行結果: Enter number: 8 Enter strings: Blue Pink Yellow Green Red Black White Purple
The new sequence: Black Blue Green Pink Purple Red White Yellow
-------------------------------- Process exited after 41.32 seconds with return value 0 Press any key to continue . . .
|
|
81樓 巨大八爪鱼
2016-1-16 21:24
注意,雖然puts可以在螢幕上輸出換行符,但是fputs不行。。 所以要單獨加一個fputc: fputs(strlist[i], fp); fputc('\n', fp);
|
|
82樓 巨大八爪鱼
2016-1-16 21:35
【9-8】 #include <stdio.h> #include <stdlib.h>
#define N 10
struct student { int id; char name[50]; int age; char address[50]; } stus[N];
void save() { FILE *fp; int i; if ((fp = fopen("stu.dat", "w")) == NULL) { printf("文件打不開\n"); exit(0); } for (i = 0; i < N; i++) fwrite(&stus[i], sizeof(stus[i]), 1, fp); fclose(fp); }
void main() { int i; printf("Please enter data of 10 students:\n"); for (i = 0; i < N; i++) { scanf("%d%s%d%s", &stus[i].id, stus[i].name, &stus[i].age, stus[i].address); } save(); }
|
|
83樓 巨大八爪鱼
2016-1-16 21:46
【9-9】 #include <stdio.h>
struct student { int id; char name[50]; int age; char address[50]; } stu;
void main() { int i; FILE *fp; if ((fp = fopen("stu.dat", "r")) == NULL) { printf("文件打不開!\n"); exit(0); } for (i = 1; i <= 9; i += 2) { fseek(fp, (i - 1) * sizeof(struct student), SEEK_SET); fread(&stu, sizeof(struct student), 1, fp); printf("%d %10s %d %s\n", stu.id, stu.name, stu.age, stu.address); } fclose(fp); } 運行結果: 201101 Lily 19 room01 201103 Sunny 21 room03 201105 John 19 room05 201107 Sunny 20 room06 201109 Joe 20 room07
-------------------------------- Process exited after 0.006697 seconds with return value 0 Press any key to continue . . .
|
|
84樓 巨大八爪鱼
2016-1-16 21:47
201101 Lily 19 room01 201103 Sunny 21 room03 201105 John 19 room05 201107 Sunny 20 room06 201109 Joe 20 room07
-------------------------------- Process exited after 0.004761 seconds with return value 0 Press any key to continue . . . 輸出的時候注意要寫%-10s才對!
|