欢迎来到三一文库! | 帮助中心 三一文库31doc.com 一个上传文档投稿赚钱的网站
三一文库
全部分类
  • 研究报告>
  • 工作总结>
  • 合同范本>
  • 心得体会>
  • 工作报告>
  • 党团相关>
  • 幼儿/小学教育>
  • 高等教育>
  • 经济/贸易/财会>
  • 建筑/环境>
  • 金融/证券>
  • 医学/心理学>
  • ImageVerifierCode 换一换
    首页 三一文库 > 资源分类 > DOC文档下载  

    《c++程序设计》课件C++上机指导答案.doc

    • 资源ID:21712744       资源大小:335KB        全文页数:56页
    • 资源格式: DOC        下载积分:8
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录 QQ登录   微博登录  
    二维码
    微信扫一扫登录
    下载资源需要8
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    《c++程序设计》课件C++上机指导答案.doc

    实验答案实验一(略)实验二1、程序1:运行结果:This is a C+ program.程序2:运行结果:Please input to a and b:1 2 a+b=3This is a C+ program.2、测试的知识点:若函数的调用在前,定义在后,则需要在函数调用语句前对该函数作原型声明。3、(1)#include <iostream>/包含头文件命令using namespace std;/使用名字空间stdint main()int a,b,c; /定义两个变量a和b cout<<"Please input to a, b and c: "<<endl;/输出提示信息到屏幕cin>>a>>b>>c;/等待用户从键盘输入数据 int max(int x, int y)  /有两个形参的max函数原型声明cout<< "Max(a,b)="<< max(a,b)<<endl;/输出结果信息至屏幕int max(int x, int y, int z)  /有三个形参的max函数原型声明cout<< "Max(a,b,c)="<< max(a,b,c)<<endl;/输出结果信息至屏幕return 0;/主函数返回0至操作系统int max(int x, int y) return (x> y) ?x:y; /求两个数中的大者的函数int max(int x, int y, int z) /求三个数中的大者的函数 int temp;temp=max(x,y) temp=max(temp,z) return temp  (2)#include <iostream>using namespace std;int main()int max(int a,int b,int c=0); int a,b,c; cin>>a>>b>>c; cout<<"max(a,b,c)="<<max(a,b,c)<<endl; cout<<"max(a,b)="<<max(a,b)<<endl; return 0;int max(int a,int b,int c)if(b>a) a=b; if(c>a) a=c; return a;4、(1)#include <iostream>using namespace std;int main()void sort(int *,int *,int *); int a,b,c,a1,b1,c1; cout<<"Please enter 3 integers:" cin>>a>>b>>c; a1=a;b1=b;c1=c; sort(&a1,&b1,&c1); cout<<a<<" "<<b<<" "<<c<<" in sorted order is " cout<<a1<<" "<<b1<<" "<<c1<<endl; return 0;void sort(int *i,int *j,int *k) void change(int *,int *); if (*i>*j) change(i,j); if (*i>*k) change(i,k); if (*j>*k) change(j,k);void change(int *x,int *y) int temp; temp=*x; *x=*y; *y=temp;(2)#include <iostream>using namespace std;int main()void sort(int &,int &,int &); int a,b,c,a1,b1,c1; cout<<"Please enter 3 integers:" cin>>a>>b>>c; a1=a;b1=b;c1=c; sort(a1,b1,c1); cout<<a<<" "<<b<<" "<<c<<" in sorted order is " cout<<a1<<" "<<b1<<" "<<c1<<endl; return 0;void sort(int &i,int &j,int &k) void change(int &,int &); if (i>j) change(i,j); if (i>k) change(i,k); if (j>k) change(j,k);void change(int &x,int &y) int temp; temp=x; x=y; y=temp;5、#include <iostream>#include <string>using namespace std;int main() int a5=1,9,0,23,-45; float b5=2.4f, 7.6f, 5.5f, 6.6f, -2.3f; string c5=”student”, ”teacher”,”library”,”class”,”school”; void sort(int ); void sort(float ); void sort(double ); sort(a); sort(b); sort(c); return 0;void sort(int a)/冒泡排序int i,j,t; for (j=0;j<5;j+) for(i=0;i<5-j;i+) if (ai>ai+1) t=ai;ai=ai+1;ai+1=t; cout<<"the sorted numbers :"<<endl; for(i=0;i<5;i+) cout<<ai<<" " cout<<endl<<endl; void sort(float a)/改进的冒泡排序int i,j; float t; int flag; for (j=0;j<5;j+) flag=1; for(i=0;i<5-j;i+) if (ai>ai+1) flag=0; t=ai;ai=ai+1;ai+1=t; if (flag=1) break; cout<<"the sorted numbers :"<<endl; for(i=0;i<5;i+) cout<<ai<<" " cout<<endl<<endl;void sort(string a)/选择排序int i,j;string min; long t; for(i=0;i<5;i+) min=i; for (j=i+1;j<5;j+) if(amin>aj) min=j;t=ai; ai=amin;amin=t; cout<<"the sorted numbers :"<<endl; for(i=0;i<5;i+) cout<<ai<<" " cout<<endl<<endl;6、#include <iostream>#include <string>using namespace std;int main() string s1="week",s2="end" cout<<"s1="<<s1<<endl; cout<<"s2="<<s2<<endl; s1=s1+s2; cout<<"The new string is:"<<s1<<endl; return 0;实验三1、(1)程序错误:错误1:set_clock()函数和show_clock()函数放在Clock类的类体中,这表示它们是Time类的成员函数,但是在定义这两个函数时是按一般函数定义的。错误2:在Time类中没指定访问权限(public或private),按C+的规定,如不指定访问权限,按private处理。私有的成员在类外是不能被访问的,在set_clock()函数和show_clock()函数中都访问了私有成员hour,minute,second,main函数调用set_clock()函数和show_clock()函数都是不允许的。错误3:在main函数中调用set_clock()函数和show_clock()函数,而这两个函数都是在main()函数之后定义的,而在main()函数中并未对这两个函数进行原型声明。对该程序景象修改的思路有以下两种:修改思路1:把set_clock()和show_clock()函数看作是类Clock的成员函数。#include <iostream>using namespace std;class Clockpublic:void set_clock(void); void show_clock(void); private: int hour; int minute; int second;int main() Clock clock; clock.set_clock(); clock.show_clock ();return 0;void Clock:set_clock(void) cin>>hour; cin>>minute; cin>>second;void Clock:show_clock(void) cout<<hour<<":"<<minute<<":"<<second<<endl; 修改思路2:把set_clock()和show_clock()函数看作是普通函数。#include <iostream>using namespace std;class Clockpublic: int hour; int minute; int second;Clock clock;int main() void set_clock(void); void show_clock(void); set_clock(); show_clock ();return 0;void set_clock(void) cin>>clock.hour; cin>>clock.minute; cin>>clock.second;void show_clock(void) cout<<clock.hour<<":"<<clock.minute<<":"<<clock.second<<endl; (2)程序错误:错误1:构造函数指定了返回值类型,删除其前面是void错误2:析构函数指定了返回值类型,删除其前面是void错误3:m为类A的私有数据成员,类外不能访问。可以在类A中增加设置m的值的公用成员函数set()。可以对程序代码做如下修改:#include <iostream>using namespace std;class Apublic: A(int i=0)m=i; void set(int x)m=x;void show()cout<<m<<endl; A()private: int m;int main() A a(5);a.set(10);a.show();return 0;(3)程序错误:错误1:类X的私有数据成员a的初始化不能在类中进行,只能通过构造函数完成。错误2:类X的私有数据成员b定义为引用,但没有初始化。错误3:类X的私有成员函数setA()不能在类外调用。错误4:构造函数:X(int i) a=i;的访问权限不能定义为私有的。错误5:构造函数:X(int i) a=i;没有对常数据成员c在参数初始化表中进行初始化。错误6:构造函数:int X() a=b=0;不能定义返回类型错误7:构造函数:int X() a=b=0; 没有对常数据成员c在参数初始化表中进行初始化。错误8:构造函数:X(int i, int j, int k) a=i; b=j; c=k; 对对常数据成员c的初始化不是在参数初始化表中完成的。错误9:常成员函数:setC(int k) const c=c+k;修改常数据成员c的值。可以对程序代码做如下修改:#include <iostream>using namespace std;class X private: int a; int b; const int c; public:X(int i):c(0) a=i; X():c(0) a=b=0; X(int i, int j, int k):c(k) a=i; b=j; void showC(int k) const cout<<c<<endl;void setA(int i)a=i;int main() X x1; X x2(2) ; X x3(1,2,3) ; x1.setA(3) ; return 0 ;2、(1)运行结果:Initalizing defaultInitalizing default0 0Destructor is activeDestructor is active(2)运行结果:a=4 b=X:X(int,char,float) c=32a=0 b=X:X() c=10a=10 b=X:X(.) c=10a=0 b=X:X(const X &other) c=103、#include <iostream>using namespace std;class Salarypublic:Salary()/初始化工资数据的各分项数据为0Wage=0; Subsidy=0; Rent=0; WaterFee=0; ElecFee=0; Salary(double W, double S, double R, double WF, double EF)/初始化工资数据的各分项数据Wage=W; Subsidy=S; Rent=R; WaterFee=WF; ElecFee=EF; void setWage(double w) Wage=w; double getWage() return Wage; void setSubsidy(double s) Subsidy=s; double getSubsidy() return Subsidy; void setRent(double r) Rent=r; double getRent() return Rent; void setWaterFee(double wf) WaterFee=wf; double getWaterFee() return WaterFee; void setElecFee(double ef) ElecFee=ef; double getElecFee() return ElecFee; double RealSalary()/计算实发工资 return Wage+Subsidy-Rent-WaterFee-ElecFee; void Display()cout<<"Wage="<<Wage<<endl;cout<<"Subsidy="<<Subsidy<<endl;cout<<"Rent="<<Rent<<endl;cout<<"WaterFee="<<WaterFee<<endl;cout<<"ElecFee="<<ElecFee<<endl;cout<<"RealSalary="<<RealSalary()<<endl;private: double Wage, Subsidy, Rent, WaterFee, ElecFee;int main()Salary s(2000,1000,500,50,200);s.Display();s.setWage(3000);cout<<endl;s.Display();return 0;4、#include <iostream>using namespace std;#include <string>class Clockpublic: Clock();Clock(int h,int m,int s);void SetClock(int h,int m,int s);void ShowClock();private: int hour,minute,second;Clock:Clock()hour=0;minute=0;second=0;Clock:Clock(int h,int m,int s)hour=h;minute=m;second=s;void Clock:SetClock(int h,int m,int s)hour=h;minute=m;second=s; void Clock:ShowClock() if(hour<10) cout<<'0'cout<<hour<<":"if(minute<10) cout<<'0' cout<<minute<<":"if(second<10) cout<<'0' cout<<second<<endl;int main() Clock today;today.SetClock(12,1,1); today.ShowClock();today.SetClock(13,1,1); today.ShowClock(); return 0;5、#include <iostream.h>#include <string.h>class Employee public: Employee(char *nm,char *str,char *city, char *prov, char *zip); Employee(const Employee &);void SetData(char *newName,char *newaddr,char *newcity, char *newprov, char *newcode); void Display();private: char name20;char street100;char city20;char province20;char postalcode10;Employee:Employee(char *nm, char *str, char *cit, char *prov, char *code) strcpy(name,nm); strcpy(street,str); strcpy(city,cit); strcpy(province,prov); strcpy(postalcode,code); Employee:Employee(const Employee &other) strcpy(name, other.name); strcpy(street, other.street); strcpy(city, other.city); strcpy(province, other.province); strcpy(postalcode, other.postalcode); void Employee:SetData(char *newName,char *newstr,char *newcity, char *newprov, char *newcode) strcpy(name, newName);strcpy(street,newstr); strcpy(city,newcity); strcpy(province,newprov); strcpy(postalcode,newcode); void Employee:Display()cout<<"name="<<name;cout<<" street="<<street;cout<<" city="<<city;cout<<" province="<<province;cout<<" postalcode="<<postalcode;cout<<endl;int main() Employee employee1("Tom","AAAAAAAA","BB","CC","DD"); employee1.Display(); employee1.SetData("Tom","AAAAAAAA","BB","CC","EE");employee1.Display();return 0;6、#include <iostream>using namespace std;#include <cmath>class Triangle /定义三角形类public: Triangle(int x,int y,int z);void SetTriangle(int x, int y, int z); /设置三角形成员函数doubleGetArea(); /求三角形面积int GetPerimeter(); /求三角形周长void Print(); /输出三角形信息private:int a,b,c; /数据成员;Triangle:Triangle(int x,int y,int z)a = x;b = y;c = z; void Triangle:SetTriangle(int x, int y, int z) /设置三角形成员函数a = x;b = y;c = z; double Triangle:GetArea() /求三角形面积double s;s = (a + b + c)/2.0;return sqrt( s*(s-a)*(s-b)*(s-c); int Triangle:GetPerimeter() return (a + b + c); /求三角形周长void Triangle:Print() /输出三角形信息cout<<"the three side of the triangle is:"<<a<<','<<b<<','<<c<<endl;cout<<"the perimeter of the triangle is:"<<GetPerimeter()<<endl;cout<<"the area of the triangle is:"<<GetArea()<<endl;int main()Triangle t1(4,5,6); /定义三角形对象Triangle t2(7,8,9); /定义三角形对象t1.Print();t2.Print();return 0;7、#include <iostream>using namespace std;class intArraypublic:intArray(int size);/构造函数intArray(const intArray &x);/复制构造函数intArray();/析构函数bool Set(int i, int elem);/设置第i个数组元素的值,设置成功返回true,失败返回falsebool Get(int i, int &elem); /获取第i个数组元素的值,获取成功返回true,失败返回falseint Length( ) const;/获取数组的长度void ReSize ( int size ); /重置数组void Print();/输出数组private:int *elements; /指向动态数组的指针int arraysize; /数组的大小;intArray:intArray(int size)if ( size <= 0 ) cerr << "Invalid Array Size" << endl; return; elements=new intsize;if ( elements = NULL ) cerr << "Memory Allocation Error"<<endl; return; else arraysize=size;intArray:intArray(const intArray &x)int n = arraysize = x.arraysize; elements = new intn; if ( elements = NULL ) cerr << "Memory Allocation Error"<< endl; int *srcptr = x.elements; int *destptr = elements;while ( n- ) * destptr+ = * srcptr+;intArray:intArray()delete elements; bool intArray:Set(int i, int elem)if(i<0 | i >arraysize) cout<<"I is invalid!"<<endl; return false; else elementsi=elem; return true;bool intArray:Get(int i, int &elem)if(i<0 | i >arraysize) cout<<"I is invalid!"<<endl; return false;elseelem=elementsi; return true;int intArray:Length( ) const return arraysize; void intArray:ReSize ( int size )if ( size >= 0 && size != arraysize ) int * newarray = new intsize; if ( newarray = NULL ) cerr << "Memory Allocation Error" << endl; int n = ( size <= arraysize ) ? size: arraysize; int *srcptr = elements; int *destptr = newarray; while ( n- ) * destptr+ = * srcptr+; delete elements; elements = newarray;arraysize = size; void intArray:Print()for(int i=0;i<arraysize;i+) cout<<elementsi<<" "cout<<endl;int main()intArray arr(5);for(int i=0;i<5;i+)if(!arr.Set(i,i) exit(1);arr.Print();return 0;8、#include <iostream>using namespace std;class intListprotected: struct Node Node * next; int data;Node * pFirst;public: intList();/构造函数 intList(); /析构函数 /向链表的第i个位置插入一个元素,插入成功返回true,失败返回false bool Insert(int i, int elem) ; /删除链表的第i个位置的元素,删除成功返回true,失败返回false bool Remove(int i, int &elem) ; int Find(int elem)const; /查找值为elem的元素,返回指向该元素的指针 int Length( ) const;/返回链表长度 void PrintList();/输出链表;intList:intList() pFirst=new Node; pFirst->next=NULL; ;intList:intList() Node *p,*q; p=pFirst; while (p->next!=NULL ) q=p->next; delete p; p=q; ;delete p;pFirst=NULL;int intList:Length() const Node *p;int j; p= pFirst->next; j=0; while (p!=NULL ) p=p->next; j+; return j;int intList:Find(int elem)const Node *p; int j; int data; p=pFirst->next; j=1; while (p!=NULL ) data=p->data; if (data=elem) break; p=p->next; j=j+1; ; if (p!=NULL ) return(j); else return (0);bool intList:Insert(int loc,int elem) Node *p,*s;int j; p= pFirst; j=0; while (p!=NULL ) && (j<loc-1) p=p->next;j=j+1; ; if (p!=NULL )&& (j=loc-1) s=new Node; s->data=elem; s->next=p->next; p->next=s; return true; else return false;bool intList:Remove(int i,int &elem) Node *p,*q; int j; p= pFirst; j=0; while (p->next!=NULL ) && (j<i-1) p=p->next;j=j+1; ; if (p->next!=NULL )&&(j=i-1) q=p->next; p->next=p->next->next; elem=q->data; delete(q); return true; else return false;void intList:PrintList()Node *p; p=pFirst->next; while (p!=NULL

    注意事项

    本文(《c++程序设计》课件C++上机指导答案.doc)为本站会员(eieieie)主动上传,三一文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一文库(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    经营许可证编号:宁ICP备18001539号-1

    三一文库
    收起
    展开