c/c++练习–20

c/c++练习–20


  • 习题来源:C语言经典编程282例

191.使用宏定义实现数组值的互换

#include <stdio.h>
#include <stdlib.h>

#define  swap(a,b)      int c;c=a,a=b,b=c

int main(void){
    int a=1,b=2;
    swap(a,b);
    printf("%d\t%d\n",a,b);

    return(EXIT_SUCCESS);
}                                           

192.利用宏定义求偶数和

利用宏定义求1~100的偶数和,定义一个宏判断一个数时候为偶数。

#include <stdio.h>
#include <stdlib.h>

#define  num(a) (a%2==0?1:0)

int main(void){
    int sum=0;
    for(int i=0;i<100;i++){
        if(num(i))sum+=i;
    }
    printf("%d\n",sum);

    return(EXIT_SUCCESS);
}                                                   

193.关闭所有打开的文件

在程序中打开已有的3个文件,读取文件内容并显示在屏幕上,要求调用fcloseall函数一次关闭三个文件。

#include <stdio.h>
#include <stdlib.h>

int main(void){
    FILE    *fp1,*fp2,*fp3;
    int     a[3][3]={0};

    fp1 = fopen("1.txt", "r");
    fp2 = fopen("2.txt", "r");
    fp3 = fopen("3.txt", "r");

    fscanf(fp1,"%d %d %d",*a,*a+1,*a+2);
    fscanf(fp2,"%d %d %d",*(a+1),*(a+1)+1,*(a+1)+2);
    fscanf(fp3,"%d %d %d",*(a+2),*(a+2)+1,*(a+2)+2);
    fcloseall();
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
            printf("%d\t",a[i][j]);
    return(EXIT_SUCCESS);
}                                               

194.同时显示两个文件的内容

将两个不同文件的内容在屏幕的指定位置显示出来,第5行第3列显示“file1:”,从第6行第1列开始显示第1个文件的内容,第13行第3列显示“file2:”第14行第1列开始显示第2个文件的内容。

#include <stdio.h>
#include <stdlib.h>

int main(void){
    FILE    *fp1,*fp2;
    int     a[3][3]={0};

    fp1 = fopen("1.txt", "r");
    fp2 = fopen("2.txt", "r");

    fscanf(fp1,"%d %d %d",*a,*a+1,*a+2);
    fscanf(fp2,"%d %d %d",*(a+1),*(a+1)+1,*(a+1)+2);    
    fcloseall();
    for(int i=0;i<15;i++){
        if(i<4) {
            printf("\n");
            continue;
        }
        if(i==4){
            printf("file1:\n");
            continue;
        }
        if(i==5){
            printf("%d %d %d\n",a[0][0], a[0][1], a[0][2]);
            continue;
        }
        if(i<13) {
            printf("\n");
            continue;
        }
        if(i==13){
            printf("file2:\n");
            continue;
        }
        if(i==14){
            printf("%d %d %d\n",a[1][0], a[1][1], a[1][2]);
            continue;
        }
    }
    return(EXIT_SUCCESS);
}                                               

195.创建文件

从键盘中输入名称,无论创建成功与否军显示提示信息。

#include <stdio.h>
#include <stdlib.h>

int main(void){
    FILE    *fp1;
    char        name[10];

    printf("please input your file name:");
    scanf("%s",name);
    fp1 = fopen(name,"w");
    if(fp1) printf("%s create sucess\n",name);
    else    printf("%s create failure\n",name);
    fcloseall();
    return(EXIT_SUCCESS);
}               

196.格式化文件读写

将输入的小写字母写入磁盘文件,再讲磁盘文件的内容读出,并以小写字母的形式显示在屏幕上。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(void){
    FILE    *fp1;
    char        ch,name[10];

    printf("please input your file name:");
    scanf("%s",name);
    fp1 = fopen(name,"w");
    printf("please input your char:");
    while((ch=getchar())!=EOF && ch != '\n');
    while((ch=getchar())!=EOF && ch != '\n')
        fprintf(fp1,"%c",ch);
    fclose(fp1);

    fp1 = fopen(name,"r");
    while(ch != 'n'){
        fscanf(fp1,"%c",&ch);
        putchar(ch);
    }

    fcloseall();
    return(EXIT_SUCCESS);
}                                                       

197.成块读写操作

学生成绩信息的输入与输出,通过键盘输入学生成绩信息,保存到指定磁盘文件中,输入全部信息后,将磁盘文件中保存的信息输出到屏幕。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

struct student{
    int num;
    char    name[10];
    float   chi;
    float   eng;
    float   math;
};

int main(void){
    struct student s;
    char        name[10];
    FILE    *fp1;

    printf("please input your filename:");
    gets(name);
    printf("please input the information of your students:");
    scanf("%d %s %f %f %f",&(s.num),s.name,&(s.chi),&(s.eng),&(s.math));
    fp1 = fopen(name,"w");
    fwrite(&s,sizeof(s),1,fp1); 
    fcloseall();
    printf("%d %s %f %f %f\n",s.num,s.name,s.chi,s.eng,s.math);
    return(EXIT_SUCCESS);
}                                           

198.随机读写文件

输入若干学生信息,保存到指定磁盘文件中,要求将奇数条学生信息从磁盘中读入并显示在屏幕上

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

struct student{
    int num;
    char    name[10];
    float   chi;
    float   eng;
    float   math;
};

int main(void){
    struct student s;
    char        name[10];
    FILE    *fp1;
    int i=0;
    printf("please input your filename:");
    gets(name);
    fp1 = fopen(name,"w");
    if(!fp1)printf("w\n");  
    while(i<5){
        printf("please input the information of your students:");
        scanf("%d %s %f %f %f",&(s.num),s.name,&(s.chi),&(s.eng),&(s.math));
        printf("%d %s %f %f %f\n",s.num,s.name,s.chi,s.eng,s.math);
        fwrite(&s,sizeof(student),1,fp1);
        i++;
    }       
    fcloseall();
    i=0;
    fp1 = fopen(name,"r");
    while(i<5){
        fread(&s,sizeof(student),1,fp1);
        fseek(fp1,sizeof(student),SEEK_CUR);
        printf("%d %s %f %f %f\n",s.num,s.name,s.chi,s.eng,s.math);
        i+=2;
    }   
    fcloseall();
    return(EXIT_SUCCESS);
}

199.以行为单位读写文件

从键盘中输入字符串“同一个世界,同一个梦想”,要求将字符串内容输出到磁盘文件,在从磁盘文件中读取字符串到数组s,最终将其输出在屏幕上。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int main(void){
    char        name[10];
    FILE    *fp1;
    char        str[100];


    printf("please input your filename:");
    gets(name);
    fp1 = fopen(name,"w");
    if(!fp1)printf("w\n");  
    gets(str);
    fprintf(fp1,"%s",str);
    fcloseall();

    fp1 = fopen(name,"r");
    fscanf(fp1,"%s",str);
    puts(str);
    fcloseall();
    return(EXIT_SUCCESS);
}                                               

200.删除文件中的记录

输入文件名,输入员工姓名及工资,输入完毕显示文件中的内容,输入要删除的员工姓名,进行删除操作,最后将删除后的内容显示在屏幕上。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

struct student{
    char        name[10];
    float   sal;
};

int main(void){
    struct student s;
    char        name[10];
    FILE    *fp1;
    char        ch;
    int i=0;
    printf("please input your filename:");
    gets(name);
    fp1 = fopen(name,"w");
    if(!fp1)printf("w\n");  
    while(i<5){
        printf("please input the information of your students:");
        scanf("%f %s",&(s.sal),s.name);
        printf("%f %s\n",s.sal,s.name);
        fwrite(&s,sizeof(student),1,fp1);
        i++;
    }       
    fcloseall();
    i=0;
    fp1 = fopen(name,"r");
    printf("please input name you want to delete:");
    while((ch=getchar())!='\c');
    gets(name);
    while(i<5){
        fread(&s,sizeof(student),1,fp1);
        if(strcmp(s.name,name)==0){ 
            printf("%f %s\n",s.sal,s.name);
            fread(&s,sizeof(student),1,fp1);
            fseek(fp1,-2*sizeof(student),SEEK_CUR);
            fwrite(&s,sizeof(student),1,fp1);
            fseek(fp1,sizeof(student),SEEK_CUR);
            break;
        }
        i++;
    }   
    fcloseall();
    return(EXIT_SUCCESS);
}                                                   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值