鱼C论坛

 找回密码
 立即注册
查看: 1977|回复: 9

[已解决]买菜小程序

[复制链接]
发表于 2017-12-26 16:15:08 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
菜价(单位是元/千克,1斤等于0.5千克):番茄(3.7),芥蓝(7),西芹(1.3),空心菜(8),洋葱(2.4),油菜(9),黄瓜(6.3),白萝卜(0.5)。按顾客需求,计算需要支付的价格,并打印出来。
我写的代码是:
  1. #include <stdio.h>
  2. #define FQ(a) 3.7*a/2.0
  3. #define JL(b) 7*b/2.0
  4. #define XQ(c) 1.3*c/2.0
  5. #define KXQ(d) 8*d/2.0
  6. #define YCONG(e) 2.4*e/2.0
  7. #define YC(f) 9*f/2.0
  8. #define HG(g) 6.3*g/2.0
  9. #define BLB(h) 0.5*h/2.0
  10. void main()
  11. {
  12.     float a,b,c,d,e,f,g,h;
  13.     float TOTAL;
  14.         TOTAL=FQ(a)+JL(b)+XQ(c)+KXQ(d)+YCONG(e)+YC(f)+HG(g)+BLB(h);
  15.         scanf("%f %f %f %f %f %f %f %f",&a,&b,&c,&d,&e,&f,&g,&h);
  16.         printf("需要支付:%.2f元",TOTAL);
  17. }
复制代码

QQ截图20171226161015.png
得不到正确数,找不到原因
大神还有没有什么好的方法?这要是开个超市,顾客只买一样东西,我岂不是要输入一堆0?
最佳答案
2017-12-26 16:18:59
请把Total的计算放到scanf函数后面,还没获得每一样的值,你进行求和是没有意义的

你可以将这些参数都初始化为0,然后用循环来判断时候需要输入,如果你学了循环的话
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-12-26 16:18:59 | 显示全部楼层    本楼为最佳答案   
请把Total的计算放到scanf函数后面,还没获得每一样的值,你进行求和是没有意义的

你可以将这些参数都初始化为0,然后用循环来判断时候需要输入,如果你学了循环的话
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-12-26 16:29:18 | 显示全部楼层
本帖最后由 fairylee83 于 2017-12-26 16:34 编辑
BngThea 发表于 2017-12-26 16:18
请把Total的计算放到scanf函数后面,还没获得每一样的值,你进行求和是没有意义的

你可以将这些参数都初 ...


#include <stdio.h>
#define FQ(a) 3.7*a/2.0
#define JL(b) 7*b/2.0
#define XQ(c) 1.3*c/2.0
#define KXQ(d) 8*d/2.0
#define YCONG(e) 2.4*e/2.0
#define YC(f) 9*f/2.0
#define HG(g) 6.3*g/2.0
#define BLB(h) 0.5*h/2.0
void main()
{
    float a,b,c,d,e,f,g,h;
    float TOTAL;
        scanf("%f %f %f %f %f %f %f %f",&a,&b,&c,&d,&e,&f,&g,&h);
        TOTAL=FQ(a)+JL(b)+XQ(c)+KXQ(d)+YCONG(e)+YC(f)+HG(g)+BLB(h);
        printf("需要支付:%.2f元",TOTAL);
}
就第一个问题,修改了以后就成功啦。 QQ截图20171226162630.png QQ截图20171226162748.png
循环目前仅限于能看一点点,还不会用。
我再继续修行,以后再解这道题吧。
QQ截图20171226162828.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-26 17:20:55 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <string.h>

  3. typedef struct
  4. {
  5.         char   name[20];
  6.         union
  7.         {
  8.                 double price;
  9.                 int    number;
  10.         } u;
  11. } Vegetable;

  12. double CalculatePrice(Vegetable *v)
  13. {
  14.         const Vegetable veg[] =
  15.         {
  16.                 {"番茄",   3.7},
  17.                 {"芥蓝",   7  },
  18.                 {"西芹",   1.3},
  19.                 {"空心菜", 8  },
  20.                 {"洋葱",   2.4},
  21.                 {"油菜",   9  },
  22.                 {"黄瓜",   6.3},
  23.                 {"白萝卜", 0.5}
  24.         };
  25.         const int vegLength = sizeof(veg) / sizeof(veg[0]);
  26.         double price;
  27.        
  28.         for(int i = 0; i < vegLength; i++)
  29.         {
  30.                 if(strcmp(veg[i].name, v->name) == 0)
  31.                 {
  32.                         price = veg[i].u.price * v->u.number;
  33.                         break;
  34.                 }
  35.         }

  36.         return price;
  37. }

  38. int GetInput(Vegetable *v)
  39. {
  40.         scanf("%s", v->name);
  41.         if(strncmp(v->name, "#", 1) == 0) // 输入#停止录入
  42.                 return 1;

  43.         scanf("%d", &v->u.number);

  44.         return 0;
  45. }

  46. int main(int argc, char *argv[])
  47. {
  48.         Vegetable v;
  49.         double totalPrice = 0;

  50.         //printf("\n"); // 这里还需要提示些什么,但我实在想不出该提示些什么 ^_^
  51.         printf("蔬菜名    数量(千克)\n");
  52.         while(1)
  53.         {
  54.                 if(GetInput(&v))
  55.                         break;

  56.                 totalPrice += CalculatePrice(&v);
  57.         }

  58.         printf("需要支付:%.2lf元\n", totalPrice);

  59.         return 0;
  60. }
复制代码


无标题.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2017-12-27 09:38:08 | 显示全部楼层

哇!好赞!但以我现在的水平,看不太懂非常感谢大神辛苦编了这么一段给我开眼让我一饱眼福!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-12-27 09:49:21 | 显示全部楼层
大神,我试运行了您的这段程序。报错了。但以我的水平,找不到错在哪。在i++后和括号后分别都加了分号还是运行报错。
QQ截图20171227094455.png
微信截图_20171227094350.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-27 10:20:43 From FishC Mobile | 显示全部楼层
因为他是用c++写的,c++的变量定义和c不同
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-27 10:24:25 From FishC Mobile | 显示全部楼层

这里加  int i;
const Vegetable veg[] =
        {
                {"番茄",   3.7},
                {"芥蓝",   7  },
                {"西芹",   1.3},
                {"空心菜", 8  },
                {"洋葱",   2.4},
                {"油菜",   9  },
                {"黄瓜",   6.3},
                {"白萝卜", 0.5}
        };
        const int vegLength = sizeof(veg) / sizeof(veg[0]);
        double price;
        
        for( i = 0; i < vegLength; i++)这里改成这样

union
        {
                double price;
                int    number; 这里最好用double number;因为有可能0.5千克的情况
        } u;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2017-12-27 14:31:37 | 显示全部楼层
fairylee83 发表于 2017-12-27 09:49
大神,我试运行了您的这段程序。报错了。但以我的水平,找不到错在哪。在i++后和括号后分别都加了 ...
  1. #include <stdio.h>
  2. #include <string.h>

  3. typedef struct
  4. {
  5.         char   name[20];
  6.         union
  7.         {
  8.                 double price;
  9.                 int    number;
  10.         } u;
  11. } Vegetable;

  12. double CalculatePrice(Vegetable *v)
  13. {
  14.         const Vegetable veg[] =
  15.         {
  16.                 {"番茄",   3.7},
  17.                 {"芥蓝",   7  },
  18.                 {"西芹",   1.3},
  19.                 {"空心菜", 8  },
  20.                 {"洋葱",   2.4},
  21.                 {"油菜",   9  },
  22.                 {"黄瓜",   6.3},
  23.                 {"白萝卜", 0.5}
  24.         };
  25.         const int vegLength = sizeof(veg) / sizeof(veg[0]);
  26.         double price;
  27.         int i;
  28.         
  29.         for(i = 0; i < vegLength; i++)
  30.         {
  31.                 if(strcmp(veg[i].name, v->name) == 0)
  32.                 {
  33.                         price = veg[i].u.price * v->u.number;
  34.                         break;
  35.                 }
  36.         }

  37.         return price;
  38. }

  39. int GetInput(Vegetable *v)
  40. {
  41.         scanf("%s", v->name);
  42.         if(strncmp(v->name, "#", 1) == 0) // 输入#停止录入
  43.                 return 1;

  44.         scanf("%d", &v->u.number);

  45.         return 0;
  46. }

  47. int main(int argc, char *argv[])
  48. {
  49.         Vegetable v;
  50.         double totalPrice = 0;

  51.         //printf("\n"); // 这里还需要提示些什么,但我实在想不出该提示些什么 ^_^
  52.         printf("蔬菜名    数量(千克)\n");
  53.         while(1)
  54.         {
  55.                 if(GetInput(&v))
  56.                         break;

  57.                 totalPrice += CalculatePrice(&v);
  58.         }

  59.         printf("需要支付:%.2lf元\n", totalPrice);

  60.         return 0;
  61. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-12-27 15:13:31 | 显示全部楼层
sp1ral 发表于 2017-12-27 10:24
这里加  int i;
const Vegetable veg[] =
        {
  1. #include <stdio.h>
  2. #include <string.h>

  3. typedef struct
  4. {
  5.         char   name[20];
  6.         union
  7.         {
  8.                 double price;
  9.                 double number;
  10.         } u;
  11. } Vegetable;

  12. double CalculatePrice(Vegetable *v)
  13. {
  14.     int i;   
  15.         const Vegetable veg[] =
  16.         {
  17.                 {"番茄",   3.7},
  18.                 {"芥蓝",   7  },
  19.                 {"西芹",   1.3},
  20.                 {"空心菜", 8  },
  21.                 {"洋葱",   2.4},
  22.                 {"油菜",   9  },
  23.                 {"黄瓜",   6.3},
  24.                 {"白萝卜", 0.5}
  25.         };
  26.         const int vegLength = sizeof(veg) / sizeof(veg[0]);
  27.         double price;
  28.         
  29.         for(int i = 0; i < vegLength; i++)
  30.         {
  31.                 if(strcmp(veg[i].name, v->name) == 0)
  32.                 {
  33.                         price = veg[i].u.price * v->u.number;
  34.                         break;
  35.                 }
  36.         }

  37.         return price;
  38. }

  39. int GetInput(Vegetable *v)
  40. {
  41.         scanf("%s", v->name);
  42.         if(strncmp(v->name, "#", 1) == 0) // 输入#停止录入
  43.                 return 1;

  44.         scanf("%d", &v->u.number);

  45.         return 0;
  46. }

  47. int main(int argc, char *argv[])
  48. {
  49.         Vegetable v;
  50.         double totalPrice = 0;

  51.         //printf("\n"); // 这里还需要提示些什么,但我实在想不出该提示些什么 ^_^
  52.         printf("蔬菜名    数量(千克)\n");
  53.         while(1)
  54.         {
  55.                 if(GetInput(&v))
  56.                         break;

  57.                 totalPrice += CalculatePrice(&v);
  58.         }

  59.         printf("需要支付:%.2lf元\n", totalPrice);

  60.         return 0;
  61. }
复制代码

按照您说的改了。不知是我没改对,还是什么原因,在不加.c后缀的文件里面运行,应该是C++了吧。还是报错。 QQ截图20171227150703.png
我看报错的信息应该是重复定义。所以把int i;又去掉了,然后就能运行了。
谢谢您解答小白的疑惑。
  1. #include <stdio.h>
  2. #include <string.h>

  3. typedef struct
  4. {
  5.         char   name[20];
  6.         union
  7.         {
  8.                 double price;
  9.                 double number;
  10.         } u;
  11. } Vegetable;

  12. double CalculatePrice(Vegetable *v)
  13. {
  14.    
  15.         const Vegetable veg[] =
  16.         {
  17.                 {"番茄",   3.7},
  18.                 {"芥蓝",   7  },
  19.                 {"西芹",   1.3},
  20.                 {"空心菜", 8  },
  21.                 {"洋葱",   2.4},
  22.                 {"油菜",   9  },
  23.                 {"黄瓜",   6.3},
  24.                 {"白萝卜", 0.5}
  25.         };
  26.         const int vegLength = sizeof(veg) / sizeof(veg[0]);
  27.         double price;
  28.         
  29.         for(int i = 0; i < vegLength; i++)
  30.         {
  31.                 if(strcmp(veg[i].name, v->name) == 0)
  32.                 {
  33.                         price = veg[i].u.price * v->u.number;
  34.                         break;
  35.                 }
  36.         }

  37.         return price;
  38. }

  39. int GetInput(Vegetable *v)
  40. {
  41.         scanf("%s", v->name);
  42.         if(strncmp(v->name, "#", 1) == 0) // 输入#停止录入
  43.                 return 1;

  44.         scanf("%d", &v->u.number);

  45.         return 0;
  46. }

  47. int main(int argc, char *argv[])
  48. {
  49.         Vegetable v;
  50.         double totalPrice = 0;

  51.         //printf("\n"); // 这里还需要提示些什么,但我实在想不出该提示些什么 ^_^
  52.         printf("蔬菜名    数量(千克)\n");
  53.         while(1)
  54.         {
  55.                 if(GetInput(&v))
  56.                         break;

  57.                 totalPrice += CalculatePrice(&v);
  58.         }

  59.         printf("需要支付:%.2lf元\n", totalPrice);

  60.         return 0;
  61. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 21:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表