首页 > 代码库 > thoughtworksd一道编程题
thoughtworksd一道编程题
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
商品税的计算:
国内商品除食物、书籍、药品外均收取10%的基本税收
进口商品要收取除基本税以外的关税
计算的税收以0.05为单位,进行入整
输入1:
一本书 12.49
一本音乐CD 14.99
一条巧克力棒0.85
输出1:
一本书:12.49
一本音乐CD 14.99+14.99*0.1 = 14.99 +1.499 = 14.99+1.5=16.49
一条巧克力棒 0.85
税收:1.5
总价钱:12.49+16.49+0.85=29.83
输入2:
一盒进口巧克力:10
一瓶进口香水:47.50
输出2:
一盒进口巧克力:10+10*0.05=10.5
一瓶进口香水:47.5+47.5*0.15=47.5+7.125=47.5+7.15=54.65
税收:0.5+7.15=7.65
总价:10.5+54.65=65.15
输入3:
一瓶进口香水:27.99
一瓶香水:18.99
一盒感冒药:9.75
一盒进口巧克力:11.25
输出3:
一瓶进口香水:27.99+27.99*0.15=27.99+4.1985=27.99+4.20=32.19
一瓶香水:18.99+18.99*0.1=18.99+1.899=18.99+1.90=20.89
一盒感冒药:9.75
一盒进口巧克力:11.25+11.25*0.05=11.25+0.5625=11.25+0.60=11.85
税收:4.20+1.90+0.60=6.70
总价:74.68
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
实现方法1:
/**********************************************
*文件名:sales_taxes.cpp
*作者:
*联系方式:
*功能说明:
*上次修改:
**********************************************/
#include<iostream>
#define N 10 //暂定结构体数组的长度为10,可根据实际情况设置
#define JM 0.05 //进口单不收税
#define JB 0.15 //进口且收税
#define BB 0.10 //非井口不免费
typedef struct goods{ //存储商品属性的结构体
double price;
bool is_import;
bool is_free;
}goods;
double two_point(double num);
void sales_taxes_func(goods shoping[N],double& taxes,double& total,int n );
void output_info(double taxes,double total);
void input_info();
int main(void)
{
using namespace std;
goods shoping[N];
input_info();
int n=0; //实现数据的输入,因为价格不能为0,以0为结束标志
{
int cycle=1;
double buffer;
while(cycle){
cin>>buffer;
if(buffer!=0){
shoping[n].price=buffer;
cin>>shoping[n].is_import
>>shoping[n].is_free;
n++;
}
else cycle=0;
}
}
double taxes=0;
double total=0;
sales_taxes_func(shoping,taxes,total,n);
output_info(taxes,total);
system("pause");
return 0;
}
//precisiong two points
double two_point(double num){
double result=0.0;
int temp=static_cast<int>(num*100); //强制把double转换为int,实现向下取整
int flag=temp%10;
if(0==flag){
result=temp/100.0;
}else if(flag>=5){
temp+=(10-flag);
result=temp/100.0;
}else{
temp+=(5-flag);
result=temp/100.0;
}
return result;
}
//sales taxes count
void sales_taxes_func(goods shoping[N],double& taxes,double& total,int n ){
for(int i=0;i<n;i++){
if(shoping[i].is_free){
if(!shoping[i].is_import){
total+=shoping[i].price;
}else{
double temp=0.0;
temp=two_point(shoping[i].price*JM);
taxes+=temp;
total+=shoping[i].price+temp;
}
}else{
if(!shoping[i].is_import){
double temp=0.0;
temp=two_point(shoping[i].price*BB);
taxes+=temp;
total+=shoping[i].price+temp;
}else{
double temp=0.0;
temp=two_point(shoping[i].price*JB);
taxes+=temp;
total+=shoping[i].price+temp;
}
}
}
return;
}
//result output
void output_info(double taxes,double total){
using namespace std;
cout<<endl;
cout<<"---------------------------result------------------------------\n";
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout<<"taxes: "<<taxes<<endl
<<"total: "<<total<<endl<<endl<<endl;
return;
}
//input infomation
void input_info(){
using namespace std;
cout<<"--------------------------help info-----------------------------\n"
<<"price is two points.\n"
<<"inport is true meaning inport;false is meaning export.\n"
<<"free is true meaing free;false is meaing not free.\n"
<<"-------------------------input info-----------------------------\n"
<<"please input goods(interupt table key,0 is end):\n"
<<"price\t"
<<"import\t"
<<"free\t"
<<endl;
return;
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
实现方法2:
/**********************************************
*document name:sales_taxes.cpp
*author:wuyongxian
*contact information:the e-mail is 344706612@qq.com and the telephone number is 15191433756
*the function specification:it is able to print out the details which are the total prices and the sales taxes of the shopping goods.
**********************************************/
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;
#define JM 0.05 // rate of the imported taxes
#define JB 0.15 // rate of the imported taxes and the basic taxes
#define BB 0.10 // rate of the basic taxes
typedef struct goods{ //the structure stores the attribute of the goods
int count;
char *name;
double price;
bool is_import;
bool is_free;
struct goods *next;
}goods;
double rounding(double num);
void sales_taxes_func(goods *head,double& taxes,double& total );
void print(goods *head);
goods *input_goodsinformation();
int main()
{
goods *q;
double taxes=0.0;
double total=0.0;
q=input_goodsinformation();
sales_taxes_func(q,taxes,total);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
print(q);
cout<<"Sales Taxes:"<<taxes<<endl;
cout<<"Total:"<<total<<endl;
system("pause");
return 0;
}
/***************input goods information**********/
goods *input_goodsinformation()
{
goods *p,*s,*head;
int count;
char *name;
double price;
bool import;
bool free;
int cycle=1;
head=(goods*)malloc(sizeof(goods));
p=head;
while(cycle)
{
cout<<"please input count(0 end)\n"<<endl;
name=(char*)malloc(100);
cin >> count;
if(count)
{
cout<<"please input name (string)\n"<<endl;
cin >> name;
cout<<"please input price(double)\n"<<endl;
cin>>price;
cout<<"please input name import(0-made in country,1-imported)\n"<<endl;
cin>>import;
cout<<"please input free(1-exempt,0- nonexempt)\n"<<endl;
cin>>free;
s=(goods*)malloc(sizeof(goods));
s->count=count;
s->name=name;
s->price=price;
s->is_import=import;
s->is_free=free;
p->next=s;
p=s;
}
else
cycle=0;
}
head=head->next;
p->next=NULL;
return head;
}
/************output goods list**************************/
void print(goods *head)
{
goods *p;
cout<<"*********List of goods **********"<<endl;
p=head;
if(head!=NULL)
while(p!=NULL)
{
cout<<p->count<<" "<<p->name<<":"<<p->price<<endl;
p=p->next;
}
}
/*************precisiong two points*******************/
double rounding(double num){
double result=0.0;
int temp=static_cast<int>(num*100);
int flag=temp%10;
if(0==flag){
result=temp/100.0;
}else if(flag>=5){
temp+=(10-flag);
result=temp/100.0;
}else{
temp+=(5-flag);
result=temp/100.0;
}
return result;//make the price of taxes rounding up to the nearest 0.05
}
/****************Calculation of commodity price and the total tax revenues***********/
void sales_taxes_func(goods *head,double& taxes,double& total )
{
goods *p;
p=head;
double temp1=0.0;
double temp2=0.0;
double temp3=0.0;
if(head!=NULL)
{
while(p!=NULL)
{
if(p->is_free)
{
if(!p->is_import)
{
total+=p->price*p->count;
p=p->next;
}
else
{
temp1=rounding(p->price*JM);
taxes+=temp1*p->count;
total+=p->price*p->count+temp1*p->count;
p=p->next;
}
}
else
{
if(!p->is_import)
{
temp2=rounding(p->price*BB);
taxes+=temp2*p->count;
total+=p->price*p->count+temp2*p->count;
p=p->next;
}
else
{
temp3=rounding(p->price*JB);
taxes+=temp3*p->count;
total+=p->price*p->count+temp3*p->count;
p=p->next;
}
}
}
}
return;
}
thoughtworksd一道编程题