[英]printf without comma after the last number in c
这个程序的功能是你输入任意数量的值,程序会做一些数学运算。 问题是我需要以这种格式从输入中输出数字:number1, number2,..., numbern 但我不知道该怎么做,我只能以这种格式打印数字 number1, number2,... , numbern,<-- 用这个逗号在 and 但我不想要逗号。
#include <stdio.h>
int main()
{
int a=0, pc=0, pk=0, pz=0, ps=0, pl=0, suma=0, max = a, min = a;
while (1==scanf("%i, ", &a))
{
suma+=a;
if(-10000<a<10000)
{
pc++;
}
if(a<0)
{
pz++;
}
if(a>0)
{
pk++;
}
if(a%2==0)
{
ps++;
}
if(a%2!=0)
{
pl++;
}
if (a>max)
{
max = a;
}
if (a<min)
{
min = a;
}
if (a>10000||a< -10000)
{
printf("Error:!");
return 100;
}
if (a<10000||a> -10000) //here is my problem xd
printf("%i, ", a);
}
int b = a;
float pkf=pk;
float plf=pl;
float pzf=pz;
float psf=ps;
float prk = pkf/pc*100;
float prz = pzf/pc*100;
float prs = psf/pc*100;
float prl = plf/pc*100;
float sumaf=suma;
float avg = sumaf/pc;
printf("\n");
printf("quantity of numbers: %i\n", pc);
printf("positive numbers: %i\n", pk);
printf("negative numbets: %i\n", pz);
printf("Percentage of positive numbers: %.2f%\n", prk);
printf("Percentage of negative numbers: %.2f%\n", prz);
printf("even numbers: %i\n", ps);
printf("odd numbers: %i\n", pl);
printf("Percentage of even numbers: %.2f%\n", prs);
printf("Percentage of odd numbers: %.2f%\n", prl);
printf("avg: %.2f\n", avg);
printf("Max: %i\n", max);
printf("Min: %i\n", min);
return 0;
}
首先,您的代码缺少正确的语法。
即使语法已更正,您的代码似乎也没有做任何事情。 当有输入且不执行任何操作时,while 循环条件为真。 请更正您的代码并描述您想要实现的目标。
此外,一般来说,为了让您的输出没有最后一个逗号,您可以使用 if 条件来检查正在打印的元素是否是最后一个并省略逗号的打印。 您可以通过在打印元素之后的另一个打印语句中打印逗号来完成此操作。 或者,如果您知道第一个元素,则可以省略打印第一个元素的逗号并打印其余元素(通过在元素前打印逗号)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.