📄 stack.cpp
字号:
#include<iostream>
#include <assert.h>
#define N 100
using namespace std;
class stack//顺序栈(数组)
{
private:
int size;
int top;
float*listarray;
public:
stack(const int sz=N)
{ size=sz;top=0; listarray=new float[sz];}
~stack(){delete[] listarray;}
/////////////////////////////////////////////////
void clear(){top=0;}
void push(const float&item){assert(top<size);listarray[top++]=item;}//入栈
float pop(){assert(!isEmpty());return listarray[--top];}//出栈
//float topValue()const{assert(!isEmpty());return listarray[top-1];}//返回栈顶元素
bool isEmpty()const{return top==0;}
};
void main()
{
int n;
int num;
char ch;
stack A;
cout<<"想要10进制数转换为几进制如:123 2\n";
do{
cin>>num;
cin>>n;
while(num!=0) //实现将一个十进制的正整数转换成任意进制的数的输出的算法;
{
int k=num%n;
A.push(k);
num/=n;
}
cout<<endl;
cout<<"转换为"<<n<<"进制结果为:\n";
cout<<endl;
while(!A.isEmpty())
{
int x=A.pop();
if(x<10)
cout<<x;
else
{
switch(x)
{
case 10: cout<<'A';
break;
case 11:cout<<'B';
break;
case 12:cout<<'C';
break;
case 13:cout<<'D';
break;
case 14:cout<<'E';
break;
case 15:cout<<'F';
break;
default:cout<<"error\n";
}
}
}
A.clear();
cout<<endl;
cout<<endl;
cout<<"继续否 y/n?\n";
cout<<endl;
cin>>ch;
}while(ch=='y');
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -