最近要复习一下C和C++的基础知识,于是计划把之前学过的谭浩强的《C程序设计》和《C++程序设计》习题重新做一遍。
编译环境为:操作系统32位Win7,编译工具VC++6.0
第十三章:文件
13.4)从键盘输入一个字符串,将其中的小写字母全部转换成大写字母,然后输出到文件test中,输入的字符以'!'结束
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
char* filename = "test.txt";
if((fp = fopen(filename, "w")) == NULL)
{
printf("Open File %s Failed!\n", filename);
}
char ch;
while(ch = getchar())
{
//putchar(ch);
fputc(ch, fp);
if(ch == '!')
{
break;
}
}
fclose(fp);
}
13.5)有两个磁盘文件A和B,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列),输出到一个新文件C中
#include<stdio.h>
#include<stdlib.h>
void main()
{
char ch, s[100];
int counter = 0;
FILE *fp1;
char *file1 = "A.txt";
if((fp1 = fopen(file1, "r")) == NULL)
{
printf("Open File %s Failed!\n", file1);
exit(0);
}
while(!feof(fp1))
{
ch = fgetc(fp1);
putchar(ch);
if(ch != '\n' && ch != '\r')
{
s[counter++] = ch;
}
}
fclose(fp1);
FILE *fp2;
char *file2 = "B.txt";
if((fp2 = fopen(file2, "r")) == NULL)
{
printf("Open File %s Failed!\n", file2);
exit(0);
}
while(!feof(fp2))
{
ch = fgetc(fp2);
putchar(ch);
if(ch != '\n' && ch != '\r')
{
s[counter++] = ch;
}
}
fclose(fp2);
s[counter] = '\0';
int i, j;
for(i = 0; i < counter; i++)
{
for(j = i + 1; j < counter; j++)
{
if(s[i] > s[j])
{
ch = s[i];
s[i] = s[j];
s[j] = ch;
}
}
}
printf("处理后的字符串:%s\n", s);
}
13.6)有5个学生,每个学生有3门课程的成绩,从键盘输入学生数据(包括学号、姓名、3门课的成绩),计算平均成绩,将原有数据和计算出的平均分数存放在磁盘文件stud中
#include<stdio.h>
struct Student
{
int id;
char name[20];
int score[3];
}
student[3];
void main()
{
/* 示例输入
1 tsybius 1 2 3
2 galatea 2 3 4
3 aurelia 3 4 5
*/
int i;
for(i = 0; i < 3; i++)
{
scanf("%d%s%d%d%d",
&student[i].id,
student[i].name,
&student[i].score[0],
&student[i].score[1],
&student[i].score[2]);
}
FILE* fp;
char* filename = "stud.txt";
if((fp = fopen(filename, "w")) == NULL)
{
printf("Open File %s Failed!\n", filename);
}
for(i = 0; i < 3; i++)
{
fprintf(fp, "%d %s %d %d %d %d\n",
student[i].id,
student[i].name,
student[i].score[0],
student[i].score[1],
student[i].score[2],
(student[i].score[0] + student[i].score[1] + student[i].score[2]) / 3);
}
fclose(fp);
}
13.7)将13.6中stud文件中的学生数据,按平均分进行排序处理,将已经排序的学生数据存入到一个新文件stu_sort中
#include<stdio.h>
#include<stdlib.h>
struct Student
{
int id;
char name[20];
int score[3];
int average;
};
void main()
{
int i, j;
Student* student[3];
student[0] = new Student;
student[1] = new Student;
student[2] = new Student;
FILE *fp;
char *filename1 = "stud.txt";
if((fp = fopen(filename1, "r")) == NULL)
{
printf("Open File %s Failed!\n", filename1);
exit(0);
}
for(i = 0; i < 3; i++)
{
fscanf(fp, "%d%s%d%d%d%d",
&student[i] -> id,
student[i] -> name,
&student[i] -> score[0],
&student[i] -> score[1],
&student[i] -> score[2],
&student[i] -> average);
}
fclose(fp);
Student* temp;
for(i = 0; i < 3; i++)
{
for(j = i + 1; j < 3; j++)
{
if(student[i] -> average < student[j] -> average)
{
temp = student[i];
student[i] = student[j];
student[j] = temp;
}
}
}
char* filename2 = "stu_sort.txt";
if((fp = fopen(filename2, "w")) == NULL)
{
printf("Open File %s Failed!\n", filename2);
}
for(i = 0; i < 3; i++)
{
fprintf(fp, "%d %s %d %d %d\n",
student[i] -> id,
student[i] -> name,
student[i] -> score[0],
student[i] -> score[1],
student[i] -> score[2]);
}
fclose(fp);
}
13.8)将13.7中已排序的学生成绩文件进行插入处理,插入一个学生的3门课程成绩,程序先计算新插入学生的平均成绩,然后将它按成绩高低顺序插入,插入后建立一个新文件
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Student
{
int id;
char name[20];
int score[3];
};
void main()
{
Student* newperson = new Student;
newperson -> id = 4;
strcpy(newperson -> name, "julius");
newperson -> score[0] = 2;
newperson -> score[1] = 3;
newperson -> score[2] = 4;
int avrg_newperson = (newperson -> score[0] +
newperson -> score[1] + newperson -> score[2]) / 3;
Student* student[3];
student[0] = new Student;
student[1] = new Student;
student[2] = new Student;
FILE *fp;
char* filename1 = "stu_sort.txt";
if((fp = fopen(filename1, "r")) == NULL)
{
printf("Open File %s Failed!\n", filename1);
}
int i;
for(i = 0; i < 3; i++)
{
fscanf(fp, "%d%s%d%d%d",
&student[i] -> id,
student[i] -> name,
&student[i] -> score[0],
&student[i] -> score[1],
&student[i] -> score[2]);
}
fclose(fp);
char* filename2 = "stu_sort_2.txt";
if((fp = fopen(filename2, "w")) == NULL)
{
printf("Open File %s Failed!\n", filename2);
}
int average;
for(i = 0; i < 3; i++)
{
average = (student[i] -> score[0] +
student[i] -> score[1] + student[i] -> score[2]) / 3;
if(average < avrg_newperson)
{
fprintf(fp, "%d %s %d %d %d\n",
newperson -> id,
newperson -> name,
newperson -> score[0],
newperson -> score[1],
newperson -> score[2]);
}
fprintf(fp, "%d %s %d %d %d\n",
student[i] -> id,
student[i] -> name,
student[i] -> score[0],
student[i] -> score[1],
student[i] -> score[2]);
}
fclose(fp);
}
13.9)将题13.8结果仍存入到原有的stu_sort文件而不是另立新文件
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Student
{
int id;
char name[20];
int score[3];
};
void main()
{
Student* newperson = new Student;
newperson -> id = 4;
strcpy(newperson -> name, "julius");
newperson -> score[0] = 2;
newperson -> score[1] = 3;
newperson -> score[2] = 4;
int avrg_newperson = (newperson -> score[0] +
newperson -> score[1] + newperson -> score[2]) / 3;
Student* student[3];
student[0] = new Student;
student[1] = new Student;
student[2] = new Student;
FILE *fp;
char* filename1 = "stu_sort.txt";
if((fp = fopen(filename1, "r")) == NULL)
{
printf("Open File %s Failed!\n", filename1);
}
int i;
for(i = 0; i < 3; i++)
{
fscanf(fp, "%d%s%d%d%d",
&student[i] -> id,
student[i] -> name,
&student[i] -> score[0],
&student[i] -> score[1],
&student[i] -> score[2]);
}
fclose(fp);
char* filename2 = "stu_sort.txt";
if((fp = fopen(filename2, "w")) == NULL)
{
printf("Open File %s Failed!\n", filename2);
}
int average;
for(i = 0; i < 3; i++)
{
average = (student[i] -> score[0] +
student[i] -> score[1] + student[i] -> score[2]) / 3;
if(average < avrg_newperson)
{
fprintf(fp, "%d %s %d %d %d\n",
newperson -> id,
newperson -> name,
newperson -> score[0],
newperson -> score[1],
newperson -> score[2]);
}
fprintf(fp, "%d %s %d %d %d\n",
student[i] -> id,
student[i] -> name,
student[i] -> score[0],
student[i] -> score[1],
student[i] -> score[2]);
}
fclose(fp);
}
13.10)有一个磁盘文件employee,内存放职工的数据,每个职工的数据包括:职工姓名、职工号、性别、年龄、住址、工资、健康状况、文化程度。今要求将职工名、工资信息单独抽出来另建一个简明的职工工资文件
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define filename1 "employee.txt"
#define filename2 "employee2.txt"
struct Person
{
char name[20];
int salary;
Person* next;
};
void main()
{
Person* pHead = new Person;
strcpy(pHead -> name, "head");
pHead -> salary = -1;
pHead -> next = NULL;
Person* pTemp = pHead;
FILE *fp;
if((fp = fopen(filename1, "r")) == NULL)
{
printf("Open File %s Failed!\n", filename1);
}
char name[20];
int salary;
while(fscanf(fp, "%s%*d%*s%*s%*s%d%*s%*s", name, &salary) != -1)
{
pTemp -> next = new Person;
strcpy(pTemp -> next -> name, name);
pTemp -> next -> salary = salary;
pTemp -> next -> next = NULL;
pTemp = pTemp -> next;
printf("%s %d\n", pTemp -> name, pTemp -> salary);
}
fclose(fp);
//
if((fp = fopen(filename2, "w")) == NULL)
{
printf("Open File %s Failed!\n", filename2);
}
for(pTemp = pHead -> next; pTemp; pTemp = pTemp -> next)
{
fprintf(fp, "%s %d\n", pTemp -> name, pTemp -> salary);
}
fclose(fp);
}
13.11)从13.10的职工工资文件中删去一个职工的数据,再存回原文件
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define filename1 "employee.txt"
#define filename2 "employee.txt"
struct Person
{
char name[20];
int salary;
Person* next;
};
void main()
{
Person* pHead = new Person;
strcpy(pHead -> name, "head");
pHead -> salary = -1;
pHead -> next = NULL;
Person* pTemp = pHead;
FILE *fp;
if((fp = fopen(filename1, "r")) == NULL)
{
printf("Open File %s Failed!\n", filename1);
}
char name[20];
int salary;
while(fscanf(fp, "%s%*d%*s%*s%*s%d%*s%*s", name, &salary) != -1)
{
pTemp -> next = new Person;
strcpy(pTemp -> next -> name, name);
pTemp -> next -> salary = salary;
pTemp -> next -> next = NULL;
pTemp = pTemp -> next;
printf("%s %d\n", pTemp -> name, pTemp -> salary);
}
fclose(fp);
char del[20];
printf("Input a person to delete: ");
scanf("%s", del);
//
if((fp = fopen(filename2, "w")) == NULL)
{
printf("Open File %s Failed!\n", filename2);
}
for(pTemp = pHead -> next; pTemp; pTemp = pTemp -> next)
{
if(strcmp(pTemp -> name, del) == 0)
{
continue;
}
fprintf(fp, "%s %d\n", pTemp -> name, pTemp -> salary);
}
fclose(fp);
}
13.12)从键盘输入若干行字符(每行长度不等),输入后把它们存储到一磁盘文件中。再从该文件中读取这些数据,将其中小写字母转换成大写字母后在显示屏上输出
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define LINES 2
void main()
{
int i;
//将输入的内容保存到文件
char s[10][100];
for(i = 0; i < LINES; i++)
{
gets(s[i]);
}
FILE *fp;
char *filename1 = "temp.txt";
if((fp = fopen(filename1, "w")) == NULL)
{
printf("Open File %s Failed!\n", filename1);
}
for(i = 0; i < LINES; i++)
{
fputs(s[i], fp);
}
fclose(fp);
//从文件中将字符读取并将小写字母转化为大写字母输出
if((fp = fopen(filename1, "r")) == NULL)
{
printf("Open File %s Failed!\n", filename1);
}
char temp[100 * LINES];
fgets(temp, 100 * LINES, fp);
for(i = 0; temp[i] != '\0'; i++)
{
if(temp[i] >= 'a' && temp [i] <= 'z')
{
temp[i] = temp[i] - 'a' + 'A';
}
}
printf("%s\n", temp);
fclose(fp);
}
END