#include<stdio.h>#define N 5typedefstruct student {long sno;char name[10];float score[3];} STU;voidfun(char*filename, STU n){ FILE *fp;/**********found**********/
fp =fopen(filename,"rb+");/**********found**********/fseek(fp,-(long)sizeof(STU),SEEK_END);/**********found**********/fwrite(&n,sizeof(STU),1, fp);fclose(fp);}main(){ STU t[N]={{10001,"MaChao",91,92,77},{10002,"CaoKai",75,60,88},{10003,"LiSi",85,70,78},{10004,"FangFang",90,82,87},{10005,"ZhangSan",95,80,88}};
STU n={10006,"ZhaoSi",55,70,68}, ss[N];int i,j; FILE *fp;
fp =fopen("student.dat","wb");fwrite(t,sizeof(STU), N, fp);fclose(fp);
fp =fopen("student.dat","rb");fread(ss,sizeof(STU), N, fp);fclose(fp);printf("\nThe original data :\n\n");for(j=0; j<N; j++){printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);for(i=0; i<3; i++)printf("%6.2f ", ss[j].score[i]);printf("\n");}fun("student.dat", n);printf("\nThe data after modifing :\n\n");
fp =fopen("student.dat","rb");fread(ss,sizeof(STU), N, fp);fclose(fp);for(j=0; j<N; j++){printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);for(i=0; i<3; i++)printf("%6.2f ", ss[j].score[i]);printf("\n");}getchar();}
计算特殊数列前n项和
#include<stdio.h>#include<math.h>doublefun(double x,int n){double f, t;int i;/**********found**********/
f =1;
t =-1;for(i=1; i<n; i++){/**********found**********/
t *=(-1)*x/i;/**********found**********/
f += t;}return f;}main(){double x, y;
x=2.5;
y =fun(x,15);printf("\nThe result is :\n");printf("x=%-12.6f y=%-12.6f\n", x, y);getchar();}#include<stdio.h>#pragma warning (disable:4996)doublefun(double x,int n){double f, t;int i;
f =1.0;/**********found**********/
t =1;/**********found**********/for(i=1; i<n; i++){/**********found**********/
t *= x/i;
f += t;}return f;}main(){double x, y;
x=2.5;
y =fun(x,12);printf("\nThe result is :\n");printf("x=%-12.6f y=%-12.6f\n", x, y);}
素数筛选法
#include<stdio.h>#pragma warning (disable:4996)intfun(int n){int a[10000], i,j, count=0;for(i=2; i<=n; i++) a[i]= i;
i =2;while(i<n){/**********found**********/for(j=a[i]*2; j<=n; j+=a[i])//素数倍数的筛选,筛选的步长为a[i]
a[j]=0;
i++;/**********found**********/while(a[i]==0)
i++;}printf("\nThe prime number between 2 to %d\n", n);for(i=2; i<=n; i++)/**********found**********/if(a[i]!=0){ count++;printf( count%15?"%5d":"\n%5d",a[i]);}return count;}main(){int n=20, r;
r =fun(n);printf("\nThe number of prime is : %d\n", r);}
字符处理
#include<stdio.h>voidfun(char*a,int n ){/* 以下代码仅供参考 */int i=0,j,k=0;while(a[k]=='*') k++;/* k为统计*字符个数 */if(k>n){
i=n;j=k;/* 以下完成将下标为k至串尾的字符前移k-n个位置 */}}main(){char s[81];int n;void NONO ();printf("Enter a string:\n");gets(s);printf("Enter n : ");scanf("%d",&n);fun( s,n );printf("The string after deleted:\n");puts(s);NONO();getchar();}void NONO (){/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
FILE *in,*out ;int i, n ;char s[81];
in =fopen("..\\..\\in.dat","r");
out =fopen("..\\..\\out.dat","w");for(i =0; i <10; i++){fscanf(in,"%s", s);fscanf(in,"%d",&n);fun(s,n);fprintf(out,"%s\n", s);}fclose(in);fclose(out);}
综合测试二
#include<stdio.h>#include<stdlib.h>voidfun(char*s,int a,double f){/**********found**********/
FILE *fp;char str[100], str1[100], str2[100];int a1;double f1;
fp =fopen("file1.txt","w");fprintf(fp,"%s %d %f\n", s, a, f);/**********found**********/fclose(fp);///
fp =fopen("file1.txt","r");/**********found**********/fscanf(fp,"%s%s%s", str, str1, str2);fclose(fp);
a1 =atoi(str1);
f1 =atof(str2);printf("\nThe result :\n\n%s %d %f\n", str, a1, f1);}main(){char a[10]="Hello!";int b=12345;double c=98.76;fun(a,b,c);getchar();}#include<stdio.h>#include<string.h>voidfun(char t[]){char c;int i, j;/**********found***********/for( i =strlen( t )-1; i; i--)//for( j =0; j < i; j++)/**********found***********/if( t[j]> t[ j +1])///{
c = t[j];
t[j]= t[ j +1];
t[j +1]= c;}}main(){char s[81];printf("\nPlease enter a character string: ");gets( s );printf("\n\nBefore sorting:\n \"%s\"", s );fun( s );printf("\nAfter sorting decendingly:\n \"%s\"\n", s );getchar();}#include<stdio.h>#define N 12typedefstruct{char num[10];double s;} STREC;doublefun( STREC *a, STREC *b,int*n ){int i,j;double aver=0;*n=0;for( i =0; i < N; i++){
aver=aver+a[i].s;
aver=aver/N;}for( i =0; i < N; i++){if(a[i].s>aver){
b[*n].s=a[i].s;}}for( j =0; j <10; j++){
b[*n].num[j]=a[j].num[j];*n=*n+1;}return(aver);}main(){ STREC s[N]={{"GA05",85},{"GA03",76},{"GA02",69},{"GA04",85},{"GA01",91},{"GA07",72},{"GA08",64},{"GA06",87},{"GA09",60},{"GA11",79},{"GA12",73},{"GA10",90}};
STREC h[N], t;FILE *out ;int i,j,n;double ave;
ave=fun( s,h,&n );printf("The %d student data which is higher than %7.3f:\n",n,ave);for(i=0;i<n; i++)printf("%s %4.1f\n",h[i].num,h[i].s);printf("\n");
out =fopen("..\\..\\out.dat","w");fprintf(out,"%d\n%7.3f\n", n, ave);for(i=0;i<n-1;i++)for(j=i+1;j<n;j++)if(h[i].s<h[j].s){t=h[i];h[i]=h[j]; h[j]=t;}for(i=0;i<n; i++)fprintf(out,"%4.1f\n",h[i].s);fclose(out);getchar();}
综合测试三
#include<stdio.h>#define N 5typedefstruct student {long sno;char name[10];float score[3];} STU;voidfun(char*filename, STU n){ FILE *fp;////**********found**********/
fp =fopen(filename,"rb+");///**********found**********/fseek(fp,-(long)sizeof(STU),SEEK_END);/**********found**********/fwrite(&n,sizeof(STU),1, fp);//fclose(fp);}main(){ STU t[N]={{10001,"MaChao",91,92,77},{10002,"CaoKai",75,60,88},{10003,"LiSi",85,70,78},{10004,"FangFang",90,82,87},{10005,"ZhangSan",95,80,88}};
STU n={10006,"ZhaoSi",55,70,68}, ss[N];int i,j; FILE *fp;
fp =fopen("student.dat","wb");fwrite(t,sizeof(STU), N, fp);fclose(fp);
fp =fopen("student.dat","rb");fread(ss,sizeof(STU), N, fp);fclose(fp);printf("\nThe original data :\n\n");for(j=0; j<N; j++){printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);for(i=0; i<3; i++)printf("%6.2f ", ss[j].score[i]);printf("\n");}fun("student.dat", n);printf("\nThe data after modifing :\n\n");
fp =fopen("student.dat","rb");fread(ss,sizeof(STU), N, fp);fclose(fp);for(j=0; j<N; j++){printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);for(i=0; i<3; i++)printf("%6.2f ", ss[j].score[i]);printf("\n");}getchar();}#include<stdio.h>int fun (char*str,char*substr){int i,j,k,num=0;/************found************/for(i =0; str[i]; i++)for(j=i;k=0;substr[k]==str[j];k++,j++)/************found************/if(substr[k+1]=='\0'){ num++;break;}return num;}main(){char str[80],substr[80];printf("Input a string:");gets(str);printf("Input a substring:");gets(substr);printf("%d\n",fun(str,substr));getchar();}#include<stdio.h>#define N 16typedefstruct{char num[10];int s;} STREC;voidfun( STREC a[]){
STREC tmp;int i,j;for(i =0; i < N; i++)for(j = i+1; j < N; j++){/* 请按题目要求完成以下代码 */
STREC tmp;int i,j;for( i =0; i < N; i++){for( j =0; j < N; j++){if(a[i].s<a[j].s){
tmp=a[i];
a[i]=[j];
a[j]=tmp;}}}}}main(){ STREC s[N]={{"GA005",85},{"GA003",76},{"GA002",69},{"GA004",85},{"GA001",91},{"GA007",72},{"GA008",64},{"GA006",87},{"GA015",85},{"GA013",91},{"GA012",64},{"GA014",91},{"GA011",66},{"GA017",64},{"GA018",64},{"GA016",72}};int i;FILE *out ;fun( s );printf("The data after sorted :\n");for(i=0;i<N; i++){if((i)%4==0)printf("\n");printf("%s %4d ",s[i].num,s[i].s);}printf("\n");
out =fopen("..\\..\\out.dat","w");for(i=0;i<N; i++){if((i)%4==0&& i)fprintf(out,"\n");fprintf(out,"%4d ",s[i].s);}fprintf(out,"\n");fclose(out);getchar();}
综合测试四
#include<stdio.h>#include<math.h>doublefun(double x,int n){double f, t;int i;/**********found**********/
f =1;
t =-1;for(i=1; i<n; i++){/**********found**********/
t *=(-1)*x/i;/**********found**********/
f += t;}return f;}main(){double x, y;
x=2.5;
y =fun(x,15);printf("\nThe result is :\n");printf("x=%-12.6f y=%-12.6f\n", x, y);getchar();}#include<stdio.h>//删除所有cvoidfun(char*s ){int i,j;for(i=j=0; s[i]!='\0'; i++)if(s[i]!='c')/************found************/
s[j++]=s[i];/************found************/
s[j]='\0';}main(){char s[80];printf("Enter a string: ");gets(s);printf("The original string: ");puts(s);fun(s);printf("The string after deleted : ");puts(s);printf("\n\n");getchar();}#include<stdio.h>voidfun(int a,int b,long*c){int x,y,x1,y1
x=a%10;
y=%100;
x1=a%10;
y1=%100;int result=x*1000+x1*100+y*10+y1;}main(){int a,b;long c;void NONO ();printf("Input a b:");scanf("%d%d",&a,&b);fun(a, b,&c);printf("The result is: %ld\n", c);NONO();getchar();}void NONO (){/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
FILE *rf,*wf ;int i, a,b ;long c ;
rf =fopen("..\\..\\in.dat","r");
wf =fopen("..\\..\\out.dat","w");for(i =0; i <10; i++){fscanf(rf,"%d,%d",&a,&b);fun(a, b,&c);fprintf(wf,"a=%d,b=%d,c=%ld\n", a, b, c);}fclose(rf);fclose(wf);}
综合测试五
#include<stdio.h>#pragma warning (disable:4996)doublefun(double x,int n){double f, t;int i;
f =1.0;/**********found**********/
t =1;/**********found**********/for(i=1; i<n; i++){/**********found**********/
t *= x/i;
f += t;}return f;}main(){double x, y;
x=2.5;
y =fun(x,12);printf("\nThe result is :\n");printf("x=%-12.6f y=%-12.6f\n", x, y);}#include<stdio.h>/**********found**********/voidfun(char*s,int a,int b){while(*s){if(*s>='A'&&*s <='Z')/**********found**********/*a=*a+1;//看main函数if(*s>='a'&&*s <='z')/**********found**********/*b=*b+1;
s++;}}main(){char s[100];int upper=0,lower=0;printf("\n Please a string:");gets(s);fun(s,&upper,&lower);printf("\n upper=%d lower=%d\n",upper,lower);getchar();}#include<stdio.h>#include<string.h>#pragma warning (disable:4996)#define N 16typedefstruct{char num[10];int s;} STREC;
STREC fun( STREC *a,char*b ){int i,f=0;
STREC p;
p=a[0];for( i =0; i < N; i++){if(strcmp(a[i].num,b)==0)//学号字符串比较{
p=a[i];
f=1;}if(f==0)//没找到{
p.num[0]='\0';
p.s=-1;}return p;}}main(){ STREC s[N]={{"GA005",85},{"GA003",76},{"GA002",69},{"GA004",85},{"GA001",91},{"GA007",72},{"GA008",64},{"GA006",87},{"GA015",85},{"GA013",91},{"GA012",64},{"GA014",91},{"GA011",77},{"GA017",64},{"GA018",64},{"GA016",72}};
STREC h;char m[10];int i;FILE *out ;printf("The original data:\n");for(i=0; i<N; i++){if(i%4==0)printf("\n");printf("%s %3d ",s[i].num,s[i].s);}printf("\n\nEnter the number: ");gets(m);
h=fun( s,m );printf("The data : ");printf("\n%s %4d\n",h.num,h.s);printf("\n");
out =fopen("..\\..\\out.dat","w");
h=fun(s,"GA013");fprintf(out,"%s %4d\n",h.num,h.s);fclose(out);getchar();}
字符串转数字
long fun (char*p){/* 以下代码仅供参考 */int i,len;/* len为串长 */long x=0;
len=strlen(p);/* 以下完成数字字符串转换为一个数字。注意:字符'0'不是数字0 */
len=strlen(p);for(int i=0;i<len;i++){
x=x*10+p[i]-'0';}return x;}