检查是否有东西被3分割

时间:2017-11-18 18:34:09

标签: c

我有东西要读取文本文件,然后是像这样的函数文件

int Myiseven(int x)
{
    int isOdd = 0;
    if (x % 2 == 1) {
        isOdd = 1;
    }
}

这样所有奇数都会有isodd = 1

我将如何检查数字是否可被3整除

原始主文件是这个

#define _CRT_SECURE_NO_DEPRECATE


#include<stdio.h>
#include "ProblemHeader_4.h"
int main()
{
    FILE *myfile = fopen("input.txt", "w");

    for (int i = 1; i <= 33; i++)
    {
        fprintf(myfile, "%d\n", i);
    }
    fclose(myfile);

    FILE *myfileRead = fopen("input.txt", "r");
    FILE *myfileWrite = fopen("outputEven.txt", "w");

    int readBuff;
    while (!feof(myfileRead))
    {
        fscanf(myfileRead, "%d", &readBuff);
        printf("These numbers were read: %d\n", readBuff);

        int isOdd = Myiseven(readBuff);
        if (isOdd == 1)
        {
            fprintf(myfileWrite, "%d\n", readBuff);
            printf("This number is divisible by 3: %d\n", readBuff);
        }
    }
    fclose(myfileWrite);
    fclose(myfileRead);

    return 0;
}

和标题

#ifndef MY_VAR
#define MY_VAR

#include<stdio.h>

int Myiseven(int x);

#endif

1 个答案:

答案 0 :(得分:0)

看起来您只想打印可被3整除的奇数。您可以按如下方式执行此操作:

if (isOdd == 1 && readBuff%3==0)
        {
            fprintf(myfileWrite, "%d\n", readBuff);
            printf("This number is divisible by 3: %d\n", readBuff);
        }

此外,您需要在return函数中使用Myiseven()语句才能成功执行代码:

int Myiseven(int x)
{
    int isOdd = 0;
    if (x % 2 == 1) {
        isOdd = 1;
    }
   return isOdd;
}