




已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
132007二编写程序或者程序段2、实现描述超市的类Suppermarket类,记录系统中现有商品(用链表实现),定义:初始化链表的构造函数增加商品的函数Append;删除商品的函数Delete;查询商品的函数Query,并显示查询结果其中,商品具有Id,名称Name,价格Price和数量Number等特性。/商品72#includestruct Goodsdouble Id;char *Name;float Price;int Number;Goods *next;class Supermarketpublic:Goods *L;Supermarket();void Append(double id,char *name,float price,int number);void Delete(double i); Goods *query(double i)Goods *r;r=L-next;while(r)if(r-Id=i)coutGoods ID:Idendl;coutName:Nameendl;coutPrice:Priceendl;coutNumber:Numbernext;coutNo goods found!next=NULL;void Supermarket:Append(double id,char *name,float price,int number)Goods *r=L;Goods *p;while(r-next!=NULL)r=r-next;p=new Goods;p-Id=id;p-Name=name;p-Price=price;p-Number=number;p-next=NULL;/将添加的商品放在最后面r-next=p;void Supermarket:Delete(double i)Goods *p,*r;p=L;/头指针r=L-next;while(r!=NULL)if(r-Id=i) break;p=r;r=r-next;if(r=NULL)coutgoods do nit exitsnext=r-next;/删除了商品delete r;void main()Supermarket s;s.Append(1,apple,5.7,10);s.Append(2,meat,9.8,5);s.Append(3,milk,2.7,8);s.query(1);s.query(2);s.query(3);s.Delete(1); s.query(2);运行结果:Goods ID:1Name:applePrice:5.7Number:10Goods ID:2Name:meatPrice:9.8Number:5Goods ID:3Name:milkPrice:2.7Number:8Goods ID:2Name:meatPrice:9.8Number:5Press any key to continue06二、2假定居民的基本数据包括身份证号,姓名,性别和出生日期,而居民中的成年人又多出两项数据:最高学历和职业,成年中的党员又多出一项数据:党派类别,现要求建立三个类,让成年人继承居民类,党员类继承成年人类,并要求每个类中都提供有数据添加,数据删除,数据输出的功能。/商品72#includeclass Personpublic:Person()Person(char *id,char *name,char sex,char *birthday)Id=id;Name=name;Sex=sex;Birthday=birthday;void Add(char *id,char *name,char sex,char *birthday)Id=id;Name=name;Sex=sex;Birthday=birthday;void Delete()Id=;Name=;Sex=0;Birthday=;void Show()coutId:Idendl;coutName:Nameendl;coutSex:Sexendl;coutBirthday:Birthdayendl;private:char *Id;char *Name;char Sex;char *Birthday;class Adult:public Personpublic:Adult()Adult(char *id,char *name,char sex,char *birthday,char *edu,char *Occ):Person(id,name,sex,birthday)Education=edu;Occupation=Occ;void Add(char *id,char *name,char sex,char *Birthday,char *edu,char *Occ)Person:Add(id,name,sex,Birthday);Education=edu;Occupation=Occ;void Delete()Person:Delete();Education= ;Occupation= ;void Show()Person:Show();coutEducation:Educationendl;coutOccupation:Occupationendl;private:char *Education;char *Occupation;class PartyMember:public Adultpublic:PartyMember()PartyMember(char *id,char *name,char sex,char *Birthday,char *Edu,char *Occ,char *cate): Adult(id,name,sex,Birthday,Edu,Occ)category=cate;void Add(char *id,char *name,char sex,char *Birthday,char *cate)Person:Add(id,name,sex,Birthday);category=cate;void Delete()Person:Delete();category=;void Show()Person:Show();coutCategory:categoryendl;private:char *category;void main()Person p1;Adult a1;PartyMember pm1;p1.Add(11041062,Liming,m,19881112);p1.Show();p1.Delete();p1.Show();coutendl;a1.Add(11041063,Liyan,f,19881002,graduate,monitor);a1.Show();a1.Delete();a1.Show();coutendl;pm1.Add(11041063,Liyan,f,19881002,Party);pm1.Show();pm1.Delete();pm1.Show();coutnext;while(p)if(LB-datadata)head=LB-next;temp-next=LA;temp1-next=LB;LB-next=temp-next;else if(LB-data=LA-data) head=LB-next;elsetemp1=LA;LA=LA-next;LB=head;2、定义一个串类,在串上的操作包括:判断串是否为空;求串长;删除串中从i开始的第k个字符;将串中小写变大写;将串中大写变小写;#include#include#includeclass sstringpublic:sstring()s=NULL;bool isempty()if(*s)!=0)coutThis String is not empty.endl;return true;int length();/求长度void del_k(int i,int k);/删除串中从i开始的第k个字符void upper();/小写变大写void lower();/大写变小写char *s;int sstring:length()int n=0;char *temp=s;while(*temp)!=0)n+;temp+;return n;coutThe length of this string isnendl;void sstring:del_k(int i,int k)int j;int leg=length();for(j=i+k;jleg;j+)sj-k=sj;coutThe new String is sendl;void sstring:lower()/大写变成小写char *temp=s;int leg=length();for(int i=0;i=A&*temp=Z)(*temp)+=32;temp+;coutThe new String is sendl;void sstring:upper()/大写变成小写char *temp=s;int leg=length();for(int i=0;i=a&*temp=z)(*temp)-=32;temp+;coutThe new String is sendl;int main()sstring r;char str=asdsaksKKDJHJ;printf(%sn,str);r.s=str;r.isempty();r.length();r.del_k(2,2);r.lower();r.upper(); 运行结果:asdsaksKKDJHJThis String is not empty.The new String is asaksKKDJHJHJThe new String is asakskkdjhjhjThe new String is ASAKSKKDJHJHJPress any key to continue3、定义一个栈类,与栈有关的操作包括:栈初始化;判栈空;出栈操作;入栈操作;取栈元素;假定栈元素为整型。注:栈的实现可以采用顺序结构,也可以采用链表结构,要求接口与存储结构无关。方法一、用链表实现#includetypedef int type;struct StackNodetype element;StackNode *previous;StackNode *next;class Stackpublic:enum MaxStack=5;Stack()head=new StackNode;head-element=0;head-previous=NULL;head-next=NULL;top=head;void push(type n)if(isFull()errMsg(*The Stack is Full!*);return;StackNode *p;p=top;top=new StackNode;p-next=top;top-element=n;top-previous=p;top-next=NULL;type pop()if(isEmpty()errMsg(*The Stack is Empty!*);return dummy_val;type n;StackNode *p;p=top;n=p-element;top=p-previous;delete p;return n;bool isEmpty()return top=head;bool isFull()return head-elementMaxStack;void prtypeStack()if(isEmpty()errMsg(*The Stack is Empty!*);return ;StackNode *p;p=top;coutStack contents,top to button;while(p!=head) coutnelementprevious;private:void errMsg(char *msg)coutmsg;StackNode *head;StackNode *top;type dummy_val;void main()Stack s;type a,b;coutab;s.push(a);s.push(b);s.prtypeStack();coutnpop:s.pop()n;s.prtypeStack();/system(Pause); 方法二、采用顺序结构#ifndef stack_class /如果没定义stack_class,#define stack_class /那么现在开始定义stack_class#includeiostream.h#includestdlib.hconst int stack_size=50;/ 栈中元素最大个数template /模板 T表示一个类class stackprivate:int top;T stackliststack_size;public:stack(void);void push(const T& item);/入栈T pop(void); /出栈void clearstack(void); /清栈T peek(void) const; /返回栈顶的元素int empty(void); /判断栈空int full(void); /判断栈满;/构造stack函数template /对于模板类,在每一个申明和函数定义都得写这句话stack:stack(void):top(-1)/入栈template /对于模板类,在每一个申明和函数定义都得写这句话void stack:push(const T&item)if(top=stack_size-1)coutstack overflow!endl;/栈满就退出exit(1);top-;stacklisttop=item;/出栈template /对于模板类,在每一个申明和函数定义都得写这句话T stack:pop(void)T temp;if(top=-1)cout栈空了!endl;/栈满就退出exit(1);temp=stacklisttop;top+;return temp;/返回栈顶元素/访问栈顶元素template /对于模板类,在每一个申明和函数定义都得写这句话T stack:peek(void)constif(top=-1)cout栈空了!endl;/栈满就退出exit(1);return stacklisttop;/返回栈顶元素/判断栈空template int stack:empty(void)return top=-1;/判断栈满template int stack:full(void)return top=stack_size-1;/清空栈template void stack:clearstack(void)top=-1;#endif stack_class/结束定义int main()stack sta(123);return 0;2003一1、/* 本程序从键盘读入整数,并将按从大到小的顺序输入整数中互不相等的那些整数。 程序一边读入整数,一边构造一个从大到小顺序连接的链表,直至不能从键盘读入 整数,然后顺序输出链表上各表元的整数值。主函数每读入一个整数,就调用函数Insert, 函数Insert将还未出现在链表上的整数按从大到小的顺序插入到链表中。 为了插入方便,链表在表首有一个辅助接点。*/#includestdio.h#includestdlib.htypedef struct nodeint val;struct node *next;NODE;void Insert(NODE *list, int x)NODE *u,*v,*p;int i=0;u=list;v=u-next;while(v&xval)/位置【1】寻找插入的位置 u=v;v=v-next;if(v=NULL|xv-val) /位置【2】判断是否要插入新表元p=(NODE *)malloc(sizeof(NODE);p-val=x;p-next=v; /位置【3】 u-next=p; /位置【4】i+; /i是用来观察函数void main()int x;NODE *head,*p;/首先建立只有辅助节点的空链表head=(NODE *)malloc(sizeof(NODE);head-next=NULL; /位置【5】printf(enter integers:);while(scanf(%d,&x)=1)Insert(head,x);for(p=head-next;p;p=p-next)printf(%5d,p-val);printf(n);2、计算并求出K!的阶乘。/计算k的阶乘,因为数字太大,所以按位存储在a数组中,且按倒序存储 #includestdio.h#define MAXN 1000int aMAXN,bMAXN;void pnext(int k,int *cp)int c=*cp,i,m,r;for(i=0;ic;i+)bi=ai;ac=0;for(m=1;mk;m+)for(i=0;i0) /当数值较大时,如k=100,则累加后高位将有可能是多位溢出ac+=r%10;r=r/10;/输入数值小于13的数的阶乘unsigned long jiecheng(int k)int i;unsigned long s=1;for(i=1;i=k;i+)s*=i;return s;void main()int k,i,cp;unsigned long m;cp=0;printf(请输入数据); scanf(%d,&k);m=jiecheng(k);printf(n%d!=%ldn,k,m); m=jiecheng(k-1);while(m)acp+=m%10;m=m/10;pnext( k,&cp);printf(按倒序将%d的阶乘输出n,k);printf(%d!=,k);for(i=0;i=cp;i+)printf(%d,ai);printf(n);运行结果:请输入数据55!=120按倒序将5的阶乘输出5!=021Press any key to continue二、1、编写一个函数,求集合的交运算,假定两个集合A和B用递增有序的线性链表和表示,运算结果用表示,LA和LB不受影响。#includetypedef struct Nodeint data;Node *next;Node *Intersect(Node *LA,Node *LB)Node *p,*q,*LC,*pc,*temp;p=LA-next;q=LB-next;pc=LC=(Node*)malloc(sizeof(Node);LC-next=NULL;while(p&q)if(p-data=q-data)temp=(Node*)malloc(sizeof(Node);temp-data=p-data;temp-next=NULL;pc-next=temp;pc=temp;p=p-next;q=q-next;else if(p-datadata)p=p-next;else q=q-next;return LC;2、已知一组无序的整数用一个链表表示,编写一个函数对该链表表示的整数序列进行排列。/*2003已知一组无序的整数用一个链表表示,编写一个函数对该链表表示的整数序列进行排列*/假定整数有个,链表LT,有空头head#define M 5#define NULL 0struct Nodeint data;struct Node *next;Usort(struct Node *LT)int i=0;int j=0;struct Node *temp2,*temp1;for(i=0;iM;i+)for(j=0;jnext;temp2=LT-next;if(j=M-1)LT-next=NULL;if(LT-datatemp2-data)&(LT-next)LT-next=temp2-next;temp2-next=LT;temp1-next=temp2;3、定义一个时间类,类成员包括小时,分钟,秒,在该类上的操作包括:(1)时间初始化,利用构造函数的重载函数按下列情况初始化:只给出小时进行初始化(分,秒为0);只给出小时,分(秒为0);给出小时,分,秒;(2)设置时间(参数:小时,分,秒)(3)按上午(am)或者下午(pm)输出时间。class Timeprivate:int hour,minute,second;public:Time();Time(int h);Time(int h,int m);Time(int h,int m,int s);void SetTime(int h,int m,int s);void PrintTime();Time:Time()hour=0;minute=0;second=0;Time:Time(int h)hour=h;minute=0;second=0;Time:Time(int h,int m)hour=h;minute=m;second=0;Time:Time(int h,int m,int s)hour=h;minute=m;second=s;void Time:SetTime(int h,int m,int s)hour=h;minute=m;second=s;void Time:PrintTime()if(hour24)|(minute60)|(second60)couterro!endl;if(hour=0)coutam:hour,minute,secondendl;if(hour=12)coutpm:hour,minute,secondendl;#include#includeTime.hvoid main()Time time1;time1.SetTime(12,2,2);time1.PrintTime();运行结果:pm:12,2,2Press any key to continue4、定义一个队列类(与前面相同)2002二、1编写一个函数,求出集合A和集合B的交集#includevoid Bubble_ordination(unsigned char *pucTmp,unsigned char ucNum)/冒泡排序unsigned char i,j;unsigned char ucTmp;unsigned char ucFlag;for(i=0;iucNum;i+)ucFlag=0;for(j=0;j*(pucTmp+i+1)/前一个比后一个大,因此要转换顺序ucTmp=*(pucTmp+i);*(pucTmp+i)=*(pucTmp+i+1);*(pucTmp+i+1)=ucTmp;ucFlag=1;if(ucFlag=0)break;/可以提前结束了main()unsigned char i,j,k;/控制循环的变量unsigned char ucNum_Inter=0;/交集中的元素
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 电的知识培训课件
- 高粱简介课件
- 电焊知识干货培训总结报告课件
- 高热护理课件
- S-2-2-1H-Indol-3-yl-ethyl-amino-2-oxoethyl-COA-S-2-2-1H-Indol-3-yl-ethyl-amino-2-oxoethyl-coenzyme-A-生命科学试剂-MCE
- Brivoligide-sodium-AYX1-sodium-生命科学试剂-MCE
- 保育员三级证考试题目及答案
- 中长导管考试题及答案
- 班长竞聘考试题及答案
- 高校消防安全知识培训课件
- 工业机器人讲课件
- 2025年湖北省中考英语试卷真题(含答案解析)
- 中医适宜技术的临床应用讲课件
- 质量工作痕迹管理制度
- 输血反应急救流程
- 中职语文拓展模块上册教学计划
- 微纳尺度熔覆涂层制备-洞察及研究
- DB33-1036-2021 公共建筑节能设计标准
- 教学用具AI应用行业深度调研及发展项目商业计划书
- 防泄漏及防腐蚀管理制度
- 呼和浩特市赛罕区城市发展投资有限公司招聘笔试题库2025
评论
0/150
提交评论