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

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

复数类

问题描述

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();
}


目录
相关文章
|
1月前
|
设计模式 机器学习/深度学习 存储
|
1月前
|
算法 开发者
程序设计 (4)
程序设计 (4)
13 0
|
1月前
|
Python
程序设计 (2)
程序设计 (2)
13 0
|
1月前
|
程序员
程序设计 (3)
程序设计 (3)
16 0
|
5月前
|
C++
20 C++ - 面向对象程序设计案例
20 C++ - 面向对象程序设计案例
53 0
|
6月前
|
人工智能 算法 数据挖掘
什么是程序设计
一、什么是程序设计 程序设计是指通过编写、测试和维护计算机程序来解决问题或实现特定功能的过程。它涉及到确定问题的需求、设计算法、选择合适的编程语言、编写代码、调试和测试程序等步骤。程序设计的目标是创建高效、可靠、易于理解和维护的软件。 二、程序设计具有以下特点 1. 抽象性:程序设计是一种高度抽象的活动,它涉及到将实际问题转化为计算机可以理解和执行的指令。 2. 逻辑性:程序设计需要遵循严格的逻辑结构和规则,以确保程序的正确性和可靠性。逻辑思维和分析能力是程序设计的重要组成部分。 3. 创造性:程序设计是一种创造性的活动,程序员需要在解决问题的过程中提出新的思路和方法,以实现更好的效果。
303 0
|
11月前
|
存储 Java
面向对象程序设计概述
面向对象程序设计概述
162 0
|
11月前
|
程序员 测试技术 C语言
c++面向对象程序设计入门
c++面向对象程序设计入门
139 0
|
11月前
|
编译器 C++
c++面向对象程序设计教程——类(一)
c++面向对象程序设计教程——类(一)
109 0
|
11月前
|
C++
c++面向对象程序设计教程——类(二)
c++面向对象程序设计教程——类(二)
99 0