zoukankan      html  css  js  c++  java
  • 第十章编程题

    1.从键盘输入一个字符串,将其中的小写字母全部转化成大写字母
    然后输出到一个磁盘文件test中保存,输入的字符串以!结束

    fgetc(fp):从fp指向的文件读入一个字符
    读成功带回所读的字符,失败则返回文件结束标志EOF(即-1)
    fputc(ch,fp):把字符ch写到文件指针变量fp所指向的文件中
    输出成功,返回值就是输出的字符;输出失败,则返回EOF

    fgets(str,n,fp) 从fp指向的文件读入一个长度为n-1的字符串,存放到字符数组str中
    读成功,返回地址str,失败则返回NULL
    fputs(str,fp) 把str所指向的字符串写到文件指针变量fp所指向的文件中
    输出成功,返回0;否则返回非0值。

    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    #include <stdlib.h>
    int main(){
        FILE *fp;
        char str[100];
        int i=0;
        if((fp=fopen("test.txt","w+"))==NULL){
            printf("cannot open the file
    ");
            exit(0);
        }
        printf("input a string:
    ");
        gets(str);
        while(str[i]!='!'){
            if(str[i]>='a'&&str[i]<='z')
              str[i]=str[i]-32;
            fputc(str[i],fp);
            i++; 
        }
        fclose(fp);
        fp=fopen("test.txt","r+");
        fgets(str,strlen(str)+1,fp);
        printf("%s
    ",str);
        fclose(fp);
        return 0;
    } 
    View Code

    2.有两个磁盘文件A和B,各存放一行字母,今要求把这两个文件中的信息合并

    (按字母顺序排列),输出到一个新文件C中去。

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    int main(){
        FILE *fp;
        int i,j,n,i1;
        char c[100],t,ch;
        if((fp=fopen("testa.txt","r+"))==NULL){
            printf("cannot open the file
    ");
            exit(0);
        }
        printf("file A:
    ");
        for(i=0;(ch=fgetc(fp))!=EOF;i++){
            c[i]=ch;
            putchar[c[i]];
        }
        fclose(fp);
        i1=i;
        if((fp=fopen("testb.txt","r+"))==NULL){
            printf("cannot open the file
    ");
            exit(0);
        }
        printf("
    file B:
    ");
        for(i=i1;(ch=fgetc(fp))!=EOF;i++){
            c[i]=ch;
            putchar(c[i]);
        }
        fclose(fp);
        n=i;
        for(i=0;i<n;i++){
            for(j=i+1;j<n;j++){
                if(c[i]>c[j]){
                    t=c[i];
                    c[i]=c[j];
                    c[j]=t;
                }
            }
        }
        printf("
    file C:
    ");
        fp=fopen("testc.txt","w+");
        for(i=0;i<n;i++){
            putc(c[i],fp);
            putchar(c[i]);
        }
        printf("
    ");
        fclose(fp);
        return 0;
    } 
    View Code

    3.有5个学生,每个学生有三门课程的成绩,从键盘输入学生数据

    包括学号,姓名,和三门课程成绩,计算出平均成绩,将原有数据和计算出的
    平均分存放在磁盘文件stud中

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define N 5
    struct student{
        int no;
        char name[20];
        double score[3];
        double sum;
        double avg;
    }stu[N]; 
    int main(){
        int i,j,k;
        FILE *fp;
        if((fp=fopen("stud","w+"))==NULL){
            printf("cannot open the file
    ");
            exit(0);
        }
        double sum;
        printf("please input the information
    ");
        for(i=0;i<N;i++){
            scanf("%d%s",&stu[i].no,stu[i].name);
            sum=0;
            for(j=0;j<3;j++){
                scanf("%lf",&stu[i].score[j]);
                sum+=stu[i].score[j];
            }
            stu[i].sum=sum;
            stu[i].avg=sum/3; 
        }
        for(i=0;i<N;i++){
            fprintf(fp,"%d	%s	%lf	%lf	%lf	%lf	%lf
    ",
            stu[i].no,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[3],
            stu[i].sum,stu[i].avg);
        }
        return 0;
    }
    View Code

    4.5个学生,每个学生有三门课程的成绩,从键盘输入学生数据

    包括学号,姓名,和三门课程成绩,计算出平均成绩,按照平均分进行排序
    排序结果存档到stud的文件中。

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define N 5
    struct student{
        int no;
        char name[20];
        double score[3];
        double sum;
        double avg;
    }stu[N]; 
    void avg_sort(){
        //按照平均分成绩进行排序,降序
        struct student temp;
        int i,j,k;
        for(i=0;i<N;i++){
            k=i;
            for(j=i+1;j<N;j++){
                if(stu[k].avg<stu[j].avg) k=j;
            }
            temp=stu[i];
            stu[i]=stu[k];
            stu[k]=temp;
        } 
    }
    void print(){
        //输出全体学生的信息
        int i,j,k;
        for(i=0;i<N;i++){
          printf("%d	%s	%lf	%lf	%lf	%lf	%lf
    ",
            stu[i].no,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[3],
            stu[i].sum,stu[i].avg);    
        } 
    }
    int main(){
        int i,j,k;
        FILE *fp;
        if((fp=fopen("stud","w+"))==NULL){
            printf("cannot open the file
    ");
            exit(0);
        }
        double sum;
        printf("please input the information
    ");
        for(i=0;i<N;i++){
            scanf("%d%s",&stu[i].no,stu[i].name);
            sum=0;
            for(j=0;j<3;j++){
                scanf("%lf",&stu[i].score[j]);
                sum+=stu[i].score[j];
            }
            stu[i].sum=sum;
            stu[i].avg=sum/3; 
        }
        avg_sort();
        print();
        for(i=0;i<N;i++){
            fprintf(fp,"%d	%s	%lf	%lf	%lf	%lf	%lf
    ",
            stu[i].no,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[3],
            stu[i].sum,stu[i].avg);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    GNU build system / autoconf 和 automake 生成 Makefile 文件
    使用you-get下载网站内嵌的视频
    ffmpeg偷懒
    GB28181的PS流完全分析(封装 / 分包发送 / 接收组包 / 解析)
    JRE与JDK的区别
    图解SQL的inner join、left join、right join、full outer join、union、union all的区别
    linux常用查看日志命令
    Hadoop2.6.2的Eclipse插件的使用
    path与classpath区别(转)
    在Eclipse上运行Spark(Standalone,Yarn-Client)
  • 原文地址:https://www.cnblogs.com/byczyz/p/13831064.html
Copyright © 2011-2022 走看看