早教吧作业答案频道 -->其他-->
请教一道c++设计题。英文的。2.DesignaclassnamedStackOfInteger.Thestackisadatastructurethatholdsdatainalast-in.first-out.ItholdstheIntegerdata.TheUML(UnifiedModelingLanguage)diagramfortheclassisshowninthe
题目详情
请教一道c++设计题。英文的。
2. Design a class named StackOfInteger. The stack is a data structure that holds data in a last-in. first-out. It holds the Integer data. The UML (Unified Modeling Language) diagram for the class is shown in the following figure.
2. Design a class named StackOfInteger. The stack is a data structure that holds data in a last-in. first-out. It holds the Integer data. The UML (Unified Modeling Language) diagram for the class is shown in the following figure.
▼优质解答
答案和解析
大概写了段,先给你。
话说你的figure呢?
#include<iostream>
using namespace std;
class StackOfInteger {
private:
int *num;
int size;
int space; //为Stack分配空间,默认255,可以通过构造函数设定
public:
StackOfInteger();
StackOfInteger(int);
~StackOfInteger();
void push(int);
int pop();
int length();
bool isEmpty();
};
StackOfInteger::StackOfInteger() {
space = 0xff;
size = 0;
num = new int[space];
}
StackOfInteger::StackOfInteger(int space) {
this->space = space;
size = 0;
num = new int[space];
}
StackOfInteger::~StackOfInteger() {
delete num;
}
void StackOfInteger::push(int data) {
num[size++] = data;
}
int StackOfInteger::pop() {
int retval = num[0];
for(int i = 0; i < size - 1; i++)
num[i] = num[i + 1];
return retval;
}
int StackOfInteger::length() {
return size;
}
bool StackOfInteger::isEmpty() {
return size == 0 ? true : false;
}
int main()
{
StackOfInteger soi = StackOfInteger();
cout<<"initi:\t"<<soi.length()<<"\t"<<soi.isEmpty()<<endl;
for(int i = 0; i < 100; i++)
soi.push(i);
cout<<"stored:\t"<<soi.length()<<"\t"<<soi.isEmpty()<<endl;
cout<<soi.pop()<<endl;
cout<<soi.pop()<<endl;
cout<<soi.pop()<<endl;
}
看了 请教一道c++设计题。英文的...的网友还看了以下:
英语翻译1.English is a language branch belonging to t 2020-05-14 …
英语翻译water production begins first in wells comple 2020-05-14 …
英语翻译Boffins are the clever ones in class .They al 2020-05-15 …
英语 1st 表示的是first 还是the first?in the 1st centry英语 2020-05-16 …
比较first,at first,in the first placethank you~ 2020-05-16 …
辨析 first(first of all);at first;in the first plac 2020-05-16 …
at first /first/ in first / firstly的差异? 2020-05-16 …
英语翻译British Airways in first profit for two years 2020-05-16 …
in[out]与in[out]side还有incldeincluding如何运用? 2020-07-26 …
DoyouknowOurcountry'saircraftcarrier,Liaoning?ite 2020-07-27 …