Nanjing Normal University training program retest eleven

101. A common type of storage

#include <stdio.h>
union Data
{
    int i;
    char ch;
    float f;   //i,ch,f共用同一存储单元
} a;  //一瞬间只能放其中一个变量成员
int main()
{
    a.i=97;
    printf("%d\n",a.i);  //输出int
    printf("%c\n",a.ch); //输出对应ASCII码的字符
    printf("%f\n",a.f);  //float输出int的低两个字节
    return 0;
}

operation result:

102. The union of teachers and students examples

#include <stdio.h>

struct Person
{
    int num;
    char name[20];
    char sex;
    char job;
    union   //共用体
    {
        int clas;
        char position[10];
    } category;
} person[2];
int main()
{
    int i;
    printf("输入教师或学生的信息:\n");
    for(i=0; i<2; i++)
    {
        scanf("%d %s %c %c",&person[i].num,person[i].name,&person[i].sex,&person[i].job);
        if(person[i].job=='s')   //若job为学生,则输入班级
            scanf("%d",&person[i].category.clas);
        else if(person[i].job=='t')  //若job为教师,则输入职位
            scanf("%s",person[i].category.position);
        else
            printf("Input error!\n");
    }
    printf("打印教室或学生的成绩:\n");
    for(i=0; i<2; i++)
    {
        printf("%d %s %c %c ",person[i].num,person[i].name,person[i].sex,person[i].job);
        if(person[i].job=='s')
            printf("%d",person[i].category.clas);
        else if(person[i].job=='t')
            printf("%s",person[i].category.position);
        printf("\n");
    }

    return 0;
}

operation result:

103. enumerated type

#include <stdio.h>

int main()
{
    enum Weekday {sun,mon,tue,wed,thu,fri,sat} workday,weekend;
    workday=mon;
    weekend=sun;//可以直接进行赋值
    printf("mon=%d\ntue=%d\nwed=%d\nthu=%d\nfri=%d\nsat=%d\nsun=%d\n",mon,tue,wed,thu,fri,sat,sun);
    printf("for语句打印enum:\n");
    for(int i=sun; i<=sat; i++)    //用for进行循环enum
        printf("%d\n",i);
    if(workday==mon)  
        printf("workday,I want to play with you!\n");
    if(weekend==sun)
        printf("Weekend,I want to work very hard!\n");
    printf("另一种赋初值方法:\n");
    enum week {Mon = 1, Tues, Wed, Thurs, Fri = 10, Sat, Sun};  //进行赋值之后的值
    printf("Mon=%d\nTues=%d\nWed=%d\nThurs=%d\nFri=%d\nSat=%d\nSun=%d\n", Mon, Tues, Wed, Thurs, Fri, Sat, Sun);

    return 0;
}

operation result:

to sum up:

1, in the case of no instructions are displayed, the enumeration constant (ie constant name in curly braces) first enumeration constant default value is 0, the next each enumeration constants in ascending order

2, in the case of partial explanation of the display, unspecified value will depend on the enumeration name of a specified value before the most backward in ascending order

3, an integer can not 直接赋值to assign to a post-enumeration must be type cast to an enumerated type with the enumeration belongs

4, the same enumeration type different enumeration members may have the same value

5, the same application can not define an enumerated type with the same name, of different types can not be enumerated enum member with the same name exist (enumeration constants)

104. The use fgetc function-by-character input from the keyboard, and then written to a disk file with fputc function.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE *fp;
    char ch,filename[10];
    printf("请输入所用的文件名:");
    scanf("%s",filename);
    if((fp=fopen(filename,"w"))==NULL)  //以只写的方式打开文件,若没有文件则新建一个
    {
        printf("无法打开此文件\n");
        exit(0);
    }
    ch=getchar();        //用来接收最后输入的回车符
    printf("输入一个字符串(以#结束):");
    ch=getchar();        //输入一个字符
    while(ch!='#')
    {
        fputc(ch,fp);    //输入文件中
        putchar(ch);     //屏幕上打印
        ch=getchar();    //继续接受下一个字符
    }
    fclose(fp);   //关闭文件
    putchar('\n');
    return 0;
}

operation result:

105. copy information to a disk file to another disk file. For example: file1.dat-> file2.dat

#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE *in,*out;
    char ch,infile[10],outfile[10];
    printf("输入读入文件的名字:");
    scanf("%s",infile);
    printf("输入读出文件的名字:");
    scanf("%s",outfile);
    if((in=fopen(infile,"r"))==NULL)  //"r"打开方式,找不到就报错
    {
        printf("无法打开此文件\n");
        exit(0);
    }
    if((out=fopen(outfile,"w"))==NULL)  //"w"打开方式,找不到就新建
    {
        printf("无法打开此文件\n");
        exit(0);
    }
    while(!feof(in))  //未遇到输入文件的结束标志
    {
        ch=fgetc(in);  //从输入文件中读一个字符
        fputc(ch,out);  //将ch写入到输出文件中
        putchar(ch);   //将ch显示在屏幕上
    }
    putchar('\n');
    fclose(in);
    fclose(out);
    return 0;
}

operation result:

Read from the keyboard 106. The plurality of strings, sort them in order of size of the letters, then the sorted string to disk file saved.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    FILE *fp;
    char str[3][10],temp[10];
    int i,j,k,n=3;
    printf("Enter strings:\n");
    for(i=0; i<n; i++)   //输入n个字符串
        gets(str[i]);
    for(i=0; i<n-1; i++)   //对输入的n个字符串,由小到大,选择排序
    {
        k=i;
        for(j=i+1; j<n; j++)
            if(strcmp(str[k],str[j])>0)
                k=j;
        if(k!=i)
        {
            strcpy(temp,str[i]);
            strcpy(str[i],str[k]);
            strcpy(str[k],temp);
        }
    }
    if((fp=fopen("file1.dat","w"))==NULL)  //打开文件
    {
        printf("can't open file!\n");
        exit(0);
    }
    printf("\nThe new sequence:\n");
    for(i=0; i<n; i++)
    {
        fputs(str[i],fp);       //将字符串输入到文件中
        fputs("\n",fp);         //将换行输入到文件
        printf("%s\n",str[i]);  //在屏幕上显示结果
    }
    return 0;
}

operation result:

107. read from the file back to a string and displayed on the screen.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE *fp;
    char str[3][10];
    int i=0;
    if((fp=fopen("file1.dat","r"))==NULL)  //打开文件
    {
        printf("can't open file!\n");
        exit(0);
    }
    while(fgets(str[i],10,fp)!=NULL)  //fgets(str,n,fp)从fp所指向的文件读入一个长度为(n-1)的字符串
    {
        printf("%s",str[i]);  //因为读入字符串数组中的每个字符串后都有"\n"
        i++;                  //因此,printf()中不必加"\n"
    }
    fclose(fp);
    return 0;
}

operation result:

108. The way to read and write files formatted

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a,c;
    float b,d;
    FILE *fp;
    if((fp=fopen("file1.dat","w"))==NULL)  //必须以"w"打开文件
    {
        printf("can't open file!\n");
        exit(0);
    }
    printf("请输入整型a和浮点型b:\n");
    scanf("%d %f",&a,&b);
    fprintf(fp,"%d,%5.2f\n",a,b);   //格式化输入到文件中
    fclose(fp);

    if((fp=fopen("file1.dat","r"))==NULL)  //必须以"r"打开文件,否则会出错
    {
        printf("can't open file!\n");
        exit(0);
    }
    printf("\n读取文件中的数据:\n");
    fscanf(fp,"%d,%f",&c,&d);        //从文件中读出数据
    printf("%d,%5.2f\n",c,d);        //打印在屏幕上
    fclose(fp);

    return 0;
}

operation result:

A set of data 109. The read and write binary mode

#include <stdio.h>
#include <stdlib.h>
#define SIZE 4
struct Student_type    //学生结构体
{
    char name[10];
    int num;
    int age;
    char addr[15];
} stud[SIZE];

void save()   //将数据保存到文件中
{
    FILE *fp;
    int i;
    if((fp=fopen("std.dat","wb"))==NULL)
    {
        printf("can't open file!\n");
        return;
    }
    for(i=0; i<SIZE; i++)
        if(fwrite(&stud[i],sizeof(struct Student_type),1,fp)!=1)  //二进制形式输出到文件中
            printf("file write error!\n");
    fclose(fp);
}

void print()   //将数据读出
{
    struct Student_type st[SIZE];
    FILE *fp;
    int i;
    if((fp=fopen("std.dat","rb"))==NULL)
    {
        printf("can't open file!\n");
        return;
    }
    for(i=0; i<SIZE; i++)
    {
        if(fread(&st[i],sizeof(struct Student_type),1,fp)!=1)  //二进制形式读取文件数据
            printf("file write error!\n");
        printf("%s %d %d %s\n",st[i].name,st[i].num,st[i].age,st[i].addr);  //将数据打印在屏幕上
    }
    fclose(fp);
}

void print2()
{
    struct Student_type stu[SIZE];
    FILE *fp;
    int i;
    if((fp=fopen("std.dat","rb"))==NULL)
    {
        printf("can't open file!\n");
        return;
    }
    for(i=0; i<SIZE; i+=2)
    {
        fseek(fp,i*sizeof(struct Student_type),0);      //随机读,用fseek指定位置,0表示距文件开始位置,1表示距文件当前位置,2表示距文件末尾位置
        if(fread(&stu[i],sizeof(struct Student_type),1,fp)!=1)  //二进制形式读取文件数据
            printf("file write error!\n");
        printf("%s %d %d %s\n",stu[i].name,stu[i].num,stu[i].age,stu[i].addr);  //将数据打印在屏幕上
    }
    fclose(fp);
}
int main()
{
    int i;
    printf("Please enter data of students:\n");
    for(i=0; i<SIZE; i++)  //输入学生数据
        scanf("%s%d%d%s",stud[i].name,&stud[i].num,&stud[i].age,stud[i].addr);
    save();    //存入文件中
    printf("读出文件内容:\n");
    print();   //读取文件数据

    printf("随机读取文件内容:\n");
    print2();
    return 0;
}

operation result:

110. random read and write data files rewind

#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE *fp1,*fp2;
    if((fp1=fopen("file1.dat","r"))==NULL)   //打开输入文件
    {
        printf("can't open file!\n");
        exit(0);
    }
    if((fp2=fopen("file2.dat","w"))==NULL)   //打开输出文件
    {
        printf("can't open file!\n");
        exit(0);
    }
    while(!feof(fp1))
        putchar(fgetc(fp1));   //从文件中逐个读入字符,然后输出到屏幕
    putchar('\n');

    rewind(fp1);   //此时文件指针已指向末尾,使其重新回到文件头
    while(!feof(fp1))
        fputc(fgetc(fp1),fp2);  //从文件头重新读字符,并输入到文件file2中

    fclose(fp1);
    fclose(fp2);
    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/105070134