我有一些东西要读取一个文本文件,然后再读取一个这样的功能文件

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


这样所有的奇数都等于isodd = 1

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

原始的主文件是这个

#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

最佳答案

看起来您只想打印可被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;
}

关于c - 检查某物是否可被3整除,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47369643/

10-12 16:13