gpt4 book ai didi

c - 用于查找一组数字模式的高级计算器

转载 作者:太空宇宙 更新时间:2023-11-04 08:50:26 25 4
gpt4 key购买 nike

我目前正在为学校开展一个项目,我需要对计算器进行编程以确定一组数字的众数。参数是必须介于 1 和 30 之间的数字。必须检查用户是否插入该范围内的数字,并且该数字必须验证为整数。我已经完成了大部分工作,除了我的主要问题是输入数字和验证它们并确保我的模式功能正常工作的 for 循环。在解决循环问题方面有什么建议吗?此外,我必须使用模式函数来计算模式,我正在使用的模式是否运行良好,或者是否有更好的方法?

#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;
}
}
}

最佳答案

正如 Charlie 和 user2533527 已经指出的,OP 代码中存在错误,并且他们针对这些错误提供了建议。在我对下面的原始代码进行编辑时,我还注意到了其他一些内容,如果没有解决,代码就不会构建和/或运行。因此,如果您有兴趣,请查看本文底部的内联注释,以查看对您的原始代码的一些更正。

这个答案侧重于验证输入,根据您声明的目标(必须检查用户是否在该范围内插入数字并且该数字must be validated as an integer ) 具体来说,您似乎需要验证输入的数字是否在一个范围内,并且它们都是整数。

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

int ValidateInput(char *num)
{
if(strstr(num, ".")!=NULL) return FLOAT;
if (atoi(num) < 1) return SMALL;
if (atoi(num) > 30) return LARGE;
return VALID;
}

然后可以轻松执行主用户输入循环以包括特定错误(如果有),或者使用 switch() 语句继续数据收集,例如:

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 变量。

EDIT 以包括搜索模式(组内出现次数最多的)

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

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

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;
}

这里是我的方法的完整代码:(此代码将捕获一串数字的模式,只有一种模式。您可以修改循环以确定如果字符串是多模态的,或者有两个相同出现的数字)

#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 );
}

关于c - 用于查找一组数字模式的高级计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19866440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com