|
查看: 1053|回复: 18
|
c++ programming高手请进,帮我看下是么问题..
[复制链接]
|
|
|
这个是问题..
Write a cashier program that allows the cashier to enter the price andquantity of items and then display the total amount that needs to bepaid.
In addition your cashier program needs to support the following features:
- Charge 5% tax to the total
- Round off cents based on the 1 cent rounding mechanism introduced by the government which is calculated after charging tax
- 1, 2, 6 and 7 rounded down to the nearest multiple of 5 cents
- 3, 4, 8 and 9 rounded up to the nearest multiple of 5 cents
这是做到的..
#include<iostream>
#include <math.h>
#include<conio.h>
using namespace std;
void main()
{
int decimalPoint2;
int quantity;
double price,intPart1,intPart2, tempTotal,decimalPoint1;
double total,roundTotal;
double decimal, afterTax;
cout << "Enter the unit price" << endl;
cin >> price;
cout << "The price is " << price << endl;
cout << "Enter quantity" << endl;
cin >> quantity;
total = price * quantity;
cout << "The price is " << price * quantity << endl;
afterTax = total * 105;
decimal = modf(afterTax, &intPart1);
if(decimal>=0.5){
afterTax = ceil(afterTax);
}else{
afterTax = floor(afterTax);
}
afterTax = afterTax/100;
cout << "Total after 5% tax is " << afterTax << endl;
tempTotal = afterTax * 10;
decimalPoint1 = modf(tempTotal, &intPart2);
decimalPoint2 = int(decimalPoint1 * 10);
cout << "The decimalPoint1 is " << decimalPoint1 << endl;
cout << "The decimalPoint2 is " << decimalPoint1 * 10 << endl;
switch(decimalPoint2){
case 0:case 5:
cout << "The round total is " << afterTax << endl;
break;
case 1:case 6:
afterTax = afterTax - 0.01;
cout << "The round total is " << afterTax << endl;
break;
case 2:case 7:
afterTax = afterTax - 0.02;
cout << "The round total is " << afterTax << endl;
break;
case 3:case 8:
afterTax = afterTax + 0.02;
cout << "The round total is " << afterTax << endl;
break;
case 4:case 9:
afterTax = afterTax + 0.01;
cout << "The round total is " << afterTax << endl;
break;
}
_getch();
}
但..有一些问题..
就是有写不会round..
你们试下price是9.7,
quantity是38,
price aftertax is 387.03,
it should rounds to 387.0,
但是却round to 387.01,么问题?
quantiti is 39也有这样的问题...
起初我以为是在1那边有问题..不过..
when price is 9.7, quantity is 33..
price aftertax is 336.11, it rounds to 336.1 <-- this one no problem..
怎么会那样..问题出在那里? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 20-7-2009 04:55 PM
|
显示全部楼层
另一个..
也是一样的问题,
只是用if else..
#include<iostream>
#include <math.h>
#include<conio.h>
using namespace std;
void main()
{
int decimalPoint2;
int quantity;
double price,intPart1,intPart2, tempTotal,decimalPoint1;
double total,roundTotal;
double decimal, afterTax;
cout << "Enter the unit price" << endl;
cin >> price;
cout << "The price is " << price << endl;
cout << "Enter quantity" << endl;
cin >> quantity;
total = price * quantity;
cout << "The price is " << price * quantity << endl;
afterTax = total * 105;
decimal = modf(afterTax, &intPart1);
if(decimal>=0.5){
afterTax = ceil(afterTax);
}else{
afterTax = floor(afterTax);
}
afterTax = afterTax/100;
cout << "Total after 5% tax is " << afterTax << endl;
tempTotal = afterTax * 10;
decimalPoint1 = modf(tempTotal, &intPart2);
decimalPoint1 = decimalPoint1 * 10;
if((decimalPoint1==0)||(decimalPoint1==5)){
cout << "The round total is " << afterTax << endl;
}else{
if((decimalPoint1==1)||(decimalPoint1==6)){
afterTax = afterTax - 0.01;
cout << "The round total is " << afterTax << endl;
}
else{
if((decimalPoint1==2)||(decimalPoint1==7)){
afterTax = afterTax = afterTax - 0.02;
cout << "The round total is " << afterTax << endl;
}
else{
if((decimalPoint1==3)||(decimalPoint1==8)){
afterTax = afterTax + 0.02;
cout << "The round total is " << afterTax << endl;
}
else
{
if((decimalPoint1==4)||(decimalPoint1==9)){
afterTax = afterTax + 0.01;
cout << "The round total is " << afterTax << endl;
}
}
}
}
}
_getch();
}
if else part不work..是么问题?
Thanks for reply 1st..
[ 本帖最后由 emotion 于 20-7-2009 04:58 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 20-7-2009 05:42 PM
|
显示全部楼层
回复 1# emotion 的帖子
把这个
afterTax = total * 105
改成
afterTax = total * 1.05
看清楚,有个点在一之后!然后删掉这句
afterTax = afterTax/100;
OK LOR! |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 20-7-2009 08:39 PM
|
显示全部楼层
原帖由 eyong 于 20-7-2009 05:42 PM 发表 
把这个
afterTax = total * 105
改成
afterTax = total * 1.05
看清楚,有个点在一之后!然后删掉这句
afterTax = afterTax/100;
OK LOR!
还没换,price after tax is 346.29, round = 346.3
换了就没点位数了... |
|
|
|
|
|
|
|
|
|
|
发表于 21-7-2009 10:07 AM
|
显示全部楼层
嘿,对不起,我有点混肴了!想问看你387.03是不是该round去387.05而不是387.00呢?
[ 本帖最后由 eyong 于 21-7-2009 10:10 AM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 21-7-2009 11:44 AM
|
显示全部楼层
原帖由 emotion 于 20-7-2009 04:53 PM 发表 
这个是问题..
Write a cashier program that allows the cashier to enter the price andquantity of items and then display the total amount that needs to bepaid.
In addition your cashier program needs to ...
price = 9.7, quantity = 38的时候, 在swithch那里时, 你的decimalPoint2是2, 会跑去case 2:case 7:那里, 所以你的答案是387.01 |
|
|
|
|
|
|
|
|
|
|
发表于 21-7-2009 12:30 PM
|
显示全部楼层
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #include <stdio.h>
- #include <conio.h>
- #include <math.h>
- #pragma hdrstop
- //---------------------------------------------------------------------------
- #pragma argsused
- float rounding(float value);
- int main(int argc, char* argv[])
- {
- float value = 0;
- clrscr();
- printf("Please enter amount: ");
- scanf("%f", &value);
- value *= 1.05;
- printf("\nAmmount after 5%% Tax: %.2f", value);
- printf("\nFinal amount = %.2f", rounding(value));
- getch();
- return 0;
- }
- //---------------------------------------------------------------------------
- float rounding(float value)
- {
- double ipart;
- double fpart = modf(value, &ipart);
- int cents = fpart * 100; //conver cents to integer
- int cent10 = cents/10; //10x cents part, i.e. 25cents, this will be 2
- int cent1 = cents % 10; //single cents part
- float round_value = 0;
- switch(cent1)
- {
- case 1:
- case 2: round_value = 0;
- break;
- case 3:
- case 4: round_value = 0.05;
- break;
- case 6:
- case 7: round_value = 0.05;
- break;
- case 8:
- case 9: round_value = 0.1;
- break;
- default: round_value = (float)cents / 100;
- }
- float final_value = ipart + (((float)cent10)/10 + round_value);
- return final_value;
- }
- //---------------------------------------------------------------------------
复制代码 以上是我用C++ Builder,以C语言写的,要转换成C++并不难,不过因为我没学过cin cout 之类的,所以就直接用C写了。
原本我是不想把整个program写出来的,因为这只会害了你(如果你真的照抄拿去交差的话,你是学不到东西的),但是整个program实在太短了,加上如果不把前因后果写出来,单单只是一个rounding()可能会让你看得满头雾水,所以就整个放上来了。
请记着,这只是供你参考罢了,别照抄(因为照抄的话你的导师肯定会看到满头大汗,看不懂 )。
编辑:发现了一个bug,case 8、case 9 应该是 0.1(一毛钱)。特此修改。
[ 本帖最后由 geekman 于 21-7-2009 10:06 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 21-7-2009 08:55 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 21-7-2009 08:58 PM
|
显示全部楼层
原帖由 糯米鸡 于 21-7-2009 11:44 AM 发表 
price = 9.7, quantity = 38的时候, 在swithch那里时, 你的decimalPoint2是2, 会跑去case 2:case 7:那里, 所以你的答案是387.01
那么问题是出在switch那边?
那么我应该怎样改?
请指教..
谢谢... |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 21-7-2009 09:00 PM
|
显示全部楼层
原帖由 geekman 于 21-7-2009 12:30 PM 发表 
//---------------------------------------------------------------------------
#include
#include
#include
#include
#pragma hdrstop
//------------------------------------------------------ ...
谢谢指教,
等下我试着做.. |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 21-7-2009 09:01 PM
|
显示全部楼层
原帖由 geekman 于 21-7-2009 12:30 PM 发表 
//---------------------------------------------------------------------------
#include
#include
#include
#include
#pragma hdrstop
//------------------------------------------------------ ...
谢谢指教,
等下我试着做.. |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 21-7-2009 09:03 PM
|
显示全部楼层
原帖由 geekman 于 21-7-2009 12:30 PM 发表 
//---------------------------------------------------------------------------
#include
#include
#include
#include
#pragma hdrstop
//------------------------------------------------------ ...
谢谢指教,
等下我试着做.. |
|
|
|
|
|
|
|
|
|
|
发表于 21-7-2009 10:55 PM
|
显示全部楼层
不是switch, 是你的convertion 问题
cout << "The decimalPoint1 is " << decimalPoint1 << endl;
cout << "The decimalPoint1 * 10 is " << decimalPoint1 * 10 << endl;
加入
cout << "The decimalPoint2 is " << decimalPoint2 << endl;
你就会明白了,
The decimalPoint1 is 0.3
The decimalPoint1 *10 is 3
The decimalPoint2 is 2
为什么会这样, 这个留给你思考
hint: 0.299999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
to int = 2 |
|
|
|
|
|
|
|
|
|
|
发表于 21-7-2009 10:56 PM
|
显示全部楼层
不是switch, 是你的convertion 问题
cout << "The decimalPoint1 is " << decimalPoint1 << endl;
cout << "The decimalPoint1 * 10 is " << decimalPoint1 * 10 << endl;
加入
cout << "The decimalPoint2 is " << decimalPoint2 << endl;
你就会明白了,
The decimalPoint1 is 0.3
The decimalPoint1 *10 is 3
The decimalPoint2 is 2
为什么会这样, 这个留给你思考
hint: 0.299999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
to int = 2 |
|
|
|
|
|
|
|
|
|
|
发表于 22-7-2009 10:05 PM
|
显示全部楼层
不是switch, 是你的convertion 问题
cout << "The decimalPoint1 is " << decimalPoint1 << endl;
cout << "The decimalPoint1 * 10 is " << decimalPoint1 * 10 << endl;
加入
cout << "The decimalPoint2 is " << decimalPoint2 << endl;
你就会明白了,
The decimalPoint1 is 0.3
The decimalPoint1 *10 is 3
The decimalPoint2 is 2
为什么会这样, 这个留给你思考
hint: 0.299999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
to int = 2 |
|
|
|
|
|
|
|
|
|
|
发表于 28-7-2009 02:37 AM
|
显示全部楼层
不好意识,lz借用你的贴
#include <iostream>
using namespace std;
#include <strcpy>;
float calculatetotalamount(int,float,float&)
struct customer;
void display()
{
char name[50];
char IC[50];
int baggage;
int ticket;
float kg;
float totalamount;
char origin;
};
int main
{
cout<<"***********************************"<<endl;
cout<<"************ AIRASIA ************"<<endl;
cout<<"********** Kuala Lumpur **********"<<endl;
cout<<"***********************************"<<endl;
cout<<"Please enter your name: ";
cin>>c.name.50;
cout<<"Enter ur IC: ";
cin>>c.IC.50;
cout<<"origin location (KUALA LUMPURL=1 , JOHOR BAHRU=2 , PULAU PINANG=3)";
cin>>origin;
}
switch ( origin)
case '1'
cout<<"|-------------------------|-------------------------|"<<endl;
cout<<"| KL-JB | KL-KotaKinabalu |"<<endl;
cout<<"| KL-PP | KL- |"<<endl;
cout<<"| KL-Sabah | KL- |"<<endl;
cout<<"| KL-Subang | KL- |"<<endl;
cout<<"| KL-Kedah | KL- |"<<endl;
cout<<"|-------------------------|-------------------------|"<<endl;
cout<<"enter the destination"
cin>>origin;
if (origin ==JB)
price==150
else if (grade ==pp)
price==100
else
price ==90
//不懂在switch里面加if else 可以吗?
break;
case '2'
cout<<"|-------------------------|-------------------------|"<<endl;
cout<<"| KL-JB | KL-KotaKinabalu |"<<endl;
cout<<"| KL-PP | KL- |"<<endl;
cout<<"| KL-Sabah | KL- |"<<endl;
cout<<"| KL-Subang | KL- |"<<endl;
cout<<"| KL-Kedah | KL- |"<<endl;
cout<<"|-------------------------|-------------------------|"<<endl;
cout<<"enter the destination"
break;
case '3'
cout<<"|-------------------------|-------------------------|"<<endl;
cout<<"| KL-JB | KL-KotaKinabalu |"<<endl;
cout<<"| KL-PP | KL- |"<<endl;
cout<<"| KL-Sabah | KL- |"<<endl;
cout<<"| KL-Subang | KL- |"<<endl;
cout<<"| KL-Kedah | KL- |"<<endl;
cout<<"|-------------------------|-------------------------|"<<endl;
cout<<"enter the destination"
cin>>price
break;
default:cout<<"Invalid number";
break;
}
虽然只是一个error,但我觉得结构错到很离谱了。。
有谁可以帮忙修改吗
还有我想加入日期选泽,要怎样写?
[ 本帖最后由 happinessss 于 28-7-2009 02:43 AM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 28-7-2009 03:07 PM
|
显示全部楼层
struct customer;
void display()
{
char name[50];
char IC[50];
int baggage;
int ticket;
float kg;
float totalamount;
char origin;
};
?????? |
|
|
|
|
|
|
|
|
|
|
发表于 29-7-2009 08:37 PM
|
显示全部楼层
原帖由 happinessss 于 28-7-2009 02:37 AM 发表 
虽然只是一个error,但我觉得结构错到很离谱了。。
有谁可以帮忙修改吗
还有我想加入日期选泽,要怎样写?
...
1。)编程是不要只为了能compile而修改
2。) 你连要编写怎样的东西都没告诉人,叫人怎么帮?(每次遇到这种叫你答应帮他后才讲出东西的人都翻白眼) |
|
|
|
|
|
|
|
|
|
|
发表于 30-7-2009 12:05 AM
|
显示全部楼层
原帖由 yeenfei 于 29-7-2009 08:37 PM 发表 1。)编程是不要只为了能compile而修改2。) 你连要编写怎样的东西都没告诉人,叫人怎么帮?(每次遇到这种叫你答应帮他后才讲出东西的人都翻白眼)
yenfei 兄, 不要發火啦, 很多人都不是喜歡 programming 才讀 IT 的  |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|