




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验一 数据类型和表达式选课时间:星期三下午 学号14084228 姓名 徐志鹏 【实验目的】1、熟悉并学习使用C+程序编译平台VC6.0;2、掌握如何在编译平台下编辑、编译、连接和运行一个简单的C+程序;3、掌握C+语言基础数据类型,熟悉变量及常量的定义和赋值方法;4、学会使用C+算术运算符和算术表达式;5、掌握C+程序的赋值和输入输出语句的使用方法;6、掌握简单C+程序的编写和调试【源程序】1: #include<iostream>using namespace std;int min(int a , int b)if(a<b)return a;return b;int
2、main()int a,b;cin>>a>>b;cout<<min(a,b);return 0;2:#include<iostream>using namespace std;int main()int a;double b;cin>>a>>b;if(a<b)cout<<a<<endl;else cout<<b<<endl;return 0;3:#include<iostream>using namespace std;int main()double a,
3、b;cin>>a;b=a*(9.00/5.00)+32;cout<<b;return 0;4:#include<iostream>#define fhcl 1.60934using namespace std;int main()double a,b;cin>>a;b=a/fhcl;cout<<b;return 0;5:#include<iostream>using namespace std;int main()int a,i,b; cin>>a;if(a>=1000) b=a%10000; a=a-b
4、+1111; else if(a>=100&&a<1000)a=111; else if(a>=10&&a<100)a=11; else if(a>=0&&a<10)a=1; cout<<a;return 0;【错误及解决办法】【实验总结】实验一比较简单,没有出现什么错误,代码实现和c差不多。 实验四 函数和作用域选课时间:星期三下午 学号14084228 姓名 徐志鹏 【实验目的】1、掌握函数的定义和说明方法;2、掌握函数调用时的传值调用、传址调用和引用调用方法;3、掌握函数重载的方法;4、学习使
5、用指向字符串的指针变量;5、掌握作用域的种类和范围;【源程序】 1:#include<iostream>#include<math.h>using namespace std;void conver(double *x , double *y) double a,b; a=(*x)*cos(*y); b=(*x)*sin(*y); cout<<a<<'n'<<b<<'n'void conver(double &x , double &y) double a,b; a=x*co
6、s(y); b=x*sin(y); cout<<a<<'n'<<b;int main()double x,y;cin>>x>>y;conver(&x,&y); /传值调用conver(x,y); /引用调用cout<<x<<'n'<<y;return 0;2:#include<iostream>using namespace std;typedef struct fu double zb ; double xb ;fushu;void pr
7、oduct(double &a,double &b) /浮点数相乘 double c; c=a*b; cout<<c<<'n'void product(fushu &a,fushu &b) /复数相乘 fushu c; c.zb=a.zb*b.zb-a.xb*b.xb; c.xb=a.zb*b.xb+a.xb*b.zb; cout<<c.zb<<"+"<<c.xb<<"i"<<'n'int main() fu
8、shu a,b ;double x,y;cout<<"两个浮点数"<<'n'cin>>x>>y;product(x,y);cout<<"两个复数"<<'n'cin>>a.zb>>a.xb;cin>>b.zb>>b.xb;product(a,b);return 0;3:#include<iostream>#include<math.h>#define pi 3.14using nam
9、espace std;int computeday(int year , int mot , int day) int num ; num = year*365 + mot*30 +day ; return num;double compute_physiological_index(int age) double cp ; cp = sin(2*pi)/28)*age); return cp ;double compute_emotional_index(int age) double cp ; cp = sin(2*pi)/23)*age); return cp ;double compu
10、te_mentality_index(int age) double cp ; cp = sin(2*pi)/33)*age); return cp ;int main() int y,m,d,n; double p,e,cm; cout<<"输入出生年月日" cin>>y>>m>>d; n=computeday(y,m,d); p=compute_physiological_index(n); cout<<"生理指数"<<p<<'n' e=compute
11、_emotional_index(n); cout<<"情绪指数"<<e<<'n' cm=compute_mentality_index(n); cout<<"智力指数"<<cm<<'n'【错误及解决办法】【实验总结】更加熟悉对函数的使用实验5 类与对象(一)【实验目的】1、掌握类的概念以及定义类的方法;2、学习简单面向对象程序的编写;【源程序】1:#include<iostream>using namespace std;class Fa
12、npublic : void set(double s , double r , bool o , string c) speed = s ; radius = r ; on = o ; color = c ; void display() cout<<"Speed Is : "<<speed<<'n' cout<<"Radius Is : "<<radius<<'n' cout<<"Color Is : "<&l
13、t;color<<'n' cout<<"On Is : "<<on<<'n' private:double speed , radius ;bool on ;string color ;int main() Fan fan1 , fan2 ; cout<<"The first fan's information : "<<'n' fan1.set(3,10,true,"yellow"); fan1.displ
14、ay(); cout<<"The secend fan's information : "<<'n' fan2.set(2,5,false,"blue"); fan2.display();return 0;2:#include<iostream>using namespace std;class Accountpublic : void set(int i , double b , double a) id = i ; balance = b ; annualInterestRate = a ;
15、 double getMonthlyInsterestRate(double a) return a/12 ; double withDraw(double m) balance -= m ; return balance ; double deposit(double m) balance += m ; return balance ; void display() cout<<"Id Is : "<<id<<'n' cout<<"Balance Is : "<<balance
16、<<'n' cout<<"GetMonthlyInsterestRate Is : "<<getMonthlyInsterestRate(annualInterestRate)*100<<"%"<<'n' private: int id ; double balance , annualInterestRate ;int main() Account user ; user.set(1122 , 20000 , 0.045); user.withDraw(2500
17、); user.deposit(3000); user.display();【错误及解决办法】一开始字符串用数组存发生一些莫名错误后面发现可以直接用string.【实验总结】定义类时,那些成员数据和成员函数是必须的,那些不是?public、protected、private分别表示什么意思?能否对类的成员数据在定义时初始化?在声明类时,没有必须的成员函数和成员数据。public代表公用成员,可在类外进行调用;private代表私有成员,只能被本类的成员函数访问,不能被类外访问;protect代表受保护的成员,可以被本类以及派生类的成员函数调用。不能对类的成员数据在定义时进行初始化。账户余额变量
18、balance是否必须定义为静态变量?为什么?不一定必须定义为静态变量,普通变量的加减也可以满足该题的要求。实验6 类与对象(二)【实验目的】1、掌握类的构造函数和析构函数的概念和使用方法;2、掌握对象数组、对象指针的定义和使用方法;3、掌握new和delete的使用方法;【源程序】#include<iostream>using namespace std;class CPointpublic : CPoint( double xx = 1 , double xy = 2) x = xx ; y = xy ; void SetX( double xx ) x = xx ; void
19、 SetY( double xy ) y = xy ; double GetX () return x ; double GetY () return y ; private: double x , y ; class CRectangle public : CRectangle( const CPoint & x , const CPoint & y ) l = x ; r = y ; void SetLPoint(const CPoint & x ) l = x ; void SetRPoint(const CPoint & y ) r = y ; doub
20、le GetPerimeter() double x , y ; x = 2 * ( r.GetX() - l.GetX() ) ; y = 2 * ( r.GetY() - l.GetY() ) ; return (x+y) ; double GetArea() double x , y ; x = r.GetX() - l.GetX() ; y = r.GetY() - l.GetY() ; return x*y ; private: CPoint l , r ; ;int main()CPoint x(2,5) ;CPoint y(6,8) ;double p , a ;CRectang
21、le * a_rectagnle = new CRectangle( x , y ) ; if( a_rectagnle != NULL ) p = a_rectagnle -> GetPerimeter() ; a = a_rectagnle -> GetArea(); cout<<"Perimeter is : "<<p<<'n' cout<<"Area is : "<<a<<'n' delete a_rectagnle ;实验七 继承
22、与派生【实验目的】1、理解继承在面向对象程序设计中的重要作用;2、理解继承和派生的概念;3、掌握通过继承派生出一个新类的方法;4、进一步学习简单面向对象程序的编写;【源程序】#include <iostream>using namespace std;typedef enumAssistantProfessor,AssociateProfessor,Professorrankind;class MYDatepublic: MYDate(int y=1990 , int m=1 , int d=1) year = y ; month = m ; day = d ; int diffy
23、ear(MYDate a) int y, m = 0; if(a.year > year) y = a.year - year - 1; m = a.month + 12 - month + y * 12; else y = year - a.year - 1; m = month + 12 - a.month + y * 12; return m; void print() cout<<"year:"<<year<<"month"<<month<<"day"<&
24、lt;day<<endl; int Gety() return year; int Getm() return month; int Getd() return day; private: int year , month , day ;class Personpublic: Person(string n , string i , string p) name = n ; id = i ; phonenumber = p ; void print() cout<<"name:"<<name<<endl; cout<&l
25、t;"id:"<<id<<endl; cout<<"phonenumber:"<<phonenumber<<endl; private: string name ; string id ; string phonenumber ;class Student: public Personpublic: void print() Person:print(); cout<<"Grade:"<<grade<<endl; Student(strin
26、g name,string id,string phonenumber,string grade): Person(name,id,phonenumber),grade(grade)private: const string grade;class Employee: public Personpublic: Employee(string name , string id , string phonenumber , string of , double sal , MYDate dat): Person(name , id , phonenumber) office = of ; sala
27、ry = sal ; dateHired = dat ; MYDate GetDate() return dateHired; void print() Person:print(); cout<<"office:"<<office<<endl; cout<<"salary:"<<salary<<endl; cout<<"y:"<<dateHired.Gety()<<endl; cout<<"m:"
28、;<<dateHired.Getm()<<endl; cout<<"d:"<<dateHired.Getd()<<endl; private: string office ; double salary ; MYDate dateHired ;class Faculty:public Employeepublic: Faculty(string name , string id , string phonenumber , string of , double sal , MYDate dat,int rank,
29、double Basic): Employee( name , id , phonenumber , of , sal , dat), rank(rankind(rank - 1), Basicsalary(Basic) int xinshui() return Basicsalary * (int)rank + 1); void print() Employee:print(); cout<<"rank:"<<rank<<endl; private: const rankind rank; double Basicsalary;clas
30、s Staff:public Employeepublic: Staff(string name , string id , string phonenumber , string of , double sal , MYDate dat,string po,double BasicWages, double Allowance): Employee( name , id , phonenumber , of , sal , dat), BasicWages(BasicWages),Allowance(Allowance) position = po ; int xins(MYDate d)
31、int res ; MYDate x = GetDate(); res = BasicWages + Allowance*d.diffyear(x); return res ; void print() Employee:print(); cout<<"position:"<<position<<endl; cout<<"BasicWages:"<<BasicWages<<endl; cout<<"Allowance:"<<Allowanc
32、e<<endl; private: string position ; const double BasicWages ; const double Allowance ;int main() MYDate date1(1950,1,1); MYDate date2(1996,1,1); MYDate date3(2000,1,1); Person p1("pName1","10001","7059261"); Student p2("pName2","10002","70
33、59262","freshman"); Employee p3("pName3","10003","7059263","officelala",1000,date1); Faculty p4("pName4","10004","7059264","officehaha",2000,date2,3,1000); Staff p5("pName5","10005",&qu
34、ot;7059265","officegaga",3000,date3,"zongcai",1000,200); p1.print(); p2.print(); p3.print(); p4.print(); p5.print(); MYDate now(2010,1,1); cout<<"money:"<<p4.xinshui()<<endl; cout<<"money:"<<p5.xins(now)<<endl; return 0
35、;【错误及解决办法】1:这次代码太长了,很容易出错,调bug调了半天.根据错误提示.【实验总结】 publicprotectedprivatepublic继承publicprotected不可用protected继承protectedprotected不可用private继承privateprivate不可用实验八 多态实现【实验目的】1、 了解继承和多态的作用和实现方式,掌握动态联编方法;2、 掌握使用C+语言的抽象类和派生类实现继承性;【源程序】 #include<iostream>using namespace std;class CStereoShapepublic:virtual do
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中国饲料用鱼粉行业市场前景预测及投资战略研究报告
- 2025年 达州市大竹县乡村职业经理人招聘笔试试题附答案
- 中国远程协作办公行业市场深度研究及投资战略规划报告
- 花叶项目投资可行性研究分析报告(2024-2030版)
- 红外防水摄像机外壳项目投资可行性研究分析报告(2024-2030版)
- 中国有线数字电视增值业务行业市场深度调查评估及投资方向研究报告
- 中国铁路车辆设备行业发展运行现状及发展趋势预测报告
- 钛封头行业深度研究分析报告(2024-2030版)
- 北京消防培训课件
- 中国一次性使用无菌医用敷贴行业市场占有率及投资前景预测分析报告
- 招生就业处2025年工作计划
- 【MOOC】外国文学经典导读-西北大学 中国大学慕课MOOC答案
- 医院供电合同
- 市场营销学练习及答案(吴健安)
- 2023水电工程费用构成及概(估)算费用标准
- Unit2 Bridging Cultures Discovering useful structures 课件英语人教版(2019)选择性必修第二册
- 脊柱健康与中医养生课件
- 《土地复垦介绍》课件
- 天然气管道安装施工组织方案
- 《能源培训讲义》课件
- GB/T 12996-2024电动轮椅车
评论
0/150
提交评论