|
查看: 886|回复: 2
|
[pro]C++ programming学生请进,有事请教..
[复制链接]
|
|
|
那样的,我们老师有给我们lab tutorial,
也就是assignment来的..不过给那老师教到蒙差差..=.=
简单的我会,但一来到有点复杂的我又乱了.
又没书给我们参考,只给我们website..就说那个是我们的note了..
This is the question;
Write a cashier program that allows the cashier to enter the price and quantity of items and then display the total amount that needs to be paid.
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<conio.h>
#define result
using namespace std;
void main(){
int quantity;
double price;
cin >> price;
cout << "Price per unit is " << price << endl;
cout << "Enter the quantity" << endl;
cin >> quantity;
cout << "The price is " << price * quantity << endl;
cout << "Adding 5% tax.." << price * quantity * 1.05 << endl;
_getch();
}
就只剩round down and round up那边不会做..
谁可以教教我.. |
|
|
|
|
|
|
|
|
|
|
发表于 1-7-2009 11:33 AM
|
显示全部楼层
两个方法。
1. calculate时, 用cent来做。 最后答案除100。
2。 用c 的standard function, . floor(Price); 或 ceil( Price); 要加 #include <math.h>
Details, 来这边消化吧。
http://stackoverflow.com/questions/485525/round-for-float-in-c
你得code好像有问题。
cin >> price; 要放在 cout << "Enter the quantity" << endl;之后。
要不然,他会先等你的输入然后才 print statement.
Declare多一个 double Taxed_Price;
Unit price 和 after taxed price, 用不同的名。 以后的code比较容易写下去。
祝你好运。
[ 本帖最后由 lynessmy 于 1-7-2009 11:37 AM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 1-7-2009 12:24 PM
|
显示全部楼层
给solution
http://www.cplusplus.com/forum/articles/3638/
//WK Lee@MMU, Jul 07, 2009
#include <iostream>
#include <conio.h>
#include <cmath>
#include <iomanip>
#define result //???
using namespace std;
double round(double rdval)
{
rdval = floor((rdval * 100) + 0.5) / 10;
double b = floor(rdval);
double c = (rdval - b) * 10;
double d = 0.00;
//can replaced with switch also
if ((c == 1) || (c == 1))
{
d = 0.00;
}
else if ((c == 3) || (c == 4) || (c == 5) || (c == 6) || (c == 7))
{
d = 0.50;
}
else if ((c == 8) || (c == 9))
{
d = 1.00;
}
rdval = (b + d) / 10;
return rdval;
}
int main()
{
int quantity;
double price;
cout << "Enter unit price : RM";
cin >> price;
cout << "Enter quantity : ";
cin >> quantity;
cout << "Total price before taxed : RM"
<< price * quantity << endl;
cout << "Total after 5% tax : RM"
<< price * quantity * 1.05 << endl;
//Not confirmed work.
double dtotal = price * quantity * 1.05;
double itotal = floor(dtotal);
double flval = dtotal - itotal;
dtotal = itotal + round(flval);
cout << "Round value : RM"
<< setprecision(2) << fixed << dtotal << endl;
system("PAUSE");
return 0;
}
please think more and more about the logic.
solution provided just as refrences.
[ 本帖最后由 金旦面 于 1-7-2009 01:07 PM 编辑 ] |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|