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

1、

2、

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、

9、

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 }

猜你喜欢

转载自www.cnblogs.com/GoldenEllipsis/p/11647811.html