当前位置: 首页 > 其他范文 > 其他范文

C,,实验二

作者:zhang5230 | 发布时间:2020-12-26 00:43:58 收藏本文 下载本文

实验题目:

年级:

姓名:

学号:

指导老师:

完成日期:

实验目的:

实验内容:

设计思路:

程序:

#include #include using namespace std;class CPerson { public: string name;int number;string sex;void set(string ming, int num, string xing){ name = ming;number = num;sex = xing;cout<<"教师数据为:"<>gangwei1;cout<<" 教 师 岗 位 为 :"<>gangwei2;cout<<" 职 员 岗 位 为 :"<

void main(){ int num1, num2;string name1, name2, xingbie1, xingbie2;CTeacher *p =new CTeacher;CEmployee *q = new CEmployee;cout<<"(1)请输入教师数据"<>name1;cout<<"编号:";cin>>num1;cout<<"性别:";cin>>xingbie1;p->set(name1, num1, xingbie1);p->print();cout<>name2;cout<<"编号:";cin>>num2;cout<<"性别:";cin>>xingbie2;q->set(name2, num2, xingbie2);q->print();delete p;p = NULL;delete q;q = NULL;} 结果及分析:

(1)请输入教师数据 姓名:wang 编号:2001 性别:f 教师数据为:

姓名:wang 证件号:2001 性别:f 请输入教师的岗位:professor 教师岗位为:professor(2)请输入职员数据 姓名:li 编号:2002 性别:m 教师数据为:

姓名:li 证件号:2002 性别:m 请输入职员的岗位:guard 职员岗位为:guard

高级程序设计语言 C++(实验二)姓名:刘梦曦 班级:通信 101 学号:2010101012 3 实验内容:

设计思路:

程序:

#include using namespace std;class CTimeType { public: CTimeType(int i, int j){ m_hour = i;m_minute = j;} int m_hour;int m_minute;};class CDateType { public: CDateType(int y, int m, int d){ m_year = y;m_month = m;m_day = d;} int m_year;int m_month;int m_day;};class CTimeDate:public CTimeType,public CDateType { public: CTimeDate(int i, int j, int y, int m, int d):CTimeType(i, j),CDateType(y, m, d){}

高级程序设计语言 C++(实验二)姓名:刘梦曦 班级:通信 101 学号:2010101012 4 void judge1(){ if((m_hour >= 0 && m_hour < 24)&&(m_minute >=0 && m_minute < 60)){ cout<<"时间正确!";} else { cout<<"时间错误!";} } void judge2(){ int flag = 1, flas = 1;if((m_year % 4 == 0 && m_year % 100!= 0)|| m_year % 400 == 0){ if(m_month <= 12 && m_month >=1){ if(m_month == 1 || m_month == 3 || m_month == 5 || m_month == 7 || m_month == 8 || m_month == 10 || m_month == 12){ if(0 < m_day && m_day <= 31){ flag = 0;} } else if(m_month == 4||m_month ==6 || m_month == 9 || m_month == 11){ if(0 < m_day && m_day <= 30){ flag = 0;)} else { if(0 < m_day && m_day <= 29){ flag = 0;} } } if(0 == flag){ cout<

高级程序设计语言 C++(实验二)姓名:刘梦曦 班级:通信 101 学号:2010101012 5 cout<<"输入的年月日是有效的!"<=1){ if(m_month == 1 || m_month == 3 || m_month == 5 || m_month == 7 || m_month == 8 || m_month == 10 || m_month == 12){ if(0 < m_day && m_day <= 31){ flag = 0;} } else if(m_month == 4||m_month ==6 || m_month == 9 || m_month == 11){ if(0 < m_day && m_day <= 30){ flag = 0;} } else { if(0 < m_day && m_day <= 28){ flag = 0;} } } if(0 == flag){ cout<<"输入的年月日是有效的!"<

高级程序设计语言 C++(实验二)姓名:刘梦曦 班级:通信 101 学号:2010101012 6 if(0 >year>>month>>date;cout<<"请输入时间:";cin>>hour>>minute;CTimeDate s(hour,minute,year,month,date);s.judge1();s.judge2();s.show();}结果及分析:

请输入年月日:1999 10 1 请输入时间:10 30 时间正确!1999 不是闰年 输入的年月日是有效的!输入的年月日和时间为:

1999/10/1 10:30

高级程序设计语言 C++(实验二)姓名:刘梦曦 班级:通信 101 学号:2010101012 7 实验内容:

设计思路:

序 程序 1(成员函数):成员函数部分 class Complex { public: double real, xu;Complex(){ real = 0;xu = 0;} Complex operator+(Complex);Complex operator-(Complex);Complex operator*(Complex);Complex operator/(Complex);};Complex Complex::operator+(Complex obj){ real += obj.real;xu += obj.xu;return *this;} Complex Complex::operator-(Complex obj){ real-= obj.real;xu-= obj.xu;return *this;} Complex Complex::operator*(Complex obj){ double i;i = real;real = obj.real * real-obj.xu * xu;xu = obj.real * xu + obj.xu * i;return *this;} Complex Complex::operator/(Complex obj){ double i;i = real;real =(obj.real * real + obj.xu * xu)/(obj.real *obj.real + obj.xu * obj.xu);xu =(obj.real * xu-obj.xu * i)/(obj.real *obj.real + obj.xu * obj.xu);return *this;}

高级程序设计语言 C++(实验二)姓名:刘梦曦 班级:通信 101 学号:2010101012 8 序 程序 2(友元函数):友元函数部分 class Complex { public: double real,xu;Complex(){ real=0;xu=0;} friend Complex operator+(Complex &obj1, Complex &obj2){ obj1.real=obj1.real + obj2.real;obj1.xu=obj1.xu +obj2.xu;return obj1;} friend Complex operator-(Complex &obj1, Complex &obj2){ obj1.real=obj1.real-obj2.real;obj1.xu=obj1.xu-obj2.xu;return obj1;} friend Complex operator*(Complex &obj1, Complex &obj2){ obj1.real=obj2.real * obj1.real-obj2.xu * obj1.xu;obj1.xu=obj2.real * obj1.xu + obj2.xu * obj1.real;return obj1;} friend Complex operator/(Complex &obj1, Complex &obj2){ obj1.real=(obj2.real * obj1.real + obj2.xu * obj1.xu)/(obj2.real * obj2.real + obj2.xu * obj2.xu);obj1.xu=(obj2.real * obj1.xu-obj2.xu * obj1.real)/(obj2.real * obj2.real + obj2.xu * obj2.xu);return obj1;} };程序(主函数部分):

#include using namespace std;void main(){ Complex aa;Complex bb;Complex cc;cout<<"输入第一个复数的实部和虚部:";cin>>aa.real>>aa.xu;

高级程序设计语言 C++(实验二)姓名:刘梦曦 班级:通信 101 学号:2010101012 9 cout<<"(1)加号"<>bb.real;cin>>bb.xu;cc = aa + bb;cout<<"结果: "<<"实部为"<>bb.real;cin>>bb.xu;cc = aa-bb;cout<<"结果: "<<"实部为"<>bb.real;cin>>bb.xu;cc = aa * bb;cout<<"结果: "<<"实部为"<>bb.real;cin>>bb.xu;cc = aa / bb;cout<<"结果: "<<"实部为"<

输入第一个复数的实部和虚部:4 5(1)加号 输入第二个复数的实部和虚部:1 1 结果: 实部为 5 虚部为 6(2)减号 输入第二个复数的实部和虚部:2 3 结果: 实部为 3 虚部为 3(3)乘号 输入第二个复数的实部和虚部:1 1 结果: 实部为 0 虚部为 6(4)除号 输入第二个复数的实部和虚部:1 1 结果: 实部为 3 虚部为 3

高级程序设计语言 C++(实验二)姓名:刘梦曦 班级:通信 101 学号:2010101012 10 实验内容:

设计思路:

程序:

#include using namespace std;class CShape { public: virtual float area()=0;};class CTriangle:public CShape { private: float m_H, m_W;public: CTriangle(float h, float w){ m_H=h;m_W=w;} float area(){ return(float)(m_H *m_W * 0.5);} };class CRect:public CShape { private: float m_H, m_W;public: CRect(float h, float w){ m_H = h;m_W = w;} float area(){ return(float)(m_H * m_W);} };class CCircle:public CShape

高级程序设计语言 C++(实验二)姓名:刘梦曦 班级:通信 101 学号:2010101012 11 { private: float m_R;public: CCircle(float r){ m_R=r;} friend float sum(CShape *s1, CShape *s2, CShape *s3){ return(float)(s1->area()+ s2->area()+ s3->area());} float area(){ return(float)(3.1416 * m_R * m_R);} };void main(){ CShape *s[3]={ new CTriangle(3,4), new CRect(3,4), new CCircle(5)};cout<<"三角形、矩形、圆形的面积分别为:"<area()<

三角形、矩形、圆形的面积分别为:

6 12 78.54 总面积为:96.54 存在问题及解决方案:

C,,实验五

c,,实验三

C实验六范文

c语言实验心得体会

C(II)实验六

本文标题: C,,实验二
链接地址:https://www.dawendou.com/fanwen/qitafanwen/337464.html

版权声明:
1.大文斗范文网的资料来自互联网以及用户的投稿,用于非商业性学习目的免费阅览。
2.《C,,实验二》一文的著作权归原作者所有,仅供学习参考,转载或引用时请保留版权信息。
3.如果本网所转载内容不慎侵犯了您的权益,请联系我们,我们将会及时删除。

重点推荐栏目

关于大文斗范文网 | 在线投稿 | 网站声明 | 联系我们 | 网站帮助 | 投诉与建议 | 人才招聘 | 网站大事记
Copyright © 2004-2025 dawendou.com Inc. All Rights Reserved.大文斗范文网 版权所有