首页 > 其他 > 详细

洛谷数字反转

时间:2019-11-03 13:40:10      阅读:87      评论:0      收藏:0      [点我收藏+]
  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<malloc.h>
  4 char* left( char *num,  char n);
  5 char* right( char *num,  char n);
  6 char* rever(char* p);
  7 int main()
  8 {
  9     char num[20];
 10     scanf("%s", &num);
 11     int type = 0;
 12     int i;
 13     
 14     for(i = 0;i < strlen( num); i++){
 15         if(num[i] == .){
 16             type = 1;
 17         }else if(num[i] == /){
 18             type = 2;
 19         }else if(num[i] == %){
 20             type = 3;
 21         }
 22         
 23     }
 24     
 25     switch (type)
 26     {
 27                 
 28     
 29         case 0:{ 
 30         char *result = rever(num);
 31         printf("%s",result);
 32         break;
 33         } 
 34         case 1:{ 
 35         char *result = left(num,.);
 36         char *part = right(num, .);
 37         strcat(result, part);
 38         printf("%s",result);
 39         break;
 40         }
 41         case 2:{ 
 42         char *result = left(num, /);
 43         char *part = right(num , /);
 44         strcat(result, part);
 45         printf("%s",result); 
 46         break;
 47         }
 48         case 3:{ 
 49         char *result = left(num, %);
 50         char *part = right(num , %);
 51         strcat(result, part);
 52         printf("%s",result); 
 53         break;
 54         }
 55         
 56     }
 57         
 58     return 0;
 59         
 60  }  
 61  
 62  char* left( char *num,  char n)//将小数以左的数字逆转去零 
 63  {
 64     char *tem =  strchr(num, n);
 65     int i;
 66     *tem = \0;
 67     char *lef = (char*)malloc(sizeof(num) + 1) ;
 68     strcpy(lef, num);
 69     *tem = n;
 70     char *end = rever(lef);
 71 return end;
 72  } 
 73  
 74  char* right( char *num,  char n)//将小数以左的数字逆转去零 
 75  {
 76     char *tem =  strchr(num, n);
 77     int i;
 78     int len = strlen(tem);
 79     char *p = (char*)malloc(sizeof(tem) ) ;
 80     char *q = (char*)malloc(sizeof(tem) ) ; 
 81     strcpy(p, tem);
 82     q[0] = n;
 83     for(i = 1; i < strlen(p)-1; i++){            //数字反转 
 84         q[len] = p[i];
 85         len--;
 86     }
 87     for(i = strlen(q); i > 0; i--){                 //去零 
 88         if(q[i] == 0){
 89             q[i] = \0;
 90         }else{
 91         break;
 92         }
 93     }
 94     return q;
 95  } 
 96  
 97 char* rever(char* p)                           //去零加反转 
 98 {
 99     int i;
100     for(i = strlen(p); i > 0; i--){
101         if(p[i] == 0){
102             p[i] = \0;
103         }else{
104         break;
105         }
106     }
107     char *q = (char*)malloc(sizeof(p) ) ; 
108     int len = strlen(p); 
109         for(i = 0; i < strlen(p)-1; i++){            //字符反转 
110         q[len] = p[i];
111         len--;
112     }
113     return q;
114 }
115  
116  
117  12

 

洛谷数字反转

原文:https://www.cnblogs.com/chenlitao/p/11785850.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!