面向对象程序设计-第二章

简介: 面向对象程序设计-第二章

复数类

问题描述

image.png


样例输入

3 3 4 -3

样例输出

c1:3+3i
c2:4-3i
c3:7

代码

main.cpp

#include"Complex.h"
#include<iostream>
using namespace std;
int main()
{
  float a, b, c, d;
  cin >> a >> b >> c >> d;
  Complex c1(a,b), c2(c,d),c3(0,0);
  c3 = c1.add(c2);
  cout << "c1:";
  c1.show();
  cout << "c2:";
  c2.show();
  cout << "c3:";
  c3.show();
  return 0;
}

Complex.h

#include<iostream>
using namespace std;
class Complex
{
private:
  float real;
  float imag;
public:
  Complex(float r = 0.0, float i = 0.0);
  Complex add(const Complex& t);
  void show();
};

Complex.cpp

#include "Complex.h"
using namespace std;
void Complex::show()
{
  cout << real;
  if (imag != 0)
  {
    if (imag > 0)
      cout << "+";
    else;
    cout << imag << "i"<<endl;
  }
  else
    cout << endl;
}
Complex::Complex(float r, float i)
{
  real = r;
  imag = i;
}
Complex Complex::add(const Complex &t)
{
  Complex t1(0,0);
  t1.real = real + t.real;
  t1.imag = imag + t.imag;
  return t1;
}

编写程序,实现动态顺序表

编写程序,实现动态顺序表

#include <iostream>
using namespace std;
class myArrayList
{
public:
  myArrayList(int size=0);
  //myArrayList(const myArrayList &t);
  ~myArrayList(void);
  void Input(int n);
  void Output();
  void SortBySelect();//选择排序
  void Append(int num);//在最后插入一个数num
private:
  int *m_arr;
  int m_size;
  int m_len;
};
myArrayList::myArrayList(int size)
{
  if (size > 0)
  {
    this->m_arr = new int[size];
    this->m_size = size;
    this->m_len = 0;
    for (int i = 0; i < this->m_size; i++)
    {
      m_arr[i] = 0;
    }
  }
  else
  {
    m_arr = nullptr;
    m_size = 0;
    m_len = 0;
  }
}
void myArrayList::Append(int t)
{
  if (m_len < m_size)
  {
    m_arr[m_len] = t;
    m_len++;
  }
}
void myArrayList::SortBySelect()
{
  int i, j,t;
  for (i = 0; i < m_len; i++)
  {
    for (j = 0; j < m_len - i-1; j++)
    {
      if (m_arr[j] > m_arr[j + 1])
      {
        t = m_arr[j];
        m_arr[j] = m_arr[j + 1];
        m_arr[j + 1] = t;
      }
    }
  }
}
myArrayList::~myArrayList(void)
{
  delete[]m_arr;
}
void myArrayList::Input(int n)
{
  this->m_len=n;
  for(int i=0;i<this->m_len;i++)
    cin>>this->m_arr[i];
}
void myArrayList::Output()
{
  cout<<endl;
  for(int i=0;i<this->m_len;i++)
    cout<<this->m_arr[i]<<"  ";
    cout<<endl;
}
int main()
{
  myArrayList list1(20);
  list1.Input(5);
  list1.SortBySelect();
  list1.Output();
  list1.Append(8);
  list1.Output();
  //myArrayList list2(list1);
  list1.Output();
}


目录
相关文章
|
9月前
|
Java 物联网 测试技术
Java面向对象程序设计3面向对象基础
Java面向对象程序设计3面向对象基础
358 0
|
6月前
|
算法 安全 uml
【 第十三章】软件设计师 之 面向对象程序设计
软件设计师 之 面向对象程序设计备考资料
【 第十三章】软件设计师 之 面向对象程序设计
|
9月前
|
算法 Java 程序员
【C++专栏】C++入门 | 类和对象 | 面向过程与面向对象的初步认识
【C++专栏】C++入门 | 类和对象 | 面向过程与面向对象的初步认识
74 0
|
C++
20 C++ - 面向对象程序设计案例
20 C++ - 面向对象程序设计案例
115 0
|
8月前
|
存储 算法 安全
面向对象程序设计C++
面向对象程序设计C++
|
9月前
|
C++
25面向对象的程序设计
25面向对象的程序设计
56 1
|
机器学习/深度学习 存储 算法
|
存储
面向对象程序设计第四章
面向对象程序设计第四章
144 1
面向对象程序设计第四章
|
程序员 测试技术 C语言
c++面向对象程序设计入门
c++面向对象程序设计入门
172 0
|
C++
c++面向对象程序设计教程——类(二)
c++面向对象程序设计教程——类(二)
126 0