Nanjing Normal University training program retest twelve

111. The keyboard input from a string, which will be all lowercase letters to uppercase letters, and then output to a disk file "file.txt" save the input string to "!" End.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char ch;
    FILE *fp;
    if((fp=fopen("file.txt","w"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    while((ch=getchar())!='!')
    {
        if(ch>='a'&&ch<='z')   //小写字母转换成大写字母
            ch=ch-32;
        fputc(ch,fp);     //存入文件中
        putchar(ch);      //输出到屏幕上
    }
    putchar('\n');
    fclose(fp);
    return 0;
}

operation result:

112. There are two disk files "A" and "B", each of the discharge line of characters, this required information combining these two files (in alphabetical order), output to a new file "C" go

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char a[100],ch,temp;
    int i,j,k;
    FILE *fp1,*fp2,*fp3;
    if((fp1=fopen("file1.txt","r"))==NULL)   //将文件file1中的字母存放到数组a中
    {
        printf("can't open file!\n");
        exit(0);
    }
    i=0;
    while((ch=fgetc(fp1))!=EOF)   //若用!feof(fp1),后面会多复制空格,所以不能用
    {
        a[i++]=ch;
        putchar(ch);
    }
    fclose(fp1);

    if((fp2=fopen("file2.txt","r"))==NULL)  //将文件file2中的字母存放到数组a中
    {
        printf("can't open file!\n");
        exit(0);
    }
    while((ch=fgetc(fp2))!=EOF)
    {
        a[i++]=ch;
        putchar(ch);
    }
    fclose(fp2);

    a[i]='\0';
    for(j=0; j<i-1; j++)          //进行冒泡排序
        for(k=0; k<i-1-j; k++)
        {
            if(a[k]>a[k+1])
            {
                temp=a[k];
                a[k]=a[k+1];
                a[k+1]=temp;
            }
        }
    printf("\n%s",a);

    if((fp3=fopen("file3.txt","w"))==NULL)    //将数组a中的字母存到文件file3中
    {
        printf("can't open file!\n");
        exit(0);
    }
    for(j=0; j<i; j++)
        fputc(a[j],fp3);
    fclose(fp3);

    putchar('\n');
    return 0;
}

operation result:

113. There are five students, each student has a score of three courses, student input data from the keyboard (including number, name, three course grade), calculate the average score, the original data and the calculated average scores stored in a disk file "stud" in.

 

#include <stdio.h>
#include <stdlib.h>
#define N 3
struct Student    //学生结构体
{
    long num;
    char name[15];
    float score[3];
    float avg;
} stud[N];
int main()
{
    int i,j;
    float sum;
    FILE *fp;
    for(i=0; i<N; i++)    //从键盘输入数据
    {
        sum=0;
        scanf("%ld %s ",&stud[i].num,stud[i].name);
        for(j=0; j<3; j++)
        {
            scanf("%f",&stud[i].score[j]);
            sum+=stud[i].score[j];
        }
        stud[i].avg=sum/3.0;
    }

    if((fp=fopen("stud.txt","w"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    for(i=0; i<N; i++)      //写入文件stud.txt中
    {
        if(fwrite(&stud[i],sizeof(struct Student),1,fp)!=1)
            printf("File write error!\n");
    }
    fclose(fp);

    if((fp=fopen("stud.txt","r"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    printf("打印文件数据:\n");
    for(i=0; i<N; i++)      //从文件stud.txt中读出数据
    {
        fread(&stud[i],sizeof(struct Student),1,fp);
        printf("%ld %s ",stud[i].num,stud[i].name);
        for(j=0; j<3; j++)
            printf("%.2f ",stud[i].score[j]);
        printf("%.2f\n",stud[i].avg);
    }
    fclose(fp);
    return 0;
}

operation result:

Students 114. The student data on cases of "stud" file, sort the process by grade point average, will have been sorted into a new file "stud_sort" in.

#include <stdio.h>
#include <stdlib.h>
#define N 3
struct Student    //学生结构体
{
    long num;
    char name[15];
    float score[3];
    float avg;
} stud[N];
int main()
{
    int i,j;
    FILE *fp;
    struct Student temp;
    if((fp=fopen("stud.txt","r"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    printf("stud.txt文件中已有数据:\n");
    for(i=0; i<N; i++)      //从文件stud.txt中读出数据
    {
        fread(&stud[i],sizeof(struct Student),1,fp);
        printf("%ld %s ",stud[i].num,stud[i].name);
        for(j=0; j<3; j++)
            printf("%.2f ",stud[i].score[j]);
        printf("%.2f\n",stud[i].avg);
    }
    fclose(fp);

    for(i=0; i<N-1; i++) //进行冒泡排序
        for(j=0; j<N-i-1; j++)
            if(stud[j].avg>stud[j+1].avg)
            {
                temp=stud[j];
                stud[j]=stud[j+1];
                stud[j+1]=temp;
            }

    if((fp=fopen("stud_sort.txt","w"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    for(i=0; i<N; i++)     //写入stud_sort.txt文件中
    {
        if(fwrite(&stud[i],sizeof(struct Student),1,fp)!=1)
            printf("File write error!\n");
    }
    fclose(fp);

    if((fp=fopen("stud_sort.txt","r"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    printf("stud_sort.txt文件中的数据:\n");
    for(i=0; i<N; i++)      //从文件stud_sort.txt中读出数据
    {
        fread(&stud[i],sizeof(struct Student),1,fp);
        printf("%ld %s ",stud[i].num,stud[i].name);
        for(j=0; j<3; j++)
            printf("%.2f ",stud[i].score[j]);
        printf("%.2f\n",stud[i].avg);
    }
    fclose(fp);
    return 0;
}

operation result:

115. The above example has been sort of student achievement insertion process. Insert a student's course grade 3, the program first calculates the newly inserted student's grade point average, and then insert it by achievement level, create a new file after insertion.

#include <stdio.h>
#include <stdlib.h>
#define N 3
struct Student    //学生结构体
{
    long num;
    char name[15];
    float score[3];
    float avg;
} stud[N+1];
int main()
{
    int i,j;
    FILE *fp;
    struct Student temp;
    float sum=0;
    scanf("%ld %s ",&stud[3].num,stud[3].name);//输入一个学生的数据
    scanf("%f %f %f",&stud[3].score[0],&stud[3].score[1],&stud[3].score[2]);
    sum=stud[3].score[0]+stud[3].score[1]+stud[3].score[2];
    stud[3].avg=sum/3.0;

    if((fp=fopen("stud_sort.txt","r"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }

    for(i=0; i<N; i++)      //从文件stud_sort.txt中读出数据
        fread(&stud[i],sizeof(struct Student),1,fp);
    fclose(fp);

    for(i=0; i<N; i++)  //进行冒泡排序
        for(j=0; j<N-i; j++)
            if(stud[j].avg>stud[j+1].avg)
            {
                temp=stud[j];
                stud[j]=stud[j+1];
                stud[j+1]=temp;
            }

    if((fp=fopen("stud_sort1.txt","w"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    for(i=0; i<N+1; i++)     //写入stud_sort1.txt文件中
    {
        if(fwrite(&stud[i],sizeof(struct Student),1,fp)!=1)
            printf("File write error!\n");
    }
    fclose(fp);

    if((fp=fopen("stud_sort1.txt","r"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    printf("读出stud_sort1.txt文件中的数据:\n");
    for(i=0; i<N+1; i++)     //读出stud_sort1.txt文件中的数据
    {
        fread(&stud[i],sizeof(struct Student),1,fp);
        printf("%ld %s ",stud[i].num,stud[i].name);
        for(j=0; j<3; j++)
            printf("%.2f ",stud[i].score[j]);
        printf("%.2f\n",stud[i].avg);
    }
    fclose(fp);

    return 0;
}

operation result:

116. There is a disk file "Employee", putting data of workers. Data for each employee, including employee name, employee number, sex, age, address, salary, health, education. This requires that employees name, salary information to another individual out of the wages of workers to build a concise document.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 3
struct Employee    //职工结构体
{
    long num;
    char name[15];
    char sex;
    int age;
    char addr[20];
    int salary;
    char health[8];
    char clas[10];
} employee[N];

struct Emp   //简洁职工结构体
{
    char name[15];
    int salary;
} emp[N];

int main()
{
    FILE *fp;
    int i;
    printf("输入职工详细信息:\n");
    for(i=0; i<N; i++) //键盘输入职工详细信息
        scanf("%ld %s %c %d %s %d %s %s",&employee[i].num,employee[i].name,&employee[i].sex,&employee[i].age,employee[i].addr,&employee[i].salary,employee[i].health,employee[i].clas);

    if((fp=fopen("employee.txt","w"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    for(i=0; i<N; i++)     //写入employee.txt文件中
    {
        if(fwrite(&employee[i],sizeof(struct Employee),1,fp)!=1)
            printf("File write error!\n");
    }
    fclose(fp);

    if((fp=fopen("employee.txt","r"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    for(i=0; i<N; i++)     //读出employee.txt文件中的数据,并传数据
    {
        fread(&employee[i],sizeof(struct Employee),1,fp);
        strcpy(emp[i].name,employee[i].name);
        emp[i].salary=employee[i].salary;
    }
    fclose(fp);

    if((fp=fopen("emp.txt","w"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    for(i=0; i<N; i++)     //将数据写入emp.txt文件中
    {
        if(fwrite(&emp[i],sizeof(struct Emp),1,fp)!=1)
            printf("File write error!\n");
    }
    fclose(fp);

    if((fp=fopen("emp.txt","r"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    printf("打印简略职工信息:\n");
    for(i=0; i<N; i++)     //读出emp.txt文件中的数据
    {
        fread(&emp[i],sizeof(struct Emp),1,fp);
        printf("%s %d\n",emp[i].name,emp[i].salary);
    }
    fclose(fp);

    return 0;
}

operation result:

117. deleting data from a worker on the theme "wages File", and then save back to the original file.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 3

struct Emp   //简洁职工结构体
{
    char name[15];
    int salary;
} emp[N];

int main()
{
    FILE *fp;
    int i,j;
    char n[15];   //要删除的职工的名字

    printf("输入职工工资文件数据:\n");
    for(i=0; i<N; i++)   //键盘输入职工工资文件数据
        scanf("%s %d",emp[i].name,&emp[i].salary);
    if((fp=fopen("emp.txt","w"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    for(i=0; i<N; i++)
    {
        if(fwrite(&emp[i],sizeof(struct Emp),1,fp)!=1)
            printf("File write error!\n");
    }
    fclose(fp);


    if((fp=fopen("emp.txt","r"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    printf("读出职工工资文件数据:\n");
    for(i=0; i<N; i++)           //读出emp.txt文件中的数据
    {
        fread(&emp[i],sizeof(struct Emp),1,fp);
        printf("%s %d\n",emp[i].name,emp[i].salary);
    }
    fclose(fp);

    printf("输入要删除的职工的名字:\n");    //进行删除
    scanf("%s",n);
    for(i=0; i<N; i++)
        if(strcmp(emp[i].name,n)==0)
            break;
    for(j=i+1; j<N; j++)
    {
        strcpy(emp[j-1].name,emp[j].name);
        emp[j-1].salary=emp[j].salary;
    }

    if((fp=fopen("emp.txt","w"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    for(i=0; i<N-1; i++)     //将前N-1个数据写入emp.txt文件中
    {
        if(fwrite(&emp[i],sizeof(struct Emp),1,fp)!=1)
            printf("File write error!\n");
    }
    fclose(fp);

    if((fp=fopen("emp.txt","r"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    printf("读出删除后的职工工资文件数据:\n");
    for(i=0; i<N-1; i++)     //读出emp.txt文件中的数据
    {
        fread(&emp[i],sizeof(struct Emp),1,fp);
        printf("%s %d\n",emp[i].name,emp[i].salary);
    }
    fclose(fp);

    return 0;
}

operation result:

118. The plurality of lines of characters from the keyboard (unequal length of each line), the input store them in a disk file. And then reads the data from the file, in which writing lowercase letters converted to uppercase letters, the output on the screen.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int flag=1,i;
    char c,str[80];
    FILE *fp;
    if((fp=fopen("text.txt","w"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    while(flag==1)        //逐次输入字符串
    {
        printf("Input string:");
        gets(str);
        fprintf(fp,"%s",str);
        fprintf(fp,"\n");
        printf("Continue?");
        c=getchar();
        if(c=='N'||c=='n')
            flag=0;
        getchar();
    }
    fclose(fp);

    if((fp=fopen("text.txt","r"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    printf("读取文件中的字符串,将小写字母转换成大写字母\n");
    while(fscanf(fp,"%s",str)!=EOF)    //读取字符串
    {
        for(i=0; str[i]!='\0'; i++)
            if(str[i]>='a'&&str[i]<='z')
                str[i]-=32;
        printf("%s\n",str);
    }
    fclose(fp);

    return 0;
}

operation result:

 

Published 462 original articles · won praise 55 · views 320 000 +

Guess you like

Origin blog.csdn.net/LY_624/article/details/105120977