<span style="font-family: Arial, Helvetica, sans-serif;">#include "stdafx.h"</span>
#include <windows.h>
#define PRICEOFARTICHOKES_PERPOUND 1.255
#define PRICEOFBEET_PERPOUND 0.65
#define PRICEOFCARROT_PERPOUND 0.89
float get_the_price_artichokes(float f)
{
float price;
price = 0;
if (f<5)
{
price = 3.5 + f*PRICEOFARTICHOKES_PERPOUND;
}
else if (f<20&&f>=5)
{
price = 10 + f*PRICEOFARTICHOKES_PERPOUND;
}
else
{
price = 8 + f*PRICEOFARTICHOKES_PERPOUND+0.1*f;
}
if (price>100)
{
price = price*0.95;
}
return price;
}
float get_the_price_beets(float f)
{
float price;
price = 0;
if (f<5)
{
price = 3.5 + f*PRICEOFBEET_PERPOUND;
}
else if (f<20 && f >= 5)
{
price = 10 + f*PRICEOFBEET_PERPOUND;
}
else
{
price = 8 + f*PRICEOFBEET_PERPOUND + 0.1*f;
}
if (price>100)
{
price = price*0.95;
}
return price;
}
float get_the_price_carrots(float f)
{
float price;
price = 0;
if (f<5)
{
price = 3.5 + f*PRICEOFCARROT_PERPOUND;
}
else if (f<20 && f >= 5)
{
price = 10 + f*PRICEOFCARROT_PERPOUND;
}
else
{
price = 8 + f*PRICEOFCARROT_PERPOUND + 0.1*f;
}
if (price>100)
{
price = price*0.95;
}
return price;
}
int _tmain(int argc, _TCHAR* argv[])
{
float a, b, c,p,f;
char q;
char ch;
a = 0;
b = 0;
c = 0;
p = 0;
ch = ' ';
while (ch!='q')
{
printf("a. input the pounds of artichokes \n");
printf("b. input the pounds of beets \n");
printf("c. input the pounds of carrots \n");
printf("q. quit \n");
scanf("%c", &ch);
getchar();
printf("input the pounds:\n");
scanf("%f", &f);
switch (ch)
{
case 'a': a =a+ get_the_price_artichokes(f); break;
case 'b': b =b+ get_the_price_beets(f); break;
case 'c': c =c+ get_the_price_carrots(f); break;
}
p = p + a + b + c;
printf("the price is %f \n", p);
}
system("pause");
return 0;
}
初步构想
#define PRICEOFARTICHOKES_PERPOUND 1.255
#define PRICEOFBEET_PERPOUND 0.65
#define PRICEOFCARROT_PERPOUND 0.89
这三个功能表示每磅的价格,然后将其写入到对应的函数中
float get_the_price_artichokes(float f)
float get_the_price_beets(float f)
float get_the_price_carrots(float f)
也就是这三个函数,返回的值是根据折扣类型和售价得到的数,float型直接得到对应菜的总价,输入是菜的重量,定义一个float型
实际运行时发现循环有问题。
改错版本1
#include "stdafx.h"
#include <windows.h>
#define PRICEOFARTICHOKES_PERPOUND 1.255
#define PRICEOFBEET_PERPOUND 0.65
#define PRICEOFCARROT_PERPOUND 0.89
float get_the_price_artichokes(float f)
{
float price;
price = 0;
if (f<5)
{
price = 3.5 + f*PRICEOFARTICHOKES_PERPOUND;
}
else if (f<20&&f>=5)
{
price = 10 + f*PRICEOFARTICHOKES_PERPOUND;
}
else
{
price = 8 + f*PRICEOFARTICHOKES_PERPOUND+0.1*f;
}
if (price>100)
{
price = price*0.95;
}
return price;
}
float get_the_price_beets(float f)
{
float price;
price = 0;
if (f<5)
{
price = 3.5 + f*PRICEOFBEET_PERPOUND;
}
else if (f<20 && f >= 5)
{
price = 10 + f*PRICEOFBEET_PERPOUND;
}
else
{
price = 8 + f*PRICEOFBEET_PERPOUND + 0.1*f;
}
if (price>100)
{
price = price*0.95;
}
return price;
}
float get_the_price_carrots(float f)
{
float price;
price = 0;
if (f<5)
{
price = 3.5 + f*PRICEOFCARROT_PERPOUND;
}
else if (f<20 && f >= 5)
{
price = 10 + f*PRICEOFCARROT_PERPOUND;
}
else
{
price = 8 + f*PRICEOFCARROT_PERPOUND + 0.1*f;
}
if (price>100)
{
price = price*0.95;
}
return price;
}
int _tmain(int argc, _TCHAR* argv[])
{
float a, b, c,p,f;
char q;
char ch;
a = 0;
b = 0;
c = 0;
p = 0;
ch = ' ';
printf("a. input the pounds of artichokes \n");
printf("b. input the pounds of beets \n");
printf("c. input the pounds of carrots \n");
printf("q. quit \n");
scanf("%c", &ch);
while (ch!='q')
{
switch (ch)
{
case 'a':
printf("input the pounds:\n");
scanf("%f", &f);
a = a + get_the_price_artichokes(f); ch = ' '; break;
case 'b':
printf("input the pounds:\n");
scanf("%f", &f);
b = b + get_the_price_beets(f); ch = ' '; break;
case 'c':
printf("input the pounds:\n");
scanf("%f", &f);
c = c + get_the_price_carrots(f); ch = ' '; break;
default: break;
}
printf("a. input the pounds of artichokes \n");
printf("b. input the pounds of beets \n");
printf("c. input the pounds of carrots \n");
printf("q. quit \n");
scanf("%c", &ch);
}
p = a + b + c;
printf("the price is %f \n", p);
system("pause");
return 0;
}
实现买菜功能