习题12

//习题12.3+12.4
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void reverse(char a[],int l)
{
	char x;
	for(int i=0;i<l/2;i++)
	{
		x=a[i];
		a[i]=a[l-i-1];
		a[l-i-1]=x;
	}
	printf("%s\n",a);
}
int main(int argc, char const *argv[])
{
	FILE *fp;
	if((fp=fopen("/Users/hellooks/Desktop/fileio/filetest.txt","w+"))==NULL)
	{
		printf("fail\n");
		exit(0);
	}
	char a[100];
	int k=1;
	while(k==1)
	{
		gets(a);
		int l=strlen(a);
		for(int i=0;i<l;i++)
		{
			if(a[i]>='a'&&a[i]<='z')
			a[i]=a[i]+'A'-'a';
		}
		reverse(a,l);
		fputs(a,fp);
		fputc('\n',fp);
		printf("是否继续输入?(1/0)\n");
		scanf("%d",&k);
		getchar();
	}
		fseek(fp,0,SEEK_SET);
		while(fgets(a,100,fp))
		{
			printf("%s\n",a);
		}
		fclose(fp);
	return 0;
}

//12.4
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void reverse(char a[])
{
	int l=strlen(a);
	for(int i=0;i<l/2;i++)
	{
		char q=a[i];
		a[i]=a[l-i-2];
		a[l-i-2]=q;
		//printf("%s\n",a );
	}
}
int main(int argc, char const *argv[])
{
	FILE *fp,*fq;
	if((fp=fopen("/Users/hellooks/Desktop/fileio/filetest.txt","w+"))==NULL)
	{
		printf("fail1\n");
		exit(0);
	}
	if((fq=fopen("/Users/hellooks/Desktop/fileio/filenew.txt","w"))==NULL)
	{
		printf("fail\n");
		exit(0);
	}
	char a[100];
	int k=1;
	while(k==1){
		gets(a);
		fputs(a,fp);
		fputc('\n',fp);
		printf("go on?\n");
		scanf("%d",&k);
		getchar();
	}
	fseek(fp,0,SEEK_SET);
	while(fgets(a,100,fp))
	{
		printf("%s\n",a);
	}
	fseek(fp,0,SEEK_SET);
	while(fgets(a,100,fp))
	{
		//printf("%s\n",a);
		reverse(a);
		//printf("%s\n",a);
		printf("%s\n",a);
		fputs(a,fq);
	}
	fseek(fq,0,SEEK_SET);
	while(fgets(a,100,fq))
	{printf("%s\n",a);}
	fclose(fp);
	fclose(fq);
	return 0;
}

//12.5
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
int main(int argc, char const *argv[])
{
	FILE *fp;
	if((fp=fopen("/Users/hellooks/Desktop/fileio/filenew.txt","r"))==NULL)
	{
		printf("fail\n");
		exit(0);
	}
	char a;
	int sum=0;
	int flag=0;
	int i=0;
	while((a=fgetc(fp))!=EOF){
		if((a>'a'&&a<'z')||(a>'A'&&a<'Z')) flag=1;
		if(flag==1&&(((a>'a'&&a<'z')||(a>'A'&&a<'Z'))==0)) 
			{
				printf("%d\n",i);
				sum++;
				flag=0;
			}
			i++;
			}
		//if(flag==1) sum++;
			printf("%d\n",sum);
			fclose(fp);
	return 0;
}

//12.6
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(int argc, char const *argv[])
{
	FILE *fp,*fp_new;
	if((fp=fopen("/Users/hellooks/Desktop/fileio/filefile.txt","w+"))==NULL)
	{
		printf("fail\n");
		exit(0);
	}
	if((fp_new=fopen("/Users/hellooks/Desktop/fileio/filenew2.txt","w+"))==NULL)
	{
		printf("fail\n");
		exit(0);
	}
	fputc('a',fp);
	fputc('a',fp);
	fputc('\t',fp);
	fputc('a',fp);
	fputc('a',fp);
	fputc('\t',fp);
	fputc('a',fp);
	fputc('a',fp);
	fputc('\t',fp);
	fputc('a',fp);
	fputc('a',fp);
	fputc('\t',fp);
	fputc('a',fp);
	fputc('a',fp);
	fputc('\t',fp);
	fseek(fp,0,SEEK_SET);
	char ch;
	while((ch=fgetc(fp))!=EOF)
	{
		//printf("%c",ch);
		if(ch=='\t')
		{
			fputc(' ',fp_new);
			fputc(' ',fp_new);
			fputc(' ',fp_new);
			fputc(' ',fp_new);
		}
		else{
			fputc(ch,fp_new);
		}
	}
	fseek(fp_new,0,SEEK_SET);
	while((ch=fgetc(fp_new))!=EOF)
	{
		printf("%c",ch);
	}
	fclose(fp);
	fclose(fp_new);
	remove("/Users/hellooks/Desktop/fileio/filefile.txt");
	rename("/Users/hellooks/Desktop/fileio/filenew2.txt","/Users/hellooks/Desktop/fileio/filefile.txt");
	return 0;
}

//12.7
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student{
	long long int no;
	char name[100];
	float math;
	float english;
	float c;
}stu;
int main(int argc, char const *argv[])
{
	FILE *fp;
	if((fp=fopen("/Users/hellooks/Desktop/fileio/file.rec","w+b"))==NULL)
	{
		printf("fail\n");
		exit(0);
	}
	for(int i=0;i<5;i++)
	{
		printf("name?\n");
		scanf("%s",stu.name);
		printf("no?\n");
		scanf("%lld",&stu.no);
		printf("math?\n");
		scanf("%f",&stu.math);
		printf("c?\n");
		scanf("%f",&stu.c);
		printf("english?\n");
		scanf("%f",&stu.english);
		fwrite(&stu,sizeof(stu),1,fp);
	}
	fseek(fp,0,SEEK_SET);
	while(fread(&stu,sizeof(stu),1,fp)==1)
	{
		printf("%s\n",stu.name);
		printf("%lld\n",stu.no);
	}
	
	return 0;
}



//12.8
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student{
	long long int no;
	char name[100];
	float math;
	float english;
	float c;
}stu;
struct studentpaixu{
		char name[100];
		float sum;
	}people[20];
int cmp(const void *a,const void *b)
{
	return (*(struct studentpaixu*)b).sum-(*(struct studentpaixu*)a).sum;	

}
int main(int argc, char const *argv[])
{
	FILE *fp,*fq;
	if((fp=fopen("/Users/hellooks/Desktop/fileio/file.rec","r"))==NULL)
	{
		printf("fail\n");
		exit(0);
	}
	if((fq=fopen("/Users/hellooks/Desktop/fileio/filefinal.txt","w+"))==NULL)
	{
		printf("fail\n");
		exit(0);
	}
	int i=0;
	while(fread(&stu,sizeof(stu),1,fp)==1)
	{	
		people[i].sum=stu.english+stu.c+stu.math;
		strcpy(people[i].name,stu.name);
		i++;
	}
	for(int i=0;i<5;i++)
		printf("%s %f\n",people[i].name,people[i].sum);
	qsort(people,5,sizeof(people[0]),cmp);
	for(int i=0;i<5;i++)
		printf("%s %f\n",people[i].name,people[i].sum);
	struct studentpaixu final;
	for(int i=0;i<5;i++)
	{
		strcpy(final.name,people[i].name);
		final.sum=people[i].sum;
		fwrite(&final,sizeof(final),1,fq);
		printf("shuru\n");
	}
	fseek(fq,0,SEEK_SET);
	while((fread(&final,sizeof(final),1,fq))==1)
	{
		printf("%s\n",final.name);
	}
	fclose(fp);
	fclose(fq);
	return 0;
}

//12.9 
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct studentpaixu{
		char name[100];
		float sum;
	}people;
int cmp(const void *a,const void *b)
{
	return (*(struct studentpaixu*)b).sum-(*(struct studentpaixu*)a).sum;	

}
int main(int argc, char const *argv[])
{
	FILE *fp,*fq,*fw;
	if((fp=fopen("/Users/hellooks/Desktop/fileio/filefinal.txt","r"))==NULL)
	{
		printf("fail1\n");
		exit(0);
	}
	if((fq=fopen("/Users/hellooks/Desktop/fileio/filefinalnew.txt","w+"))==NULL)
	{
		printf("fail2\n");
		exit(0);
	}
	if((fw=fopen("/Users/hellooks/Desktop/fileio/filefinal2.txt","w+"))==NULL)
	{
		printf("fail3\n");
		exit(0);
	}
	int i=0;
	while(fread(&people,sizeof(people),1,fp)==1)
	{	
		if(i%2==0)
		{
			fwrite(&people,sizeof(people),1,fq);
			fwrite(&people,sizeof(people),1,fw);
		}
		i++;
	}
	remove("/Users/hellooks/Desktop/fileio/filefinal.txt");
	rename("/Users/hellooks/Desktop/fileio/filefinal2.txt","/Users/hellooks/Desktop/fileio/filefinal.txt");

}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct word
{
	char key[100];
}structword[1000];
int cmp(const void *a,const void *b)
{
	return strcmp((*(struct word*)a).key,(*(struct word*)b).key);
}
int main(int argc, char const *argv[])
{
	FILE *fp,*fq,*fw;
	if((fp=fopen("/Users/hellooks/Desktop/fileio/a.txt","r+"))==NULL)
	{
		printf("fail1\n");
		exit(0);
	}
	if((fq=fopen("/Users/hellooks/Desktop/fileio/b.txt","r+"))==NULL)
	{
		printf("fail2\n");
		exit(0);
	}
	if((fw=fopen("/Users/hellooks/Desktop/fileio/c.txt","r+"))==NULL)
	{
		printf("fail3\n");
		exit(0);
	}
	char ch;
	char words[10000];
	int i=0;
	while((ch=fgetc(fp))!=EOF)
	{
			words[i]=ch;
			//putchar(ch);
			i++;
	}
	words[i++]=' ';
	while((ch=fgetc(fq))!=EOF)
	{
			words[i]=ch;
			//printf("%c",words[i]);
			i++;
	}
	words[i++]=' ';
	int sum=i;
	int t=0;
	for(int i=0;i<sum-1;i++)
	{
		int k=0;
		char wd[100]={};
		while(words[i]!=' '){
			wd[k]=words[i];
			k++;
			i++;
		}
		//printf("%s\n",wd);
		strcpy(structword[t].key,wd);
		t++;
	}
	for(int i=0;i<t;i++)
		printf("%s\n",structword[i].key);
	qsort(&structword,t,sizeof(structword[0]),cmp);
	struct word final;
	for(int i=0;i<t;i++)
	{
		printf("%s\n",structword[i].key);
		
		strcpy(final.key,structword[i].key);
		fwrite(&final,sizeof(final),1,fw);
	}
	fseek(fw,0,SEEK_SET);
	printf("c中结果\n");
	while(fread(&final,sizeof(final),1,fw))
	{
		printf("%s\n",final.key);
	}
	fclose(fp);
	fclose(fw);
	fclose(fq);
}




 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值