我目前正在为学校做一个项目,我需要编写一个计算器来确定一组数字的模式。参数是数字必须在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已经指出的,操作代码中存在错误,他们已经提供了关于这些错误的建议。我在下面对原始代码的编辑中还注意到一些其他问题,即如果没有寻址,代码就无法生成和/或运行。因此,如果您感兴趣,请查看本文底部的内联注释,以查看对原始代码的一些更正。
这个答案的重点是验证输入,根据你的既定目标(必须检查用户是否在该范围内插入了一个数字,并且该数字必须验证为整数),特别是你需要验证输入的数字是否在一个范围内,并且它们都是整数。
如果将所有验证步骤移到一个函数中,例如:
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;
}
总之,这种方法不使用模式函数,而是用
ValidateInput()
来代替它,这样可以确保modearray
变量中只包含整数,并且在指定范围内。编辑以包括搜索模式(组中出现的最高数字)
我的方法将做三件事来获得模式
对数组排序,
查看沿途匹配的排序数组跟踪计数。
保持最高的火柴串。
为此,我将在
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/