Chinaunix首页 | 论坛 | 博客
  • 博客访问: 483304
  • 博文数量: 59
  • 博客积分: 345
  • 博客等级: 二等列兵
  • 技术积分: 1380
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-18 22:44
个人简介

to be myself

文章分类

全部博文(59)

文章存档

2017年(5)

2013年(47)

2012年(3)

2011年(4)

分类: C/C++

2013-03-02 15:57:01


点击(此处)折叠或打开

  1. 感觉写得比较乱,真是情况中有情况!
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>

  5. char mirror[35]
  6. = {'A','0','0','0','3','0','0','H','I','L','0','J','M','0','O','0','0','0','2','T','U','V','W','X','Y','5',
  7.    '1','S','E','0','Z','0','0','8','0'
  8. };

  9. int main()
  10. {
  11.     void len1(char c);
  12.     char To_O(char c);
  13.     int is_mirror(char c1,char c2);
  14.     int i, j, len, pflag, mflag;
  15.     char str[10000], prins[10000];
  16.     while (scanf("%s", str)!=EOF)
  17.     {
  18.         strcpy(prins, str);//因为0和O被看作一样,所以附加保存原始数组最后输出
  19.         len = strlen(str);
  20.         mflag = 0;
  21.         pflag = 1;
  22.         if (len == 1)
  23.         {
  24.             len1(str[0]);
  25.             continue;
  26.         }
  27.         if (len%2==1)//字符串为奇数长度
  28.          {
  29.             i = len/2;
  30.             for (j=1; j<=i&&j<len-i; j++)
  31.             {
  32.                 str[i-j] = To_O(str[i-j]);
  33.                 str[i+j] = To_O(str[i+j]);
  34.                 if (str[i-j]!=str[i+j])
  35.                 {
  36.                     pflag = 0;
  37.                     if (is_mirror(str[i-j], str[i+j]))
  38.                     {
  39.                         mflag = 1;
  40.                     }
  41.                     else
  42.                     {
  43.                         mflag = 0;
  44.                         break;
  45.                     }
  46.                 }
  47.                 else
  48.                 {
  49.                     if (is_mirror(str[i-j], str[i+j]))
  50.                     {
  51.                         mflag = 1;
  52.                     }
  53.                     else
  54.                     {
  55.                         mflag = 0;
  56.                         break;
  57.                     }
  58.                 }
  59.             }
  60.         }
  61.         else//偶数长度
  62.         {
  63.             i = len/2;
  64.             j = i-1;
  65.             for (; i<len; --j, ++i)
  66.             {
  67.                 str[i] = To_O(str[i]);
  68.                 str[j] = To_O(str[j]);
  69.                 if (str[j]!=str[i])
  70.                 {
  71.                     pflag = 0;
  72.                     if (is_mirror(str[j], str[i]))
  73.                     {
  74.                         mflag = 1;
  75.                     }
  76.                 }
  77.                 else
  78.                 {
  79.                     if (is_mirror(str[j], str[i]))
  80.                     {
  81.                         mflag = 1;
  82.                     }
  83.                     else
  84.                     {
  85.                         mflag = 0;
  86.                         break;
  87.                     }
  88.                 }
  89.             }
  90.         }
  91.         if (!mflag&&!pflag)
  92.         {
  93.             printf("%s -- is not a palindrome.nn", prins);
  94.         }
  95.         else
  96.         {
  97.             if (mflag&&pflag)
  98.             {
  99.                 printf("%s -- is a mirrored palindrome.nn", prins);
  100.             }
  101.             else
  102.             {
  103.                 if (pflag)
  104.                 {
  105.                     printf("%s -- is a regular palindrome.nn", prins);
  106.                 }
  107.                 else
  108.                 {
  109.                     printf("%s -- is a mirrored string.nn", prins);
  110.                 }
  111.             }
  112.         }
  113.     }
  114.     return 0;
  115. }

  116. /*1个字符的特殊情况*/
  117. void len1(char c)
  118. {
  119.     if (isalpha(c))
  120.     {
  121.         if (mirror[c-'A']!='0')
  122.         {
  123.             printf("%c -- is a mirrored palindrome.nn", c);
  124.         }
  125.         else
  126.         {
  127.             printf("%c -- is a regular palindrome.nn", c);
  128.         }
  129.     }
  130.     else
  131.     {
  132.         if (c =='0')
  133.         {
  134.             c = 'O';
  135.         }
  136.         if (mirror[c-'A']!='0')
  137.         {
  138.             printf("%c -- is a mirrored palindrome.nn", c);
  139.         }
  140.         else
  141.         {
  142.             printf("%c -- is a regular palindrome.nn", c);
  143.         }
  144.     }
  145.     return;
  146. }

  147. /*判断是不是"反射"字母*/
  148. int is_mirror(char c1, char c2)
  149. {
  150.     if (isalpha(c1))
  151.     {
  152.         if (mirror[c1-'A'] == c2)
  153.         {
  154.             return 1;
  155.         }
  156.         else
  157.         {
  158.             return 0;
  159.         }
  160.     }
  161.     else
  162.     {
  163.         if (mirror[25+c1-'0'] == c2)
  164.         {
  165.             return 1;
  166.         }
  167.         else
  168.         {
  169.             return 0;
  170.         }
  171.     }
  172. }

  173. /*若为(数字)0, 变成(字母)O*/
  174. char To_O(char c)
  175. {
  176.     if (c == '0')
  177.     {
  178.         return 'O';
  179.     }
  180.     return c;
  181. }
  182. //程序完
2010-08-18 14:36 发表于百度空间,今搬至CU。
阅读(1051) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~