| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 717 人关注过本帖
标题:困扰了好几天的 scanf 输入函数应用
只看楼主 加入收藏
lanskyxti
Rank: 1
等 级:新手上路
帖 子:14
专家分:4
注 册:2014-8-29
结帖率:80%
收藏
已结贴  问题点数:20 回复次数:7 
困扰了好几天的 scanf 输入函数应用
程序代码:
#include<stdio.h>
#include<stdlib.h>

int main(viod)
{
    double price,discount,new_discount;
    float cost_discount=0.38;
    printf("产品价格 :");
    scanf("%d",&price);//getchar();
    printf("产品折扣 :");
    scanf("%f",&discount);//getchar();
    printf("产品价格是%d,折扣是%.2f,成本折扣是%f.\n",price,discount,discount-cost_discount);
    double profit=price*(discount-cost_discount);
    printf("产品利润是%d:\n",profit);
    printf("产品新折扣是 :");//getchar();
    scanf("%f",&new_discount);
    double profit1=price*(new_discount-cost_discount);
    if(profit>=profit1)
    printf("前数量= %d 数量.\n",profit/profit1);
    else
    printf("亏本.\n");
    system("pause");
    return 0;
}
算出来的结果不对,是scanf函数调入了什么,请各位帮忙指正那里错了,谢谢!
搜索更多相关主题的帖子: price color discount 
2014-09-20 16:31
erty1001
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:4
帖 子:331
专家分:1433
注 册:2014-8-31
收藏
得分:20 
简单说说:

毫无头绪

#include<stdio.h>
#include<stdlib.h>

int main(void)
{
    double price,discount,new_discount,x;
    float cost_discount=0.38f;

    printf("产品价格 :");
    scanf("%lf",&price);

    printf("产品折扣 :");
    scanf("%lf",&discount);

    x=discount-cost_discount;

    printf("产品价格是%.2lf,折扣是%.2lf,成本折扣是%.2lf\n",price,discount,x);

    double profit=price*(discount-cost_discount);

    printf("产品利润是%.2lf:\n",profit);

    printf("产品新折扣是 :");

    scanf("%lf",&new_discount);

    double profit1=price*(new_discount-cost_discount);

    if(profit1>=0)
    printf("前数量= %.2lf 数量.\n",profit/profit1);
    else
    printf("亏本.\n");
    system("pause");
    return 0;
}
2014-09-20 17:21
vvvcuu
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:12
帖 子:353
专家分:1253
注 册:2014-4-22
收藏
得分:0 
printf("前数量= %d 数量.\n",profit/profit1);


profit和profit1都是double型,输出却是“%d”,会产生误差。

代码测试环境:  WinXP+C-Free5.0.
2014-09-20 21:15
tlliqi
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:204
帖 子:15453
专家分:65956
注 册:2006-4-27
收藏
得分:0 
没错
2014-09-21 07:28
fogmaple
Rank: 4
来 自:江西省赣州市于都县
等 级:业余侠客
威 望:1
帖 子:119
专家分:233
注 册:2014-7-28
收藏
得分:0 
慢慢地来
2014-09-21 09:59
lanskyxti
Rank: 1
等 级:新手上路
帖 子:14
专家分:4
注 册:2014-8-29
收藏
得分:0 
总结
总结:printf函数有些编译器不支持变量计算,输出的只是变量的值。
2014-09-24 16:32
embed_xuel
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:58
帖 子:3845
专家分:11385
注 册:2011-9-13
收藏
得分:0 
回复 6 楼 lanskyxti
懒得说了,祝楼主顺利

总有那身价贱的人给作业贴回复完整的代码
2014-09-24 16:47
YUANMINGMIN
Rank: 2
等 级:论坛游民
帖 子:20
专家分:10
注 册:2014-9-21
收藏
得分:0 
使用浮点型double(双精度)输入输出时应该使用%lf
%d是整数型int
2014-10-10 09:15
快速回复:困扰了好几天的 scanf 输入函数应用
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029193 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved