c++结构体解决复数辐角问题

 结构体相关知识及运行代码(来自发发老师)


/*
  ch10_structs.cc
 
  介绍:
  这里解释了结构体的使用方法。包括:
  (1)定义和初始化。
  (2)赋值。
  (3)结构体和数组一起使用。注意数据成员和函数成员的访问。
  (4)结构体和向量一起使用。
  (5)结构体和函数。
*/

#include<iostream>
#include<iomanip>

#include<cmath>
#include<vector>

using namespace std;

//定义复数的结构体(struct),包括:
//(1)数据成员:实数部分real、虚数部分imag;
//(2)构造函数/初始化函数:默认初始化函数CNStrt()、给定值的初始化函数CNStrt(double,double);
//(3)成员函数:求复数的模Mode(),并返回结果;
struct CNStrt {
// public:
  //(1)数据成员;
  double real;
  double imag;

  //(2)构造函数;
  CNStrt();//默认构造函数;
  CNStrt(double);//real;
  CNStrt(double, double);//real,imag;

  //(3)成员函数;
  double Mode();

  void   Doubled();
};

CNStrt::CNStrt() {
  real = 0;
  imag = 0;
};

CNStrt::CNStrt(double r) {
  real = r;
  imag = 0;
};

CNStrt::CNStrt(double r, double i) {
  real = r;
  imag = i;
};

double CNStrt::Mode() {
  return sqrt(real * real + imag * imag);
};

void CNStrt::Doubled() {
  real *= 2;
  imag *= 2;
};

//定义函数;
void OutputCNStrt(CNStrt);

int main()
{
  //(1)结构体变量的定义;
  cout<<"@(2)结构体的赋值:"<<endl;
  
  CNStrt cn1;//赋值数据成员;
  cn1.real = 0;
  cn1.imag = 2;

  cout<<"cn1: real = "<<cn1.real<<", imag = "<<cn1.imag<<endl;
  
  CNStrt cn2 = CNStrt();//使用构造函数;
  
  cout<<"cn2: real = "<<cn2.real<<", imag = "<<cn2.imag<<endl;

  double real = 4;//可任意取值;
  double imag = 3;

  CNStrt cn3 = CNStrt(real, imag);

  cout
   <<"cn3: real = "
   <<cn3.real
   <<", imag = "
   <<cn3.imag
   <<"; mode = "
   <<cn3.Mode()
   <<endl;
  
  CNStrt cn4 = cn2;//使用同类型变量赋值;

  cout<<"cn4: real = "<<cn4.real<<", imag = "<<cn4.imag<<endl;
	
  //(3)结构体和数组结合使用;
  cout<<endl<<"@(3)结构体和数组: "<<endl;
  
  const int array_size = 5;
  CNStrt CNArray[array_size];

  for (int ind = 0; ind < array_size; ind++) {
    CNArray[ind] = CNStrt(ind, ind + 1);
  }

  CNStrt* pcn = &(CNArray[0]);
  for (int ind = 0; ind < array_size; ind++, pcn++) {
    cout
     <<" ind = "
     <<setw(3)
     <<ind
     <<": real = "
     <<setw(3)
     <<(*pcn).real//指针:解引用+数据成员;
     <<" or "
     <<setw(3)
     <<pcn->real//指针:使用箭头运算符来读取数据成员;
     <<", imag = "
     <<setw(3)
     <<(*pcn).imag
     <<" or "
     <<setw(3)
     <<pcn->imag
     <<endl;
  }
  
  //(4)结构体和向量结合使用;
  cout<<endl<<"@(4)结构体和向量: "<<endl;
  
  vector<CNStrt> CN;
  
  //复数结构体的初始化;
  int size = 10;
  for (int ind = 0; ind < size; ind++) {
    CN.push_back( CNStrt(ind, ind * 2) );
  }

  //使用迭代器来删除矢量的前n个元素:使用函数erase();
  cout<<"  删除前CN的大小:"<<CN.size()<<endl;

  vector<CNStrt>::iterator pvc;

//  CN.insert( pvc, CNStrt() );
  
  int remove_size = 2;

  while (remove_size-- > 0) {
    pvc = CN.begin();
    
    CN.erase(pvc);
  }

  cout
   <<"  删除后CN的大小: size() = "
   <<CN.size()
   <<", or end() - begin() = "
   <<CN.end() - CN.begin()
   <<endl;

  //使用迭代器来输出结构体的数据;
  for (pvc = CN.begin(); pvc != CN.end(); pvc++) {
    cout
     <<" real = "
     <<setw(3)
     <<(*pvc).real//迭代器:解引用+数据成员;
     <<" or "
     <<setw(3)
     <<pvc->real//迭代器:使用箭头运算符来读取数据成员;
     <<", imag = "
     <<setw(3)
     <<(*pvc).imag
     <<" or "
     <<setw(3)
     <<pvc->imag
     <<"; mode = "
     <<setw(8)
     <<(*pvc).Mode()
     <<" or "
     <<setw(8)
     <<pvc->Mode()//通过箭头运算符来读取函数成员;
     <<endl;
  }

  //(5)结构体和函数的使用;
  cout<<endl<<"@(5)结构体和函数:"<<endl;
  
  for (pvc = CN.begin(); pvc != CN.end(); pvc++) {
    pvc->Doubled();

    OutputCNStrt(*pvc);//CNStrt;
  }
  
  return 1;
};

void OutputCNStrt(CNStrt cn)
{
  cout
   <<" real = "
   <<setw(3)
   <<cn.real
   <<", imag = "
   <<setw(3)
   <<cn.imag
   <<endl;
};

/*
double Tripled(double c)
{
	
};

CNStrt Tripled(CNStrt cn)
{




};
*/

题目要求:

以下为实际代码计算复数: 

#include<iostream>
#include<cmath>
#include<iomanip>
#include<vector>
using namespace std;
struct CNStrt{
	double real;
	double imag;
	CNStrt();
	CNStrt(double);
	CNStrt(double,double);
	double ArguAngle();
};
CNStrt::CNStrt(){
	real = 0;
	imag = 0;
}
CNStrt::CNStrt(double r,double i){
	real = r;
	imag = i;
}
double CNStrt::ArguAngle(){
	return atan2(imag,real);
}
int main(){
	CNStrt cn1;
	cn1.real = 3;
	cn1.imag = 4;
	cout<<cn1.ArguAngle()<<" "<<cn1.ArguAngle()/M_PI<<"π"<<endl;
	
	cn1.real = -3;
	cn1.imag = -3;
	cout<<cn1.ArguAngle()<<" "<<cn1.ArguAngle()/M_PI<<"π"<<endl;
	
	cn1.real = sqrt(3.0);
	cn1.imag = 1.0;
	cout<<cn1.ArguAngle()<<" "<<cn1.ArguAngle()/M_PI<<"π"<<endl;
	
	return 0;
}

结构体是体现c++面向对象编程很有用的一个表现,将所有函数用法集成在结构体中,能够大大提高代码效率,可以在这个小例子中体悟一下。

也可以用数组来存储复数对象。

  • 10
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值