【发布时间】:2014-11-09 17:24:38
【问题描述】:
我需要仅将文件中的负数与内容相乘:12 7 -14 3 -8 10。所以我需要-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
}
那么如何只乘负数呢?如果我使用 x 变量输出 all 数字,如何输出 only negative 数字? 对不起我的英语
【问题讨论】:
-
请正确格式化您的代码以使其可读。
标签: c file while-loop output scanf