首页 > 其他分享 >实验3

实验3

时间:2024-11-06 22:19:04浏览次数:1  
标签:std const string int void 实验 cout

#pragma once

#include <iostream>
#include <string>

using std::string;
using std::cout;

// 按钮类
class Button {
public:
    Button(const string &text);
    string get_label() const;
    void click();

private:
    string label;
};

Button::Button(const string &text): label{text} {
}

inline string Button::get_label() const {
    return label;
}

void Button::click() {
    cout << "Button '" << label << "' clicked\n";
}


#pragma once
#include "button.hpp"
#include <vector>
#include <iostream>

using std::vector;
using std::cout;
using std::endl;

// 窗口类
class Window{
public:
    Window(const string &win_title);
    void display() const;
    void close();
    void add_button(const string &label);

private:
    string title;
    vector<Button> buttons;
};

Window::Window(const string &win_title): title{win_title} {
    buttons.push_back(Button("close"));
}

inline void Window::display() const {
    string s(40, '*');

    cout << s << endl;
    cout << "window title: " << title << endl;
    cout << "It has " << buttons.size() << " buttons: " << endl;
    for(const auto &i: buttons)
        cout << i.get_label() << " button" << endl;
    cout << s << endl;
}

void Window::close() {
    cout << "close window '" << title << "'" << endl;
    buttons.at(0).click();
}

void Window::add_button(const string &label) {
    buttons.push_back(Button(label));
}


#include "window.hpp"
#include <iostream>

using std::cout;
using std::cin;

void test() {
    Window w1("new window");
    w1.add_button("maximize");
    w1.display();
    w1.close();
}

int main() {
    cout << "用组合类模拟简单GUI:\n";
    test();
}

实验2

#include <iostream>
#include <vector>

using namespace std;

void output1(const vector<int> &v) {
    for(auto &i: v)
        cout << i << ", ";
    cout << "\b\b \n";
}

void output2(const vector<vector<int>> v) {
    for(auto &i: v) {
        for(auto &j: i)
            cout << j << ", ";
        cout << "\b\b \n";
    }
}

void test1() {
    vector<int> v1(5, 42);
    const vector<int> v2(v1);

    v1.at(0) = -999;
    cout << "v1: ";  output1(v1);
    cout << "v2: ";  output1(v2);
    cout << "v1.at(0) = " << v1.at(0) << endl;
    cout << "v2.at(0) = " << v2.at(0) << endl;
}

void test2() {
    vector<vector<int>> v1{{1, 2, 3}, {4, 5, 6, 7}};
    const vector<vector<int>> v2(v1);

    v1.at(0).push_back(-999);
    cout << "v1: \n";  output2(v1);
    cout << "v2: \n";  output2(v2);

    vector<int> t1 = v1.at(0);
    cout << t1.at(t1.size()-1) << endl;
    
    const vector<int> t2 = v2.at(0);
    cout << t2.at(t2.size()-1) << endl;
}

int main() {
    cout << "测试1:\n";
    test1();

    cout << "\n测试2:\n";
    test2();
}

实验3

#pragma once

#include <iostream>
#include <cassert>

using std::cout;
using std::endl;

// 动态int数组对象类
class vectorInt{
public:
    vectorInt(int n);
    vectorInt(int n, int value);
    vectorInt(const vectorInt &vi);
    ~vectorInt();

    int& at(int index);
    const int& at(int index) const;

    vectorInt& assign(const vectorInt &v);
    int get_size() const;

private:
    int size;
    int *ptr;       // ptr指向包含size个int的数组
};

vectorInt::vectorInt(int n): size{n}, ptr{new int[size]} {
}

vectorInt::vectorInt(int n, int value): size{n}, ptr{new int[size]} {
    for(auto i = 0; i < size; ++i)
        ptr[i] = value;
}

vectorInt::vectorInt(const vectorInt &vi): size{vi.size}, ptr{new int[size]} {
    for(auto i = 0; i < size; ++i)
        ptr[i] = vi.ptr[i];
}

vectorInt::~vectorInt() {
    delete [] ptr;
}

const int& vectorInt::at(int index) const {
    assert(index >= 0 && index < size);

    return ptr[index];
}

int& vectorInt::at(int index) {
    assert(index >= 0 && index < size);

    return ptr[index];
}

vectorInt& vectorInt::assign(const vectorInt &v) {  
    delete[] ptr;       // 释放对象中ptr原来指向的资源

    size = v.size;
    ptr = new int[size];

    for(int i = 0; i < size; ++i)
        ptr[i] = v.ptr[i];

    return *this;
}

int vectorInt::get_size() const {
    return size;
}


#include "vectorInt.hpp"
#include <iostream>

using std::cin;
using std::cout;

void output(const vectorInt &vi) {
    for(auto i = 0; i < vi.get_size(); ++i)
        cout << vi.at(i) << ", ";
    cout << "\b\b \n";
}


void test1() {
    int n;
    cout << "Enter n: ";
    cin >> n;

    vectorInt x1(n);
    for(auto i = 0; i < n; ++i)
        x1.at(i) = i*i;
    cout << "x1: ";  output(x1);

    vectorInt x2(n, 42);
    vectorInt x3(x2);
    x2.at(0) = -999;
    cout << "x2: ";  output(x2);
    cout << "x3: ";  output(x3);
}

void test2() {
    const vectorInt  x(5, 42);
    vectorInt y(10, 0);

    cout << "y: ";  output(y);
    y.assign(x);
    cout << "y: ";  output(y);
    
    cout << "x.at(0) = " << x.at(0) << endl;
    cout << "y.at(0) = " << y.at(0) << endl;
}

int main() {
    cout << "测试1: \n";
    test1();

    cout << "\n测试2: \n";
    test2();
}

实验4

#pragma once

#include <iostream>
#include <cassert>
#define value 0

using namespace std;

// 类Matrix的声明
class Matrix {
public:
    Matrix(int n, int m);           // 构造函数,构造一个n*m的矩阵, 初始值为value
    Matrix(int n);                  // 构造函数,构造一个n*n的矩阵, 初始值为value
    Matrix(const Matrix &x);        // 复制构造函数, 使用已有的矩阵X构造
    ~Matrix();

    void set(const double *pvalue);         // 用pvalue指向的连续内存块数据按行为矩阵赋值
    void clear();                           // 把矩阵对象的值置0
    
    const double& at(int i, int j) const;   // 返回矩阵对象索引(i,j)的元素const引用
    double& at(int i, int j);               // 返回矩阵对象索引(i,j)的元素引用
    
    int get_lines() const;                  // 返回矩阵对象行数
    int get_cols() const;                   // 返回矩阵对象列数

    void display() const;                    // 按行显示矩阵对象元素值

private:
    int lines;      // 矩阵对象内元素行数
    int cols;       // 矩阵对象内元素列数
    double *ptr;
};

Matrix::Matrix(int n,int m)
{
    lines=n;
    cols=m;
    ptr=new double[lines*cols];
    for (int i =0;i<n;++i)
        for (int j=0;j<m;++j)
            ptr[i*lines+j]=value;
}
Matrix::Matrix(int n)
{
    lines=n;
    cols=n;
    ptr=new double[lines*cols];
    for (int i =0;i<n;++i)
        for (int j=0;j<n;++j)
            ptr[i*lines+j]=value;
}
Matrix::Matrix(const Matrix &x){
    lines=x.lines;
    cols=x.cols;
    ptr=new double[lines*cols];
    for (int i =0;i<x.lines;++i)
        for (int j=0;j<x.cols;++j)
        {
            ptr[i*lines+j]=x.ptr[i*lines+j];
        }
}

Matrix::~Matrix()
{
    delete [] ptr;
}

void Matrix::set(const double *pvalue)
{
    
    for(int i=0;i<lines*cols;++i)
        ptr[i]=*pvalue++;
}

void Matrix::clear()
{
    for(int i=0;i<lines;++i)
        for(int j=0;j<cols;++j)
        {
            ptr[i*lines+j]=value;
        }
}

const double& Matrix::at(int i, int j) const
{
    return ptr[i*lines+j];
}

double& Matrix::at(int i, int j)
{
    return ptr[i*lines+j];
}

int Matrix::get_lines() const
{
    return lines;
}

int Matrix::get_cols() const
{
    return cols;
}

void Matrix::display() const
{
    for(int i=0;i<lines*cols;++i) 
    {
        if ((i+1)%cols!=0)
            cout<<ptr[i]<<' ';
        else 
            cout<<ptr[i]<<"\n";
    }
        
}

实验5

#pragma once
#include <iostream>
#include <string>

using namespace std;

class User{
public:
    User(string text);
    User(string text1,string text2,string text3);
    void set_email();
    void change_password();
    void display(); 
private:
    string name;
    string password;
    string email;
};

User::User(string text)
{
    name=text;
    password="123456";
    email="";
}

User::User(string text1,string text2,string text3)
{
    name=text1;
    password=text2;
    email=text3;
}

void User::set_email()
{
    cout<<"Enter email address:  ";
    string text;cin>>text;
    while(text.find('@')==std::string::npos)
    {
        cout<<"illegal email. Please re_enter email:  ";
        cin>>text;
    }
    cout<<"email is set successfully..."<<endl;
    email=text;    
}
void User::change_password()
{
    cout<<"Enter old password:";
    string pre_password;cin>>pre_password;
    int cnt=1;
    for (int i=0;i<2;++i)
    {
        if (pre_password!=password)
        {
            cout<<"password input error. Please re_enter angain:  ";
            cin>>pre_password;
            cnt++;
        }
        else break;
    }
    if (cnt==3) 
    {
        cout<<"password input error. Please try after a while."<<endl; 
        return ;
    }
    cout<<"Enter new password: ";
    string new_password;cin>>new_password;
    password=new_password;
    cout<<"new pssword is set successfully..."<<endl;
}

void User::display()
{
    cout<<"name:   "<<name<<endl;
    string s(password.length(), '*');
    cout<<"pass:   "<<s<<endl;
    cout<<"email:  "<<email<<endl;
}


#include "user.hpp"
#include <iostream>
#include <vector>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;

void test() {
    vector<User> user_lst;

    User u1("Alice", "2024113", "[email protected]");
    user_lst.push_back(u1);
    cout << endl;

    User u2("Bob");
    u2.set_email();
    u2.change_password();
    user_lst.push_back(u2);
    cout << endl;

    User u3("Hellen");
    u3.set_email();
    u3.change_password();
    user_lst.push_back(u3);
    cout << endl;

    cout << "There are " << user_lst.size() << " users. they are: " << endl;
    for(auto &i: user_lst) {
        i.display();
        cout << endl;
    }
}

int main() {
    test();
}

实验6

#ifndef __DATE_H__
#define __DATE_H__
class Date{
private:
    int day;
    int month;
    int year;
    int totalDays;
public:
    Date(int year,int month,int day);
    int getDay()const {return day;}
    int getMonth()const {return month;}
    int getYear()const {return year;}
    int getMaxDay() const;
    bool isLeapYear() const
    {
        return year%4==0&&year%100!=0||year%400==0;
    }
    void show() const;
    int Distance(const Date& date) const{
        return totalDays-date.totalDays;
    }
};
#endif


#include "date.h"
#include <iostream>
#include <cstdlib>
using namespace std;
namespace{
    const int DAYS_BEFORE_MONTH[]={0,31,59,90,120,151,181,212,243,273,304,334,365};
}
Date::Date(int year,int month,int day):year(year),month(month),day(day)
{
    if (day<=0||day>getMaxDay())
    {
        cout<<"Invalid date"<<endl;
        show();
        cout<<endl;
        exit(-1);
    }
    int years=year-1;
    totalDays=years*365+years/4-year/100+year/400+DAYS_BEFORE_MONTH[month-1]+day;
    if (isLeapYear()&&month>2)totalDays++;
}
int Date::getMaxDay() const
{
    if (isLeapYear()&&month==2)
    {
        return 2;
    }
    else 
    {
        return DAYS_BEFORE_MONTH[month]-DAYS_BEFORE_MONTH[month-1];
    }
}
void Date::show() const
{
    cout<<getYear()<<"-"<<getMonth()<<"-"<<getDay();
}


#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__
#include "date.h"
#include <string>
class SavingsAccount{
private:
    std::string id;
    double balance;
    double rate;
    Date lastDate;
    double accumulation;
    static double total;
    void record(const Date &date,double amount,const std::string &desc);
    void error(const std::string &msg) const;
    double accumulate(const Date &date) const
    {
        return accumulation+balance*date.Distance(lastDate);
    }
public:
    SavingsAccount(const Date &date,const std::string &id,double rate);
    const std::string &getId() const {return id;}
    double getBalance() const {return balance;}
    double getRate() const {return rate;}
    static double getTotal() {return total;}
    void deposit (const Date &date,double amount,const std::string &desc);
    void withdraw (const Date &date,double amount,const std::string &desc);
    void settle (const Date &date);
    void show() const;
}; 
#endif


#include "account.h"
#include <cmath>
#include <iostream>
using namespace std;
double SavingsAccount::total=0;
SavingsAccount::SavingsAccount(const Date &date,const std::string &id,double rate)
    :id(id),balance(0),rate(rate),lastDate(date),accumulation(0)
{
    date.show();
    cout<<"\t#"<<id<<"created"<<endl;
}
void SavingsAccount::record(const Date &date,double amount,const std::string &desc)
{
    accumulation=accumulate(date);
    lastDate=date;
    amount=floor(amount*100+0.5)/100;
    balance+=amount;
    total+=amount;
    date.show();
    cout<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<"\t"<<desc<<endl;
}
void SavingsAccount::error(const string &msg) const
{
    cout<<"Error(#"<<id<<")"<<msg<<endl;
}
void SavingsAccount::deposit(const Date &date,double amount,const string &desc)
{
    record(date,amount,desc);
}
void SavingsAccount::withdraw(const Date &date,double amount,const string &desc)
{
    if (amount>getBalance())
    {
        error("not enough money");
    }
    else 
    {
        record(date,-amount,desc);
    }
}
void SavingsAccount::settle(const Date &date)
{
    double interest=accumulate(date)*rate/date.Distance(Date(date.getYear()-1,1,1));
    if (interest!=0)
    {
        record(date,interest,"interest");
        accumulation=0;
    }
}
void SavingsAccount::show() const
{
    cout<<id<<"\tBalance:"<<balance;
}
 


#include "account.h"
#include <iostream>
using namespace std;
int main()
{
    Date date(2008,11,1);
    SavingsAccount accounts[]=
    {
        SavingsAccount(date,"03755217",0.015),
        SavingsAccount(date,"02342342",0.015)
    };
    const int n=sizeof(accounts)/sizeof(SavingsAccount);
    
    accounts[0].deposit(Date(2008,11,5),5000,"salary");
    accounts[1].deposit(Date(2008,11,25),10000,"sell stock 0323");
    accounts[0].deposit(Date(2008,12,5),5500,"salary");
    accounts[1].withdraw(Date(2008,12,20),4000,"buy a laptop");
    cout<<endl;
    for (int i=0;i<n;i++)
    {
        accounts[i].settle(Date(2009,1,1));
        accounts[i].show();
        cout<<endl;
    }
    cout<<"Total:"<<SavingsAccount::getTotal()<<endl;
    return 0;
}

 

标签:std,const,string,int,void,实验,cout
From: https://www.cnblogs.com/5742TD/p/18531179

相关文章

  • Linux 外设驱动 应用 6 摄像头采集实验
    摄像头采集实验1摄像头基础介绍1.1驱动介绍-V4L2简介1.2硬件介绍2代码编写2.1打开设备文件2.2取得设备的capability,看看设备具有什么功能,比如是否具有视频输入,或者音频输入输出等2.3选择视频输入,一个视频设备可以有多个视频输入。VIDIOC_S_INPUT,structv4l2......
  • 实验3 类和对象 基础编程
    实验一task1.cpp#include"window.hpp"#include<iostream>usingstd::cout;usingstd::cin;voidtest(){Windoww1("newwindow");w1.add_button("maximize");w1.display();w1.close();}intmain(){......
  • 基于蓝桥杯实验豆不够衍生出的【假装学习】方法
    前言首先是感谢两位大佬给的思路@okfang616@kkkkkba ,下方是两位大佬的原贴地址https://blog.csdn.net/m0_52537917/article/details/136222428?spm=1001.2014.3001.5502起因是拿豆子抽奖给抽上头了,导致实验豆子3=100痛失97实验豆 在咨询小蓝得知这个豆子给的是真的太......
  • 实验3 类和对象_基础编程2
    实验任务1:button.hpp源码:1#pragmaonce23#include<iostream>4#include<string>56usingstd::string;7usingstd::cout;89//按钮类10classButton{11public:12Button(conststring&text);13stringget_label()con......
  • 20222323 2024-2025-1 《网络与系统攻防技术》实验四实验报告
    一、实验内容(一)恶意代码文件类型标识、脱壳与字符串提取对提供的rada恶意代码样本,进行文件类型识别,脱壳与字符串提取,以获得rada恶意代码的编写作者,具体操作如下:(1)使用文件格式和类型识别工具,给出rada恶意代码样本的文件格式、运行平台和加壳工具;(2)使用超级巡警脱壳机等脱壳软件,......
  • ssm安徽新华学院实验中心管理系统的设计与实现+jsp
    前言本安徽新华学院实验中心管理系统的设计目标是实现安徽新华学院实验中心的信息化管理,提高管理效率,使得安徽新华学院实验中心管理工作规范化、科学化、高效化。本文重点阐述了安徽新华学院实验中心管理系统的开发过程,以实际运用为开发背景,基于SSM框架,运用了JSP技术和MYS......
  • 【吴恩达机器学习笔记】7.2-->logistic回归-->可选实验室笔记
    这张图展示了逻辑回归模型在处理分类数据时的一个示例,特别是关于肿瘤大小与肿瘤性质(良性或恶性)之间的关系。图中各个部分的解释:坐标轴:横轴(X轴)表示肿瘤的大小。纵轴(Y轴)表示肿瘤是良性(0)还是恶性(1)。数据点:蓝色圆圈代表良性肿瘤。红色叉号代表恶性肿瘤。决策边界:......
  • 实验4:二叉树的基本操作
    c++解释:new相当于malloc()函数,其他没有区别!点击查看代码#include<iostream>usingnamespacestd;structtree{ intdata; tree*light,*ture;};intjie,shen,maxx;//创建tree*chu(){ tree*head; head=newtree; cout<<"请输入数值:\n"; cin>&g......
  • 实验四
    task1:源代码:#include<stdio.h>#defineN4#defineM2voidtest1(){intx[N]={1,9,8,4};inti;//输出数组x占用的内存字节数printf("sizeof(x)=%d\n",sizeof(x));//输出每个元素的地址、值for(i=0;i<N;++i)pr......
  • 20222408 2024-2025-1 《网络与系统攻防技术》实验四实验报告
    1.实验内容1.1实验要求(1)对恶意代码样本进行识别文件类型、脱壳、字符串提取操作。(2)使用IDAPro静态或动态分析所给的exe文件,找到输出成功信息的方法。(3)分析恶意代码样本并撰写报告,回答问题。(4)对于Snort收集的蜜罐主机5天的网络数据源进行分析,回答问题。1.2学习内容恶意代码......