C 查找一组数字模式的高级计算器

C 查找一组数字模式的高级计算器,c,calculator,C,Calculator,我目前正在为学校做一个项目,我需要编写一个计算器来确定一组数字的模式。参数为,数字必须介于1和30之间。必须检查用户是否在该范围内插入数字,并且必须验证该数字是否为整数。我已经完成了大部分工作,除了我的主要问题是输入数字和验证数字的for循环,以及确保我的mode函数工作。有没有关于解决循环问题的建议?另外,我必须使用一个模式函数来计算模式,我正在使用的模式是否工作得很好,或者是否有更好的方法来进行计算 #include <stdio.h> #include <string.h

我目前正在为学校做一个项目,我需要编写一个计算器来确定一组数字的模式。参数为,数字必须介于1和30之间。必须检查用户是否在该范围内插入数字,并且必须验证该数字是否为整数。我已经完成了大部分工作,除了我的主要问题是输入数字和验证数字的for循环,以及确保我的mode函数工作。有没有关于解决循环问题的建议?另外,我必须使用一个模式函数来计算模式,我正在使用的模式是否工作得很好,或者是否有更好的方法来进行计算

#include <stdio.h>
#include <string.h>
#include <math.h>

int mode(int *num, int size);

int main(int n, char **p) {
    int modearray[], size, i;

    printf("What is the size of the Array?");
    scanf("%d", &size);

    for (i=0; i<modearray[size]; i++) {
        printf("Enter an integer value (1 to 30): ");
        scanf("%d", modearray[i]);

        if (modearray[i] < 1 || modearray[i] > 30) {
            printf("Please enter a value within the range");
            scanf("%d", modearray[i])
        }

        else if (sscanf(p[i], "%i", &a[i]) != 1) {
            printf("ERROR\n");
            return -1;
        }
    }
}



//used the mode function code frome http://www.dreamincode.net/forums/topic/43713-   pointers-and-modefunction/
int mode(int *num, int size) {
    int currentnum = (*num);
    int count = 0;
    int modenum = -1;
    int modecount = 1;

    for (int x=0; x<size; x++) {
        if (currentnum==(*num + x)) count ++;
        else {
           if(count > modecount) {
               modenum = currentnum;
               // modecount = count; 
               x--;
           }
           currentnum=*(num + x);
           count = 0;
        }
    }
}
#包括
#包括
#包括
int模式(int*num,int size);
int main(int n,字符**p){
int modearray[],大小,i;
printf(“数组的大小是多少?”);
scanf(“%d”,大小(&S);
对于(i=0;i 30){
printf(“请输入范围内的值”);
scanf(“%d”,modearray[i])
}
else if(sscanf(p[i],%i],&a[i])!=1){
printf(“错误\n”);
返回-1;
}
}
}
//使用了来自E的模式功能代码http://www.dreamincode.net/forums/topic/43713-   指针和模式函数/
整数模式(整数*num,整数大小){
int currentnum=(*num);
整数计数=0;
int modenum=-1;
int modecount=1;
用于(int x=0;x modecount){
modenum=currentnum;
//modecount=计数;
x--;
}
currentnum=*(num+x);
计数=0;
}
}
}

假设问题是在扫描到阵列后崩溃:

int main(int n, char **p) {
    int *modearray, size, i;

    printf("What is the size of the Array?");
    scanf("%d", &size);

    modearray = malloc(size * sizeof(int)); //imo size of int is 4 so u can replace with

    for (i=0; i<modearray[size]; i++) {
        printf("Enter an integer value (1 to 30): ");
        scanf("%d", modearray[i]);

        if (modearray[i] < 1 || modearray[i] > 30) {
            printf("Please enter a value within the range");
            scanf("%d", &modearray[i])
        }

        else if (sscanf(p[i], "%i", &a[i]) != 1) {
            printf("ERROR\n");
            return -1;
        }
    }
}
int main(int n,char**p){
int*modearray,大小,i;
printf(“数组的大小是多少?”);
scanf(“%d”,大小(&S);
modearray=malloc(size*sizeof(int));//int的imo大小是4,所以u可以替换为
对于(i=0;i 30){
printf(“请输入范围内的值”);
scanf(“%d”、&modearray[i])
}
else if(sscanf(p[i],%i],&a[i])!=1){
printf(“错误\n”);
返回-1;
}
}
}
正如Charlie和user2533527已经指出的那样,OP代码中存在错误,并且他们提供了关于这些错误的建议。我在下面编辑您的原始代码时注意到,如果没有寻址,代码不会生成和/或运行。因此,如果您感兴趣,请查看本文底部的内联注释,查看对原始代码的一些更正

这个答案的重点是验证输入,根据您所述的目标(必须检查用户是否在该范围内插入一个数字,并且该数字必须验证为整数),具体来说,您需要验证输入的数字是否在某个范围内,并且它们都是整数

如果将所有验证步骤移动到一个函数中,例如:

int ValidateInput(char *num)
{
    if(strstr(num, ".")!=NULL) return FLOAT;
    if (atoi(num) < 1) return SMALL;
    if (atoi(num) > 30) return LARGE;
    return VALID;
}
status = ValidateInput(number);
switch(status)  {
    case VALID:
        modearray[i] = atoi(number);
        printf("Enter an integer value %d: (1 to 30): ", i+2);

        break;
    case FLOAT:
        printf("float detected, enter an integer");
        i--;//try again
        break;
    case SMALL:
        printf("value too small, enter value from 1 to 30");
        i--;//try again
        break;
    case LARGE:
        printf("value too large, enter value from 1 to 30");
        i--;//try again
        break;
    default:
        //do something else here
        break;
}
总之,这种方法不使用mode函数,而是将其替换为
ValidateInput()
,以确保
modearray
变量中只包含整数和规定范围内的数字

编辑以包括搜索模式(组内出现的最高数量)

我的方法将做三件事来获得模式
对数组进行排序
遍历已排序的数组跟踪沿途的匹配计数。
保留最高的匹配字符串

为此,我将在
mode()
函数中使用
qsort()
和循环

int mode(int *num, int size) {
    int count = 0;
    int countKeep=0;
    int modenum = -1;

    qsort(num, size, sizeof(int), cmpfunc);
    //now we have size in ascending order, get count of most occuring
    for (int x=1; x<size; x++) 
    {
        if(num[x-1] == num[x]) 
        {
            count++; 
            if(count > countKeep)
            {
                countKeep = count;
                modenum=num[x];
            }
            else
            {
                count = 0;
            }
        }
    }
    return modenum;
}
int模式(int*num,int-size){
整数计数=0;
int countKeep=0;
int modenum=-1;
qsort(num、size、sizeof(int)、cmpfunc);
//现在我们有了按升序排列的大小,得到最常发生的数
对于(int x=1;x countKeep)
{
countKeep=计数;
modenum=num[x];
}
其他的
{
计数=0;
}
}
}
返回模式;
}
这里是我的方法的完整代码:(此代码将捕获只有一种模式的数字字符串的模式。您可以修改循环以确定该字符串是多模式的,还是具有两个相同的数字)

#包括//malloc
//#包括//我不需要这些,你可以
//#包括
//#包括
int ValidateInput(字符*num);
int模式(int*num,int size);
int cmpfunc(常数无效*a,常数无效*b);
枚举{
有效,
浮动
小的
大的
};
int main(int n,字符**p)
{
int*modearray,大小,i;
int*a;
字符数[10];
int status=-1;
int模式阵列;
printf(“数组的大小是多少?”);
scanf(“%d”,大小(&S);
modearray=malloc(size*sizeof(int));
a=malloc(尺寸);
printf(“输入整数值1:(1到30):”;
对于(i=0;i 30)返回大;
返回有效;
}
整数模式(整数*num,整数大小){
整数计数=0;
int countKeep=0;
int modenum=-1;
qsort(num、size、sizeof(int)、cmpfunc);
//现在我们有了按升序排列的大小,得到最常发生的数
对于(int x=1;x countKeep)
{
countKeep=计数;
modenum=num[x];
}
其他的
{
计数=0;
}
}
}
返回模式;
}
int cmpfunc(常数无效*a,常数无效*b)
{
返回(*(int*)a-*(int*)b);
}

你能具体说明问题的具体原因吗?好的,我想我看到了。modeArray[]未分配,如果要访问它,应首先运行modeArray=malloc(size*sizeof(int))//我对iPhone上的malloc语法不太清楚。请解释一下你所说的模式是什么意思?这是您想要执行的操作类型吗?(一)
#include <ansi_c.h> //malloc
//#include <stdio.h>//I did not need these others, you might
//#include <string.h>
//#include <math.h>  
int ValidateInput(char *num);
int mode(int *num, int size);
int cmpfunc (const void * a, const void * b);

enum  {
    VALID,
    FLOAT,
    SMALL,
    LARGE
};

int main(int n, char **p)
{
    int *modearray, size, i;  
    int *a; 
    char number[10];
    int status=-1;
    int modeOfArray;

    printf("What is the size of the Array?");
    scanf("%d", &size);

    modearray = malloc(size*sizeof(int));
    a = malloc(size);

    printf("Enter an integer value 1: (1 to 30): ");
    for (i=0; i<size; i++) 
    {
        scanf("%s", number); 

        //Validate Number:
        status = ValidateInput(number);
        switch(status)  {
            case VALID:
                modearray[i] = atoi(number);
                printf("Enter an integer value %d: (1 to 30): ", i+2);

                break;
            case FLOAT:
                printf("float detected, enter an integer");
                i--;//try again
                break;
            case SMALL:
                printf("value too small, enter value from 1 to 30");
                i--;//try again
                break;
            case LARGE:
                printf("value too large, enter value from 1 to 30");
                i--;//try again
                break;
            default:
                //do something else here
                break;
        }
    }
    modeOfArray = mode(modearray, size);
    getchar();//to view printf before execution exits
}
int ValidateInput(char *num)
{
    if(strstr(num, ".")!=NULL) return FLOAT;
    if (atoi(num) < 1) return SMALL;
    if (atoi(num) > 30) return LARGE;
    return VALID;
}


int mode(int *num, int size) {
    int count = 0;
    int countKeep=0;
    int modenum = -1;

    qsort(num, size, sizeof(int), cmpfunc);
    //now we have size in ascending order, get count of most occuring
    for (int x=1; x<size; x++) 
    {
        if(num[x-1] == num[x]) 
        {
            count++; 
            if(count > countKeep)
            {
                countKeep = count;
                modenum=num[x];
            }
            else
            {
                count = 0;
            }
        }

    }

    return modenum;
}

int cmpfunc (const void * a, const void * b)
{
   return ( *(int*)a - *(int*)b );
}