Advertisement
Guest User

עבודה 3 זמנית

a guest
Aug 30th, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. //FIRST
  4. int Merge_Strings (char str1[500], int index1, char str2[500],int index2, char result[1000], int index3);
  5.  
  6. //SECOND
  7. void remove_duplicates (char str[1000] , int index);
  8. void Help (char str[1000], int index, int j);
  9. void Arrange (int j, char str[1000]);
  10.  
  11. //THIRD
  12. void selection_sort(int arr[1000], int size);
  13. void Sort(int arr[1000],int i,int j);
  14. int CorrectSize(int size);
  15. void CorrectArray (int size, int arr[1000]);
  16. int ScanArray (int arr[1000],int i,int size);
  17. void PrintArray(int arr[1000], int size, int i);
  18. void delete_char(char check);
  19. int delete_char2(char check);
  20. int HelpMenu (int, char[255]);
  21.  
  22. //FORTH
  23. int max_set(int Arr[1000], int size, int start, int start2, int i, int j, int max , int count);
  24. int CorrectSizeFor4(int size);
  25.  
  26. void Menu();
  27.  
  28.  
  29. //*********NOW MAIN**********/
  30.  
  31. void main(){
  32.  
  33. int size=73462944;
  34. int arr[1000];
  35. size = CorrectSize(size);
  36. CorrectArray(size,arr);
  37. selection_sort(arr,size);
  38. printf("Answer:\n");
  39. PrintArray(arr,size,0);
  40. }
  41. /*************************************************/
  42. void Menu(){
  43. char input[255];
  44. int i;
  45. int length;
  46. printf("------------------------------------------------\nPlease enter your choice:\n1) Merge strings.\n2) Remove duplicates in the string.\n3) Sort the array using recursive selection sort.\n4) Calculate the length of the max increasing set.\n5) Check partition.\n0) Exit.\n");
  47.  
  48. scanf("%s", input);
  49. i=1;
  50. if (input[0]=='0')
  51. {
  52. if (input[1] == '\0')
  53. return;
  54. else
  55. {
  56. if (input[i] == '0')
  57. {
  58. if (input[i+1] == '\0')
  59. return;
  60. else
  61. {
  62. if (HelpMenu((i+1),input))
  63. {
  64. length = strlen(input);
  65. switch (input[length-1])
  66. {
  67. case '0':
  68. printf("\nInput incorregain.\n\n");
  69. return;
  70.  
  71. case '1': return;
  72.  
  73. case '2':return;
  74.  
  75. case '3':return;
  76.  
  77. case '4':return;
  78.  
  79. case '5':return;
  80.  
  81. default : printf("\nInput incorrect! Please try again.\n\n");
  82. }
  83.  
  84. Menu();
  85. }
  86. else
  87. {
  88. length = strlen(input);
  89. if (input[length-1] == 0)
  90. return;
  91. else
  92. {
  93. printf("\nInput incorrect! Please try again.\n\n");
  94. Menu();
  95. }
  96. }
  97.  
  98. }
  99. }
  100. }
  101. }
  102. }
  103.  
  104.  
  105. /*******************************/
  106.  
  107. int HelpMenu(int i, char input[255]){
  108.  
  109. if (input[i] == '0')
  110. {
  111. if (input[i+1] == '\0')
  112. return 0;
  113. if (input[i+1] == '0')
  114. HelpMenu(i+1,input);
  115. }
  116. else
  117. {
  118. if ('1'<=input[i+1] && input[i+1]<='5')
  119. {
  120. if (input[i+2] == '\0')
  121. return 1;
  122. }
  123. else return 0;
  124. }
  125. }
  126.  
  127. /****************************************************/
  128.  
  129. // MERGE STRINGS //
  130.  
  131. int Merge_Strings (char str1[500], int index1, char str2[500],int index2, char result[1000], int index3){
  132. int help;
  133.  
  134. if (str1[index1] != '\0' && str1[index1] > str1[index1 + 1] && str1[index1 + 1] != '\0' || (65 > str1[index1] || (90 < str1[index1] && str1[index1] <97) || str1[index1] > 122)) return 0; //checking if first string is ok
  135. if (str2[index2] != '\0' && str2[index2] > str2[index2 + 1] && str2[index2 + 1] != '\0' || (65 > str2[index2] || (90 < str2[index2] && str2[index2] <97) || str2[index2] > 122)) return 0; //checking if second string is ok
  136.  
  137. if (str1[index1] == '\0') // when to stop
  138. {
  139. if (str2[index2] == '\0')
  140. {
  141. result[index3] = '\0';
  142. return 2;
  143. }
  144. else
  145. {
  146. result[index3] = str2[index2];
  147. index3++;
  148. index2++;
  149. help = Merge_Strings (str1, index1, str2, index2, result, index3);
  150. if (help==1)
  151. return 1;
  152. if(help==2)
  153. return 2;
  154. return 0;
  155. }
  156. }
  157.  
  158. if (str2[index2] == '\0') // when to stop
  159. {
  160. if (str1[index1] == '\0')
  161. {
  162. result[index3] = '\0';
  163. return 1;
  164. }
  165. else
  166. {
  167. result[index3] = str1[index1];
  168. index3++;
  169. index1++;
  170. help = Merge_Strings (str1, index1, str2, index2, result, index3);
  171. if (help==1)
  172. return 1;
  173. if (help==2)
  174. return 2;
  175. return 0;
  176. }
  177. }
  178.  
  179. if (str1[index1]<=str2[index2] && result[index3 + 1] != '\0') //building the 3rd string
  180. {
  181. result[index3] = str1[index1];
  182. index1++;
  183. index3++;
  184. help = Merge_Strings (str1, index1, str2, index2, result, index3);
  185. if (help==1)
  186. return 1;
  187. if (help==2)
  188. return 2;
  189. return 0;
  190. }
  191.  
  192. else
  193. {
  194. if (result[index3 + 1] != '\0')
  195. {
  196. result[index3] = str2[index2];
  197. index2++;
  198. index3++;
  199. help = Merge_Strings (str1, index1, str2, index2, result, index3);
  200. if (help==1)
  201. return 1;
  202. if (help==2)
  203. return 2;
  204. return 0;
  205. }
  206. }
  207.  
  208. }
  209. /*
  210. char str1[500];
  211. int index1 =0;
  212. char str2[500];
  213. int index2 =0;
  214. char result[1000];
  215. int index3 =0;
  216.  
  217. printf("Enter first string:\n");
  218. scanf("%s", str1);
  219. printf("Enter second string:\n");
  220. scanf("%s", str2);
  221.  
  222. printf("Answer:\n");
  223. if (!Merge_Strings (str1, index1, str2, index2, result, index3))
  224. printf("Input incorrect!\n");
  225. else
  226. printf("%s", result);
  227. //Menu();
  228.  
  229. **/
  230.  
  231. /*2*/void remove_duplicates (char str[1000] , int index){
  232. int j;
  233. if (str[index] == '\0')
  234. return;
  235. j = index + 1;
  236. Help(str, index, j);
  237. remove_duplicates(str, index+1);
  238. }
  239.  
  240. void Help (char str[1000], int index, int j){
  241. if (str[index] == str[j])
  242. {
  243. Arrange(j,str);
  244. Help (str, index, j);
  245. }
  246. else
  247. if (str[j] != '\0') Help (str, index, j+1);
  248. return;
  249. }
  250.  
  251. void Arrange (int j, char str[1000]){
  252. if (str[j] == '\0')
  253. return;
  254. str[j] = str[j+1];
  255. Arrange (j+1, str);
  256. }
  257. /*
  258. int index=0;
  259. char str[1000];
  260.  
  261. printf("Enter string:\n");
  262. scanf("%s", str);
  263. remove_duplicates(str , index);
  264. printf("Answer:\n");
  265. printf("%s\n", str);
  266. */
  267.  
  268. /*3*3*3*3*3*3*/int CorrectSize(int size){
  269. char check;
  270.  
  271. printf("Please enter the size of array you want to sort:\n");
  272. scanf("%d", &size);
  273. scanf("%c", &check);
  274. if (size!=0)
  275. {
  276. if (!delete_char2(check))
  277. {
  278. if (0<size && size <=1000)
  279. return size;
  280. }
  281. }
  282.  
  283. printf("\nInput incorrect! Please try again.\n\n");
  284. delete_char2(check);
  285. CorrectSize(size);
  286. }
  287. void CorrectArray (int size, int arr[1000]){
  288. printf("Please enter %d values for the array (in one row):\n", size);
  289. if (!ScanArray(arr,0,size))
  290. return;
  291. else
  292. {
  293. printf("\nInput incorrect! Please try again.\n\n");
  294. CorrectArray (size,arr);
  295. }
  296. }
  297. int ScanArray (int arr[1000], int i, int size){
  298. char check;
  299. scanf("%d", &arr[i]);
  300. scanf("%c", &check);
  301. if(check == 10)
  302. {
  303. if (i==size-1)
  304. return 0;
  305. else
  306. {
  307. delete_char(check);
  308. return 1;
  309. }
  310. }
  311. if(check!=' ')
  312. {
  313. delete_char(check);
  314. return 1;
  315. }
  316. return ScanArray (arr, i+1, size);
  317. }
  318. void PrintArray (int arr[1000],int size, int i){
  319. if (i==size) return;
  320. if (i==size-1)
  321. printf("%d\n", arr[i]);
  322. else
  323. printf("%d ", arr[i]);
  324. PrintArray(arr,size,i+1);
  325. }
  326. void selection_sort(int arr[1000], int size){
  327. int i,j;
  328. if (size==0)
  329. return;
  330. i = size-1;
  331. j = size-2;
  332. Sort(arr, i, j);
  333. selection_sort(arr,size-1);
  334. }
  335. void Sort(int arr[1000], int i, int j){
  336. int temp;
  337. if (j<0)
  338. return;
  339. if (arr[i]>arr[j])
  340. Sort(arr,i,j-1);
  341. else
  342. {
  343. temp = arr[i];
  344. arr[i] = arr[j];
  345. arr[j] = temp;
  346. Sort(arr,i,j-1);
  347. }
  348. }
  349. void delete_char(char check){
  350. if (check == 10)
  351. return;
  352.  
  353. scanf("%c", &check);
  354. delete_char(check);
  355. return;
  356. }
  357. int delete_char2(char check){
  358. if(check==10)
  359. return 0;
  360. if(check == ' '){
  361. scanf("%c", &check);
  362. if(delete_char2(check))
  363. return 1;
  364. return 0;
  365. }
  366. delete_char(check);
  367. return 1;
  368.  
  369. }
  370. /*int size=0;
  371. int arr[1000];
  372. size = CorrectSize(size);
  373. CorrectArray(size,arr);
  374. selection_sort(arr,size);
  375. printf("Answer:\n");
  376. PrintArray(arr,size,0);
  377. */
  378.  
  379.  
  380. int max_set(int Arr[1000], int size, int start1, int start2, int i, int j, int maxsize , int count){
  381. //int maxsize=1;
  382. if (count >= maxsize)
  383. maxsize = count;
  384. if (j >= size)
  385. return maxsize;
  386. else
  387. {
  388. if (Arr[i] <= Arr[j])
  389. {
  390. count++;
  391. maxsize = max_set(Arr, size, start1, start2, j, j+1, maxsize, count); // 1 21 3 33 6 53 9 18
  392. maxsize = max_set(Arr, size, start1, start2, j, j+2, maxsize, count);
  393. }
  394. else
  395. {
  396. maxsize = max_set(Arr, size, start1, start2, i, j+1, maxsize, count);
  397. maxsize = max_set(Arr, size, start1, start2, i, j+2, maxsize, count);
  398. }
  399. }
  400.  
  401. if (start2 != j)
  402. return maxsize; //FIXED
  403.  
  404. else // (start2==i+1)
  405. {
  406. maxsize = max_set(Arr, size, start1, start2 +1, i, j+1, maxsize, 1);
  407. if (start1!=i)
  408. return maxsize;
  409. else
  410. maxsize = max_set(Arr, size, start1 + 1, start2 +1, i+1, i+2, maxsize, 1);
  411. }
  412.  
  413. return maxsize;
  414. }
  415.  
  416. int CorrectSizeFor4(int Size){
  417. char check;
  418. printf("Please enter the size of the array:\n");
  419. scanf("%d", &Size);
  420. scanf("%c", &check);
  421. if (Size!=0)
  422. {
  423. if (!delete_char2(check))
  424. {
  425. if (0<Size && Size <=1000)
  426. return Size;
  427. }
  428. }
  429.  
  430. printf("\nInput incorrect! Please try again.\n\n");
  431.  
  432. CorrectSize(Size);
  433. }
  434. /*int bigsize=0;
  435. int Size=48392483920;
  436. int Arr[1000];
  437. Size = CorrectSize(Size);
  438. CorrectArray(Size,Arr);
  439. bigsize = max_set(Arr,Size,0,1,0,1,1,1);
  440. printf("Answer: ");
  441. printf("%d\n", bigsize); */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement