数值转换和回文
熟制转换
#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
100 //存储空间初始分配量 #define STACK_INIT_SIZE
#define STACKINCREMENT 10 //存储空间分配增量
#define N 8 //转换成 N 进制
/*****************************************
友情提示:请用C++方式编译
*****************************************/
typedef struct
{
int *base ; //栈底
int *top ; //栈顶
int stacksize ; //栈容量
}SqStack ;
int InitStack ( SqStack &S ) { //构造一个空栈
S.base = (int * ) malloc ( STACK_INIT_SIZE * sizeof( int ) ) ;
if ( ! S.base ) exit(0) ; S.top = S.base ;
S.stacksize = STACK_INIT_SIZE ; return 0 ;
}
int GetTop ( SqStack S , int &e ) { //若栈不为空,则用e返回S的栈顶元素,并返回0,否则返回1 if ( S.top == S.base )
return 1 ;
e = * ( S.top - 1 ) ;
return 0 ;
}
int Push ( SqStack &S , int e ) { //插入元素e为新的栈顶元素
if( S.top - S.base >= S.stacksize ) //栈满,追加存储空间 {
S.base = ( int * ) realloc ( S.base ,
(S.stacksize + STACKINCREMENT ) * sizeof ( int ) ) ;
if (!S.base) return 1 ; //存储分配失败
S.top = S.base + S.stacksize ;
S.stacksize += STACKINCREMENT ; }
*S.top++ = e ;
return 0 ;
}
int Pop ( SqStack &S , int &e ) { //若栈不空,则删除S的栈顶元素,用e返回其值,并返回0,否则返回1 if ( S.top == S.base ) return 1 ; e = * --S.top ;
return 0 ;
}
int StackEmpty (SqStack S) { //若栈空,则返回1,否则返回0
if ( S.top == S.base )
return 1 ;
return 0 ;
}
int DestroyStack (SqStack &S ) { //销毁栈S,栈S不再存在
free( S.base ) ;
S.base = S.top = NULL ; //防止程序后面不小心使用了它 return 0 ;
}
int main()
{
int n , e ; //n为要转换的十进制数,e 为临时用的的int型变量 SqStack S ;
InitStack( S ) ; //构造一个空栈
scanf("%d",&n) ;
while(n)
{
Push( S , n % 8 ) ; //压入栈
n /= N ; //转换成 N 进制
}
while( ! StackEmpty( S ) ) {
Pop( S , e ) ; //弹出栈
printf("%d", e ) ;
}
printf("\n") ; DestroyStack( S ) ; //销毁栈S,栈S不再存在
return 0 ; }
#include
using namespace std;
/*//*******************************************************************
链栈
**********************************************************************/
template class LinkStack
{
public:
struct Node
{
LSELEM data;
Node* next;
};
LinkStack (); //构造函数
~LinkStack(); //析构函数
void SetEmpty(); //置空堆栈
bool IsEmpty(); //判断堆栈是否为空
bool Push(LSELEM); //入栈
bool Pop(LSELEM&); //出栈
bool GetTop(LSELEM&); //获得栈顶元素 private:
Node* top;
};
template LinkStack ::LinkStack()
{
top=NULL;
}
template LinkStack ::~LinkStack()
{
SetEmpty();
top=NULL;
}
template void LinkStack ::SetEmpty()
{
//释放堆栈占用的内存
Node* temp;
while (top!=NULL)
{
temp=top;
top=top->next;
delete temp;
}
}
template bool LinkStack ::IsEmpty()
{
return (top==NULL); }
template bool LinkStack ::Push(LSELEM element)
{
Node* temp=new Node();
if (temp ==NULL)
return false ;
temp->data=element;
temp->next=top;
top=temp;
return true;
}
template bool LinkStack ::Pop(LSELEM& element)
{
if ( IsEmpty())
return false;
Node* q = top;
element = top->data;
top=top->next;
delete q;
return true;
}
template bool LinkStack ::GetTop(LSELEM& element)
{
if ( IsEmpty())
return false;
element = top->data;
return true;
}
/*//*******************************************************************
**********************************************************************/
void main(){
LinkStack stack;
int N,M;
int tmp;
cout<<"输入要转换的十进制数字。\n";
cin>>N;
cout<<"选择要转换的制式。\n";
cout<<"1.二进制。\n";
cout<<"2.八进制。\n";
cout<<"3.十六进制。\n";
do{
cin>>M;
if(M<0 || M>3)
cout<<"输入错误,请重新选择。\n";
}while(M<0 || M>3);
cout<<"转换结果:\n";
switch(M){
case 1:
while(N){
stack.Push(N%2);
N=N/2;
}
while(!stack.IsEmpty()){
stack.Pop(tmp);
cout< using namespace std;
/*//*******************************************************************
链栈
**********************************************************************/
template class LinkStack
{
public:
struct Node
{
LSELEM data;
Node* next;
};
LinkStack (); //构造函数
~LinkStack(); //析构函数
void SetEmpty(); //置空堆栈
bool IsEmpty(); //判断堆栈是否为空
bool Push(LSELEM); //入栈
bool Pop(LSELEM&); //出栈
bool GetTop(LSELEM&); //获得栈顶元素 private:
Node* top;
};
template LinkStack ::LinkStack()
{
top=NULL;
}
template LinkStack ::~LinkStack()
{
SetEmpty();
top=NULL;
}
template void LinkStack ::SetEmpty()
{
//释放堆栈占用的内存
Node* temp;
while (top!=NULL)
{
temp=top;
top=top->next;
delete temp;
}
}
template bool LinkStack ::IsEmpty()
{
return (top==NULL); }
template bool LinkStack ::Push(LSELEM element)
{
Node* temp=new Node();
if (temp ==NULL)
return false ;
temp->data=element;
temp->next=top;
top=temp;
return true;
}
template bool LinkStack ::Pop(LSELEM& element)
{
if ( IsEmpty())
return false;
Node* q = top;
element = top->data;
top=top->next;
delete q;
return true;
}
template bool LinkStack ::GetTop(LSELEM& element)
{
if ( IsEmpty())
return false;
element = top->data;
return true;
}
/*//*******************************************************************
**********************************************************************/
void main(){
LinkStack stack;
int N,M;
int tmp;
cout<<"输入要转换的十进制数字。\n";
cin>>N;
cout<<"选择要转换的制式。\n";
cout<<"1.二进制。\n";
cout<<"2.八进制。\n";
cout<<"3.十六进制。\n";
do{
cin>>M;
if(M<0 || M>3)
cout<<"输入错误,请重新选择。\n";
}while(M<0 || M>3);
cout<<"转换结果:\n";
switch(M){
case 1:
while(N){
stack.Push(N%2);
N=N/2;
}
while(!stack.IsEmpty()){
stack.Pop(tmp);
cout<
#include
Status InitStatus(SqStack &S){
////构造一个空栈
S.base=(SElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if (!S.base)exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status Push(SqStack &S,SElemType e){
////插入元素e为新的栈顶
if(S.top - S.base >=S.stacksize){////栈满,追加空间
S.base=(ElemType *)realloc(S.base,(S.stacksize + STACKINCREMET) * sizeof(ElemType));
if (!S.base)
{exit(OVERFLOW);////存储失败
}
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMET;
}
*S.top++=e;
return OK;
}////PUSH
bool StackEmpty(SqStack &S){
if (S.top==S.base)return true;
else return false; }
Status Pop(SqStack &S,SElemType &e){
////若栈不空,则删除栈顶元素,返回到e,ok
if (S.top==S.base)return ERROR;
e=*(--S.top);
return OK;
}//////POP
void Convert(){//////将一个10进制转化成八进制数
SqStack S;
int N;
int e;
InitStatus(S);
scanf("%d",&N);
while (N)
{
Push(S,N % 8);
N=N/8;
}
while(!StackEmpty(S)){
Pop(S,e);
printf("%d",e);
}
}
int main(int argc, char* argv[])
{
printf("Hello World!\n");
Convert();
return 0;
}
///////////////define.h #define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1 #define OVERFLOW -2 #define ElemType int #define SElemType int #define STACK_INIT_SIZE 100/////存储空间初始分量 #define STACKINCREMET 10////存储空间增量 typedef int Status; typedef struct{
SElemType *base;///栈构造前和销毁以后base为null SElemType *top;///栈顶指针
int stacksize;////当前已分配大小
}SqStack;
回文
typedef struct Node {
char data;
struct Node *next; }Node,*LinkStack;
#define NULL 0
#include int InitStack();
void main()
{
InitStack() ? printf("Yes!\n") :printf("No!\n");
getch();
}
int InitStack()
{
int i, j = 0;
char *p;
LinkStack top;
Node *r, *s, *temp;
top = (Node *)malloc(sizeof(Node));
if(top == NULL)
{
printf("overflow!\n");
exit(0);
}
top -> next = NULL;
s = top;
printf("please input the strint:\n");
gets(p);
for(i = 0; i < strlen(p); i++)
{
r = (LinkStack)malloc(sizeof(Node));
r -> data = p[j++];
r -> next = s -> next;
s -> next = r;
}
j = 0;
for(i = strlen(p) - 1; i >= 0; i--)
{
temp = s -> next;
if(temp -> data == p[j++])
{
s -> next = temp -> next;
free(temp);
}
else
{
break;
}
}
if(i == -1) return 1;
else return 0;
}
给定一字符串,试判断它是否为回文串。例如输入12344321,该串为回文串,程序输出yes,否则输出no
程序代码:
#include
#include
#include
#define MAX 30
typedef struct stack
{
char data[MAX];
int top;
}*STACK,node;
void initstack(STACK st) {
st->top=-1;
}
int push(STACK st,char *stringin)
{
if(st->top==MAX-1)
return 0;
while(*stringin)
st->data[++st->top]=*(stringin++);
return 1;
}
int pop(STACK st,char *stringout)
{
if(st->top==-1)
return 0;
while(st->top!=-1)
*(stringout++)=st->data[st->top--];
*stringout='\0';
return 1;
}
int compare(char *stringin,char *stringout)
{
if(!strcmp(stringin,stringout))
return 1;
return 0;
}
void showanswer(int judge) {
if(judge)
printf("yes\n");
else
printf("no\n");
}
void main()
{
char *stringin=NULL,*stringout=NULL;
int judge;
STACK st=NULL;
stringin=(char *)malloc(MAX*sizeof(char));
stringout=(char *)malloc(MAX*sizeof(char));
gets(stringin);
st=(STACK)malloc(sizeof(node));
initstack(st);
push(st,stringin);
pop(st,stringout);
judge=compare(stringin,stringout);
showanswer(judge);
}
运行环境:vc++