回复看看
多学习点例子,帮助很大!
hh
抓紧时间学习
膜拜膜拜
33333333333333333333333
学习
{:1_1:}
感谢小甲鱼老师
小甲鱼,你真好
真的好极了
99
我看看
来学习
#include <stdio.h>
#include <stdlib.h>
#define STACKSIZE 100
#define INIT_STACK_SIZE 10
typedef struct SqStack
{
char *base;
char *top;
int stacksize;
}SqStack;
void InitSqStack(SqStack *s)
{
s->base=(char*)malloc(STACKSIZE*sizeof(char));
if(!s->base)
exit(0);
s->top=s->base;
s->stacksize=STACKSIZE;
}
void Push(SqStack *s,char mychar)
{
if(s->top-s->base==s->stacksize)
{
s->base=(char*)realloc(s->base,(STACKSIZE+INIT_STACK_SIZE)*sizeof(char));
if(!s->base)
exit(0);
s->top=s->base+s->stacksize;
s->stacksize+=INIT_STACK_SIZE;
}
*(s->top++)=mychar;
}
void Pop(SqStack *s,char *ch)
{
if(s->top==s->base)
exit(0);
*ch=*(--s->top);
}
int EmptySqStack(SqStack *s)
{
if(s->top!=s->base)
return 1;
else
return 0;
}
int main()
{
char a_mychar,b_mychar,c_mychar;
char a_elem,b_elem,c_elem;
int temp=0;
SqStack a,b,c;
InitSqStack(&a);
InitSqStack(&b);
InitSqStack(&c);
scanf("%c",&a_mychar);
while(a_mychar!='#')
{
Push(&a,a_mychar);
scanf("%c",&a_mychar);
}
fflush(stdin);
scanf("%c",&b_mychar);
while(b_mychar!='#')
{
Push(&b,b_mychar);
scanf("%c",&b_mychar);
}
while(EmptySqStack(&a)&&EmptySqStack(&b))
{
Pop(&a,&a_elem);
Pop(&b,&b_elem);
c_mychar=((a_elem-'0')+(b_elem-'0')+temp)%10+'0';
temp=((a_elem-'0')+(b_elem-'0'+temp))/10;
Push(&c,c_mychar);
}
while(EmptySqStack(&a))
{
Pop(&a,&a_elem);
c_mychar=((a_elem-'0')+temp)%10+'0';
temp=((a_elem-'0')+temp)/10;
Push(&c,c_mychar);
}
while(EmptySqStack(&b))
{
Pop(&b,&b_elem);
c_mychar=((b_elem-'0')+temp)%10+'0';
temp=((b_elem-'0')+temp)/10;
Push(&c,c_mychar);
}
if(temp==1)
Push(&c,'1');
while(EmptySqStack(&c))
{
Pop(&c,&c_elem);
printf("%c",c_elem);
}
return 0;
}
自己实现的代码,希望大家多多交流,欢迎指正{:5_92:}
谢谢甲鱼哥
{:5_90:}
#include<cstdio>
#include<cstdlib>
typedef int SElemType;
#define MAXSIZE 100000
typedef struct StackNode{
SElemType data;
struct StackNode *next;
};
typedef struct LinkStack{
StackNode *top;
int count;
};
void initLinkStack(LinkStack *S){
S->top=NULL;
S->count=0;
}
void Push(LinkStack *S,SElemType e){
StackNode *p=(StackNode *)malloc(sizeof(StackNode));
p->data=e;
p->next=S->top;
S->top=p;
S->count++;
}
void Pop(LinkStack *S,SElemType *e){
StackNode *p;
p=S->top;
*e=S->top->data;
S->top=S->top->next;
S->count--;
free(p);
}
int main()
{
LinkStack a,b,c;
char a_t;char b_t;
int i=0;
int e,d;
int t=0;
initLinkStack(&a);initLinkStack(&b);initLinkStack(&c);
printf("请输入两个大整数,两数用空格隔开:\n");
scanf("%s%s",a_t,b_t);
while(a_t!='\0'){
Push(&a,a_t-'0');
++i;
}
i=0;
while(b_t!='\0'){
Push(&b,b_t-'0');
++i;
}
while(a.count && b.count){
Pop(&a,&e);
Pop(&b,&d);
Push(&c,(e+d+t)%10);
t=(int)((e+d+t)/10);
}
while(a.count){
Pop(&a,&e);
Push(&c,(e+t)%10);
t=(int)((e+t)/10);
}
while(b.count){
Pop(&b,&e);
Push(&c,(e+t)%10);
t=(int)((e+t)/10);
}
printf("计算的结果为:\n");
if(t>0)printf("%d",t);
while(c.count){
Pop(&c,&e);
printf("%d",e);
}
printf("\n");
return 0;
}
/*
【个人原创】
以链栈的形式实现。。
欢迎大家互相学习*/
很喜欢小甲鱼
123333333333333