我在c写了一个迷你项目。代码编译得很好,但运行不顺利

时间:2017-01-30 17:24:55

标签: c arrays dynamic-memory-allocation

该项目由五个例外组成: ex1)对数组进行排序,以便奇数值出现在start中,然后出现偶数值。 ex2)检查一个数字是否超过11 ex3)找到两个未排序数组的交集数组(及其大小)。 ex4)检查两个数组是否相互排列。 ex5获取字符串和char,并从字符串中删除该字符。

Tt似乎ex3和ex4运行不佳,我不明白为什么。

以下是代码:

        #define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

void Ex1();
void Ex2();
void Ex3();
void Ex4();
void Ex5();
void merge_sort(int *a,int first, int last);
void merge(int *a, int p, int q, int r);
int *input_array_dyn(int n);
char* input_long();
void swap(int* a, int* b);
void PrintArray(int arr[], int size);
void ParityOfArray(int arr[], int size);
int SumOfDigits(int num);
int IsDividesBy11(int num);
int* IntersectionOfArrays(int* arr1, int size1, int* arr2, int size2, int* sizeRes);
int CheckPermutation1(int arr1[], int arr2[], int size);
int CheckPermutation2(int arr1[], int arr2[], int size);
int CheckPermutation3(int arr1[], int arr2[], int size);
void removeChar(char str[], char ch);


void main()
{

    int select, fcontinue=1;

    while (fcontinue==1)
    {
        printf ("please enter numbers 1-5 to choose excercise , or 0 to exit\n");
        scanf ("%d", &select);

        switch (select) 
        {
        case 1: Ex1(); break;
        case 2: Ex2(); break;
        case 3: Ex3(); break;
        case 4: Ex4(); break;
        case 5: Ex5(); break;
        case 0: fcontinue=0; break;
        default: printf ("invalid option, try again\n");
        } 
    }

}

void Ex1()
{
    int *arr, size;

    printf ("enter the size of the array: ");
    scanf ("%d", &size);
    printf("\n");

    arr=input_array_dyn(size);
    ParityOfArray(arr, size);
    printf("\n the array after the change: ");
    PrintArray(arr, size);
    printf("\n");

    free(arr);

}

void Ex2()
{
    int num;

    printf ("enter a non-negative, integer number \n");
    scanf ("%d",&num);

    if (IsDividesBy11(num)==1) 
        printf("%d divides by 11\n", num);
    else
        printf("%d does not divide by 11\n", num);

}

void Ex3()
{
    int *arr1, *arr2, *res;
    int size1,size2,sizeRes;

    printf("array number 1: ");
    printf ("enter the size of the array: ");
    scanf ("%d", &size1);
    printf("\n");
    arr1=input_array_dyn(size1);
    printf("\n array number 2: ");
    printf ("enter the size of the array: ");
    scanf ("%d", &size2);
    printf("\n");
    arr2=input_array_dyn(size2);
    printf("\n");

    res=IntersectionOfArrays(arr1, size1, arr2, size2, &sizeRes);
    printf("the intersection of the arrays is: ");
    PrintArray(res, sizeRes);
    printf(" and the size of the intersection is %d\n", sizeRes);

    free(res);
    free(arr2);
    free(arr1);

}

void Ex4()
{
    int *arr1, *arr2;
    int size;

    printf ("solutin number 1 (non-recursive): \n enter the size of two arrays (same size): ");
    scanf ("%d", &size);
    printf("\n");
    arr1=input_array_dyn(size);
    printf("\n");
    arr2=input_array_dyn(size);
    printf("\n");
    if (CheckPermutation1(arr1,  arr2, size)==1)
        printf("these arrays are permutation of each other\n");
    else 
        printf("these arrays are not permutation of each other\n");

    printf ("solutin number 2 (recursive): \n enter the size of two arrays (same size): ");
    scanf ("%d", &size);
    printf("\n");
    arr1=input_array_dyn(size);
    printf("\n");
    arr2=input_array_dyn(size);
    printf("\n");
    if (CheckPermutation2(arr1,  arr2, size)==1)
        printf("these arrays are permutation of each other\n");
    else 
        printf("these arrays are not permutation of each other\n");

    printf ("solutin number 1 (values from 1 to 100!): \n enter the size of two arrays (same size): ");
    scanf ("%d", &size);
    printf("\n");
    arr1=input_array_dyn(size);
    printf("\n");
    arr1=input_array_dyn(size);
    printf("\n");
    if (CheckPermutation3(arr1,  arr2, size)==1)
        printf("these arrays are permutation of each other\n");
    else 
        printf("these arrays are not permutation of each other\n");
    free(arr1);
    free(arr2);

}

void Ex5()
{
    char *str, ch;

    str=input_long();
    printf("\n please enter a char to be removed from the string ");
    scanf("%c", &ch);

    removeChar(str, ch);
    printf("\n the string after the removal is %s \n", str);

    free(str);
}

void ParityOfArray(int arr[], int size) 
{
    int i = 0, j = size - 1;

    while (i < j)
    {
        if ((arr[i] % 2) != 0) 
            i++;
        else if ((arr[j] % 2) == 0)
            j--;
        else 
        {
            swap (&arr[i],&arr[j]);
            i++;
            j--;
        }
    }
}

int SumOfDigits(int num) 
{
    int digit1, digit2;

    if (num < 10) 
        return num;
    else 
    {
        digit1 = num % 10;
        num /= 10;
        digit2 = num % 10;
        return digit1 - digit2 + SumOfDigits(num / 10);
    }
}

int IsDividesBy11(int num) 
{
    int sum=SumOfDigits(num);

    while (sum>=10)
        sum=abs(SumOfDigits(sum));
    if (sum==0)
        return 1;
    return 0;
}

int* IntersectionOfArrays(int* arr1, int size1, int* arr2, int size2, int* sizeRes) 
{
    int* res=(int*)malloc(1*sizeof(int));
    int i = 0, j = 0;
    *sizeRes = 0;

    merge_sort(arr1,0, size1 -1);
    merge_sort(arr2,0, size2 -1);

    while (i < size1 && j < size2) 
    {
        if (arr1[i] < arr2[j])
            i++;
        else if (arr1[i] > arr2[j]) 
            j++;
        else 
        {   res[*sizeRes] = arr1[i];
        i++;
        j++;
        *sizeRes++;
        res = (int*)realloc(res, 1*sizeof(int));
        }   
    }

    if (*sizeRes==0)
        return NULL;
    return res;
}

void removeChar(char str[], char ch) 
{
    int i,j = 0;
    for (i = 0; str[i] != '\0'; i++)
    {
        if (str[i] != ch)
        {
            str[j] = str[i];
            j++;
        }
    }
    str[j]='\0';
}

int CheckPermutation1(int arr1[], int arr2[], int size)
{
    int i;

    merge_sort(arr1,0, size -1);
    merge_sort(arr2,0, size -1);

    for (i=0;i<size;i++)
        if (arr1[i]!=arr2[i])
            return 1;
    return 0;
}

int CheckPermutation2(int arr1[], int arr2[], int size)
{
    int i; 

    if (size == 0) 
        return 1;
    for (i = 0; i < size; i++) 
    {
        if (arr2[i] == arr1[0]) 
        {
            swap(&arr2[0],&arr2[i]);
            return CheckPermutation2(arr1 + 1, arr2 + 1, size - 1);
        }
    }
    return 0;
}

int CheckPermutation3(int arr1[], int arr2[], int size)
{
    int count[100];
    int i;

    for(i=0;i<100;i++)
        count[i]=0;
    for(i=0;i<size;i++)
        count[arr1[i]]++;
    for(i=0;i<size;i++)
        count[arr2[i]]--;
    for(i=0;i<100;i++)
        if (count[i]!=0)
            return 0;
    return 1;
}

void merge_sort(int *a,int first, int last)
{
    int middle;
    if(first < last)
    {
        middle=(first+last)/2;
        merge_sort(a, first, middle);
        merge_sort(a, middle+1, last);
        merge(a, first, middle, last);
    }
}

void merge(int *a, int p, int q, int r)
{
    int i=p, j=q+1, k=0;
    int* temp=(int*)malloc((r-p+1) * sizeof(int));
    while ((i<=q) && (j<=r))
        if(a[i] < a[j])
            temp[k++] = a[i++];
        else
            temp[k++] = a[j++];
    while(j<=r) // if( i>q )
        temp[k++]=a[j++];
    while(i<=q) // j>r
        temp[k++]=a[i++];
    for(i=p,k=0; i<=r; i++,k++) // copy temp[] to a[]
        a[i]=temp[k];
    free(temp);
}

int *input_array_dyn(int n)
{
    int i;
    int *arr;

    arr=(int*)calloc(n, sizeof(int));
    assert(arr); /* calloc() worked */
    printf("enter the array of length %d\n",n);
    for(i=0;i<n;i++) 
        scanf("%d",&arr[i]);

    return arr;
}

char* input_long()
{
    char tempstr[80], *longnum;

    printf("enter a string\n");
    flushall();
    gets(tempstr);
    longnum=(char*)malloc((strlen(tempstr)+1)*sizeof(char));
    strcpy(longnum,tempstr);

    return longnum;
}

void swap(int* a, int* b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

void PrintArray(int arr[], int size)
{
    int i;

    for(i=0;i<size;i++)
        printf("%d ",arr[i]);
}

0 个答案:

没有答案