2018 test1-4

it2023-09-15  83

#include<stdio.h> #include<math.h> #include<string.h> #include<stdlib.h> //1 // int fun(int n){ // if(n==0) return 0; // if(n%2==1) return 2*fun(n-1)+1; // else return 2*fun(n-1)-1; // } // int main(int argc, char const *argv[]) // { // int n; // scanf("%d",&n); // //printf("%d\n",n ); // int c=fun(n); // printf("%d\n",c); // } //2 // void pick(char *s,char *sub,int beg,int end,int step) // { // int j=0; // for(int i=beg;i<=end-1;i=i+step) // { // sub[j]=s[i]; // j++; // }sub[j]='\0'; // printf("%s",sub); // } // int main(int argc, char const *argv[]) // { // char s[100]; // char sub[100]; // gets(s); // pick(s,sub,2,8,2); // return 0; // } //3 // int cmp(const void *a,const void *b) // { // return *(int *)a-*(int *)b; // } // int main(int argc, char const *argv[]) // { // FILE *fp,*fq; // if((fp=fopen("/Users/hellooks/Desktop/Code/numbernew.txt","r+"))==NULL) // { // printf("fail\n"); // exit(0); // } // if((fq=fopen("/Users/hellooks/Desktop/Code/number2.txt","w"))==NULL) // { // printf("fail\n"); // exit(0); // } // char str[100]; // int a[100]; // int i=0; // while(fgets(str,100,fp)!=NULL) // { // a[i++]=atoi(str); // } // for(int j=0;j<i;j++) // printf("%d\n",a[j]); // qsort(a,i,sizeof(a[0]),cmp); // for(int j=0;j<i;j++) // printf("%d\n",a[j]); // for(int j=0;j<i;j++) // fprintf(fq,"%d\n",a[j]); // remove("/Users/hellooks/Desktop/Code/number.txt"); // rename("/Users/hellooks/Desktop/Code/number2.txt","/Users/hellooks/Desktop/Code/numbernew.txt"); // fclose(fq); // fclose(fp); // return 0; // }//可以直接fseek(fp,0,SEEK_SET) fprintf(fp,"%d\n",a[j]) //4 struct goods { char no[21]; char name[22]; float price; int num; }something; int main(int argc, char const *argv[]) { FILE *fp; if((fp=fopen("/Users/hellooks/Desktop/Code/new.txt","w+"))==NULL) { printf("fail\n"); exit(0); } char c='Y'; while(c!='N') { char s[100]; printf("输入编号\n"); gets(s); strcpy(something.no,s); printf("输入名称\n"); gets(s); strcpy(something.name,s); printf("输入单价\n"); gets(s); something.price=atof(s); printf("输入数量\n"); gets(s); something.num=atoi(s); fwrite(&something,sizeof(something),1,fp); printf("继续输入》\n"); scanf("%c",&c); getchar(); } while(1) { char s[100]; printf("输入你要查询的编号\n"); scanf("%s",s); fseek(fp,0,SEEK_SET); while(fread(&something,sizeof(something),1,fp)==1) { if(strcmp(something.no,s)==0) { printf("find\n"); printf("%s %s %f %i",something.name,something.no,something.price,something.num); } } } fclose(fp); return 0; }

 

最新回复(0)