

以下是我的代碼,只能輸出一個Apple的資料。
#include <stdio.h>
int main()
{
int m;
char d;
double g=0.0,sumprice=0.0,sumweight=0.0;
double weight[4] = {0.0};
while(scanf("%d", &m)!=EOF)
{
for (int i = 0; i < m; i++)
{
scanf("%c %lf\n",&d,&g);
if(d=='a')
{ weight[0] += g;}
if(d=='o')
{ weight[1] += g;}
if(d=='b')
{weight[2] += g;}
if (d == 'p')
{weight[3] += g;}
}
for (int j = 0; j < 4; j++)
{
sumweight += weight[j];
}
sumprice = weight[0] * 1.5 + weight[1] * 1.4 + weight[2] * 1.48 + weight[3] * 1.08;
printf(" ");
printf("apple orange banana pineapple sum\n");
printf("price ");
printf("%-7.2lf", weight[0] * 1.5);
printf("%-7.2lf", weight[1] * 1.4);
printf("%-7.2lf", weight[2] * 1.48);
printf("%-10.2lf", weight[3] * 1.08);
printf("%-7.2lf", sumprice);
printf("\n");
printf("weight ");
printf("%-7.2lf", weight[0] );
printf("%-7.2lf", weight[1]);
printf("%-7.2lf", weight[2] );
printf("%-10.2lf", weight[3]);
printf("%-7.2lf", sumweight);
printf("\n");
printf("\n");
for (int k = 0; k < 3;k++)
{
weight[k] = 0.0;
}
sumweight = sumprice = 0.0;
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/104453.html
標籤:C語言
上一篇:一道演算法題!
