分数计算器用C++怎摸编??
- 提问者网友:趣果有间
- 2021-07-18 07:06
- 五星知识达人网友:举杯邀酒敬孤独
- 2021-07-18 08:06
分数计算器 1 程序功能简介分数计算及测试程序,具有对输入的运算进“+,-,*,/,^”的功能。 2 课程设计要求(1)将主程序设计成一个分数计算器。(2)可完成“+,-,*,/,^”等基本运算。(3)可将分数化为十进制小数和带分数。(4)可执行“+,-,*,/,^”(含括号的)四则运算。 3.2.3 课程设计说明(1)详细的菜单选项 不退出就可继续(2)遇到0就结束(3)添加语句 使其具有测试功能 既能自动出题测试
#include <stdio.h>
#include "malloc.h"
#include "string.h"
typedef char datetype1;
typedef struct dateare1
{ datetype1 elem;
struct dateare1 *forth; }
datestruct1;
typedef struct {
datestruct1 *base,*top; int length; }
OPTR;
typedef int datetype;
typedef struct sdateare
{ datetype elem; struct sdateare *forth; }
datestruct;
typedef struct
{ datestruct *base,*top; int length; }
OPND; OPND createOPND()
{ OPND L; datestruct *p;
p=(datestruct *)malloc(sizeof(datestruct));
if(p) {L.base=p; L.top=p; L.top->forth=NULL;
L.length=0; return L;}
else {printf("\nINITSTACK ERROR");exit(0);
} }
void pushOPND(OPND *Lsp,datetype e)
{datestruct *p; p=(datestruct *)malloc(sizeof(datestruct)); if(p)
{
(*Lsp).top->elem=e;
p->forth=(*Lsp).top; (*Lsp).top=p; (*Lsp).length++; //printf("\nThe %d in OPND",e); }
else {printf("\nAPPLY MEMORY ERROR");exit(0);
} }
datetype popOPND(OPND *Lsp) {
datestruct *p; p=(*Lsp).top;
if((*Lsp).length)
{ (*Lsp).top=(*Lsp).top->forth; free(p);
(*Lsp).length--; //printf("\nThe %d out OPND",(*Lsp).top->elem);
return (*Lsp).top->elem; }
else printf("STACCK EMPTY"); }
void clearOPND(OPND *Lsp)
{ while((*Lsp).length) popOPND(Lsp); }
datetype getOPND(OPND *Lsp)
{ datestruct *p; if((*Lsp).length)
{ p=(*Lsp).top->forth; return p->elem; }
else printf("STACK EMPTY"); }
OPTR createOPTR(){ OPTR L;
datestruct1 *p; p=(datestruct1 *)malloc(sizeof(datestruct1));
if(p) {L.base=p; L.top=p;
L.top->forth=NULL; L.length=0; return L;}
else {printf("\nINITSTACK ERROR");exit(0);
} }
void pushOPTR(OPTR *Lsp,datetype1 e)
{datestruct1 *p; p=(datestruct1 *)malloc(sizeof(datestruct1));
if(p) { (*Lsp).top->elem=e; p->forth=(*Lsp).top;
(*Lsp).top=p; (*Lsp).length++; //printf("\nThe %c in OPTR",e); }
else {printf("\nAPPLY MEMORY ERROR");exit(0);
} }
datetype1 popOPTR(OPTR *Lsp)
{ datestruct1 *p; p=(*Lsp).top; if((*Lsp).length)
{ (*Lsp).top=(*Lsp).top->forth; free(p); (*Lsp).length--;
//printf("\nThe %c out OPTR",(*Lsp).top->elem);
return (*Lsp).top->elem; }
else printf("\nSTACCK EMPTY"); }
void clearOPTR(OPTR *Lsp)
{ while((*Lsp).length) popOPTR(Lsp); }
datetype1 getOPTR(OPTR *Lsp)
{ datestruct1 *p; if((*Lsp).length)
{ p=(*Lsp).top->forth; return p->elem; }
else printf("\nSTACK EMPTY"); }
char IN(OPND *opnd){ int a=0;char c;int flag=0; c=getchar();
while(c>='0'&&c<='9')
{ a=a*10+c-48; c=getchar(); flag=1;}
if(flag==1) pushOPND(opnd,a)
; return c;}
char precede(char topc,char c)
{ if(topc=='+'||topc=='-')if(c=='*'||c=='/'||c=='(')return '<';
else return '>';
else if(topc=='*'||topc=='/')
if(c=='(') return '<';
else return '>';
else if(topc=='(')if(c==')')return '=';
else return '<';
else if(topc=='#')if(c=='#')return '=';
else return '<'; else return '>'; }
int operate(int a,char c,int b){ switch(c)
{ case '+':a+=b;break;
case '-':a-=b;break;
case '*':a*=b;break;
case '/':a/=b;break;}
return a;}
main()
{ int a,b; OPND opnd; char th; OPTR optr; char c; opnd=createOPND(); optr=createOPTR(); printf("\n请输入你所要计算的表达式:例如4*(5+1)#:\n"); pushOPTR(&optr,'#'); c=IN(&opnd); while(1) { if(c=='#'&&getOPTR(&optr)=='#') break;
switch(precede(getOPTR(&optr),c))
{ case '<': pushOPTR(&optr,c);
c=IN(&opnd); break;
case '=': popOPTR(&optr);
c=IN(&opnd); break;
case '>':a=popOPND(&opnd);
b=popOPND(&opnd);
th=popOPTR(&optr);
a=operate(b,th,a);
pushOPND(&opnd,a); break;
} }
printf("\nThe result is %d\n",getOPND(&opnd)); } 13