第三周(补)

来源:互联网 发布:杭州少儿编程教育推荐 编辑:程序博客网 时间:2024/04/30 07:36
 /* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名称: * 作 者:韩云龙 * 完成日期: 2012 年 3 月 19日 * 版 本 号: * 对任务及求解方法的描述部分 * 输入描述: * 问题描述: * 程序输出: * 程序头部的注释结束 */ 3. 完成任务的时间包括周一8:00-19:30在402/3的课内上机,19:30后在103/5的时间,每周五晚上CSDN集体活动时间机房一楼全开放可以去。另外,本学期学院计算中心101、102、103、105四个机房免费对学生开放,没课的学生携带校园卡可以在上班时间(周一至周五,上午8:00-11:30,下午2:00-5:30)前去免费上机(http://topic.csdn.net/u/20120228/17/27299056-bb03-4b0e-824e-8ebe0643a3e8.html?99479)。请安排好看书和实践的时间,保证学习的质量。 4. 在完成任务之余,上学期遗留问题也要解决。网络习题课可能是一个途径,我会尽量抽时间建设。 【任务1】(不写报告)程序文本 #include <iostream> using namespace std; class Student { public: void set_data(int n, char *p,char s); void display( ); private: int num; char name[20]; char sex; }; void Student::set_data(int n, char *p,char s) { num=n; strcpy(name,p); sex=s; } void Student::display( ) { cout<<"num: "<<num<<endl; cout<<"name: " <<name<<endl; cout<<"sex: " <<sex<<endl; } int main() { Student stud1,stud2; stud1.set_data(1,"He",'f'); stud2.set_data(2,"She",'m'); stud1.display(); stud2.display(); return 0; } 【任务2】(教材P261页第1题)上机前在纸上先找出错误,然后上机调试,使之正常运行。运行时输入时分秒,检查输出是否正确。 要求:在实验报告中,在改动处加注释说明理由 #include <iostream> using namespace std; class Time { void set_time(void) ; void show_time(void); int hour; int minute; int sec; }; Time t; int main() { set_time(); show_time(); return 0; } int set_time(void) { cin>>t.hour; cin>>t.minute; cin>>t.sec; } int show_time(void) { cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl; } #include <iostream> using namespace std; class Time {public:  void set_time(void) ;  void show_time(void); public:  int hour;     int minute;     int sec; }; Time t; int main() {   t.set_time();  t.show_time();  return 0; } void Time::set_time() {  cin>>t.hour;  cin>>t.minute;  cin>>t.sec; } void Time::show_time() {  cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl; } 【任务3】下面是已有的程序 #include <iostream> using namespace std; class Time { public: void set_time( ); void show_time( ); private: bool is_time(int, int, int); int hour; int minute; int sec; }; int main( ) { Time t1; Time &t2=t1; t1.set_time( ); t2.show_time( ); return 0; } void Time::set_time( ) { char c1,c2; cout<<"请输入时间(格式hh:mm:ss)"; while(1) { cin>>hour>>c1>>minute>>c2>>sec; if(c1!=':'||c2!=':') cout<<"格式不正确,请重新输入"<<endl; else if (!is_time(hour,minute,sec)) cout<<"时间非法,请重新输入"<<endl; else break; } } void Time::show_time( ) { cout<<hour<<":"<<minute<<":"<<sec<<endl; } bool Time::is_time(int h,int m, int s) { if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60) return false; return true; } 要求:请在原类基础上,增加下列成员函数,要求前三个设计成内置函数,在main()数中增加适当的调用以展示扩充类定义后的功能(最好能一次运行)。 add_a_sec() //增加1秒钟 add_a_minute() //增加1分钟 add_an_hour() //增加1小时 add_seconds(int) //增加n秒钟 add_minutes(int) //增加n分钟 add_hours(int) //增加n小时 提示:要考虑增加后超出取值范围的情形
  • #include <iostream>
  • using namespace std;
  • class Time
  • {
  • public:
  • void set_time( );
  • void show_time( );
  • void add_seconds(int);
  • void add_minutes(int);
  • void add_hours(int) ;
  • private:
  • bool is_time(int,int, int);
  • int hour;
  • int minute;
  • int sec;
  • };
  • int main( )
  • {
  • int h,m,s;
  • Time t1;
  • Time &t2=t1;
  • t1.set_time( );
  • cout << "需要增加的秒数" << endl;
  • cin >> s;
  • t1.add_seconds(s);
  • cout << "需要增加的分钟数" << endl;
  • cin >> m;
  • t1.add_minutes(m);
  • cout << "需要增加的小时数" << endl;
  • cin >> h;
  • t1.add_hours(h);
  • t2.show_time();
  • t2.show_time( );
  • return 0;
  • }
  • void Time::set_time( )
  • {
  • char c1,c2;
  • cout<<"请输入时间(格式hh:mm:ss)";
  • while(1)
  • {
  • cin>>hour>>c1>>minute>>c2>>sec;
  • if(c1!=':'||c2!=':')
  • cout<<"格式不正确,请重新输入"<<endl;
  • else if (!is_time(hour,minute,sec))
  • cout<<"时间非法,请重新输入"<<endl;
  • else
  • break;
  • }
  • }
  • void Time::show_time( )
  • {
  • cout<<hour<<":"<<minute<<":"<<sec<<endl;
  • }
  • bool Time::is_time(int h,int m,int s)
  • {
  • if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)
  • return false;
  • return true;
  • } void Time::add_seconds(int n)
  • {
  • sec = sec + n;
  • while(sec >= 60)
  • {
  • sec = sec - 60;
  • minute++;
  • }
  • }
  • void Time::add_minutes(int n)
  • {
  • minute = minute + n;
  • while(minute >= 60)
  • {
  • minute = minute - 60;
  • hour++;
  • }
  • }
  • void Time::add_hours(int n)
  • {
  • hour = hour + n;
  • } 【任务4】(改自教材P262第6题)仿照你阅读过的程序,编写基于对象的程序,求3个长方柱的体积。数据成员包括长(length)、宽(width)、高(heigth)、体积,要求用成员函数实现下面的功能: (1)由键盘输入3个长方柱的长、宽、高; (2)计算长方柱的体积(volume)和表面积(areas); (3)输出这3个长方柱的体积和表面积;
  • #include <iostream>
  • using namespace std;
  • class Box
  • {
  • public:
  • void get_value();
  • float volume();
  • float areas();
  • void display ( float );
  • public:
  • float length;
  • float width;
  • float height;
  • float vol;
  • float are;
  • };
  • void Box::get_value()
  • {
  • cout << "请输入长方体的长、宽、高: " << endl;
  • cin >> length >> width >> height;
  • }
  • float Box::volume()
  • {
  • vol = length * width * height;
  • return vol;
  • }
  • float Box::areas()
  • {
  • are = 2 * ( length * width + length * height + width * height );
  • return are ;
  • }
  • void Box::display( float x )
  • {
  • cout << x << endl;
  • }
  • int main ()
  • {
  • Box box1,box2,box3;
  • box1.get_value(); //第一个长方体
  • box1.volume();
  • box1.areas();
  • cout << " 第一个长方体的体积为: " ;
  • box1.display( box1.vol );
  • cout << " 第一个长方体的表面积为: ";
  • box1.display( box1.are );
  • box2.get_value (); //第二个长方体
  • box2.volume();
  • box2.areas();
  • cout << " 第二个长方体的体积为: " ;
  • box2.display( box2.vol );
  • cout << " 第二个长方体的表面积为: ";
  • box2.display( box2.are );
  • box3.get_value(); //第三个长方体
  • box3.volume();
  • box3.areas();
  • cout << " 第三个长方体的体积为: ";
  • box3.display (box3. vol );
  • cout << " 第三个长方体的表面积为: " ;
  • box3.areas();
  • box3.display (box3.are );
  • return 0;
  • }