一 项目名称 :时间类
02.#include <iostream>
03.using namespace std;
04.class Time
05.{
06.public:
07. void set_time( );
08. void show_time( );
09. add_an_hour( ) //增加1小时
10. {
11.
12. hour++;
13. if(hour>24)
14. {
15. hour-=24;
16. }
17. }
18.
19. add_a_minute( ) //增加1分钟
20. {
21.
22. minute++;
23. if(minute>=60)
24. {
25. add_an_hour( );
26. minute=0;
27. }
28.
29. }
30. add_a_sec( )//增加1秒钟
31. {
32.
33. sec++;
34. if(sec>=60)
35. {
36. sec=0;
37. add_a_minute();
38. }
39. }
40.
41. int add_seconds(int); //增加n秒钟
42. int add_minutes(int); //增加n分钟
43. int add_hours(int);//增加n小时
44.private:
45. bool is_time(int, int, int); //这个成员函数设置为私有的,是合适的,请品味
46. int hour;
47. int minute;
48. int sec;
49.
50.};
51.void Time::set_time( )
52.{
53. char c1,c2;
54. cout<<"请输入时间(格式hh:mm:ss)";
55. while(1)
56. {
57. cin>>hour>>c1>>minute>>c2>>sec;
58. if(c1!=':'||c2!=':')
59. cout<<"格式不正确,请重新输入"<<endl;
60. else if (!is_time(hour,minute,sec))
61. cout<<"时间非法,请重新输入"<<endl;
62. else
63. break;
64. }
65.}
66.void Time::show_time( )
67.{
68. cout<<hour<<":"<<minute<<":"<<sec<<endl;
69.}
70.bool Time::is_time(int h,int m, int s)
71.{
72. if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)
73. return false;
74. return true;
75.}
76.int main( )
77.{
78.
79. Time t1;
80. t1.set_time( );
81. t1.show_time( );
82. cout<<endl;
83.
84. t1.add_a_sec( );
85. cout<<"时间增加1秒后"<<endl;
86. t1.show_time( );
87. cout<<endl;
88. t1.add_a_minute( );
89. cout<<"时间增加1分钟后"<<endl;
90. t1.show_time();
91. cout<<endl;
92. t1.add_an_hour( );
93. cout<<"时间增加1小时后"<<endl;
94. t1.show_time( );
95. cout<<endl;
96. int n;
97. cin>>n;
98. t1.add_seconds(n);
99. cout<<"时间增加n秒后"<<endl;
100. t1.show_time();
101. cout<<endl;
102. t1.add_minutes(n);
103. cout<<"时间增加n分钟后"<<endl;
104. t1.show_time();
105. cout<<endl;
106. t1.add_hours(n);
107. cout<<"时间增加n小时后"<<endl;
108. t1.show_time();
109. cout<<endl;
110. return 0;
111.}
112.int Time::add_seconds(int n) //增加n秒钟
113.{
114. sec+=n;
115. if(sec>=60)
116. {
117. int add_minutes( );
118. sec-=60;
119.
120. }
121.}
122.int Time::add_minutes(int n)//增加n分钟
123.{
124. minute+=n;
125. if(minute>=60)
126. {
127. int add_hours();
128. minute-=60;
129. }
130.
131.}
132.int Time::add_hours(int n)//增加n小时
133.{
134. hour+=n;
135. if(hour>24)
136. hour-=24;
137.}
138.二 项目名称:正整数类
139.#include<iostream>
140.using namespace std;
141.class NaturalNumber
142.{
143.public:
144. void setValue (int x);//置数据成员n的值,要求判断是否是正整数
145. int getValue(); //返回私有数据成员n的值
146. bool isPrime(); //判断数据成员n是否为素数,是返回true,否则返回false
147. void printFactor(); //输出数据成员n的所有因子,包括1和n自身
148. bool isPerfect(); //判断数据成员n是否为完全数。若一个正整数n的所有小于n的因子之和等于n, 则称n为完全数, 如6=1+2+3是完全数。
149. bool isReverse(int x);//判断形式参数x是否为数据成员n的逆向数(例321是123的逆向数)。
150. bool isDaffodil(int x); //判断形式参数x是否是水仙花数。水仙花数的各位数字立方和等于该数,如153=1*1*1+5*5*5+3*3*3
151. void printDaffodils(); //显示所有大于1,且小于数据成员n的水仙花数;
152.
153.private:
154. int n;
155.};
156.int main()
157.{
158. NaturalNumber nn; //定义类的一个实例(对象)
159. nn.setValue (6);
160. cout<<nn.getValue()<<(nn.isPrime()?"是":"不是")<<"素数"<<endl;
161. cout<<nn.getValue()<<"的所有因子有:"<<endl;
162. nn.printFactor();
163. cout<<endl;
164. cout<<nn.getValue()<<(nn.isPerfect()?"是":"不是")<<"完全数"<<endl;
165. cout<<nn.getValue()<<(nn.isReverse(6)?"是":"不是")<<"逆向数"<<endl;
166. cout<<nn.getValue()<<(nn.isDaffodil(6)?"是":"不是")<<"水仙花数"<<endl;
167.
168. cout<<2<<"到"<<nn.getValue()<<"的水仙花数有:"<<endl;
169. nn.printDaffodils();
170. cout<<endl;
171. nn.setValue (37);
172. cout<<nn.getValue()<<(nn.isPrime()?"是":"不是")<<"素数" <<endl;
173. cout<<nn.getValue()<<"的所有因子有:"<<endl;
174. nn.printFactor();
175. cout<<endl;
176. cout<<nn.getValue()<<(nn.isPerfect()?"是":"不是")<<"完全数"<<endl;
177. cout<<nn.getValue()<<(nn.isReverse(37)?"是":"不是")<<"逆向数"<<endl;
178. cout<<nn.getValue()<<(nn.isDaffodil(37)?"是":"不是")<<"水仙花数"<<endl;
179. cout<<2<<"到"<<nn.getValue()<<"的水仙花数有:"<<endl;
180. nn.printDaffodils();
181. cout<<endl;
182. nn.setValue (123);
183. cout<<nn.getValue()<<(nn.isPrime()?"是":"不是")<<"素数" <<endl;
184. cout<<nn.getValue()<<"的所有因子有:"<<endl;;
185. nn.printFactor();
186. cout<<endl;
187. cout<<nn.getValue()<<(nn.isPerfect()?"是":"不是")<<"完全数"<<endl;
188. cout<<nn.getValue()<<(nn.isReverse(123)?"是":"不是")<<"逆向数"<<endl;
189. cout<<nn.getValue()<<(nn.isDaffodil(123)?"是":"不是")<<"水仙花数"<<endl;
190. cout<<2<<"到"<<nn.getValue()<<"的水仙花数有:"<<endl;
191. nn.printDaffodils();
192. cout<<endl;
193.
194. //随着成员函数的实现,增加代码以完成相关的测试。注意判断类的成员函数需要测试是或否两种情况……
195. return 0;
196.}
197.void NaturalNumber::setValue (int x)
198.{
199. n=x;
200. if((int(n)-n==0)&&n>0)
201. cout<<n<<"是正整数"<<endl;
202. else
203. cout<<n<<"不是正整数"<<endl;
204.}
205.int NaturalNumber::getValue()
206.{
207. return n;
208.}
209.bool NaturalNumber::isPrime()
210.{
211. int i,s=0;
212. for(i=2; i<n; i++)
213. {
214.
215. if(n%i==0)
216. {
217.
218. s=1;
219. break;
220. }
221. }
222. if(s==0)
223. return true;
224. else
225. return false;
226.
227.}
228.void NaturalNumber::printFactor()
229.{
230. int i;
231. for(i=1; i<=n; i++)
232. {
233. if(n%i==0)
234. cout<<i<<" ";
235. else
236. continue;
237. }
238.
239.}
240.bool NaturalNumber::isPerfect()
241.{
242. int i,s=0;
243. for(i=1; i<n; i++)
244. {
245. if(n%i==0)
246. s+=i;
247. }
248. if(s==n)
249. return true;
250. else
251. return false;
252.
253.}
254.bool NaturalNumber::isReverse(int x)
255.{
256. n=x;
257. int l=n;
258. while(l||x)
259. {
260. if((l%=10)!=(x%=10))
261. return false;
262. l/=10;
263. x/=10;
264.
265. }
266. return true;
267.}
268.bool NaturalNumber::isDaffodil(int x)
269.{
270. int s=0,l,t[10]= {0};
271. int i;
272. n=x;
273. for(i=0; l>0; i++)
274. {
275. t[i]=l%10;
276. s+=t[i]*t[i]*t[i];
277. l/=10;
278. }
279. if(s==n)
280. return true;
281. else
282. return false;
283.}
284.void NaturalNumber::printDaffodils()
285.{
286. int i,j,l,s=0;
287. int b[i];
288.
289. for(i=2; i<n; i++)
290. {
291. l=i;
292. for(j=0; l>0; j++)
293. {
294. b[j]=l%10;
295. s+=b[j]*b[j]*b[j];
296. l/=10;
297. }
298. if(s==n)
299. cout<<i<<" ";
300.
301.
302. }
303.}
304.三 项目名称: Book类
305.#include<iostream>
306.#include<string>
307.using namespace std;
308.class Book
309.{
310.public:
311. void setBook(string na, string wr,string pub,int pr,int nu,int no);
312. int borrow();
313. int restore();
314. void print();
315. void set_NO(int n);
316. int get_NO();
317.
318.
319.private:
320. string name;
321. string writer;
322. string publicer;
323. int price,number,NO;
324.
325.};
326.int main()
327.{
328. Book x1;
329. x1.setBook("<<24章经>>","一战到底编著","江苏凤凰科学技术出版社",29,5,9);
330. x1.print();
331. x1.borrow();
332. x1.print();
333. x1.restore();
334. x1.print();
335. x1.set_NO(15);
336. x1.get_NO();
337. x1.print();
338. return 0;
339.
340.
341.}
342.void Book::setBook(string na,string wr,string pub,int pr,int nu,int no)//给书籍的数据成员赋值
343.{
344.
345. name=na;
346. writer=wr;
347. publicer=pub;
348. price=pr;
349. number=nu;
350. NO=no;
351.}
352.int Book::borrow()//书的数量减一
353.{
354. number-=1;
355. return number;
356.
357.}
358.int Book::restore()//书的数量加一
359.{
360. number+=1;
361. return number;
362.
363.}
364.void Book::print()//输出有关书的所有信息
365.{
366. cout<<"书名:"<<name<<endl;
367. cout<<"作者:"<<writer<<endl;
368. cout<<"出版社:"<<publicer<<endl;
369. cout<<"价格:"<<price<<endl;
370. cout<<"数量:"<<number<<endl;
371. cout<<"书号:"<<NO<<endl;
372. cout<<endl;
373.
374.}
375.void Book::set_NO(int n)//修改类的书号
376.{
377. NO=n;
378.}
379.int Book::get_NO()//获得类对象的书名
380.{
381. return NO;
382.}
C++第二次作业
最新推荐文章于 2024-09-03 19:59:32 发布