Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果输入中没有二进制、三元、ect,如何将输出设置为“无打印”?_C - Fatal编程技术网

如果输入中没有二进制、三元、ect,如何将输出设置为“无打印”?

如果输入中没有二进制、三元、ect,如何将输出设置为“无打印”?,c,C,这是我为一个项目编写的代码,如果你输入数字的偶数和奇数基数,以及一组20个数字,最后一个是-1,它将使用基数输入转换数字,但如果没有,我需要它打印无 #包括 #包括 #包括 int odddinput(); int输入(); void dataInput(); 无效转换(整数n,整数b); 无效检查类型(int-base); 无效打印奇数(int arr[],int base); 无效打印偶数(int arr[],int base); int数据集[20]; int main() { int

这是我为一个项目编写的代码,如果你输入数字的偶数和奇数基数,以及一组20个数字,最后一个是-1,它将使用基数输入转换数字,但如果没有,我需要它打印无

#包括
#包括
#包括
int odddinput();
int输入();
void dataInput();
无效转换(整数n,整数b);
无效检查类型(int-base);
无效打印奇数(int arr[],int base);
无效打印偶数(int arr[],int base);
int数据集[20];
int main()
{
int oddbase=odddinput();
int evenbase=evenInput();
数据输入();
printf(“\n”);
printOdd(数据集,oddbase);
printf(“\n”);
printEven(数据集,evenbase);
返回0;
}
void dataInput()
{
int i,温度;
printf(“最多输入20个整数:”;
对于(i=0;温度!=-1;i++)
{
scanf(“%d”、&temp);
数据集[i]=温度;
}
}
int输入()
{
int-oddbase;
printf(“输入奇数的基数:”);
scanf(“%d”&oddbase);
而(oddbase<2 | | oddbase>9)
{
printf(“\n错误!基数必须在2到9之间”);
printf(“\n输入奇数的基数:”);
scanf(“%d”&oddbase);
}
返回基地;
}
int输入()
{
伊文贝斯;
printf(“输入奇数的基数:”);
scanf(“%d”&evenbase);
而(evenbase<2 | | evenbase>9)
{
printf(“\n错误!基数必须在2到9之间”);
printf(“\n输入奇数的基数:”);
scanf(“%d”&evenbase);
}
返回基准;
}
无效转换(整数n,整数b)
{
int rem;//模运算余数的值
如果(n==0)
{
返回;
}
其他的
{
rem=n%b;
转换(不适用,不适用);
printf(“%d”,rem);
}
}
void checkType(int-base)
{
开关(底座)
{
案例2:
printf(“二进制值:”);
打破
案例3:
printf(“三值:”);
打破
案例4:
printf(“四元值:”);
打破
案例5:
printf(“五进制值:”);
打破
案例6:
printf(“原始值:”);
打破
案例7:
printf(“七元值:”);
打破
案例8:
printf(“八进制值:”);
打破
案例9:
printf(“非实数值:”);
打破
}
}
无效打印偶数(int arr[],int base)
{
int i=0;
支票类型(基本);
做
{
如果(arr[i]%2==0)
{
转换(arr[i],基准);
printf(“”);
}
i++;
}while(arr[i]!=-1);
}
无效打印奇数(int arr[],int base)
{
int i=0;
支票类型(基本);
做
{
如果(arr[i]%2!=0)
{
转换(arr[i],基准);
printf(“”);
}
i++;
}while(arr[i]!=-1);
}

使用状态变量来记住是否打印了内容。比如:

void printOdd(int arr[], int base)
{
    int i = 0;
    int did_print = 0;
    checkType(base);
    do
    {
        if (arr[i] % 2 != 0)
        {
            did_print = 1;  
            convert(arr[i], base);
            printf(" ");
        }
        i++;
    } while (arr[i] != -1);
    
    if (did_print == 0)
    {
        printf("none");
    }
}
其他评论:

您的
evenInput
函数将打印“奇数”而不是“偶数”

而且。。。在这里:

int i, temp;
printf("Enter upto 20 integers: ");
for (i = 0; temp != -1; i++)
temp
在循环启动时未初始化。确保将其初始化为某个值,例如零。此外,您应该确保不会使数组溢出。比如:

int i, temp = 0;
printf("Enter upto 20 integers: ");
for (i = 0; temp != -1 && i < 20; i++) // stop when input is -1 OR the array is full
{
    ...
}
arr[19] = -1;  // Make sure the always have a -1 at the last position

只需在print偶数和printOdd中保留一个int,初始化为零,并在每次调用convert时递增,然后在循环后检查它是否为零并打印“none”
int i, temp = 0;
printf("Enter upto 20 integers: ");
for (i = 0; temp != -1 && i < 20; i++) // stop when input is -1 OR the array is full
{
    ...
}
arr[19] = -1;  // Make sure the always have a -1 at the last position
if (scanf("%d", ...) != 1)
{
    // Input error
    ... add error handling ...
}