forked from Dashark/hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnits.cpp
243 lines (223 loc) · 4.93 KB
/
Units.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//单位转换程序,输入值和原单位,新单位,返回新单位下的值。
//重量单位:Kg, g, lb, gr,......
//压力单位:Pa, MPa, psi,......
//长度单位:m, km, ft, in,......
//其它单位:md, cp,......
//还有很多单位转换,有些单位是其它单位组合出来的
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
//定义一个总的单位类
class Danwei{
public:
double chuzhi;
string firstdanwei;
string seconddanwei;
Danwei(double n,string first, string second)
{
chuzhi = n;
firstdanwei = first;
seconddanwei = second;
}
double get_chuzhi()
{
return chuzhi;
}
string get_first()
{
return firstdanwei;
}
string get_second()
{
return seconddanwei;
}
};
//定义一个单位为长度的类
class Length : public Danwei
{
public:
Length(double n,string first, string second):Danwei(n,first,second) {}
double to_middle()
{
if(firstdanwei == "m")
return chuzhi;
else if(firstdanwei == "km")
return chuzhi*pow(10,3);
else if(firstdanwei == "dm")
return chuzhi*pow(10,-1);
else if(firstdanwei == "cm")
return chuzhi*pow(10,-2);
else if(firstdanwei == "mm")
return chuzhi*pow(10,-3);
else if(firstdanwei == "nm")
return chuzhi*pow(10,-9);
}
double to_second()
{
string s = seconddanwei;
if(s == "m")
return to_middle();
else if(s == "km")
return to_middle()*pow(10,-3);
else if(s == "dm")
return to_middle()*pow(10,1);
else if(s == "cm")
return to_middle()*pow(10,2);
else if(s == "mm")
return to_middle()*pow(10,3);
else if(s == "nm")
return to_middle()*pow(10,9);
}
void length_out()
{
cout << to_second() << " " << seconddanwei << endl;
}
};
//定义一个单位为质量的类
class Mass : public Danwei
{
public:
Mass(double n,string first,string second):Danwei(n,first,second){}
double to_middle()
{
if(firstdanwei == "g")
return chuzhi;
else if(firstdanwei == "kg")
return chuzhi*pow(10,3);
else if(firstdanwei == "mg")
return chuzhi*pow(10,-3);
else if(firstdanwei == "t")
return chuzhi*pow(10,9);
}
double to_second()
{
string s = seconddanwei;
if(s == "m")
return to_middle();
else if(s == "kg")
return to_middle()*pow(10,-3);
else if(s == "mg")
return to_middle()*pow(10,3);
else if(s == "t")
return to_middle()*pow(10,-9);
}
void mass_out()
{
cout << to_second() << " " << seconddanwei << endl;
}
};
//定义一个单位为时间的类
class Time : public Danwei
{
public:
Time(double n,string first,string second):Danwei(n,first,second){}
double to_middle()
{
if(firstdanwei == "second")
return chuzhi;
else if(firstdanwei == "minute")
return chuzhi*60;
else if(firstdanwei == "hour")
return chuzhi*60*60;
else if(firstdanwei == "day")
return chuzhi*60*60*24;
else if(firstdanwei == "month")
return chuzhi*60*60*24*30;
else if(firstdanwei == "year")
return chuzhi*60*60*24*30*12;
}
double to_second()
{
string s = seconddanwei;
if(s == "second")
return to_middle();
else if(s == "minute")
return to_middle()/60;
else if(s == "hour")
return to_middle()/60/60;
else if(s == "day")
return to_middle()/60/60/24;
else if(s == "month")
return to_middle()/60/60/24/30;
else if(s == "year")
return to_middle()/60/60/24/30/12;
}
void time_out()
{
cout << to_second() << " " << seconddanwei << endl;
}
};
//判断是不是长度单位
bool is_length(string s)
{
if(s == "m" || s == "km" || s == "dm" || s == "cm" || s == "mm" || s == "nm")
return true;
else
return false;
}
//判断是不是质量单位
bool is_mass(string s)
{
if(s == "g" || s == "kg" || s == "mg" || s == "t")
return true;
else
return false;
}
//判断是不是时间单位
bool is_time(string s)
{
if(s == "second" || s == "minute" || s == "hour" || s == "day" || s == "month" || s == "year")
return true;
else
return false;
}
/*判断输入的单位是属于哪种单位
0表示长度单位;1表示重量单位;2表示时间单位;*/
int judge(Danwei d)
{
if(is_length(d.firstdanwei))
return 0;
else if(is_mass(d.firstdanwei))
return 1;
else if(is_time(d.firstdanwei))
return 2;
}
int main()
{
double chuzhi;
string firstdanwei;
string seconddanwei;
cout << "请输入初值:" << endl;
cin >> chuzhi;
cout << "请输入初始单位:" << endl;
cin >> firstdanwei;
cout << "请输入转化后的单位:" << endl;
cin >> seconddanwei;
cout << "转化后的值为:" << endl;
Danwei d1(chuzhi,firstdanwei,seconddanwei);
switch (judge(d1))
{
case 0:
{
Length l1(chuzhi,firstdanwei,seconddanwei);
l1.length_out();
break;
}
case 1:
{
Mass m1(chuzhi,firstdanwei,seconddanwei);
m1.mass_out();
break;
}
case 2:
{
Time t1(chuzhi,firstdanwei,seconddanwei);
t1.time_out();
break;
}
default:
break;
}
return 0;
}