Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- //FIRST
- int Merge_Strings (char str1[500], int index1, char str2[500],int index2, char result[1000], int index3);
- //SECOND
- void remove_duplicates (char str[1000] , int index);
- void Help (char str[1000], int index, int j);
- void Arrange (int j, char str[1000]);
- //THIRD
- void selection_sort(int arr[1000], int size);
- void Sort(int arr[1000],int i,int j);
- int CorrectSize(int size);
- void CorrectArray (int size, int arr[1000]);
- int ScanArray (int arr[1000],int i,int size);
- void PrintArray(int arr[1000], int size, int i);
- void delete_char(char check);
- int delete_char2(char check);
- int HelpMenu (int, char[255]);
- //FORTH
- int max_set(int Arr[1000], int size, int start, int start2, int i, int j, int max , int count);
- int CorrectSizeFor4(int size);
- void Menu();
- //*********NOW MAIN**********/
- void main(){
- int size=73462944;
- int arr[1000];
- size = CorrectSize(size);
- CorrectArray(size,arr);
- selection_sort(arr,size);
- printf("Answer:\n");
- PrintArray(arr,size,0);
- }
- /*************************************************/
- void Menu(){
- char input[255];
- int i;
- int length;
- 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");
- scanf("%s", input);
- i=1;
- if (input[0]=='0')
- {
- if (input[1] == '\0')
- return;
- else
- {
- if (input[i] == '0')
- {
- if (input[i+1] == '\0')
- return;
- else
- {
- if (HelpMenu((i+1),input))
- {
- length = strlen(input);
- switch (input[length-1])
- {
- case '0':
- printf("\nInput incorregain.\n\n");
- return;
- case '1': return;
- case '2':return;
- case '3':return;
- case '4':return;
- case '5':return;
- default : printf("\nInput incorrect! Please try again.\n\n");
- }
- Menu();
- }
- else
- {
- length = strlen(input);
- if (input[length-1] == 0)
- return;
- else
- {
- printf("\nInput incorrect! Please try again.\n\n");
- Menu();
- }
- }
- }
- }
- }
- }
- }
- /*******************************/
- int HelpMenu(int i, char input[255]){
- if (input[i] == '0')
- {
- if (input[i+1] == '\0')
- return 0;
- if (input[i+1] == '0')
- HelpMenu(i+1,input);
- }
- else
- {
- if ('1'<=input[i+1] && input[i+1]<='5')
- {
- if (input[i+2] == '\0')
- return 1;
- }
- else return 0;
- }
- }
- /****************************************************/
- // MERGE STRINGS //
- int Merge_Strings (char str1[500], int index1, char str2[500],int index2, char result[1000], int index3){
- int help;
- 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
- 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
- if (str1[index1] == '\0') // when to stop
- {
- if (str2[index2] == '\0')
- {
- result[index3] = '\0';
- return 2;
- }
- else
- {
- result[index3] = str2[index2];
- index3++;
- index2++;
- help = Merge_Strings (str1, index1, str2, index2, result, index3);
- if (help==1)
- return 1;
- if(help==2)
- return 2;
- return 0;
- }
- }
- if (str2[index2] == '\0') // when to stop
- {
- if (str1[index1] == '\0')
- {
- result[index3] = '\0';
- return 1;
- }
- else
- {
- result[index3] = str1[index1];
- index3++;
- index1++;
- help = Merge_Strings (str1, index1, str2, index2, result, index3);
- if (help==1)
- return 1;
- if (help==2)
- return 2;
- return 0;
- }
- }
- if (str1[index1]<=str2[index2] && result[index3 + 1] != '\0') //building the 3rd string
- {
- result[index3] = str1[index1];
- index1++;
- index3++;
- help = Merge_Strings (str1, index1, str2, index2, result, index3);
- if (help==1)
- return 1;
- if (help==2)
- return 2;
- return 0;
- }
- else
- {
- if (result[index3 + 1] != '\0')
- {
- result[index3] = str2[index2];
- index2++;
- index3++;
- help = Merge_Strings (str1, index1, str2, index2, result, index3);
- if (help==1)
- return 1;
- if (help==2)
- return 2;
- return 0;
- }
- }
- }
- /*
- char str1[500];
- int index1 =0;
- char str2[500];
- int index2 =0;
- char result[1000];
- int index3 =0;
- printf("Enter first string:\n");
- scanf("%s", str1);
- printf("Enter second string:\n");
- scanf("%s", str2);
- printf("Answer:\n");
- if (!Merge_Strings (str1, index1, str2, index2, result, index3))
- printf("Input incorrect!\n");
- else
- printf("%s", result);
- //Menu();
- **/
- /*2*/void remove_duplicates (char str[1000] , int index){
- int j;
- if (str[index] == '\0')
- return;
- j = index + 1;
- Help(str, index, j);
- remove_duplicates(str, index+1);
- }
- void Help (char str[1000], int index, int j){
- if (str[index] == str[j])
- {
- Arrange(j,str);
- Help (str, index, j);
- }
- else
- if (str[j] != '\0') Help (str, index, j+1);
- return;
- }
- void Arrange (int j, char str[1000]){
- if (str[j] == '\0')
- return;
- str[j] = str[j+1];
- Arrange (j+1, str);
- }
- /*
- int index=0;
- char str[1000];
- printf("Enter string:\n");
- scanf("%s", str);
- remove_duplicates(str , index);
- printf("Answer:\n");
- printf("%s\n", str);
- */
- /*3*3*3*3*3*3*/int CorrectSize(int size){
- char check;
- printf("Please enter the size of array you want to sort:\n");
- scanf("%d", &size);
- scanf("%c", &check);
- if (size!=0)
- {
- if (!delete_char2(check))
- {
- if (0<size && size <=1000)
- return size;
- }
- }
- printf("\nInput incorrect! Please try again.\n\n");
- delete_char2(check);
- CorrectSize(size);
- }
- void CorrectArray (int size, int arr[1000]){
- printf("Please enter %d values for the array (in one row):\n", size);
- if (!ScanArray(arr,0,size))
- return;
- else
- {
- printf("\nInput incorrect! Please try again.\n\n");
- CorrectArray (size,arr);
- }
- }
- int ScanArray (int arr[1000], int i, int size){
- char check;
- scanf("%d", &arr[i]);
- scanf("%c", &check);
- if(check == 10)
- {
- if (i==size-1)
- return 0;
- else
- {
- delete_char(check);
- return 1;
- }
- }
- if(check!=' ')
- {
- delete_char(check);
- return 1;
- }
- return ScanArray (arr, i+1, size);
- }
- void PrintArray (int arr[1000],int size, int i){
- if (i==size) return;
- if (i==size-1)
- printf("%d\n", arr[i]);
- else
- printf("%d ", arr[i]);
- PrintArray(arr,size,i+1);
- }
- void selection_sort(int arr[1000], int size){
- int i,j;
- if (size==0)
- return;
- i = size-1;
- j = size-2;
- Sort(arr, i, j);
- selection_sort(arr,size-1);
- }
- void Sort(int arr[1000], int i, int j){
- int temp;
- if (j<0)
- return;
- if (arr[i]>arr[j])
- Sort(arr,i,j-1);
- else
- {
- temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- Sort(arr,i,j-1);
- }
- }
- void delete_char(char check){
- if (check == 10)
- return;
- scanf("%c", &check);
- delete_char(check);
- return;
- }
- int delete_char2(char check){
- if(check==10)
- return 0;
- if(check == ' '){
- scanf("%c", &check);
- if(delete_char2(check))
- return 1;
- return 0;
- }
- delete_char(check);
- return 1;
- }
- /*int size=0;
- int arr[1000];
- size = CorrectSize(size);
- CorrectArray(size,arr);
- selection_sort(arr,size);
- printf("Answer:\n");
- PrintArray(arr,size,0);
- */
- int max_set(int Arr[1000], int size, int start1, int start2, int i, int j, int maxsize , int count){
- //int maxsize=1;
- if (count >= maxsize)
- maxsize = count;
- if (j >= size)
- return maxsize;
- else
- {
- if (Arr[i] <= Arr[j])
- {
- count++;
- maxsize = max_set(Arr, size, start1, start2, j, j+1, maxsize, count); // 1 21 3 33 6 53 9 18
- maxsize = max_set(Arr, size, start1, start2, j, j+2, maxsize, count);
- }
- else
- {
- maxsize = max_set(Arr, size, start1, start2, i, j+1, maxsize, count);
- maxsize = max_set(Arr, size, start1, start2, i, j+2, maxsize, count);
- }
- }
- if (start2 != j)
- return maxsize; //FIXED
- else // (start2==i+1)
- {
- maxsize = max_set(Arr, size, start1, start2 +1, i, j+1, maxsize, 1);
- if (start1!=i)
- return maxsize;
- else
- maxsize = max_set(Arr, size, start1 + 1, start2 +1, i+1, i+2, maxsize, 1);
- }
- return maxsize;
- }
- int CorrectSizeFor4(int Size){
- char check;
- printf("Please enter the size of the array:\n");
- scanf("%d", &Size);
- scanf("%c", &check);
- if (Size!=0)
- {
- if (!delete_char2(check))
- {
- if (0<Size && Size <=1000)
- return Size;
- }
- }
- printf("\nInput incorrect! Please try again.\n\n");
- CorrectSize(Size);
- }
- /*int bigsize=0;
- int Size=48392483920;
- int Arr[1000];
- Size = CorrectSize(Size);
- CorrectArray(Size,Arr);
- bigsize = max_set(Arr,Size,0,1,0,1,1,1);
- printf("Answer: ");
- printf("%d\n", bigsize); */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement