没有合适的资源?快使用搜索试试~ 我知道了~
第1次作业_王晨阳_571191011
需积分: 0 0 下载量 140 浏览量
2022-08-08
19:45:52
上传
评论
收藏 103KB DOCX 举报
温馨提示
试读
17页
第1次作业_王晨阳_571191011
资源详情
资源评论
资源推荐
EX1: Complex Class
Code
Complex.h
#ifndef COMPLEX_H
#define COMPLEX_H
#include<iostream>
using std::ostream;
using std::istream;
class Complex
{
friend ostream &operator<<(ostream &,const Complex &);
public:
Complex(double=0.0,double=0.0);
Complex operator+(const Complex&) const;
Complex operator-(const Complex&) const;
private:
double r;
double i;
};
#endif
Complex.cpp
#include "Complex.h"
ostream &operator<<(ostream &output,const Complex &c)
{
output<<"("<<c.r<<","<<c.i<<")";
return output;
}
Complex::Complex(double real,double imaginary)
{
this->r=real;
this->i=imaginary;
}
Complex Complex::operator+(const Complex&c) const
{
Complex temp(this->r + c.r,this->i + c.i);
return temp;
}
Complex Complex::operator-(const Complex&c) const
{
Complex temp(this->r - c.r,this->i - c.i);
return temp;
}
main.cpp
#include "Complex.h"
using std::cin;
using std::cout;
int main()
{
Complex x(1,7),y(9,2),z(10,1),t(11,5);
cout<<x<<'+'<<y<<'='<<x+y<<'\n';
cout<<z<<'-'<<t<<'='<<z-t<<'\n';
return 0;
}
Demo
EX2: Enhancing Time Class
Code
Time.h
#ifndef TIME_H
#define TIME_H
class Time
{
public:
friend void tick(Time &t);
explicit Time(int=0,int=0,int=0);
void setTime(int,int,int);
void setHour(int);
void setMinute(int);
void setSecond(int);
unsigned int getHour() const;
unsigned int getMinute() const;
unsigned int getSecond() const;
void printUniversal() const;
void printStandard() const;
private:
unsigned int hour;
unsigned int minute;
unsigned int second;
};
#endif
Time.cpp
#include<iostream>
#include<iomanip>
#include<stdexcept>
#include<windows.h>
#include "Time.h"
using std::invalid_argument;
void tick(Time &t)
{
while (true)
{
t.printStandard();
if (t.getSecond()==59)
{
t.setSecond(0);
if (t.getMinute()==59)
{
t.setMinute(0);
if (t.getHour()==23)
t.setHour(0);
else
t.setHour(t.getHour()+1);
}
else
t.setMinute(t.getMinute()+1);
}
else
t.setSecond(t.getSecond()+1);
Sleep(1000);
}
}
Time::Time(int hour,int minute,int second)
{
setTime(hour,minute,second);
}
void Time::setTime(int h,int m,int s)
{
if ((h>=0 && h<24) && (m>=0 && m<60) && (s>=0 && s<60))
{
hour=h;
minute=m;
second=s;
}
else
throw invalid_argument("hour,minute and/or second was out
of range");
}
void Time::setHour(int h)
{
if (h>=0 && h<24)
hour=h;
else
throw invalid_argument("hour must be 0-23");
}
void Time::setMinute(int m)
{
if (m>=0 && m<60)
minute=m;
else
throw invalid_argument("minute must be 0-59");
}
void Time::setSecond(int s)
{
if (s>=0 && s<60)
second=s;
else
throw invalid_argument("second must be 0-59");
}
unsigned int Time::getHour() const
{
return hour;
}
剩余16页未读,继续阅读
焦虑肇事者
- 粉丝: 58
- 资源: 310
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于Typescript和PHP的编程知识储备库设计源码 - study-php
- Screenshot_2024-05-28-11-40-58-177_com.tencent.mm.jpg
- 基于Dart的Flutter小提琴调音器APP设计源码 - violinhelper
- 基于JavaScript和CSS的随寻订购网页设计源码 - web-order
- 基于MATLAB的声纹识别系统设计源码 - VoiceprintRecognition
- 基于Java的微服务插件集合设计源码 - wsy-plugins
- 基于Vue和微信小程序的监理日志系统设计源码 - supervisionLog
- 基于Java和LCN分布式事务框架的设计源码 - tx-lcn
- 基于Java和JavaScript的茶叶评级管理系统设计源码 - tea
- IMG_5680.JPG
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0