首页 > 其他分享 >实验二

实验二

时间:2022-10-13 21:13:41浏览次数:44  
标签:std cout imag Complex 实验 using include

实验任务四

        main.cpp

#include"Complex.hpp"
#include<iostream>
using std::cout;
using std::endl;

void test()
{
	using namespace std;
   Complex c1(3, -4);
   const Complex c2(4.5);
   Complex c3(c1);
   cout << "c1 = ";
   c1.show();
   cout << endl;
   cout << "c2 = ";
   c2.show();
   cout << endl;
   cout << "c2.imag = " << c2.get_imag() << endl;
   cout << "c3 = ";
   c3.show();
   cout << endl;
   cout << "abs(c1) = ";
   cout << abs(c1) << endl;
   cout << boolalpha;
   cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
   cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
   Complex c4;
   c4 = add(c1, c2);
   cout << "c4 = c1 + c2 = ";
   c4.show();
   cout << endl;
   c1.add(c2);
   cout << "c1 += c2, " << "c1 = ";
   c1.show();
   cout << endl;
	
}
int main(){
    test();
}

  Complex.hpp

#pragma once
#include <iostream>
#include<cmath>
using std::cout;
using std::endl;
class Complex{
   public:
       Complex(float x=0.0,float y=0.0):real{x},imag{y}{  };
       Complex(Complex& c):real{c.real},imag{c.imag}{  };
       float get_real()const { return real; }
       float get_imag()const {return imag;  }
        void show() const {
	        if(imag>0)
		   cout<<real<<"+"<<imag<<"i";
		else if(imag<0)
		   cout<<real<<imag<<"i";
        	if(imag==0)
         	   cout<<real;
         	else if(real==0)
        	   cout<<imag<<"i";
	   } 
      void add(const Complex& c){
      	real=real+c.get_real();
      	imag=imag+c.get_imag();
	  }
       friend Complex add(Complex& c1,const Complex& c2){
	 	Complex c;
	        c.real=c1.get_real()+c2.get_real();
	  	c.imag=c1.get_imag()+c2.get_imag();
	  	return c;
	  }
      friend bool is_equal(Complex& c1,const Complex& c2){
	if(c1.get_real()==c1.get_real()&&c1.get_imag()==c2.get_imag()){
	      return true;
	}
	  	else{
	       return false;
		  }
	      return true;
       }
	  friend float abs(Complex& c){
	     return sqrt(c.get_real()*c.get_real()+c.get_imag()*c.get_imag());
	  }
    private:
    float real,imag;
};

    运行结果

 

 

 试验任务五

          main.cpp

#include"User.hpp"
#include<iostream>
void test(){
using std::cout;
using std::endl;
   cout << "testing 1......\n";
   User user1("Jonny", "92197", "[email protected]");
   user1.print_info();
   cout << endl<< "testing 2......\n\n";
   User user2("Leonard");
   user2.change_passwd();
   user2.set_email();
   user2.print_info();
   cout << endl;
   User::print_n();
}
int main() {
   test();
}

  User.hpp

#include<bits/stdc++.h>
using std::cin;
using std::cout;
using namespace std; 
class User{
	public:
	User(string a,string b="111111",string c=""):name{a},passwd{b},email{c}{count++;};
	
     void set_email(){
		cout<<"Enter email address: ";
		cin>>email;
	}
	void change_passwd(){
		int cnt=0;
	   cout<<"Enter old password: ";
	   string pswd,newwd;
	   cin>>pswd;
	   while(pswd!=passwd&&cnt!=3){
	     cout<<"password input error.Please re-enter again: ";
	     cin>>pswd; 
             cnt++;
	   }
	   if(cnt==3){
	 cout<<"password input error.Please try again later.\n";
	   return ;
	 }
	   else{
	   	cout<<"Enter new passwd: ";
	   	cin>>newwd;
	   	passwd = newwd;
	   	cout<<"new passwd is set successfully...\n";
	   }
	 
	}
	
	void print_info()const {
		cout<<"name: "<<name<<endl;
		cout<<"passwd: ";
		for(int i=0;i<=passwd.length();i++)
		cout<<"*";
		cout<<endl;
		cout<<"email: "<<email<<endl;
	}
	void static print_n(){ cout<<"there are "<<count<<" users"<<endl;}
	private:
	string name,passwd,email;
	static int count;
};
int User::count=0;

  运行结果

 

任务五那个string s1(length,'*') 测试编译报错,不得已换了个写法

后面再研究研究这个string类型具体用法

const的接口函数对应不熟练 多看 多练。

标签:std,cout,imag,Complex,实验,using,include
From: https://www.cnblogs.com/nitendoblog/p/16789679.html

相关文章

  • 实验5:开源控制器实践——POX
    h1pingh2截图h2和h3的tcpdump抓包结果截图L2_learning模块代码流程图......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实验......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践——OpenDaylight一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践——OpenDaylight一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境......
  • 实验二
    任务四Complex.hpp#include<iostream>#include<cmath>usingnamespacestd;classComplex{public:Complex(doublea=0.0,doubleb=0.0):real{a},imag{......
  • 数码管显示程序~实验板上的数码管从左到右逐个显示“0”、“1”、“2”、“3”
    1DAT_74164BITP0.6;头文件:74ALS164芯片的数据输入端接单片机引脚的p062CLK_74164BITP0.7;74LS164芯片的时钟端接单片机引脚......
  • 实验二
    实验4hpp#pragmaonce#include<iostream>usingnamespacestd;classComplex{public:Complex();Complex(doublereal0,doubleimag0=0);Complex......
  • 实验二
    Complex.hpp#include<iostream>#include<cmath>usingnamespacestd;classComplex{public:Complex(doublea=0,doubleb=0):real{a},imag{b}{};......
  • 实验5:开源控制器实践——POX
    建立拓扑hub验证h1pingh2都能收到h1pingh3都能收到switch验证h1pingh2仅h2收到h1pingh3仅h3收到流程图进阶重新建立拓扑,无流表,编写POX模块Send......
  • 实验一
    task1#include<stdio.h>intmain(){printf("OO\n");printf("<H><H>\n");printf("IIII\n");return0;}#include<stdio.h>intma......