1.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <iostream>
using namespace std;
template<class Type>
class SeqStack
{
private:
Type* data;
int maxsize;
int top;
public:
SeqStack(int sz = 10) :maxsize(sz), top(-1)
{
data = (Type*)malloc(sizeof(Type) * maxsize);
if (data == NULL) exit(1);
}
~SeqStack()
{
free(data);
data = NULL;
maxsize = 0;
top = -1;
}
int GetSize() const { return top + 1; }
bool Is_Empty() const { return GetSize() == 0; }
bool Is_Full() const { return GetSize() == maxsize; }
bool Push(const Type& x)
{
if (Is_Full())return false;
data[++top] = x;
return true;
}
Type& GetTop()
{
return data[top];
}
const Type& GetTop() const
{
return data[top];
}
void Pop()
{
--top;
}
void Clear()
{
top = -1;
}
};
int main()
{
SeqStack<int> ist = SeqStack<int>();
ist.Push(12);
ist.Push(23);
ist.Push(34);
ist.Push(45);
while (!ist.Is_Empty())
{
int x = ist.GetTop();
ist.Pop();
cout << x << endl;
}
}
2.
//1
//2
#include <iostream>
class CGoods
{
private:
char Name[20];
int Amount;
float Price;
float Total_value;
public:
//void RegisterGoods(CGoods *this,const char*, int, float);//输入数据
void RegisterGoods(const char*, int, float);//输入数据
//void CountTotal(CGoods *this)
void CountTotal() //计算商品总价值
{
Total_value = Amount * Price;
}
//void GetName(CGoods *this,char name[])
void GetName(char name[]) //读取商品名
{
strcpy_s(name, 20, Name);
}
//int GetAmount (CGoods *this)
int GetAmount() //读取商品数量
{
return Amount;
}
//float GetPrint(CGoods *this)
float GetPrice() //读取商品单价
{
return Price ;
}
//float GetTotal_value(CGoods *this)
float GetTotal_value() //读取商品总价值
{
return Total_value;
}
};
//void CGoods::RegisterGoods(CGoods *this,const char* name, int amount,float price)
void CGoods::RegisterGoods(const char* name, int amount, float price)
{
strcpy_s(this->Name, 20, name);
this->Amount = amount;
this->Price = price;
}
int main()
{
CGoods c1, c2;
c1.RegisterGoods("iphone", 10, 6800);
// RegisterGoods(&c1,"iphone",10,6800);
c1.CountTotal();
c2.RegisterGoods("huawei", 12, 7800);
// RegisterGoods(&c2,"huawei", 12, 7800);
c2.CountTotal();
}