| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 588 人关注过本帖
标题:为什么我编写的程序计算结果会不精确?
取消只看楼主 加入收藏
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
结帖率:71.88%
收藏
已结贴  问题点数:10 回复次数:2 
为什么我编写的程序计算结果会不精确?
我编写的c语言程序设计函数和指针的结合运算,为什么本来结果是1000的为什么会显示999.98呢?
搜索更多相关主题的帖子: c语言程序 
2013-02-28 08:31
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
收藏
得分:0 
要求为一个自行车的网购编写一个c语言程序,具体是:
有四种自行车:Hybrid-MT,Race,Mountain,Touring,分别用H R M T代替,基准价均为1000元。每种自行车的车轮尺寸是22-36,如果尺寸大于22,每多一寸,多收100元。
如果顾客选购 H和T,问他们是否要换舒适的座椅,此项收费为50元。如果选购R、M或T,问顾客是否要升级装备,此项收费是300元。如果购买金额大于2000元,有5%的折扣。税率为13%。
最后还要求显示一天的营业额,总售出自行车数量和价值。
输入:
CROWN BICYCLE CORPORATION – Manager XXX
BIKE MODELS – CUSTOM MADE ORDER, Hybrid-MT,Race,Mountain,Touring,Select (H, R, M or T only): Credit Card (last 6 numbers): 999999
Choose a wheel size between 22 inch and 36 inch:
输出:
CROWN BICYCLE CORPORATION    DATE: yyyy/mm/dd
TOTAL PRICE FOR BIKE:   
DISCOUNT:     (只有总共价值超过2000元才显示)
TAX (13% GST):
TOTAL ORDER:
每日总结报告:
SUMMARY for yyyy/mm/dd
Total number of bikes sold:
Total $ value of bikes sold:
要求:
1、输入输出要使用函数和指针
2、加一个循环,顾客订单结束后询问是否继续下一个订单
#include<stdio.h>
/*The purpose of this program is
to perform calculations with functions and pointers.*/
void get_inputs(double *htotal, double *rtotal, double *mtotal, double *ttotal);
main()
{
int hqty, rqty, mqty, tqty, hwsize, rwsize, mwsize, twsize, total_sold;
double hseats, tseats, rupgrade, mupgrade, tupgrade;
double htotal, rtotal, mtotal, ttotal, total, discount, tax, total_order, total_value;
int ccno;
char yn='Y';

while (yn=='Y'||yn=='y')
{
printf("\nCROWN BICYCLE CORPORATION-Manager");
printf("\nCROWN BICYCLE CORPORATION    DATE: 2013/03/08");

get_inputs(&htotal, &rtotal, &mtotal, &ttotal);
total=htotal+rtotal+mtotal+ttotal;
total_sold=hqty+rqty+mqty+tqty;
total_value+=total;
if (total>2000)
{discount=total*0.05;
tax=(total-discount)*0.13;
total_order=total-discount+tax;
printf("\nTOTAL PRICE FOR BIKE %10.2lf$\n", total);
printf("\nDISCOUNT %10.2lf$", discount);
printf("\nTAX (GST 13%) %10.2lf$", tax);
printf("\nTOTAL ORDER %10.2lf$", total_order);
}
else
{
printf("\nTOTAL PRICE FOR BIKE %10.2lf$", total);
printf("\nTAX (GST 13%) %10.2lf$", total*0.13 );
printf("\nTOTAL ORDER %10.2lf$", total*1.13);
}
printf( "\nWould you like to take another order? Y/N\n ");
scanf (" %c", &yn);
if(getchar()=='n')
break;
}
printf("\nPlease input your Credit Card number here to do transaction(last 6 numbers):");
scanf("%6d", &ccno);
printf("\nYour order has completed. Thank you for your shopping at CROWN BICYCLE CORPORATION. Goodbye!\n");


printf("\nSUMMARY for 2013/03/08");
printf("\nTotal number of bikes sold: %d", total_sold);
printf("\nTotal value of bikes sold:%10.2lf\n", total_value);
}

void get_inputs(double *htotal, double *rtotal, double *mtotal, double *ttotal)
{
int hqty, rqty, mqty, tqty, hwsize, rwsize, mwsize, twsize;
char yn='Y', choice, yn1, yn2, yn3, yn4, yn5;
while (yn=='Y'||yn=='y')
{
printf("\nBIKE MODELS-CUSTOM MADE ORDER");
printf("\nH for Hybrid-MT");
printf("\nR for Race");
printf("\nM for Mountain");
printf("\nT for Touring");
printf("\nSelect (H, R, M or T only):");
scanf(" %c", &choice);

    switch (choice)
    {
    case ('H'):
    printf("\nYour Order for H, please input the number you want to purchase:\n");
    scanf("%d", &hqty);
    printf("\nChoose a wheel size between 22 inch and 36 inch:\n");
    scanf("%d", &hwsize);
    printf("\nDo you want the more comfortable seats? This charge is $50. Please enter your choice? yn1\n");
    scanf("%c", &yn1);
    if (yn1=='Y'||yn1=='y')
    *htotal=hqty*1000.00+(hwsize-22)*100.00+50.00;
    else
    *htotal=hqty*1000.00+(hwsize-22)*100.00;
    break;

    case ('R'):
    printf("\n Your Order for R, please input the number you want to purchase:\n");
    scanf("%d", &rqty);
    printf("\nChoose a wheel size between 22 inch and 36 inch:\n");
    scanf("%d", &rwsize);
    printf("\nDo you want the upgraded custom gears? This charge is $300. Please enter your choice? yn2\n");
    scanf(" %c", &yn2);
    if (yn2=='Y'||yn2=='y')
    *rtotal=rqty*1000.00+(rwsize-22)*100.00+300.00;
    else
    *rtotal=rqty*1000.00+(rwsize-22)*50.00;
    break;
   
    case ('M'):
    printf("\n Your Order for M, please input the number you want to purchase:\n");
    scanf("%d", &mqty);
    printf("\nChoose a wheel size between 22 inch and 36 inch:\n");
    scanf("%d", &mwsize);
    printf("\nDo you want the upgraded custom gears? This charge is $300. Please enter your choice? yn3\n");
    scanf(" %c", &yn3);
    if (yn3=='Y'||yn3=='y')
    *mtotal=mqty*1000.00+(mwsize-22)*100.00+300.00;
    else
   *mtotal=mqty*1000.00+(mwsize-22)*100.00;
    break;
   
    case ('T'):
    printf("\nYour Order for t, please input the number you want to purchase:\n");
    scanf("%d", &tqty);
    printf("\nChoose a wheel size between 22 inch and 36 inch:\n");
    scanf("%d", &twsize);
    printf("\nDo you want the more comfortable seats? This charge is $50. Please enter your choice? yn4\n");
    scanf(" %c", &yn4);
    if (yn4=='Y'||yn4=='y')
    *ttotal=tqty*1000.00+(twsize-22)*100.00+50.00;
    else
    *ttotal=tqty*1000.00+(twsize-22)*50.00;
    printf("\nDo you want the upgraded custom gears? This charge is $300. Please enter your choice? yn5\n");
    scanf(" %c", yn5);
    if (yn5=='Y'||yn5=='y')
    *ttotal=*ttotal+300.00;
    else
    *ttotal=*ttotal;
    break;
    default: printf("Invalid choice, please try again.\n\n");
   }
getchar();
printf("\nWould you like to select other bike model? yn\n");
scanf (" %c", &yn);
if(getchar()=='n')
break;
}
}
2013-02-28 10:13
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
收藏
得分:0 
以下是引用wp231957在2013-2-28 10:15:44的发言:

贴过这个帖子的

一点排版都没有  还那么长  看着都头疼
版主,这边老师要求很明确,要指针来传递数据,结合函数,帮忙运行一下,看看问题出现在哪里?谢谢。
2013-02-28 10:33
快速回复:为什么我编写的程序计算结果会不精确?
数据加载中...
 
   



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

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