task4
點選檢視程式碼
#include<stdio.h>
int main(){
FILE *fp;
int sum=0;
char ch;
fp=fopen("data4.txt","r");
if(fp==NULL){
printf("不能開啟檔案\n");
return 1;
}
ch=fgetc(fp);
while((ch=fgetc(fp))!=EOF){
if(ch!=' '&&ch!='\t'){
++sum;
}
}
fclose(fp);
printf("檔案中字元數為:%d",sum);
return 0;
}
task5
點選檢視程式碼
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 10
typedef struct {
long int id;
char name[20];
float objective; // 客觀題得分
float subjective; // 操作題得分
float sum; // 總分
char ans[10]; // 考試結果
} STU;
// 函式宣告
void finput(STU st[], int n);
void foutput(STU st[], int n);
void output(STU st[], int n);
int process(STU st[], int n, STU st_pass[]);
int main() {
STU stu[N], stu_pass[N];
int cnt;
double pass_rate;
printf("從檔案讀入%d個考生資訊...\n", N);
finput(stu, N);
printf("\n對考生成績進行統計...\n");
cnt = process(stu, N, stu_pass);
printf("\n透過考試的名單:\n");
output(stu_pass, cnt); // 輸出到螢幕
foutput(stu_pass, cnt); // 輸出到檔案
pass_rate = 1.0 * cnt / N;
printf("\n本次等級考試透過率: %.2f%%\n", pass_rate*100);
return 0;
}
// 把透過考試的考生完整資訊輸出到螢幕上
// 准考證號,姓名,客觀題得分,操作題得分,總分,結果
void output(STU st[], int n) {
int i;
printf("准考證號\t姓名\t客觀題得分\t操作題得分\t總分\t\t結果\n");
for (i = 0; i < n; i++)
printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].ans);
}
// 從文字檔案examinee.txt讀入考生資訊:准考證號,姓名,客觀題得分,操作題得分
void finput(STU st[], int n) {
int i;
FILE *fin;
fin = fopen("examinee.txt", "r");
if (fin == NULL) {
printf("fail to open file\n");
exit(0);
}
while (!feof(fin)) {
for (i = 0; i < n; i++)
fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
}
fclose(fin);
}
// 把透過考試的考生完整資訊寫入檔案list_pass.txt
// 准考證號,姓名,客觀題得分,操作題得分,總分,結果
void foutput(STU s[], int n) {
FILE *fout;
int i;
// 儲存到檔案
fout = fopen("list_pass.txt", "w");
if (!fout) {
printf("fail to open or create list_pass.txt\n");
exit(0);
}
fprintf(fout, "准考證號\t\t姓名\t客觀題得分\t操作題得分\t總分\t\t結果\n");
for (i = 0; i < n; i++)
fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].ans);
fclose(fout);
}
// 對考生資訊進行處理:計算每位考生考試總分、結果;統計考試透過的人數
int process(STU st[], int n, STU st_pass[]) {
int i,j=0;
for (i = 0; i < n; i++) {
st[i].sum = st[i].objective + st[i].subjective;
if (st[i].sum > 60) {
strcpy(st[i].ans, "pass");
st_pass[j] = st[i];
j++;
}
else
strcpy(st[i].ans, "fail");
}
return j;
}
task6
點選檢視程式碼
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
#define N 50
typedef struct {
long int id;
char name[N];
char cl[N];
} STU;
STU lucky[N],st[N];
void finput();
void foutput();
int main() {
int m, n;
finput();
srand(time(NULL));
for (m = 0; m < 5; m++) {
n = rand() % 80;
lucky[m] = st[n];
printf("%ld\t %s\t %s\t\n", lucky[m].id, lucky[m].name, lucky[m].cl);
}
foutput();
getchar();
system("pause");
return 0;
}
void finput() {
int i;
FILE *fin;
fin = fopen("list.txt", "r");
if (fin == NULL) {
printf("fail to open file\n");
return;
}
while (!feof(fin)) {
for (i = 0; i < 80; i++)
fscanf(fin, "%ld %s %s", &st[i].id, st[i].name,&st[i].cl);
}
fclose(fin);
}
void foutput() {
FILE *fout;
int i,n=5;
fout = fopen("lucky.txt", "w");
if (!fout) {
printf("fail to open or create lucky.txt\n");
return;
}
for (i = 0; i < n; i++)
fprintf(fout, "%ld\t\t%s\t\t\t%s\n", lucky[i].id,lucky[i].name,lucky[i].cl);
fclose(fout);
}