1

想請教程式語言的問題(c++)

，200~300的商品打8折，300~400的打7折，400~500打6折，500以上的打5 折，請寫出一個程式可以分別輸入這五樣商品的價格並算出總數(至小數第二位)。

50 180 267 393 580
The summary is 990.70

#include <stdio.h>
#include <stdlib.h>
int main()
{
int p;
float a=0.5;
float b=0.7;
float c=0.8;
float d=0.9;
float e=pa;
float f=p
b;
float g=pc;
float h=p
d;
int count=0,count1=0,count2=0,count3=0,count4=0,count5=0;
here:
printf("請輸入價格:");
scanf("%d",&p);
count++;
if(p>500)
{
printf("價格為%.2f\n",e);
count1++;
}
else if (p>300)
{
printf("價格為%.2f\n",f);
count2++;
}
else if (p>200)
{
printf("價格為%.2f\n",g);
count3++;
}
else if (p>100)
{
printf("價格為%.2f\n",h);
count4++;
}
else
{printf("價格為%d\n",p);
count5++;
}
if (count < 5)goto here;
printf("已輸入5個價格\n\n");
}

xSummer iT邦新手 3 級 ‧ 2020-08-11 17:35:17 檢舉

Zed_Yang iT邦新手 3 級 ‧ 2020-08-11 17:50:08 檢舉
「算錢用浮點，遲早被人扁」

1 個回答

0

iT邦大神 1 級 ‧ 2020-08-11 17:42:33

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int p;
float a=0.5;
float b=0.7;
float c=0.8;
float d=0.9;
//float e=p*a;
//float f=p*b;
//float g=p*c;
//float h=p*d;
int count=0,count1=0,count2=0,count3=0,count4=0,count5=0;
here:
printf("請輸入價格:");
scanf("%d",&p);
count++;
if(p>500)
{
printf("價格為%.2f\n",p*a);
count1++;
}
else if (p>300)
{
printf("價格為%.2f\n",p*b);
count2++;
}
else if (p>200)
{
printf("價格為%.2f\n",p*c);
count3++;
}
else if (p>100)
{
printf("價格為%.2f\n",p*d);
count4++;
}
else
{printf("價格為%d\n",p);
count5++;
}
if (count < 5)goto here;
printf("已輸入5個價格\n\n");
}
``````

xSummer iT邦新手 3 級 ‧ 2020-08-11 17:49:02 檢舉

peter1111 iT邦新手 5 級 ‧ 2020-08-11 18:38:32 檢舉

peter1111 iT邦新手 5 級 ‧ 2020-08-11 18:47:19 檢舉

#include <stdio.h>
#include <stdlib.h>
int main()
{
int p;
float a=0.5;
float b=0.7;
float c=0.8;
float d=0.9;
//float e=pa;
//float f=p
b;
//float g=pc;
//float h=p
d;
int count=0,count1=0,count2=0,count3=0,count4=0,count5=0;
//float i=e+f+g+h;
here:
printf("請輸入價格:");
scanf("%d",&p);
count++;
if(p>500)
{
printf("價格為%.2f\n",pa);
count1++;
}
else if (p>300)
{
printf("價格為%.2f\n",p
b);
count2++;
}
else if (p>200)
{
printf("價格為%.2f\n",pc);
count3++;
}
else if (p>100)
{
printf("價格為%.2f\n",p
d);
count4++;
}
else
{printf("價格為%d\n",p);
count5++;
}
if (count < 5)goto here;
printf("已輸入5個價格\n\n");
printf("總價格為%.2f\n",i);
}

1.i 要先宣告(類似 count)
2.要在每個計算折扣價格之後, 把值加給 i,例;`i=i+p*a;`

peter1111 iT邦新手 5 級 ‧ 2020-08-13 02:37:43 檢舉