C语言(复杂数据,DEVFORGE学编程社区)

Posted on 2019-10-10 14:37  金色的省略号  阅读(603)  评论(0编辑  收藏  举报

1、小型数据库

 1 #include <stdio.h> 
 2 #include <stdlib.h>   
 3 #define N 100
 4 
 5 struct student{
 6     int no;
 7     char name[12];
 8     int score;    
 9 }s[N];
10 
11 int main()
12 {
13     int n;
14     scanf("%d",&n);
15     /* 保存 */
16     FILE *fp;    
17     fp = fopen("DATA5614.DB","wb");    
18     scanf("%d%s%d", &s[0].no,s[0].name,&s[0].score);
19     fprintf(fp,"%d %s %d ", s[0].no,s[0].name,s[0].score);
20     int max = s[0].score, index = 0;
21     for(int i=1; i<n; ++i)
22     {
23         scanf("%d%s%d", &s[i].no,s[i].name,&s[i].score);
24         fprintf(fp,"%d %s %d ", s[i].no,s[i].name,s[i].score);
25         if(s[i].score>max){
26             max = s[i].score;
27             index = i;
28         }            
29     }        
30     /* rewind */
31     rewind(fp);
32     /*读取 */
33     fp = fopen("DATA5614.DB","rb");
34     struct student stu;
35     for(int i=0; i<=index; ++i)
36     {
37         fscanf(fp,"%d%s%d", &stu.no,stu.name,&stu.score);
38     }
39     printf("%d %s %d", stu.no,stu.name,stu.score);        
40     fclose(fp);
41     return 0;
42 }

2、检查图像文件格式

 1 #include <stdio.h> 
 2 #include <stdlib.h> 
 3 #include <string.h>  
 4 
 5 int main()
 6 {
 7     char arr[100]="";
 8     char JFIF[]="JFIF"; //6
 9     char GIF89a[]="GIF89a";//0
10     char PNG[]="PNG";//1
11     
12     FILE *fp;    
13     fp = fopen("DATA5609.DAT","rb");    
14     fread(arr,sizeof(unsigned char),100,fp);
15     /*      */
16     if(0 == strncmp(&arr[6],JFIF,4))
17         printf("%s\n",JFIF);
18     else if(0 == strncmp(&arr[0],GIF89a,6))
19         printf("%s\n",GIF89a);
20     else if(0 == strncmp(&arr[1],PNG,3))    
21         printf("%s\n",PNG);
22     else
23         printf("NULL\n");
24     fclose(fp);
25     return 0;
26 }

3、重组字符串

 1 #include<stdio.h>
 2 #include <string.h>
 3 #define N 100
 4 void fun(char *s, char *t);
 5 int main()
 6 {
 7     char s[N] = "", t[N] = "";
 8     gets(s);
 9     fun(s,t);
10     printf("%s",t);
11     return 0;
12 }
13 void fun(char *s, char *t){
14     int index = 0;
15     for(int i=0; s[i]; ++i){
16         if(i%2&&s[i]%2)
17             t[index++] = s[i];            
18     }
19     t[index] = '\0';
20 }

4、分数加减法

 1 #include<stdio.h>
 2 #include <string.h>
 3 #define N 100
 4 void fun(int *num,int *den);
 5 int main()
 6 {
 7     char s[N] = "";
 8     gets(s);
 9     
10     int a,b,c,d,num,den;
11     a=s[2]-'0',b=s[0]-'0',c=s[4]-'0',d=s[6]-'0';
12     if(s[3]=='+')num=b*d+c*a;    
13     if(s[3]=='-')num=b*d-c*a;    
14     den=a*d;
15     if(!num) printf("0");
16     else{
17         fun(&num,&den);
18         printf("%d/%d",num,den);
19     }    
20     return 0;
21 }
22 void fun(int *num,int *den){
23     int t = *num>*den?*num:*den;
24     while(t)
25     {
26         if((*num)%t==0&&(*den)%t==0){
27             *num /= t;
28             *den /= t;
29             break;
30         }
31         t--;
32     }
33 }

5、搜索字符

 1 #include<stdio.h>
 2 #include <string.h>
 3 #define N 100
 4 char* search(char *cpsource, char ch);
 5 int main()
 6 {
 7     char s[N] = "",ch;
 8     gets(s);
 9     ch = getchar();
10     printf("%s",search(s,ch));
11     return 0;
12 }
13 char* search(char *cpsource, char ch){
14     int index=0, count=0, max=0;
15     for(int i=0; cpsource[i]; ++i){
16         if(cpsource[i]==ch){
17             count++;
18         }
19         else{
20             if(max<count){
21                 max = count;
22                 index = i-max;
23             } 
24             count=0;
25         }
26     }
27     cpsource[index+max] = '\0';
28     return &cpsource[index];
29 }

6、复数

 1 #include<stdio.h>
 2 #include <string.h>
 3 #define N 100
 4 typedef struct complex{
 5     double r;
 6     double i;
 7 }COMPLEX;
 8 
 9 int main()
10 {    /* 1 */
11     COMPLEX c[2];
12     double r,i;
13     for(int i=0; i<2; ++i){
14         scanf("%lf%lf",&c[i].r,&c[i].i);
15     }
16     /* 2 */
17     char ch[5]="+-*/";
18     for(int j=0; j<4; ++j){
19         switch(ch[j]){
20             case '+':
21                 r=c[0].r+c[1].r;
22                 i=c[0].i+c[1].i;
23                 break;
24             case '-':
25                 r=c[0].r-c[1].r;
26                 i=c[0].i-c[1].i;
27                 break;
28             case '*':   /* (ac-bd)+(bc+ad)i */
29                 r=c[0].r*c[1].r - c[0].i*c[1].i;
30                 i=c[0].i*c[1].r + c[0].r*c[1].i;
31                 break;
32             case '/':   /*(ac+bd)/(c*c+d*d) +((bc-ad)/(c*c+d*d))i*/
33                 if(c[1].r||c[1].i){
34                     r=(c[0].r*c[1].r + c[0].i*c[1].i)
35                         /(c[1].r*c[1].r+c[1].i*c[1].i);
36                     i=(c[0].i*c[1].r - c[0].r*c[1].i)
37                         /(c[1].r*c[1].r+c[1].i*c[1].i);
38                 }
39                 else {
40                     r=i=0;
41                 }                
42                 break;
43         }        
44         printf("(%.2f+%.2fi)%c(%.2f+%.2fi)=(%.2f%+.2fi)\n",
45             c[0].r,c[0].i,ch[j],c[1].r,c[1].i,r,i);    
46     }
47     return 0;
48 }

7、插入字符

 1 #include<stdio.h>
 2 #include <string.h>
 3 #define N 100
 4 
 5 int main()
 6 {    
 7     char s[N] = "", x;
 8     int i;
 9     gets(s);
10     scanf(" %c%d",&x,&i);
11     
12     char t[N] = "";
13     strncpy(t,s,i);
14     t[i] = x;
15     strcat(t,&s[i]);
16     puts(t);
17     return 0;
18 }

8、文件比较

 1 #include <stdio.h> 
 2 #include <stdlib.h> 
 3 #include <string.h>  
 4 
 5 int main()
 6 {
 7     char str1[256]="";
 8     char str2[256]="";
 9     FILE *fp1,*fp2;    
10     fp1 = fopen("DATA5613.TXT","r");
11     fp2 = fopen("DATA5613.CPP","r");
12     /*  */
13     int i,j;
14     for(i=0; !feof(fp1)&&!feof(fp2); ++i)
15     {
16         fgets(str1,sizeof(str1)-1,fp1);
17         fgets(str2,sizeof(str2)-1,fp2);
18         int flag = 0;
19         for(j=0; str1[j]&&str2[j]; ++j)
20         {
21             if(str1[j]!=str2[j]){
22                 flag = 1;
23                 break;
24             }                
25         }
26         if(flag)
27             printf("%d %d\n",i+1,j+1);
28     }
29     /*  */
30     fclose(fp1);
31     fclose(fp2);
32     return 0;
33 }

9、检测位图长宽

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #pragma pack(1)//结构体按1个字节对齐 3 
 4  typedef struct tagBITMAPFILEHEADER{//文件头 4 
 5     unsigned short bfType;//文件类型,固定为BM 5 
 6     unsigned int bfSize;//文件大小(字节) 6 
 7     unsigned short bfReserved1;//保留 7 
 8     unsigned short bfReserved2;//保留 8 
 9     unsigned int bfOffBits;//位图数据到文件头的偏移位置 9 
10 }BITMAPFILEHEADER; 
11 
12 typedef struct tagBITMAPINFOHEADER{//位图信息头 11 
13     unsigned int biSize;//位图信息头的长度 12 
14     long biWidth;//位图的宽度,以像素为单位 13 
15     long biHeight;//位图的高度,以像素为单位 14 
16     unsigned short biPlanes;//位图的位面数 15 
17     unsigned short biBitCount;//每个像素的位数
18     unsigned int biCompression;//数据压缩类型 17 
19     unsigned int biSizeImage;//位图数据大小(字节) 18 
20     long biXPelsPerMeter;//水平分辨率(像素/米) 19 
21     long biYPelsPerMeter;//垂直分辨率(像素/米) 20 
22     unsigned int biClrUsed;//位图使用的颜色数 21 
23     unsigned int biClrImportant;//调色板规范 22 
24 }BITMAPINFOHEADER;
25 
26 int main()
27 {
28     BITMAPFILEHEADER bmfh;
29     BITMAPINFOHEADER bmih;
30     FILE *bmp;
31     //printf("%d,%d\n",sizeof(BITMAPFILEHEADER),sizeof(BITMAPINFOHEADER)); 
32     bmp=fopen("DATA5611.BMP","rb");
33     if(bmp!=NULL) {
34         fread(&bmfh,sizeof(BITMAPFILEHEADER),1,bmp);
35         fread(&bmih,sizeof(BITMAPINFOHEADER),1,bmp);
36         //printf("位图宽=%d,高=%d\n",bmih.biWidth,bmih.biHeight);
37         printf("%ld %ld\n",bmih.biHeight,bmih.biWidth);
38         fclose(bmp);
39     }
40     return 0;
41 } 

10、POJer的烦恼

 1 #include<stdio.h>
 2 #include <string.h>
 3 #define N 100
 4 
 5 int main()
 6 {    
 7     long long n;
 8     scanf("%lld",&n);
 9     int num[N] = {0},index = 0;
10     while(n)
11     {
12         num[index++] = n%2;
13         n /= 2;
14     }
15     
16     for(int i=index-1; i>=0; --i)
17         printf("%d",num[i]); 
18     return 0;
19 }