复数类
问题描述
样例输入
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(); }