fork(2) download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. const int START=0;//start state
  5. const int CONTINUE=1;//common prefix identifier state
  6. const int JAVAWORD=2;//intermediate states
  7. const int CPPWORD=3;
  8. const int CPPDEL=4;//CPP-delimeter
  9. const int JAVA=5;//final states
  10. const int CPP=6;
  11. const int UNIVERSAL=7;
  12. const int JAVAERR=8;
  13. const int CPPERR=9;
  14. const int ERROR=10;
  15. char var[101];
  16. char result[203]={'\0'};
  17. int reslen=0;
  18. int state;
  19. bool is_small(char c)
  20. {
  21. return ('a'<= c && c <= 'z');
  22. }
  23. bool is_big(char c)
  24. {
  25. return ('A'<= c && c <= 'Z');
  26. }
  27. bool is_underscore(char c)
  28. {
  29. return (c == '_');
  30. }
  31. int selection_of_style(char * var, size_t len)
  32. {
  33. char c=var[0];
  34. state = START;
  35. if(is_small(c))
  36. {
  37. state = CONTINUE;
  38. }
  39. if(is_big(c))
  40. {
  41. state = ERROR;
  42. return state;
  43. }
  44. if(is_underscore(c))
  45. {
  46. state = ERROR;
  47. return state;
  48. }
  49. for (int i=1; i<len; i++)
  50. {
  51. c=var[i];
  52. if(is_small(c)) state = state;
  53. if(is_big(c))
  54. {
  55. state = JAVAWORD;
  56. return state;
  57. }
  58. if(is_underscore(c))
  59. {
  60. state = CPPWORD;
  61. return state;
  62. }
  63. }
  64. //if(state==CONTINUE)
  65. state = UNIVERSAL;
  66. return state;
  67. }
  68. int is_java(char * var, size_t len)
  69. {
  70. for (int i=0; i<len; i++)
  71. {
  72. char c=var[i];
  73. if(is_small(c))
  74. {
  75. state = state;
  76. }
  77. if(is_big(c))
  78. {
  79. state = state;
  80. }
  81. if(is_underscore(c))
  82. {
  83. state = JAVAERR;
  84. return state;
  85. }
  86. }
  87. if(state==JAVAWORD)
  88. {
  89. state = JAVA;
  90. }
  91. return state;
  92. }
  93. int is_cpp(char * var, size_t len)
  94. {
  95. for (int i=0; i<len; i++)
  96. {
  97. char c=var[i];
  98. if(is_small(c))
  99. {
  100. if(state==CPPDEL)
  101. {
  102. state=CPPWORD;
  103. }
  104. else
  105. state = state;
  106. }
  107. if(is_big(c))
  108. {
  109. state = JAVAERR;
  110. return state;
  111. }
  112.  
  113. if(is_underscore(c))
  114. {
  115. if(state==CPPDEL)
  116. {
  117. state=CPPERR;
  118. return state;
  119. }
  120. state=CPPDEL;
  121. }
  122. }
  123. if(state==CPPWORD)
  124. {
  125. state = CPP;
  126. }
  127. if(state==CPPDEL)
  128. {
  129. state=CPPERR;
  130. }
  131. return state;
  132. }
  133. void java_to_cpp(char * var, size_t len)
  134. {
  135. for(int i=0; i<len; i++)
  136. {
  137. char c=var[i];
  138. if(is_small(c))
  139. {
  140. result[reslen++]=var[i];
  141. }
  142. if(is_big(c))
  143. {
  144. result[reslen++]='_';
  145. result[reslen++]=var[i]+32;
  146. }
  147. }
  148. }
  149. void cpp_to_java(char * var, size_t len)
  150. {
  151. for(int i=0; i<len; i++)
  152. {
  153. char c=var[i];
  154. if(is_small(c))
  155. {
  156. result[reslen++]=var[i];
  157. }
  158. if(is_underscore(c))
  159. {
  160. result[reslen++]=var[++i]-32;
  161. }
  162. }
  163. }
  164. int main()
  165. {
  166. cin >> var;
  167. size_t len = strlen(var);
  168. state=selection_of_style(var, len);
  169. if(state==ERROR)
  170. {
  171. cout << "Error!" << endl;
  172. return 0;
  173. }
  174. if(state==JAVAWORD)
  175. state = is_java(var,len);
  176. if(state==CPPWORD)
  177. state = is_cpp(var,len);
  178. if (state ==UNIVERSAL)
  179. {
  180. cout <<var<<endl;
  181. return 0;
  182. }
  183. if(state==JAVAERR)
  184. {
  185. cout << "Error!" << endl;
  186. return 0;
  187. }
  188. if(state==CPPERR)
  189. {
  190. cout << "Error!" << endl;
  191. return 0;
  192. }
  193. if(state==JAVA)
  194. {
  195. java_to_cpp(var,len);
  196. cout<<result;
  197. return 0;
  198. }
  199. if(state==CPP)
  200. {
  201. cpp_to_java(var,len);
  202. cout<<result;
  203. return 0;
  204. }
  205. return 0;
  206. }
  207.  
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout