Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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
在C语言中,只乘负数,只输出文件中的负整数_C_File_While Loop_Output_Scanf - Fatal编程技术网

在C语言中,只乘负数,只输出文件中的负整数

在C语言中,只乘负数,只输出文件中的负整数,c,file,while-loop,output,scanf,C,File,While Loop,Output,Scanf,我只需要将文件中的负数与内容相乘:127-143-810。所以我需要-14乘以-8。 这是我的全部代码: #include <stdio.h> #include <math.h> int main() { FILE *in, *out; int x, negative, product; in=fopen("C:\\Users\\emachines\\Desktop\\ind\\in.txt", "r"); if(in==NULL) //If file failed t

我只需要将文件中的负数与内容相乘:
127-143-810
。所以我需要-14乘以-8。 这是我的全部代码:

#include <stdio.h>
#include <math.h>

int main()
{
FILE *in, *out;
int x, negative, product;
in=fopen("C:\\Users\\emachines\\Desktop\\ind\\in.txt", "r");
if(in==NULL) //If file failed to open
{
printf ("Cannot open the file. Exiting...");
return -1;
}

printf("Numbers are:"); //Output integers
while(fscanf(in, "%d", &x)==1)
{
printf("%d ", x); //end of outputting integers
}

printf("\nNegative numbers are:"); //Output negative numbers
while(!feof(in)) 
{
fscanf(in, "%d", &negative);
while(negative<0)
{
printf("%d", negative); //end of outputting negative numbers
                        //multiply negative numbers
printf("mupltipying... Product - %d", product)
}
}

fclose(in);
return(0); //main returns int
}
#包括
#包括
int main()
{
文件*输入,*输出;
整数x,负数,乘积;
in=fopen(“C:\\Users\\emachines\\Desktop\\ind\\in.txt”,“r”);
if(in==NULL)//如果文件打开失败
{
printf(“无法打开文件。正在退出…”);
返回-1;
}
printf(“数字为:”;//输出整数
而(fscanf(在、%d、&x中)==1)
{
printf(“%d”,x);//输出整数结束
}
printf(“\n负数为:”);//输出负数
而(!feof(in))
{
fscanf(在“%d”中,&负数);

而(负数只需在打印数字时检查,如果小于0,则打印它,同时制作此数字的乘积。这是您的工作代码

#include <stdio.h>

int main()
{
FILE *in, *out;
int x, negative, product=1;
in=fopen("a.txt", "r");
if(in==NULL) //If file failed to open
{
printf ("Cannot open the file. Exiting...");
return -1; 
}
printf("Negative Numbers are:"); //Output integers
while(fscanf(in, "%d", &x)==1)
{
    if(x<0){
        product*=x;
        printf("%d ", x); //end of outputting integers
    }
}

printf("\n");
                        //multiply negative numbers
printf("mupltipying...\n Product=%d\n", product);
//printf("%d\n",product );


fclose(in);
return(0); //main returns int
}
#包括
int main()
{
文件*输入,*输出;
整数x,负数,乘积=1;
in=fopen(“a.txt”、“r”);
if(in==NULL)//如果文件打开失败
{
printf(“无法打开文件。正在退出…”);
返回-1;
}
printf(“负数为:”;//输出整数
而(fscanf(在、%d、&x中)==1)
{

if(x您只需要一个循环,其中必须包含if语句,该语句将检查下一个数字是否为负数

比如说

long long product = 1;

//...

while ( fscanf( in, "%d", &x ) == 1 )
{
    if ( x < 0 )
    {
        printf( "%d ", x );
        product *= x;
    }
}
长积=1;
//...
而(fscanf(在、%d、&x中)==1)
{
if(x<0)
{
printf(“%d”,x);
乘积*=x;
}
}

请正确格式化您的代码以使其可读。我使用.txt只是为了调试您的代码。您可以更改它。:)谢谢。我尝试将该部分代码与输出所有整数的另一部分代码一起使用。现在我明白了为什么它不起作用。
printf(“负数是:”;//输出负整数,而(fscanf)(in,“%d”,&x)==1{if(xYou)也可以这样做。您需要做的就是重新读取文件并打印所有数字,或者将数字存储在数组中,然后再打印。