实现买菜功能(未完成代码)
2016-07-12 17:42
309 查看
<span style="font-family: Arial, Helvetica, sans-serif;">#include "stdafx.h"</span>
初步构想
实际运行时发现循环有问题。
改错版本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;
}
实现买菜功能
#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;
}
实现买菜功能
相关文章推荐
- 如何组织构建多文件 C 语言程序(二)
- 如何写好 C main 函数
- c++ primer 第五版 笔记前言
- 那些年,我还在学习C# 学习笔记续
- Ruby 魔法 学习笔记之一
- Lua和C语言的交互详解
- sqlserver 数据库学习笔记
- CSS学习笔记Padding 属性中参数的定义与使用
- prototype 1.5 & scriptaculous 1.6.1 学习笔记
- prototype 学习笔记整理
- Oracle学习笔记(六)
- 关于SQLServer2005的学习笔记 XML的处理
- Jquery 基础学习笔记
- ExtJs 学习笔记基础篇 Ext组件的使用第1/2页
- 关于C语言中参数的传值问题
- linux Shell学习笔记第五天
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- Jquery 学习笔记(二)