瑞格下册——5882
瑞格下册——7131
瑞格下册——7147
瑞格下册——7117
瑞格下册——7089
代码一
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
struct shuzi
{
int x;
int y;
};
typedef struct shuzi shuzi;
int main()
{
shuzi a[30];
char c;
int i,n;
for(i=0;i<26;i++)
{
a[i].x=i;
a[i].y=0;
}
while(~scanf("%c",&c))
{while(c!=' ')
{
if(c>='a'&&c<='z')
{
n=c%'a';
a[n].y++;
}
c=getchar();
}
for(i=0;i<26;i++)
{
if(a[i].y!=0)
{
printf("%c %d\n",'a'+a[i].x,a[i].y);
a[i].y=0;
}
}
getchar();
}
return 0;
}
代码二
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[100],b,c[30];
int num1,aaa[200],i,j;
gets(a);
memset(aaa,0,sizeof(aaa));
num1=strlen(a);
c[0]='a';c[1]='b';c[2]='c';c[3]='d';c[4]='e';c[5]='f';c[6]='g';c[7]='h';c[8]='i';
c[9]='j';c[10]='k';c[11]='l';c[12]='m';c[13]='n';
c[13]='n';
c[14]='o';c[15]='p';c[16]='q';c[17]='r';c[18]='s';c[19]='t';c[20]='u';c[20]='v';
c[21]='w';c[22]='x';c[23]='y';
for(i=0;i<num1;i++)
{
if(a[i]<='z'&&a[i]>='a')
{
aaa[a[i]]++;
}
}
for(i=97;i<200;i++)
{
if(aaa[i]!=0)
{
printf("%c %d\n",c[i%97],aaa[i]);
}
}
return 0;
}
“约瑟夫环” 瑞格下册——5859
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main()
{
int m,n,*p,i,j,num;
while(~scanf("%d%d",&m,&n))
{p=(int *)malloc(m*sizeof(int));
for(i=0;i<m;i++)
{
p[i]=1;
}
num=0;//记录出列的人数
j=0;//报数器
while(num!=m-1)
{
for(i=0;i<m;i++)
{
if(p[i]!=0)//有资格报数的
{
j++;
if(j==n)
{
p[i]=0;//出列的
num++;
j=0;
}
}
}
}
for(i=0;i<m;i++)
{
if(p[i]!=0)
{
printf("%d\n",i);
}
}
num=0;
j=0;
}
return 0;
}
链表排序
1.利用冒泡法对已经存在的链表进行排序;
int SortLinkBubble(Link_Node *pHead)
{ //从小到大
int n,i,j;
Link_Node *p,*pNext;
DataType temp;
n=Length(pHead);
for(i=0;i<n-1;i++)
{
p=pHead->next;
pNext=p->next;
for(j=0;j<n-i-1;j++)
{ if(p->data>pNext->data)
{ temp=p->data;
p->data=pNext->data;
pNext->data=temp;
}
p=p->next;
pNext=p->next;
}
}
return 0;
}
2.读取每个节点创建有序链表进行排序;
int SortLink(Link_Node *pHead)
{//从小到大
Link_Node *p,*pNext;
p=pHead->next;
pHead->next=NULL;
while(p!=NULL)
{
pNext=p->next;
InsertNodeToSorted(pHead,p);
p=pNext;
}
return 0;
}
3.链表逆序
int SortLinkBubble(Link_Node *pHead)
{
int n,i,j;
Link_Node *p,*pNext;
DataType temp;
n=NodeNumber(pHead);
for(i=0;i<n-1;i++)
{
p=pHead->next;
pNext=p->next;
for(j=0;j<n-i-1;j++)
{ if(p->data>pNext->data)
{ temp=p->data;
p->data=pNext->data;
pNext->data=temp;
}
p=p->next;
pNext=p->next;
}
}
return 0;
}
瑞格下册——5865
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main()
{
int m,n,i;
char a[300];
FILE *fp;
if((fp=fopen("text01.txt","w+"))==NULL)
{
printf("file open error!\n");
exit(1);
}
while(scanf("%d",&m)!=EOF&&m!=0)
{
getchar();
for(i=1;i<=m;i++)
{
gets(a);
fputs(a,fp);
fputc('\n',fp);
}
}
rewind(fp);
while(fgets(a,300,fp)!=NULL)
printf("%s",a);
fclose(fp);
return 0;
}
瑞格下册——5866
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
struct student
{
int no;
int length;
char name[25];
char sex;
double score[10];
};
typedef struct student student;
int main()
{
int m,i;
FILE *fp;
student stu1;
scanf("%d",&m);
if((fp=fopen("student.txt","w"))==NULL)
{
printf("file open error!\n");
exit(1);
}
for(i=1;i<=m;i++)
{
scanf("%d %[^\n] %c %lf %lf %lf",&stu1.no,stu1.name,&stu1.sex,&stu1.score[0],
&stu1.score[1],&stu1.score[2]);
stu1.score[4]=stu1.score[0]+stu1.score[1]+stu1.score[2];
stu1.score[3]=stu1.score[4]/3;
stu1.length=strlen(stu1.name);
fprintf(fp,"%d %d %s %c %.2lf %.2lf %.2lf %.2lf %.2lf\n",stu1.no,stu1.length,
stu1.name,stu1.sex,stu1.score[0],stu1.score[1],stu1.score[2],
stu1.score[3],stu1.score[4]);
}
fclose(fp);
return 0;
}
瑞格下册——5867
接上一题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
struct student
{
int no;
int length;
char name[25];
char sex;
double score[10];
};
typedef struct student student;
int main()
{
char c;
FILE *fp;
student stu1;
if((fp=fopen("student.txt","r"))==NULL)
{
printf("file open error!\n");
exit(1);
}
while(fscanf(fp,"%d %d",&stu1.no,&stu1.length)!=-1)
{
c=fgetc(fp);//要注意吞掉文件中的空格,很重要
fgets(stu1.name,stu1.length+1,fp);
c=fgetc(fp);//要注意吞掉文件中的空格,很重要
fscanf(fp,"%c %lf %lf %lf %lf %lf",&stu1.sex,&stu1.score[0],&stu1.score[1],
&stu1.score[2],&stu1.score[3],&stu1.score[4]);
printf("%d %s %c %.2lf %.2lf %.2lf %.2lf %.2lf\n",stu1.no,stu1.name,
stu1.sex,stu1.score[0],stu1.score[1],
stu1.score[2],stu1.score[3],stu1.score[4]);
}
fclose(fp);
return 0;
}
瑞格下册——5868
除了题目要求外,我还将文件中的数据又打印出来了
代码一
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
//#include "链表.h"
#define len sizeof(shuzi)
struct shuzi
{
int a;
char b[30];
char sex;
double score[10];
struct shuzi *next;
};
typedef struct shuzi shuzi;
void charu(shuzi *phead)
{
shuzi *p=phead->next;
shuzi *q=phead;
shuzi *pnew=(shuzi *)malloc(len);
scanf("%d %[^\n] %c %lf %lf %lf",&pnew->a,pnew->b,&pnew->sex,&pnew->score[0],&pnew->score[1],
&pnew->score[2]);
pnew->score[4]=pnew->score[0]+pnew->score[1]+pnew->score[2];
pnew->score[3]=pnew->score[4]/3;
pnew->next=NULL;
while(p!=NULL)
{
if(p->score[4]<=pnew->score[4])
{
q->next=pnew;
pnew->next=p;
return;
}
else
{
q=p;
p=p->next;
}
}
q->next=pnew;
return;
}
shuzi *zhijiepaixu(int m)
{
shuzi *phead;
phead=(shuzi *)malloc(len);
phead->next=NULL;
int i,n;
for(i=0;i<m;i++)
{
charu(phead);
}
return phead;
}
void baocun(shuzi *phead,FILE *fp)
{
shuzi *p=phead->next;
while(p!=NULL)
{
fwrite(p,len,1,fp);
p=p->next;
}
}
void rread(FILE *fp)
{
rewind(fp);//一定要注意要将光标移到文件开头再读,已经好几次折在这里了,还不长记性!!!
shuzi *p=(shuzi *)malloc(len);
while(fread(p,len,1,fp)!=0)
{
printf("%d %s %c %.2lf %.2lf %.2lf %.2lf %.2lf\n",p->a,p->b,p->sex,
p->score[0],p->score[1],p->score[2],p->score[3],p->score[4]);
}
}
void freee(shuzi *phead)
{
shuzi *p=phead->next;
shuzi *q=p;
while(q!=NULL)
{
q=p->next;
free(p);
p=q;
}
free(phead);
}
int main()
{
int m,n,num;
FILE *fp;
if((fp=fopen("student.txt","w+"))==NULL)
{
printf("file open error!\n");
exit(1);
}
shuzi *phead,*p;
scanf("%d",&m);
phead=zhijiepaixu(m);
//outputs(phead);
baocun(phead,fp);
rread(fp);
freee(phead);
phead=NULL;
fclose(fp);
return 0;
}
代码二
与代码一的差别仅仅在baocun函数和rread函数上。
代码一是将结构体的所有内容都保存在文件中了(包括下一个文件指针),读的时候也是要全部读出来但打印的时候不用打印文件指针就行。
代码二没有将结构体中的结构体指针保存到文件,读的时候自然也不用,但要注意数据块儿大小的变化。存和读时要相同!!!
但在真正写系统时,用代码一就行,代码二容易错。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
//#include "链表.h"
#define len sizeof(shuzi)
struct shuzi
{
int a;
char b[30];
char sex;
double score[10];
struct shuzi *next;
};
typedef struct shuzi shuzi;
void charu(shuzi *phead)
{
shuzi *p=phead->next;
shuzi *q=phead;
shuzi *pnew=(shuzi *)malloc(len);
scanf("%d %[^\n] %c %lf %lf %lf",&pnew->a,pnew->b,&pnew->sex,&pnew->score[0],&pnew->score[1],
&pnew->score[2]);
pnew->score[4]=pnew->score[0]+pnew->score[1]+pnew->score[2];
pnew->score[3]=pnew->score[4]/3;
pnew->next=NULL;
while(p!=NULL)
{
if(p->score[4]<=pnew->score[4])
{
q->next=pnew;
pnew->next=p;
return;
}
else
{
q=p;
p=p->next;
}
}
q->next=pnew;
return;
}
shuzi *zhijiepaixu(int m)
{
shuzi *phead;
phead=(shuzi *)malloc(len);
phead->next=NULL;
int i,n;
for(i=0;i<m;i++)
{
charu(phead);
}
return phead;
}
void baocun(shuzi *phead,FILE *fp)
{
shuzi *p=phead->next;
while(p!=NULL)
{
fwrite(p,len-sizeof(shuzi *),1,fp);
p=p->next;
}
}
void rread(FILE *fp)
{
rewind(fp);
shuzi *p=(shuzi *)malloc(len-sizeof(shuzi *));
while(fread(p,len-sizeof(shuzi *),1,fp)!=0)
{
printf("%d %s %c %.2lf %.2lf %.2lf %.2lf %.2lf\n",p->a,p->b,p->sex,
p->score[0],p->score[1],p->score[2],p->score[3],p->score[4]);
}
}
void freee(shuzi *phead)
{
shuzi *p=phead->next;
shuzi *q=p;
while(q!=NULL)
{
q=p->next;
free(p);
p=q;
}
free(phead);
}
int main()
{
int m,n,num;
FILE *fp;
if((fp=fopen("student.txt","w+"))==NULL)
{
printf("file open error!\n");
exit(1);
}
shuzi *phead,*p;
scanf("%d",&m);
phead=zhijiepaixu(m);
//outputs(phead);
baocun(phead,fp);
rread(fp);
freee(phead);
phead=NULL;
fclose(fp);
return 0;
}
瑞格下册——5870
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main()
{
FILE *fp1,*fp2;
int m,i=0,j;
char *p;
if((fp1=fopen("in.txt","r"))==NULL)
{
printf("file open error!\n");
exit(1);
}
if((fp2=fopen("out.txt","w+"))==NULL)
{
printf("file open error!\n");
exit(1);
}
fseek(fp1,0,SEEK_END);
m=ftell(fp1);//返回in.txt文件中的字符数
p=(char *)malloc((m+5)*sizeof(char));
rewind(fp1);//先将光标移到开头再读取。
while((p[i]=fgetc(fp1))!=-1)
{
p[i]=p[i]+3;
i++;
}
for(j=0;j<i;j++)
{
fputc(p[j],fp2);
}
rewind(fp2);//先将光标移到开头再读取
i=0;
while((p[i]=fgetc(fp2))!=-1)
{
p[i]=p[i]-3;
i++;
}
p[i]='\0';//不要忘记加\0表示数组结束
printf("%s",p);
fclose(fp1);
fclose(fp2);
return 0;
}
瑞格下册——5871
题目描述有错误,实际是以空行形式分割,不是空格
本题与上一题很相似,稍微改一改就行
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main()
{
FILE *fp1,*fp2,*fp3;
char *p;
int m,n,i=0,j;
if((fp1=fopen("in1.txt","r"))==NULL)
{
printf("file open error!\n");
exit(1);
}
if((fp2=fopen("in2.txt","r"))==NULL)
{
printf("file open error!\n");
exit(1);
}
if((fp3=fopen("out.txt","w+"))==NULL)
{
printf("file open error!\n");
exit(1);
}
fseek(fp1,0,SEEK_END);
m=ftell(fp1);
fseek(fp2,0,SEEK_END);
n=ftell(fp2);
p=(char *)malloc((m+n+5)*sizeof(char));
rewind(fp1);
rewind(fp2);
while((p[i]=fgetc(fp1))!=-1)
{
i++;
}
p[i]='\n';
i++;
p[i]='\n';
i++;
while((p[i]=fgetc(fp2))!=-1)
{
i++;
}
for(j=0;j<i;j++)
{
fputc(p[j],fp3);
}
rewind(fp3);
i=0;
while((p[i]=fgetc(fp3))!=-1)
{
i++;
}
p[i]='\0';
printf("%s",p);
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}