1个回答
展开全部
1)
#include "stdio.h"
#include <string.h>
void mysort(char (*p)[50],int n){
int i,j,k;
char t[50];
for(i=0;i<n;i++){
for(k=i,j=k+1;j<n;j++)
if(strcmp(p[k],p[j])>0)
k=j;
if(k!=i){
for(j=0;t[j]=p[i][j];j++);
for(j=0;p[i][j]=p[k][j];j++);
for(j=0;p[k][j]=t[j];j++);
}
}
}
int main(int argc,char *argv[]){
char s[100][50],n,i;
FILE *fp,*fpo;
fp=fopen("d:\\books.txt","r");
if((fpo=fopen("d:\\books_sorted.txt","w"))==NULL){
printf("Open the file failure...\n");
return 0;
}
for(n=0;fscanf(fp,"%s",s[n])!=EOF;n++);
fclose(fp);
mysort(s,n);
for(i=0;i<n;fprintf(fpo,"%s\n",s[i++]));
fclose(fpo);
return 0;
}
运行样例:
2)
#include "stdio.h"
struct stu{
char name[21];
int by,bm,bd;
};
int myfind(struct stu *p){
int m[]={31,28,31,30,31,30,31,31,30,31,30,31};
m[1]+=p->by%4==0 && p->by%100 || p->by%400==0;
return p->bm>0 && p->bm<13 && p->bd>0 && p->bd<=m[p->bm-1];
}
int main(int argc,char *argv[]){
struct stu stu;
FILE *fp;
if((fp=fopen("d:\\students.txt","r"))==NULL){
printf("Open the file failure...\n");
return 0;
}
printf("Invalid date as follows:\n");
while(fscanf(fp,"%s%d-%d-%d",stu.name,&stu.by,&stu.bm,&stu.bd)!=EOF)
if(!myfind(&stu))
printf("%s %d-%d-%d\n",stu.name,stu.by,stu.bm,stu.bd);
fclose(fp);
return 0;
}
运行样例:
3)
#include "stdio.h"
int main(int argc,char *argv[]){
int n,g,l,f,s,t;
FILE *fp;
if((fp=fopen("d:\\scores.txt","r"))==NULL){
printf("Open the file failure...\n");
return 0;
}
for(g=-1,l=101,f=n=s=0;fscanf(fp,"%d",&t)!=EOF;n++){
if(g<t)
g=t;
if(l>t)
l=t;
if(t<60)
f++;
s+=t;
}
printf("The total number of %d people.\n",n);
printf("The highest: %d\nThe lowest: %d\nThe average: %.2f\n",g,l,s/(n+0.0));
printf("%d people didn't pass the exam.\n",f);
return 0;
}
运行样例:
4)
#include "stdio.h"
int myfunc(int n){
int m,t;
for(t=n,m=0;t;t/=10)
(m*=10)+=t%10;
return m==n;
}
int main(int argc,char *argv[]){
int n,k=0;
FILE *fp;
if((fp=fopen("d:\\numbers.txt","r"))==NULL){
printf("Open the file failure...\n");
return 0;
}
while(fscanf(fp,"%d",&n)!=EOF){
if(myfunc(n))
printf(++k%5 ? "%11d" : "%11d\n",n);
}
fclose(fp);
if(k%5)
printf("\n");
return 0;
}
运行样例:
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询