c ++ 语言问题

c ++ 语言问题,第1张

//FILE:abstack.h

#include<iostream.h>

#include<assert.h>

template<class type>

class abstack{

protected:

int height

public:

bool IsEmpty(){

return height==0?true:false

}

virtual void Push(type&)=0//consider the link list,so we use the void type

virtual bool Pop(type&)=0

virtual void Clear()=0

}//---------------------------------------------abstract class declare

//FILE:LinkStack.h

#include "abstack.h"

template<class type>

struct StackNode{

type data

StackNode *next

}

template<class type>

class LinkStack:public abstack<type>{

protected:

StackNode<type>*top

LinkStack<type>&copy(LinkStack<type>&)

public:

LinkStack()

LinkStack(LinkStack<type>&g){

top=NULLcopy(g)}

~LinkStack(){

Clear()}

void Clear()

void Push(type&)

bool Pop(type&)

LinkStack&operator=(LinkStack<type>&g){

copy(g)return *this}

}////////////////////////////////////////////////////////////////////////////////////////

template<class type>

void LinkStack<type>::Clear(){

type x

while(Pop(x))

}//top=NULL finally

template<class type>

LinkStack<type>&LinkStack<type>::copy(LinkStack<type>&g){

if(top) Clear()

if(!g.top) return *this

StackNode<type>*src,*newnode,*des

src=g.top

top=new StackNode<type>

assert(top)

top->next=NULL

top->data=src->data

des=top

src=src->next

while(src){

newnode=new StackNode<type>

assert(newnode)

newnode->next=NULL

newnode->data=src->data

des->next=newnode

des=des->next

src=src->next

}

height=g.height

return *this

}//member function-------------------copy

template<class type>

LinkStack<type>::LinkStack(){

top=NULL

height=0

}//---------------------------------construction

template<class type>

void LinkStack<type>::Push(type&value){

StackNode<type>*newnode

newnode=new StackNode<type>

assert(newnode)

newnode->data=value

newnode->next=top

top=newnode

height++

}//-----------------------------------push

template<class type>

bool LinkStack<type>::Pop(type&value){

if(IsEmpty()) return false

StackNode<type>*temp

temp=top

value=temp->data

top=temp->next

delete temp

height--

return true

}//-------------------------------------Pop

//FILE: seqstack.h

#include "abstack.h"

template<class type>

class SeqStack:public abstack<type>{

protected:

int top

int maxsize

type *elements

public:

SeqStack(int=20)

SeqStack(SeqStack<type>&s){Copy(s)}

~SeqStack(){Clear()}

void Push(type&)

bool Pop(type&)

void Clear(){delete elements}

SeqStack&copy(SeqStack<type>&)

SeqStack&operator=(SeqStack<type>&s){

delete elementscopy(s)return *this}

bool IsFull(){

return top==maxsize-1}

}/////////////////////////////////////////////////////////////class SeqStack

template<class type>

SeqStack<type>::SeqStack(int i){

maxsize=i>20?i:20

top=-1

height=0

elements=new type[maxsize]

}//constructor

template<class type>

void SeqStack<type>::Push(type&value){

if(IsFull()){

cout<<"SORRY,the stack is full,program need exit!\n"

return

}

elements[++top]=value

height++

}//------------------------------------------------------------Push

template<class type>

bool SeqStack<type>::Pop(type&value){

if(IsEmpty()) return false

value=elements[top--]

height--

return true

}//--------------------------------------------------------------Pop

template<class type>

SeqStack<type>&SeqStack<type>::copy(SeqStack<type>&s){

int i=0

maxsize=s.maxsize

elements=new type[maxsize]

top=-1

while(i<=s.top)

elements[++top]=s.elements[i]

heigth=s.heigth

return *this

}//-----------

//FILE:digitchang.cpp

#include "LinkStack.h"

void main(){

int n,base

cout<<"please input the data n and the data base"<<endl

cin>>n>>base

LinkStack<int>stack

int temp

while(n){

temp=n%base

stack.Push(temp)

n/=base

}

while(stack.Pop(temp))

cout<<temp

cout<<endl

}//main

注:内包头文件以及成员函数的实现

最后是main函数所在的文件

#include "stdafx.h"

#include<iostream>

using namespace std

template<class Telem>

class SqList

{

private:

Telem * elem

int curlen

int maxlen

public:

SqList(int maxsz=100):maxlen(maxsz)

{

curlen=0

elem=new Telem{maxlen}

}

SqList(Telem a[],int n,int maxsz=100):maxlen(maxsz)

{

curlen=n

elem=new Telem[maxlen]

for(int i=0i<ni++)elem[i]=a[i]

}

~SqList(){delete[] elem}

Telem gete(int i)

{

if(i>=1&&i<=curlen)return elem[i-1]

else return NULL

}

int leng(){return curlen}

SqList<Telem>&merg(SqList<Telem>&l2)//声明两多项式相加的成员函数

}

template<class Telem>

SqList<Telem>&SqList<Telem>::merg(SqList<Telem>&l2)//两多项式相加的成员函数的实现

{

int i,j,m,n,k(0)

m=curlenn=l2.curlen

if(m+n<=maxlen)

{

for(i=0i<n/2i++)

{

for(j=0j<m/2j++)

if(elem[2*j+1]==l2.elem[2*i+1]){k++elem[2*j]+=l2.elem[2*i]break}

if(j==m/2) {elem[m+2*(i-k)]=l2.elem[2*i]elem[m+2*(i-k)+1]=l2.elem[2*i+1]}

}

}

curlen=m+n-2*k

return *this

}

void fun(double a[],int j)//该函数实现:输入2j个double型的数,并将其存储到数组中

{

int i

for(i=0i<ji++)

{

double m,n

cout<<"请输入第"<<i+1<<"项的系数和指数:"

cin>>m>>n

a[2*i]=m

a[2*i+1]=n

}

}

void main()

{

int i,j,k

cout<<"第一个多项式有多少项:"

cin>>j

cout<<"系数和指数的输入格式:系数+空格键+指数+Enter"<<endl

double *a=new double[2*j]

fun(a,j)

cout<<"第二个多项式有多少项:"

cin>>k

double *b=new double[2*k]

fun(b,k)

SqList<double>sqla(a,2*j)

SqList<double>sqlb(b,2*k)

sqla.merg(sqlb)

cout<<"这二个多项式结果为:"

for(i=1i<=sqla.leng()/2i++)//用for循环将这二个多项式结果输出

{

cout<<sqla.gete(2*i-1)<<"X^"<<sqla.gete(2*i)

if(i!=sqla.leng()/2)cout<<"+"

}

cout<<endl

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12061333.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存