C语言实现计算器的功能

liyfamily1219 2010-01-28 03:25:02
只需要实现一个较简单的计算器功能即可,要求用到堆栈或者二叉树,最好能有一些说明
...全文
2319 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
w19931024 2010-11-07
  • 打赏
  • 举报
回复
求 用C语言的windows窗体来实现一个计算器的"sqrt”的功能。
mao_pu_hua 2010-07-29
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>

int expr_pri[128];

typedef struct stacknode
{
int data;
struct stacknode *next;
}StackNode;

typedef struct
{
StackNode *top;
}LinkStack;


void init_stack( LinkStack *s )
{
s -> top = NULL ;
}

int stack_empty( LinkStack *s )
{
return s -> top == NULL ;
}

void push( LinkStack *s, int x )
{
StackNode *p = ( StackNode * ) malloc ( sizeof( StackNode ) );
p -> data = x;
p -> next = s -> top;
s -> top = p;
}

unsigned char pop( LinkStack *s )
{
unsigned char x;
StackNode *p = s -> top ;
if( stack_empty( s ) )
return -1;
x = p -> data;
s -> top = p -> next;
free( p );
return x;
}

unsigned char get( LinkStack *s )
{
unsigned char x;
StackNode *p = s -> top;
if( stack_empty( s ) )
return -1;
x = p -> data;
return x;
}
int transform( char *buf, int bufflen, LinkStack *s ,char *result )
{
char * p, *q;
int i, flag;

if( buf == NULL )
return -1;
memset( result ,'\0', 1024 );


p = buf;
q = result;
flag = -1;

while( *p )
{

if( flag == 0 )
{
flag = -1;
*q = ' ';
q ++;
}

if( *p == ' ' )
p ++;
else if( *p >= '0' && *p <= '9' )
{
while( *p >= '0' && *p <= '9' )
{
flag = 0;
*q = *p;
q ++;
p ++;
}
}else if( *p == '(' || *p == '[' || *p == '{' )
{
push( s ,*p );
p ++;
}else if( *p == ')' || *p == ']' || *p == '}' )
{
*q = pop( s );
while( *q != '(' && *q != '[' && *q != '{' )
{
q ++;
*q = pop( s );
}
p++;
}else
{
if( expr_pri[*p] != 0 && stack_empty( s) == 0)
{
int popnum;
popnum = get(s);
while( expr_pri[popnum] >= expr_pri[*p] && stack_empty( s ) == 0 )
{
popnum = pop(s);
*q = popnum ;
q++;
popnum = get( s );
}
push( s, *p );
}
else
{
push( s, *p );
}
p ++;
}

}

if( flag == 0 )
{
flag = -1;
*q = ' ';
q ++;
}


while( stack_empty( s ) == 0 )
{
*q = pop( s );
q ++;
}

return 0;
}



int init( int *p , int len)
{
int i ;
for( i = 0; i < len ; i++ )
p[i] = 0;

p['+'] = 21;
p['-'] = 21;
p['*'] = 22;
p['/'] = 22;
return 0;
}

int account( LinkStack *s,char * buf )
{
char *p;
int num, first, second, result;

p = buf;
num = 0;


while( *p )
{
if( *p >= '0' && *p <= '9' )
{
while( *p >= '0' && *p <= '9' )
{
num = num * 10 + *p - '0';
p ++;
}
}else if( *p == ' ' )
{
//printf( "num = %d\n", num );
if( num != 0 )
push( s,num );
num = 0;
p ++;
} else if( expr_pri[*p] != 0 )
{
if( !stack_empty( s ) )
{
second = pop( s );
}else
return -1;
if( !stack_empty( s ) )
{
first = pop( s );
}else return -1;

if( *p == '+' )
result = first + second ;
else if( *p == '-' )
result = first - second;
else if( *p == '*' )
result = first * second;
else if( *p == '/' )
result = first /second ;
push( s, result );
p ++;
}else
{
p++;
}
}

while( !stack_empty( s ) )
{
printf( "result = %d\n", pop( s ) );
}
return 0;
}


int main( void )
{
char buf[1024], result[1024];
int buflen;
LinkStack *op;
op = malloc( sizeof( LinkStack ) );


init_stack( op );
while( 1 )
{

printf( "\\>please input the formula\n" );
printf( ">" );
memset( result, '\0', strlen(result ) );
memset( buf, '\0', strlen( buf ) );

scanf( "%s", buf );
buflen = strlen( buf );

while( !stack_empty( op ) )
{
pop( op );
}

if( init( expr_pri, 128 ) < 0 )
{
printf( "[%s|%d] init error\n" , __FILE__, __LINE__ );
continue;
}


if( transform( buf, buflen, op ,result ) < 0 )
{
printf( "[%s|%d] transform error\n" , __FILE__, __LINE__ );
continue;
}

if( account( op,result ) < 0 )
{
printf( "[%s|%d] account error\n" , __FILE__, __LINE__ );
continue;
}
}

return 0;
}


这个是我前段时间写的,纯c语言,利用堆栈实现的。支持+,-, *, /。以及()【】,还有一些其它运算符号其实都可以自己加的,当时比较忙,没有加入。

这个是在linux下面实现的。可能有些头文件windows下没有,去掉就ok了。希望对你有帮助。
其实这个最好看看算法自己写。
simble2010 2010-07-29
  • 打赏
  • 举报
回复
你的要是算个2-9的话就会出错
taotaotheripper 2010-07-28
  • 打赏
  • 举报
回复
最后一条,当时为了动手练习练习,所以自己写了一个栈,你可以用STL的
taotaotheripper 2010-07-28
  • 打赏
  • 举报
回复
编程这个东西,不练手,肯定是要悲剧的...lz加油~
taotaotheripper 2010-07-28
  • 打赏
  • 举报
回复

//******************************
//* 作者:刘澜涛 *
//* 学号:00848200 *
//* 题目:中缀表达式的计算 *
//* 日期:2009/10/18 *
//******************************

#include<iostream>
#include<assert.h>
#include<cmath>
using namespace std;
template<class T>
class CStack//自己的定义的栈
{
private:
T *stack;
int top;
int len;
public:
CStack(int lenth)//栈的构造函数
{
stack=new T[lenth];
top=0;
len=lenth;
}
void push(T node)//栈的增加结点
{
assert(top<len);
stack[top]=node;
top++;
}
T pop()//栈顶结点弹出
{
assert(top>0);
top--;
return stack[top];
}
T gettop()//返回栈顶元素
{
assert(top>0);
return stack[top-1];
}
void makeempty()//清空栈
{
top=0;
}
bool isempty()//返回栈是否为空
{
if(top==0)
return true;
else return false;
}
};
double calculate(char c,double num2,double num1)//基本计算函数
{
switch(c)
{
case '+':return num2+num1;break;
case '-':return num2-num1;break;
case '*':return num2*num1;break;
case '/':return num2/num1;break;
}
}
bool canbedone(char c,double num)//判断是否出现除数为零情况的函数
{
if(c=='/'&&fabs(num)<1.0e-10)
{
cout<<"表达式除数不能为零,请重新输入"<<endl;
return 0;
}
else return 1;
}
int main()
{
char str[1000]={0};//用来存放表达式
CStack<double> num(1000);//用来存放待计算的数字的栈
CStack<char> sign(1000);//用来存计算符号
int len,i,time;
double temp;//过渡变量
while(1)
{
begin:
num.makeempty();
sign.makeempty();
memset(str,0,sizeof(str));
cout<<"请输入您要计算的表达式,输入'end'退出程序"<<endl;//输入提示
cin>>str;
len=strlen(str);
for(i=0;i<=len-1;i++)
{
if(strcmp(str,"end")==0)
return 0;
else
{
if(!((str[i]>='0'&&str[i]<='9')||str[i]=='+'||str[i]=='-'||
str[i]=='*'||str[i]=='/'||str[i]=='('||str[i]==')'||str[i]=='.'))//判断式子中是否存在非法字符
{
cout<<"您输入的表达式有误,请重新输入"<<endl;
goto begin;
}
}
}
temp=0;
time=0;
for(i=0;i<=len-1;i++)
{
if(str[i]>='0'&&str[i]<='9')
{
if(time==0)
temp=str[i]-'0'+temp*10;
else
{
temp=(str[i]-'0')/pow(10,time)+temp;
time++;
}
}//把字符串转化为对应数字
else if(str[i]==')')//遇到闭括号开始弹出元素并计算,到开括号后停止
{
time=0;//遇到闭括号,小数点位数清零
while(sign.gettop()!='(')
{
if(!canbedone(sign.gettop(),temp))
goto begin;//判断是否有除数为0
temp=calculate(sign.pop(),num.pop(),temp);
if(sign.isempty())//没有开括号,说明表达式有误,重新输入若
{
cout<<"表达式有误,请重新输入"<<endl;
goto begin;
}
}
sign.pop();
}
else if(str[i]=='(')
sign.push(str[i]);//遇到开括号直接压栈
else if(str[i]=='.')//若遇到小数点则temp(记录小数点位数)开始记录
time=1;
else
{
time=0;//若遇到运算符则小数点位数清零
if(sign.isempty())
sign.push(str[i]);
else if(sign.gettop()=='/'||sign.gettop()=='*'||((sign.gettop()=='+'||sign.gettop()=='-')&&
(str[i]=='+'||str[i]=='-')))//若运算符str[i],与sign栈顶的运算符同级,则弹出栈顶进行计算,再进行压栈
{
if(!canbedone(sign.gettop(),temp))
goto begin;//判断是否有除数为0
temp=calculate(sign.pop(),num.pop(),temp);
sign.push(str[i]);
}
else
sign.push(str[i]);//不同级则直接压栈
num.push(temp);//把运算符前面的数字压栈
temp=0;
}
}
while(!sign.isempty())
{
if(!canbedone(sign.gettop(),temp))
goto begin;//判断是否有除数为0
if(sign.gettop()=='(')
{
cout<<"表达式有误,请重新输入"<<endl;
goto begin;
}//遇到多余开括号说明表达式有误
temp=calculate(sign.pop(),num.pop(),temp);
}
cout<<temp<<endl<<endl;//输出结果并额外输出一个空行
}
return 0;
}

这是我大二开学时的作业,但愿你不要直接抄,还是看懂了自己写一个好点....
liyfamily1219 2010-07-28
  • 打赏
  • 举报
回复
谢谢大家了,只是还不是我想要的
z569362161 2010-01-28
  • 打赏
  • 举报
回复
#include   <stdio.h>   
#include <stdlib.h>
void min()
{
int a;
int b;
int c;
char op;
printf("1:");
scanf("%d",&a);
fflush(stdin);

printf("+-*/:");
scanf("%c",&op);
fflush(stdin);

printf("2:");
scanf("%d",&b);
fflush(stdin);

switch(op)
{
case '+':
c=a+b;
break;
case '-':
c=a-b;
break;
case '*':
c=a*b;
break;

case '/':
c=a/b;
break;
default:
printf("+-/*!!!!!error");
return;
}

printf ("%d\n",c);
/**/
return ;
}

int main()
{
while(1)
{
min();
}
return 0;
}

z569362161 2010-01-28
  • 打赏
  • 举报
回复
一个较简单的计算器功能

还要求用到堆栈或者二叉树?
CCCCCCCCCCCCCCC 2010-01-28
  • 打赏
  • 举报
回复
放假了还有作业贴?

70,018

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧