- 论坛徽章:
- 0
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
/*先来做一个char(+,-,*,/,^,sin,cos)堆栈*/
#define MAXSTACK 30
typedef struct stack
{
int top;
char entry[MAXSTACK][6];//这里的6表示压入的字符串长度.
}Stack;
/*初始化函数*/
void CreateStack(Stack* s)
{
s->top=0;
}
/*判断是否为空函数*/
bool StackEmpty(Stack* s)
{
return s->top<=0;
}
/*判断是否为满函数*/
bool StackFull(Stack* s)
{
return s->top>=MAXSTACK;
}
/*入栈和出栈*/
void Push(char item[],Stack* s)
{
if(StackFull(s))
printf("Stack is full!\n" ;
else if(strlen(item)>5)
printf("你输入的算式中运算符号出错!\n" ;
else
strcpy(s->entry[s->top++],item);
}
void Pop(char* item,Stack* s)
{
if(StackEmpty(s))
printf("Stack is empty!\n" ;
else
strcpy(item,s->entry[--s->top]);
}
/*读取top元素,但不Pop这个元素*/
void StackTop(char** ptopentry,Stack* stack)
{
if (StackEmpty(stack))
{
*ptopentry=NULL;
printf("堆栈已空" ;
}
else
{
int temp=stack->top-1;
*ptopentry=stack->entry[temp];
}
}
/*下面来做一个链表*/
typedef struct listnode
{
char entry[10];
listnode* next;
}ListNode;
typedef struct list
{
int count;
ListNode* head;
ListNode* current;
}List;
/*初始化链表函数*/
void CreateList(List* list)
{
list->count=0;
list->head=NULL;
list->current=NULL;
}
/*生成一个ListNode*/
ListNode* MakeListNode(char* str)
{
ListNode* p=(ListNode*)malloc(sizeof(ListNode));
if(p)
{
strcpy(p->entry,str);
p->next=NULL;
}
else
printf("内存不足!\n" ;
return p;
}
/*添加一个元素*/
bool AddListNode(char* str,List* list)
{
ListNode* newnode=MakeListNode(str);
if(newnode)
{
if (list->count==0)
{
list->count=1;
list->head=list->current=newnode;
}
else
{
list->count++;
list->current->next=newnode;
list->current=newnode;
}
return true;
}
return false;
}
/*释放内存函数*/
void FreeM(List* list)
{
ListNode* temp1;
ListNode* temp2;
temp1=list->head;
while(temp1)
{
temp2=temp1->next;
free(temp1);
temp1=temp2;
}
}
/*读出链表中第i个元素*/
void ReadListNode(int i,ListNode** ppln,List* list)
{
if (i>0 && i<=list->count)
{
ListNode* temp=list->head;
for (int n=2;n<=i ;n++ )
{
temp=temp->next;
}
*ppln=temp;
}
else
*ppln=NULL;
}
/*将字符串放入链表函数,已测试ok*/
void StrToList(char* str,List** list)
{
char a[10];/*链表中的一个ListNode的内容*/
char* p=a;
char temp;/*str中读取的一个字符*/
char* tstr=str;/*临时字符串*/
temp=*tstr;
while (temp!='=')
{
if((temp>='0' && temp<='9') || temp=='.')
{ while ((temp>='0' && temp<='9') || temp=='.')
{
*p=temp;
p++;
tstr++;
temp=*tstr;
}
*p='\0';
AddListNode(a,*list);
p=a;
}
else if((temp>='a' && temp<='z') ||(temp>='A' && temp<='Z'))
{ while ((temp>='a' && temp<='z') ||(temp>='A' && temp<='Z'))
{
*p=temp;
p++;
tstr++;
temp=*tstr;
}
*p='\0';
AddListNode(a,*list);
p=a;
}
else
{
*p=temp;
p++;
*p='\0';
AddListNode(a,*list);
p=a;
temp=*(++tstr);
}
}
AddListNode("=",*list);
}
/*判断并返回链表元素的类型0:是数字,1:是+,2:是-,3:*,4:/,5:^,6:!阶乘,7 ,8 9:sin,10:cos,11:tan,12:cot,13:loga(b)等*/
int TypeOfListNode(ListNode* ln)
{
if(ln->entry[0]>='0' && ln->entry[0]<='9')
return 0;
else if (ln->entry[0]=='+')
{
return 1;
}
else if (ln->entry[0]=='-')
{
return 2;
}
else if (ln->entry[0]=='*')
{
return 3;
}
else if (ln->entry[0]=='/')
{
return 4;
}
else if (ln->entry[0]=='^')
{
return 5;
}
else if (ln->entry[0]=='!')
{
return 6;
}
else if (ln->entry[0]=='(')
{
return 7;
}
else if (ln->entry[0]==')')
{
return 8;
}
else if (ln->entry[0]=='s')
{
return 9;
}
else if (ln->entry[0]=='c' && ln->entry[2]=='s')
{
return 10;
}
else if (ln->entry[0]=='t')
{
return 11;
}
else if (ln->entry[0]=='c' && ln->entry[2]=='t')
{
return 12;
}
else if (ln->entry[0]=='l' && ln->entry[1]=='o' && ln->entry[2]=='g')
{
return 13;
}
else if (ln->entry[0]=='=')
{
return 14;
}
}
/*由str判断它的类型*/
int TypeOfString(char* p)
{
if(*p>='0' && *p<='9')
return 0;
else if (*p=='+')
{
return 1;
}
else if (*p=='-')
{
return 2;
}
else if (*p=='*')
{
return 3;
}
else if (*p=='/')
{
return 4;
}
else if (*p=='^')
{
return 5;
}
else if (*p=='!')
{
return 6;
}
else if (*p=='(')
{
return 7;
}
else if (*p==')')
{
return 8;
}
else if (*p=='s')
{
return 9;
}
else if (*p=='c' && *(p+2)=='s')
{
return 10;
}
else if (*p=='t')
{
return 11;
}
else if (*p=='c' && *(p+2)=='t')
{
return 12;
}
else if (*p=='l' && *(p+1)=='o' && *(p+2)=='g')
{
return 13;
}
else if (*p=='=')
{
return 14;
}
}
/*由str判断它的运算级别*/
int Type2OfString(char* p)
{
if (*p=='+' || *p=='-')
{
return 1;
}
else if (*p=='*' || *p=='/')
{
return 2;
}
else if (*p=='(')
{
return 7;
}
else
return 3;
}
/*将中序式化成后缀式*/
void InfixToPostfix(List* list,List** pplist)
{
Stack stack;/*一个存放符号的堆栈*/
CreateStack(&stack);
char* ptopentry;/*堆栈最上面一个元素*/
char chars[10];
char* pchar=chars;//**********这个错误让我用了三个小时多!!!!!
ListNode* pln;/*从list中取出的元素*/
int type;/*取出的元素的类型*/
for (int i=1;i<=list->count ;i++ )
{
ReadListNode(i,&pln,list);
switch (type=TypeOfListNode(pln))
{
case 0:
AddListNode(pln->entry,*pplist);
break;
case 7:
Push("(",&stack);
break;
case 8:
for (Pop(pchar,&stack);TypeOfString(pchar)!=7 op(pchar,&stack) )
{
AddListNode(pchar,*pplist);
}
break;
case 14:
while (!StackEmpty(&stack))
{
Pop(pchar,&stack);
AddListNode(pchar,*pplist);
}
AddListNode("=",*pplist);
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 9:
case 10:
case 11:
case 12:
case 13:
do
{
if (StackEmpty(&stack))
break;
else
{
StackTop(&ptopentry,&stack);
if (Type2OfString(ptopentry)==7)
break;
else if (Type2OfString(ptopentry)<Type2OfString(pln->entry))
break;
else
{
Pop(pchar,&stack);
AddListNode(pchar,*pplist);
}
}
}
while (1);
Push(pln->entry,&stack);
break;
}
}
}
/*做一个float型stackf*/
#define MAXSTACKF 20
typedef struct stackf
{
int top;
float entry[MAXSTACKF];//这里的MAXSTACKF表示压入的最大数字个数.
}Stackf;
/*初始化函数*/
void CreateStackf(Stackf* s)
{
s->top=0;
}
/*判断是否为空函数*/
bool StackfEmpty(Stackf* s)
{
return s->top<=0;
}
/*判断是否为满函数*/
bool StackfFull(Stackf* s)
{
return s->top>=MAXSTACKF;
}
/*入栈和出栈*/
void Pushf(float item,Stackf* s)
{
if(StackfFull(s))
printf("Stackf is full!\n" ;
else
s->entry[s->top++]=item;
}
void Popf(float* item,Stackf* s)
{
if(StackfEmpty(s))
printf("Stackf is empty!\n" ;
else
*item=s->entry[--s->top];
}
/*计算后缀式的值函数*/
void ValueOfPostfix(List* list,float* value)
{
Stackf stack;
CreateStackf(&stack);
ListNode* pln;
float x,y;
float temp;
for (int i=1;i<=list->count ;i++ )
{
ReadListNode(i,&pln,list);
switch (TypeOfListNode(pln))
{
case 0:
Pushf(atof(pln->entry),&stack);
break;
case 1:
if (StackfEmpty(&stack))
{
printf("你输入的+表达式错误!请重新输入!\n" ;
return;
}
else
{
Popf(&y,&stack);
if (StackfEmpty(&stack))
{
Pushf(y,&stack);
}
else
{
Popf(&x,&stack);
Pushf((x+y),&stack);
}
}
break;
case 2:
if (StackfEmpty(&stack))
{
printf("你输入的-表达式错误!请重新输入!\n" ;
return;
}
else
{
Popf(&y,&stack);
if (StackfEmpty(&stack))
{
Pushf(-y,&stack);
}
else
{
Popf(&x,&stack);
Pushf((x-y),&stack);
}
}
break;
case 3:
if (StackfEmpty(&stack))
{
printf("你输入的*表达式错误!请重新输入!\n" ;
return;
}
else
{
Popf(&y,&stack);
if (StackfEmpty(&stack))
{
printf("你输入的*表达式错误!请重新输入!\n");
}
else
{
Popf(&x,&stack);
Pushf((x*y),&stack);
}
}
break;
case 4:
if (StackfEmpty(&stack))
{
printf("你输入的/表达式错误!请重新输入!\n");
return;
}
else
{
Popf(&y,&stack);
if (StackfEmpty(&stack))
{
printf("你输入的/表达式错误!请重新输入!\n");
}
else
{
Popf(&x,&stack);
Pushf((x/y),&stack);
}
}
break;
case 14:
if (stack.top==1)
{
Popf(value,&stack);
return;
}
else
{
printf("你输入的表达式整体有错误,请重新输入!\n");
return;
}
case 5:
if (StackfEmpty(&stack))
{
printf("你输入的^表达式错误!请重新输入!\n");
return;
}
else
{
Popf(&y,&stack);
if (StackfEmpty(&stack))
{
printf("你输入的^表达式错误!请重新输入!\n");
}
else
{
Popf(&x,&stack);
Pushf(powf(x,y),&stack);
}
}
break;
case 6:
if (StackfEmpty(&stack))
{
printf("你输入的!表达式错误!请重新输入!\n");
return;
}
else
{
Popf(&y,&stack);
int tmp=(int)y;
if (tmp>=0)
{
if(tmp==0)
Pushf(1,&stack);
else
{
int tmp2=1;
for (int i=1;i<=tmp ;i++ )
{
tmp2*=i;
}
Pushf((float)tmp2,&stack);
}
}
else
{
printf("在计算阶乘时出错!\n");
return;
}
}
break;
case 13:
if (StackfEmpty(&stack))
{
printf("你输入的log表达式错误!请重新输入!\n");
return;
}
else
{
Popf(&y,&stack);
if (StackfEmpty(&stack))
{
printf("你输入的log表达式错误!请重新输入!\n");
}
else
{
Popf(&x,&stack);
Pushf(log10f(y)/log10f(x),&stack);
}
}
break;
case 9:
if (StackfEmpty(&stack))
{
printf("你输入的sin表达式错误!请重新输入!\n");
return;
}
else
{
Popf(&y,&stack);
Pushf(sinf(y),&stack);
}
break;
case 10:
if (StackfEmpty(&stack))
{
printf("你输入的cos表达式错误!请重新输入!\n");
return;
}
else
{
Popf(&y,&stack);
Pushf(cosf(y),&stack);
}
break;
case 11:
if (StackfEmpty(&stack))
{
printf("你输入的tan表达式错误!请重新输入!\n");
return;
}
else
{
Popf(&y,&stack);
Pushf(tanf(y),&stack);
}
break;
case 12:
if (StackfEmpty(&stack))
{
printf("你输入的cot表达式错误!请重新输入!\n");
return;
}
else
{
Popf(&y,&stack);
Pushf(1/(tanf(y)),&stack);
}
break;
}
}
}
int main()
{
printf("\n");
//printf("2007/1/1~2007/1/2 \n");
printf("使用说明:\n");
printf(" 1,运算符号:+,-,*(乘号,不能省略),/,^(乘方,开方),!(阶乘),\n");
printf(" sin(弧度),cos,tan,cot,loga(b)\n");
printf(" 2,输出结果要=\n");
printf("请输入一个算式:\n");
/*读取100个字符*/
char str1[100];
fgets(str1,100,stdin);
List list;
List list2;
while(strcmp(str1,"exit")!=0)
{
//printf("%s",str1);
/*测试堆栈
Stack s;
CreateStack(&s);
char x[6];
Push("s123",&s);
Push("s456",&s);
Pop(x,&s);
printf("%s\n",x);
Pop(x,&s);
printf("%s\n",x);
char* p;
StackTop(&p,&s);
printf("%s\n",p);*/
/*测试链表,已ok*/
CreateList(&list);
List* plist=&list;
StrToList(str1,&plist);
/*将中序转换成后缀式*/
CreateList(&list2);
List* plist2=&list2;
InfixToPostfix(plist,&plist2);
ListNode* temp;
/* for(int i=1;i<=list.count;i++)
{
ReadListNode(i,&temp,&list);
printf("%s\n",temp->entry);
printf("%d\n",TypeOfListNode(temp));
}
for(int j=1;j<=list2.count;j++)
{
ReadListNode(j,&temp,&list2);
printf("%s",temp->entry);
}
*/
/*测试后缀式的值函数*/
float value;
ValueOfPostfix(&list2,&value);
printf("%f\n",value);
FreeM(&list);
FreeM(&list2);
fgets(str1,100,stdin);
}
FreeM(&list);
FreeM(&list2);
return 0;
} |
|