第七次实验

结果:
2.调试下列程序
#include<iostream> using namespace std; class complex { public: complex(){real=imag=0.0;} complex(double r){real=r;imag=0.0;} complex(double r,double i){real=r;imag=i;} friend complex operator + (const complex &c1,const complex &c2); friend complex operator - (const complex &c1,const complex &c2); friend complex operator * (const complex &c1,const complex &c2); friend complex operator / (const complex &c1,const complex &c2); friend void print(const complex &c); private: double real,imag; }; complex operator + (const complex &c1,const complex &c2) { return complex(c1.real+c2.real,c1.imag+c2.imag); } complex operator - (const complex &c1,const complex &c2) { return complex(c1.real-c2.real,c1.imag-c2.imag); } complex operator * (const complex &c1,const complex &c2) { return complex(c1.real*c2.real-c1.imag*c2.imag,c1.real*c2.imag+c1.imag*c2.real); } complex operator / (const complex &c1,const complex &c2) { return complex((c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag),(c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag)); } void print(const complex &c) { if(c.imag<0) cout<<c.real<<c.imag<<"i"; else cout<<c.real<<"+"<<c.imag<<"i"; } int main() { complex c1(2.0),c2(3.0,-1.0),c3; c3=c1+c2; cout<<"\nc1+c2= "; print(c3); c3=c1-c2; cout<<"\nc1-c2= "; print(c3); c3=c1*c2; cout<<"\nc1*c2= "; print(c3); c3=c1/c2; cout<<"\nc1/c2= "; print(c3); c3=(c1+c2)*(c1-c2)*c2/c1; cout<<"\n(c1+c2)*(c1-c2)*c2/c1= "; print(c3); cout<<endl; return 0; }
结果:

3.定义一个Time类用来保存时间(时,分,秒),通过重载操作符“+”实现两个时间的相加。(sy7_3.cpp)

程序如下:

#include <stdio.h>  
  class Time  
  {  
  public:  
    Time(){ hours=0;minutes=0;seconds=0;} //无参构造函数  
    Time(int h, int m,int s) //重载构造函数  
    {  
             hours=h; minutes=m; seconds=s;  
    }  
    Time operator +(Time&); //操作符重载为成员函数,返回结果为Time类  
    void gettime();  
  private:  
    int hours,minutes,seconds;  
  };  
  Time Time::operator +(Time& time)  
  {  
  int h,m,s;  
  s=time.seconds+seconds;  
  m=time.minutes+minutes+s/60;  
  h=time.hours+hours+m/60;  
  Time result(h,m%60,s%60);  
  return result;  
  }  
  void Time::gettime()  
  {  
  printf("%d:%d:%d\n",hours,minutes,seconds);  
  }  
  int main( )  
  {  
  Time t1(9,35,45),t2(12,15,32),t3;  
  t3=t1+t2;  
  t3.gettime();  
  return 0;  
  }

结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《Java程序设计》课程实验指导书程序代码(答案)(实验一),个人原创,仅供参考与交流。 实验一 Java开发环境的安装与配置,熟悉Java程序结构 一、实验目的: 1. 掌握JDK的安装步骤。 2. 理解环境变量PATH, CLASSPATH的作用,以及它们的设置方法。 3. 熟悉Editplus(或TextPad,JCreator)编辑环境,编写简单的Application程序和Applet程序,并编译和执行。 二、实验内容: 熟悉JDK的安装和配置,学习如何编写并运行简单的Application程序和Applet程序(能输出一条简单的问候信息); 三、实验要求: 1. 能正确地安装JDK 2. 熟悉环境变量PATH, CLASSPATH设置方法,熟悉编辑环境 3. 调试程序、编译,运行后得到正确的结果 4. 写出实验报告,要求记录编译和执行Java程序当中的系统错误信息提示,并给出解决办法。 四、实验步骤: 1.从http://java.sun.com 上下载最新版本的JDK,并安装。 2.设置环境变量PATH, CLASSPATH, 使得Java程序能正确编译和执行。 3.在Editplus(或JCreator ,Textpad)环境下编写一个HelloWorld.java程序, (1)在主方法static public void main(String[ ] args)中调用System.out.println()方法,使程序输出一条问候信息; (2) 编译运行程序,观察运行情况和输出结果。(使用JDK环境,调用javac.exe和java.exe编译和执行程序) 4.在Editplus(或JCreator ,Textpad)环境下编写一个HelloWorldApplet.java程序。 (1)在public void paint(Graphics g)方法中调用g.drawString()方法,使程序输出一条问候信息; (2) 编译运行程序,观察运行情况和输出结果。(使用JDK环境,调用javac.exe编译和浏览器解释执行Applet程序)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值