1. 声明一个二维数组,给其赋值成为一个对角线为1,其它元素为0的n阶矩阵,并将其各元素显示出来。2. 定义一个复数类,并定义它与复数、实数相乘除的方法。利用该类实现:输入两个复数,得到它们的积与商。

解决方案 »

  1.   

    看样子象作业。。
    给你第一个。没做错误处理,输入时候记得输入数字。
    import java.io.*;
    public class aa{
    public static void main(String[] args) throws Exception{
    System.out.println("IN PUT A NUMBER:");
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    int n=Integer.parseInt(br.readLine());
    br.close();
    String str[][]=new String[n][n];
    int i=0;
    int j=0;
    for(i=0;i<n;i++){
    for(j=0;j<n;j++){
    if(j==i || j==(n-i-1)){
    str[i][j]=new String("1");
    }
    else{
    str[i][j]=new String("0");
    }
    }
    }
    for(int k1=0;k1<n;k1++){
    for(int k2=0;k2<n;k2++){
    System.out.print(str[k1][k2] + " ");
    }
    System.out.println();
    }
    }
    }
      

  2.   

    又想了想第一个问题,应该是双对角线,可以用一维数组表示:
    设n*n阶矩阵(i,j)为矩阵元素下标
    建一个2n的一维数组s[2n]=0既可
    if(i==j)
      s[i]=1;
    else if(i+j==5)
      s[n+j]=1;
      

  3.   

    第二个
    complex.h
    ----------------------------
    ///////////////////////////////////////////////////////////////////// 
    // 工程: UseComplex
    // 文件名: complex0.h
    // ** Copyright (c) 2003-2005 安徽XX信息技术有限公司
    // 作者:ming6,日期:2005-03-10 
    // 修改者:,日期:
    // 描述: 复数类声明
    // 主要函数: 
    // 版本: 
    // 修改: 
    // 参考文献:  
    /////////////////////////////////////////////////////////////////////
    #include <iostream>using namespace std;class Complex_T
    {
    private:
    double real;//实数部分
    double imaginary;//虚数部分
    public:
    Complex_T();
    Complex_T(double dReal,double dImaginary);

    Complex_T operator+(const Complex_T& rComplex)const;

    Complex_T operator-(const Complex_T& rComplex)const;
    Complex_T operator-()const;

    Complex_T operator*(const Complex_T& rComplex)const; friend Complex_T operator*(double dNum,const Complex_T& rComplex);
    friend ostream & operator<<(ostream & os,const Complex_T & rComplex);
    friend istream & operator>>(istream & is,Complex_T & rComplex);
    };
      

  4.   

    complex.cpp
    ----------------------------
    ///////////////////////////////////////////////////////////////////// 
    // 工程: UseComplex
    // 文件名: complex0.cpp
    // ** Copyright (c) 2003-2005 安徽XX信息技术有限公司
    // 作者:ming6,日期:2005-03-10 
    // 修改者:,日期:
    // 描述: 复数类方法定义
    // 主要函数: 
    // 版本: 
    // 修改: 
    // 参考文献:  
    /////////////////////////////////////////////////////////////////////
    #include "stdafx.h"#include "complex0.h"//=================================================================// 
    // 功能: 默认构造函数
    // 参数: 
    //  入口: 
    //  出口: 
    // 返回: 
    // 主要思路:  
    // 调用方法:  
    // 作者:ming6,日期:2005-03-10 
    // 修改者:,日期:
    //=================================================================// 
    Complex_T::Complex_T()
    {
    real = 0;
    imaginary = 0;
    }
    //=================================================================// 
    // 功能: 重载构造函数
    // 参数: 
    //  入口: double,double
    //  出口: 
    // 返回: 
    // 主要思路:  
    // 调用方法:  
    // 作者:ming6,日期:2005-03-10 
    // 修改者:,日期:
    //=================================================================// 
    Complex_T::Complex_T(double dReal,double dImaginary)
    {
    real = dReal;
    imaginary = dImaginary;
    }
    //=================================================================// 
    // 功能: 重载+
    // 参数: 
    //  入口: Complex_T&
    //  出口: 
    // 返回: Complex_T
    // 主要思路:  
    // 调用方法:  
    // 作者:ming6,日期:2005-03-10 
    // 修改者:,日期:
    //=================================================================// 
    Complex_T Complex_T::operator+(const Complex_T& rComplex)const
    {
    Complex_T Temp;
    Temp.real = real+rComplex.real;
    Temp.imaginary = imaginary+rComplex.imaginary;
    return Temp;
    }
    //=================================================================// 
    // 功能: 重载-
    // 参数: 
    //  入口: Complex_T&
    //  出口: 
    // 返回: Complex_T
    // 主要思路:  
    // 调用方法:  
    // 作者:ming6,日期:2005-03-10 
    // 修改者:,日期:
    //=================================================================// 
    Complex_T Complex_T::operator-(const Complex_T& rComplex)const
    {
    Complex_T Temp;
    Temp.real = real-rComplex.real;
    Temp.imaginary = imaginary-rComplex.imaginary;
    return Temp;
    }
    //=================================================================// 
    // 功能: 重载-
    // 参数: 
    //  入口: 
    //  出口: 
    // 返回: Complex_T
    // 主要思路:  
    // 调用方法:  
    // 作者:ming6,日期:2005-03-10 
    // 修改者:,日期:
    //=================================================================// 
    Complex_T Complex_T::operator-()const
    {
    Complex_T Temp;
    Temp.real = real;
    Temp.imaginary = -imaginary;
    return Temp;
    }
    //=================================================================// 
    // 功能: 重载*
    // 参数: 
    //  入口: Complex_T&
    //  出口: 
    // 返回: Complex_T
    // 主要思路:  
    // 调用方法:  
    // 作者:ming6,日期:2005-03-10 
    // 修改者:,日期:
    //=================================================================// 
    Complex_T Complex_T::operator*(const Complex_T& rComplex)const
    {
    Complex_T Temp;
    Temp.real = real*rComplex.real-imaginary*rComplex.imaginary;
    Temp.imaginary = real*rComplex.imaginary+imaginary*rComplex.real;
    return Temp;
    }
    //=================================================================// 
    // 功能: 重载*(友元)
    // 参数: 
    //  入口: double,Complex_T&
    //  出口: 
    // 返回: Complex_T
    // 主要思路:  
    // 调用方法:  
    // 作者:ming6,日期:2005-03-10 
    // 修改者:,日期:
    //=================================================================// 
    Complex_T operator*(double dNum,const Complex_T& rComplex)
    {
    Complex_T Temp;
    Temp.real = dNum*rComplex.real;
    Temp.imaginary = dNum*rComplex.imaginary;
    return Temp;
    }
    //=================================================================// 
    // 功能: 重载<<(友元)
    // 参数: 
    //  入口: ostream&,Complex_T&
    //  出口: 
    // 返回: ostream&
    // 主要思路:  
    // 调用方法:  
    // 作者:ming6,日期:2005-03-10 
    // 修改者:,日期:
    //=================================================================// 
    ostream& operator<<(ostream& os,const Complex_T& rComplex)
    {
    os<<'('<<rComplex.real<<','<<rComplex.imaginary<<"i)";
    return os;
    }
    //=================================================================// 
    // 功能: 重载>>(友元)
    // 参数: 
    //  入口: istream&,Complex_T&
    //  出口: 
    // 返回: istream&
    // 主要思路:  
    // 调用方法:  
    // 作者:ming6,日期:2005-03-10 
    // 修改者:,日期:
    //=================================================================// 
    istream& operator>>(istream& is,Complex_T& rComplex)
    {
    cout<<"real: ";
    while (is>>rComplex.real)
    {
    cout<<"imaginary: ";
    is>>rComplex.imaginary;
    return is;
    }
    return is;
    }
      

  5.   

    usecomplex.cpp
    -----------
    #include "stdafx.h"using namespace std;#include "complex0.h"int _tmain()
    {
        Complex_T a(3.0,4.0);
    Complex_T c;
    cout<<"Enter a complex number (q to quit):\n";
    while (cin>>c)
    {
    cout<<"c is "<<c<<'\n';
    cout<<"complex conjugate is "<<-c<<'\n';
    cout<<"a is "<<a<<'\n';
    cout<<"a+c is "<<a+c<<'\n';
    cout<<"a-c is "<<a-c<<'\n';
    cout<<"a*c is "<<a*c<<'\n';
    cout<<"2*c is "<<2*c<<'\n';
    cout<<"Enter a complex number (q to quit):\n";
    }
    cout<<"Done!\n";
    return 0;
    }