c++求解如何改成c语言的源代码
#include<iostream>#include<malloc.h>
using namespace std;
#define MaxSize 16
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int top;
}SqStack;
//初始化栈
void InitStack(SqStack *&s)
{
s=(SqStack *)malloc(sizeof(SqStack));
s->top=-1;
}
//销毁栈
void ClearStack(SqStack *&s)
{
free(s);
}
//进栈
int Push(SqStack *&s,ElemType e)
{
if(s->top==MaxSize-1)
return 0;
s->top++;
s->data[s->top]=e;
return 1;
}
//出栈
int Pop(SqStack *&s,ElemType &e)
{
if(s->top==-1)
return 0;
e=s->data[s->top];
s->top--;
return 1;
}
//取栈顶元素
int GetTop(SqStack *s,ElemType &e)
{
if(s->top==-1)
return 0;
e=s->data[s->top];
return 1;
}
//显示栈中元素
void DispStack(SqStack *s)
{
int i;
for(i=s->top;i>=0;i--)
cout<<s->data[i];
}
//十六进制判断
void Judge(SqStack *s)
{
int i;
for(i=s->top;i>=0;i--)
{
if(s->data[i]==10) cout<<"A";
else if(s->data[i]==11) cout<<"B";
else if(s->data[i]==12) cout<<"C";
else if(s->data[i]==13) cout<<"D";
else if(s->data[i]==14) cout<<"E";
else if(s->data[i]==15) cout<<"F";
else cout<<s->data[i];
}
}
void function_1()
{
SqStack *s;
InitStack(s);
int i,t,j,m;//要转换的进制,j要被转换的数
cout<<"请输入要转换的数(注意:j的范围):";
cin>>j;
cout<<"请输入要转换成的进制(二进制(输入2),八进制(输入8),十六进制(输入16)):";
cin>>i;
if(i==2||i==8)
{
if(j<0)
{ m=-j;
while(m!=0)
{
t=m%i;
Push(s,t);
m=m/i;
}
Push(s,1);
}
else if(j>0)
{
while(j!=0)
{
t=j%i;
Push(s,t);
j=j/i;
}
Push(s,0);
}
cout<<"转换成"<<i<<"进制后的代码(首位为符号位):";
DispStack(s);
ClearStack(s);
}
if(i==16)
{
if(j<0)
{ m=-j;
while(m!=0)
{
t=m%i;
Push(s,t);
m=m/i;
}
Push(s,1);
}
else if(j>0)
{
while(j!=0)
{
t=j%i;
Push(s,t);
j=j/i;
}
Push(s,0);
}
cout<<"转换成"<<i<<"进制后的代码(首位为符号位):";
Judge(s);
ClearStack(s);
}
cout<<endl;
}
void getstr(char* p){
printf("input:\t");
scanf("%s", p);
return;
}
void function_3(){
char st[255];
int top = 0;
char str[255];
char k;
int i=0;
int st_error=0;
getstr(str);
while ( (k=str[i]) != 0)
{
if (k == '(' ) st[top++] = k;
if (k == ')' )
{
if (top == 0 )
{
st_error=1;
break;
}
else
top--;
}
i++;
}
if(st_error==0&&top==0) printf("匹配通过\n");
else
if(st_error==1) printf("缺少左括号!\n");
else
if(top>0) printf("缺少右括号!\n");
}
#include "stdio.h"
#include "stdlib.h"
#define OK 1
#define ERROR 0
#define OVERFLOW -1
//#define EOF -1
#define STACK_INIT_SIZE 10
#define STACKINCREMENT 1000
#define MAXQSIZE 10
static int i=0;
typedef char ElemType1;
typedef struct StackNode//构造栈
{
ElemType1 *base;
ElemType1 *top;
int stacksize;
}SqStack1;
ElemType1 InitStack(SqStack1 *S)//初始化栈
{
S->base=(ElemType1 *)malloc(STACK_INIT_SIZE*sizeof(ElemType1));
if(!S->base)
{
exit(OVERFLOW);
}
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return OK;
}
ElemType1 StackEmpty(SqStack1 *S)//判 断栈是否为空
{
if(S->top==S->base)
return OK;
else
return ERROR;
}
ElemType1 Push(SqStack1 *S,ElemType1 e)//进栈操作
{
if(S->top-S->base>=S->stacksize)
{
S->base = (ElemType1 *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(ElemType1));
if(!S->base)
{
exit(OVERFLOW);
}
S->top = S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*S->top++=e;
return OK;
}
ElemType1 Pop(SqStack1 *S,ElemType1 *e)//出栈操作
{
if(S->top==S->base)
{
return ERROR;
}
*e=*--S->top;
//printf("%d\n",e);
// return e;
return 0;
}
void ClearStack(SqStack1 *S)//清空栈
{
S->top=S->base;
}
ElemType1 LineEdit(SqStack1 *S )//文本编译
{
char ch, e, a[30];
int i ;
fflush(stdin);//刷新标准输入
ch = getchar();
while(1)//死循环,待改
{
while (ch!='\n')
{
switch(ch)
{
case '#': Pop(S,&e); break; //遇到'#',前面个字符出栈
case '@': ClearStack(S); break; //遇到'@',前面的所以字符出栈
default: Push(S,ch); break; //其他字符进栈
}
ch = getchar();
}
i = 0;
while (!StackEmpty(S))
{
Pop(S,&e);
a[i++]=e;
}
printf("循环输出结果为:");
for(--i; i>= 0; i--)
{
printf("%c",a[i]);
}
printf("\n请再输入几个字符吧:");
ClearStack(S);
ch = getchar();
}
return 0;
}
int function_2(void)
{
SqStack1 S;
printf("\n\t\t\t本程序演示数据结构中文本编辑\n\n");
printf("\t如果输入字符中包含'#',那么它前面的一个字符就会出栈,\n");
printf("\t如果输入的字符中包括'@',那么它前面的所有字符全部出栈(清空)!\n");
printf("请连续输入几个字符初始化栈(eg:abc):");
InitStack(&S);
LineEdit(&S);
system("pause");
return 0;
}
int main()
{
int i;
loop:;
printf("\n\t\t|----------------------------------------------|");
printf("\n\t\t|--------- Please input ( 0 - 3) ------------|");
printf("\n\t\t|----------------------------------------------|");
printf("\n\t\t| 1.数值转换 |");
printf("\n\t\t| 2.文本编辑 |");
printf("\n\t\t| 3.括号匹配 |");
printf("\n\t\t|----------------------------------------------|");
printf("\n\t\t| 0. Exit |");
printf("\n\t\t|----------------------------------------------|");
scanf("%d",&i);
switch(i)
{
case 0:return 0;
case 1:function_1();goto loop;
case 2:function_2();goto loop;
case 3:function_3();goto loop;
default:printf("Input Error!!!");
}
return 0;
}