您的位置:首页 > 其它

简单计算器(不支持括号)

2012-07-15 20:08 120 查看
include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct count
{
int num;
char mark;
struct count *next;
};
struct count *top1;
struct count *top2;
void creat_list()
{
top1=(struct count*)malloc(sizeof(struct count));
top2=(struct count*)malloc(sizeof(struct count));
top1=NULL;
top2=NULL;
}
void insert_list1(int num)
{
struct count *p=(struct count *)malloc(sizeof(struct count));
struct count *q=top2;
if(q!=NULL)
{
if((q->mark)=='*')
{
p=top1;
num=num*(top1->num);
top1=top1->next;
top2=top2->next;
}
else if((q->mark)=='/')
{
num=(top1->num)/num;
top2=top2->next;
top1=top1->next;
}
}
p->num=num;
p->next=top1;
top1=p;
}
void display_list1()
{
struct count *p=top1;
printf("num:");
while(p)
{
printf("%d\t",p->num);
p=p->next;
}
printf("\n");
}
void display_list2()
{
struct count *p=top2;
printf("mark:");
while(p)
{
printf("%c\t",p->mark);
p=p->next;
}
printf("\n");
}
void insert_list2(char ch)
{
if(ch=='\0');
else
{
struct count *p=(struct count*)malloc(sizeof(struct count));
p->mark=ch;
p->next=top2;
top2=p;
}
}
void show(char a[])
{
int n=0;
int i=0;
int num=0;
n=strlen(a);
for(i=0;i<=n;i++)
{
if(a[i]>='0'&&a[i]<='9')
{
num=num*10+a[i]-48;
}
else
{
insert_list1(num);
if(i!=n)
{
insert_list2(a[i]);
}

num=0;
}
}
}
void math_list()
{
int num=0;
if((top1->next)==NULL)
{
;
}
else
{
while(top1!=NULL)
{
if(top2==NULL)
{
num=num+top1->num;
top1=top1->next;
}
else
{
switch(top2->mark)
{
case '+':
num=num+top1->num;
top1=top1->next;
top2=top2->next;
break;
case '-':
num=num-top1->num;
top1=top1->next;
top2=top2->next;
break;
default:
printf("input error!");
}
}
}
}

printf("the result is :%d\n",num);
}
int main()
{
char a[20];
creat_list;
printf("please input\n");
scanf("%s",a);
show(a);
// display_list1();
// display_list2();
math_list();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐