-
Notifications
You must be signed in to change notification settings - Fork 0
/
C++ 第五次实验Part2
128 lines (122 loc) · 2.67 KB
/
C++ 第五次实验Part2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <bits/stdc++.h>
using namespace std;
struct Rule
{
int m;
int r;
}rules[105];
void inputRules (Rule rules [],int n)
{
for(int i=1;i<=n;i++)
{
cout<<"请输入第"<<i<<"条规则:";
int m,r;
cin>>m>>r;
rules[i].m=m;
rules[i].r=r;
}
}
void inputmenu(Rule rules [],int n)
{
cout<<"纳税规则如下:"<<endl;
cout<<"纳税线\t税率"<<endl;
for(int i=1;i<=n;i++)
{
cout<<rules[i].m<<"\t"<<rules[i].r<<endl;
}
}
double computeTax(Rule rules[],int n,int income)
{
if(income <= rules[1].m )
{
//cout<<"再见";
return 0;
}
else if(income > rules[n].m)
{
double sum=0;
for(int i=1;i<n;i++)
{
sum+=(rules[i+1].m-rules[i].m)*rules[i].r;
}
sum+=(income - rules[n].m)*rules[n].r;
return sum/100;
}
else
{
int x=0;
double sum=0;
for(int i=1;i<=n;i++)
{
if(income <= rules[i+1].m && income >rules[i].m )
{
x=i;
sum+= (income - rules[i].m)*rules[i].r;
break;
}
}
for(int i=1;i<x;i++)
{
sum+=(rules[i+1].m-rules[i].m)*rules[i].r;
}
return sum/100;
}
}
void outputsum(Rule rules[],int n,int income)
{
if(income > rules[n].m)
{
if(n==1)
{
printf("(%d-%d)*%d/100\n",income,rules[1].m,rules[1].r);
}
else
{
printf("[(%d-%d)*%d)",income,rules[n].m,rules[n].r);
for(int i=n;i>1;i--)
printf("+((%d-%d)*%d)",rules[i].m,rules[i-1].m,rules[i-1].r );
printf("]/100\n");
}
}
else
{
int x=0;
double sum=0;
for(int i=1;i<=n;i++)
{
if(income <= rules[i].m && income >rules[i-1].m )
{
x=(i-1);
break;
}
}
printf("[(%d-%d)*%d)",income,rules[x].m,rules[x].r);
for(int i=x;i> 1;i--)
printf("+((%d-%d)*%d)",rules[i].m,rules[i-1].m,rules[i-1].r );
printf("]/100\n");
}
}
int main()
{
int n;
cout<<"请输入规则的条数:";
cin>>n;
inputRules(rules,n);
inputmenu(rules,n);
while(1)
{
int income;
cout<<"请输入您的收入:";
cin>>income;
if(income ==-1)
break;
double t=0;
t=computeTax(rules,n,income);
cout<<"您的收入是"<<income<<",";
printf(" 应交所得税:%0.2f元\n",t);
if(t!=0)
outputsum(rules,n,income);
}
cout<<"再见"<<endl;
return 0;
}