Guest User

Untitled

a guest
Oct 18th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. //Acm暑訓程式(1) 資工二B 498720923 梁世平(阿世)
  2. //Acm暑訓程式(1) 資工二B 498720923 梁世平(阿世)
  3. //Acm暑訓程式(1) 資工二B 498720923 梁世平(阿世)
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include<string.h>
  7. #define MAX 255
  8. struct data
  9. {
  10. bool is_mir;
  11. bool is_par;
  12. char str1[MAX];
  13. char str2[MAX];
  14. }simple;
  15. void judgement(char ch,int len1,int len2);
  16. int main()
  17. {
  18. int i,j;
  19. int len;
  20. char str[MAX];
  21. while(gets(str)!=NULL){
  22. len=strlen(str);
  23. for(i=len-1;i>=0;i--){
  24. judgement(str[i],len-1,i);
  25. }
  26. if(strcmp(simple.str1,str)==0) //去比對created的mirror string有無符合輸入字串,有則設is_mir為1否則為0
  27. simple.is_mir=1;
  28. else
  29. simple.is_mir=0;
  30.  
  31. if(strcmp(simple.str2,str)==0) //去比對created的palindrome string有無符合輸入字串,有則設is_par為1否則為0
  32. simple.is_par=1;
  33. else
  34. simple.is_par=0;
  35. if(simple.is_mir==1&&simple.is_par==1)printf("%s -- is a mirrored palindrome.\n",str); //兩者皆符合則display『字串 -- is a mirrored palindrome.』
  36.  
  37. else if(simple.is_mir==1&&simple.is_par==0)printf("%s -- is a mirrored string.\n",str); //符合鏡像則display『字串 -- is a mirrored string.』
  38.  
  39. else if(simple.is_mir==0&&simple.is_par==1)printf("%s -- is a regular palindrome.\n",str); //符合迴文則display『字串 -- is a regular palindrome.』
  40.  
  41. else printf("%s -- is not a palindrome.\n",str); //皆無則display 『字串 -- is not a palindrome.』
  42. printf("\n");
  43. simple.is_mir=0; //把bool的is_mir重新設回0
  44. simple.is_par=0; //把bool的is_par重新設回0
  45. memset(simple.str1,0,len); //string lib,把str1長度為len的內容都清空
  46. memset(simple.str2,0,len); //string lib,把str2長度為len的內容都清空
  47. }
  48. return 0;
  49. }
  50.  
  51. void judgement(char ch,int len1,int len2)
  52. {
  53. if(ch=='A'||ch=='H'||ch=='I'||ch=='M'||ch=='O'||ch=='T'||ch=='U'||ch=='V'||ch=='W'||ch=='X'||ch=='Y'||ch=='1'||ch=='8')
  54. {
  55. simple.str1[len1-len2]=ch;
  56. }
  57. else if(ch=='E'||ch=='3')
  58. {
  59. if(ch=='E')
  60. simple.str1[len1-len2]='3';
  61. else
  62. simple.str1[len1-len2]='E';
  63. }
  64. else if(ch=='J'||ch=='L')
  65. {
  66. if(ch=='J')
  67. simple.str1[len1-len2]='L';
  68. else
  69. simple.str1[len1-len2]='J';
  70. }
  71. else if(ch=='S'||ch=='2')
  72. {
  73. if(ch=='S')
  74. simple.str1[len1-len2]='2';
  75. else
  76. simple.str1[len1-len2]='S';
  77. }
  78. else if(ch=='Z'||ch=='5')
  79. {
  80. if(ch=='Z')
  81. simple.str1[len1-len2]='5';
  82. else
  83. simple.str1[len1-len2]='Z';
  84. }
  85. else
  86. {
  87. simple.str1[len1-len2]=0; //記得設成空字元
  88. }
  89. simple.str2[len1-len2]=ch; //回文皆倒過來指派(如長度為9[0~8],其中index為8(end)的話回文要指派到0(start)來建立
  90. }
Add Comment
Please, Sign In to add comment