模拟计算器程序 设计一个程序来模拟一个简单的手持计算器。程序支持算术运算+、-、*、/、=、以及C(清除)

基本要求
程序运行时,显示一个窗口,等待用户输入,用户可以从键盘输入要计算的表达式,输入的表达式显示在窗口中,用户键入’=’ 符号后,窗口显示出结果。(不用考虑运算符的优先级)
测试数据
程序输入不少于5种不同的表达式进行测试。

第1个回答  2011-06-01
/*
一个计算器小程序
1. 支持加减乘除。可以扩展,只要修改cal()函数和增加运算符判断。
2. 回车和等于号或者下一个运算符都可以作为求值号,
结果可以作为下一个运算对象,也可以不作为运算对象,程序自行判断。
3. 支持容错功能,整数和实数任意可以运算,程序自行判断。
4. 一直在循环接受输入,每一个输入都认为是字符,由程序自
己判断为运算数或者运算符号并且智能转化,非法输入会被拒绝,
不会显示在终端上。
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <math.h>
#include <conio.h>

typedef unsigned char bool_t;
enum bool_value
{
PAL_FALSE = 0,
PAL_TRUE
};

enum operate_value
{
PLUS = 0,
MINUS,
MULTIP,
DIVIDE
};

char* opr_str[] = {"+", "-", "*", "/" };

#define FLOAT 0x1
#define INT 0x2
#define OPR 0x3
#define NONE 0x0

/*去掉输错的字符,重新输出*/
#define BACK_GRACEFUL(s) \
{(s)--; \
*(s) = '\0'; \
system("cls"); \
display_menu(); \
printf("%s", in_str);}

/*判断是否是数字*/
#define IS_DIGIT(x) \
((x) <= '9' && (x) >= '0')

/*判断是否是运算符*/
#define IS_OPR(x) \
(((x) == '+') \
|| ((x) == '-') \
|| ((x) == '*') \
|| ((x) == '/'))

/*判断是否是小数点*/
#define IS_POINT(x) \
((x) == '.')

#define IS_CLS(x) \
((x) == 'c' || (x) == 'C')

#define IS_RTN(x) \
((x) == 'r' || (x) == 'R')

#define IS_EQUAL(x) \
((x) == '=' || (x) == 10 || (x) == 13)

#define CPY_RST \
{ \
memcpy(&pa1, &result, sizeof(result));\
strcpy(str_pa1, in_str); \
result.type = NONE; \
result.u.i = 0; \
}

#define CLS_MEM \
{ \
memset(in_str, 0 , strlen(in_str)); \
memset(str_pa1, 0, strlen(str_pa1));\
memset(str_pa2, 0, strlen(str_pa2));\
ppa1 = str_pa1; \
ppa2 = str_pa2; \
pstr = in_str; \
pa1.type = NONE; \
pa2.type = NONE; \
opr.type = NONE; \
system("cls"); \
display_menu(); \
result.type = NONE; \
result.u.i = 0; \
}

unsigned char2int(char *str)
{
unsigned int len = 0;
int i = 0;
unsigned int rtn = 0;
char max[] = "4294967295";

if(!str)
{
return 0;
}

len = strlen(str);
if(len > 10 || ( len == 10 && strncmp(max, str, 10) < 0))
{
return 0;
}

for(i = 0; i < (int)len; i++)
{
if(str[i] > '9' || str[i] < '0')
{
return 0;
}
}

for(i = 0; i < (int)len; i++)
{
rtn += (str[i] - '0') * (unsigned)pow(10, len - 1 - i);
}
return rtn;
}

int str2int(char *str, unsigned *ret)
{
char constant[11] = "4294967295";
int digit_num = 0;
unsigned res = 0;

if(str == (char*)0)
{
return -1;
}

if(strlen(str) > 10)
{
return -1;
}
else if(strlen(str) == 10)
{
if(strcmp(constant, str) < 0)
{
return -1;
}
}

digit_num = strlen(str);

for(int i = 0; i < digit_num; i++)
{
res += (str[i] - '0') * pow((double)10, (double)(digit_num - i -1));
}

*ret = res;
return 0;
}

int str2float(char *str, float *ret)
{
char *p = NULL;
char ints[10] = {0};
char fs[10] = {0};
unsigned int i = 0;
unsigned int flo = 0;
float f = 0.0;
unsigned len_f = 0;

if(!str)
{
return -1;
}

p = str;
while(*p != 0)
{
if(*p == '.')
{
p++;
continue;
}

if(*p < '0' || *p > '9')
{
return -1;
}
p++;
}

p = strchr(str, '.');
if(!p)
{
strcpy(ints, str);
}
else
{
strncpy(ints, str, p - str);
strcpy(fs, ++p);
len_f = strlen(fs);
}

i = char2int(ints);
flo = char2int(fs);
f = (float)flo / (float)pow(10, len_f);
f += i;
*ret = f;
return 0;
}

void display_menu()
{
printf("\n**********************************************\n");
printf("* *\n");
printf("* 计算器 1.0版 *\n");
printf("* *\n");
printf("* c(清屏) r(返回菜单) *\n");
printf("* *\n");
printf("**********************************************\n");
printf("\n\nCaculator: ");
}

int get_input(char *in)
{
char ch;
ch = getch();
if(IS_DIGIT(ch) ||
IS_OPR(ch) ||
IS_POINT(ch))
{
printf("%c", ch);
}
else if(IS_CLS(ch) ||
IS_RTN(ch) ||
IS_EQUAL(ch))
{
;
}
else
{
return -1;
}

*in = ch;
return 0;
}

struct member
{
unsigned type;
union
{
unsigned i;
unsigned o;
float f;
}u;
};

int cal(struct member *pa1, struct member *opr,
struct member *pa2, struct member *ret)
{
int is_float;

if(!pa1 || !pa2 || !opr || !ret)
{
return -1;
}

if(pa1->type == FLOAT || pa2->type == FLOAT)
is_float = 1;
else
is_float = 0;

switch(opr->u.o)
{
case PLUS:
if(is_float)
{
ret->type = FLOAT;
if(pa1->type == FLOAT)
ret->u.f += pa1->u.f;
else
ret->u.f += pa1->u.i;

if(pa2->type == FLOAT)
ret->u.f += pa2->u.f;
else
ret->u.f += pa2->u.i;
}
else
{
ret->type = INT;
ret->u.i = pa1->u.i + pa2->u.i;
}
break;
case MINUS:
if(is_float)
{
ret->type = FLOAT;
if(pa1->type == FLOAT)
ret->u.f += pa1->u.f;
else
ret->u.f += pa1->u.i;

if(pa2->type == FLOAT)
ret->u.f -= pa2->u.f;
else
ret->u.f -= pa2->u.i;
}
else
{
ret->type = INT;
ret->u.i = pa1->u.i - pa2->u.i;
}
break;
case MULTIP:
if(is_float)
{
ret->type = FLOAT;
if(pa1->type == FLOAT)
ret->u.f = pa1->u.f;
else
ret->u.f = pa1->u.i;

if(pa2->type == FLOAT)
ret->u.f *= pa2->u.f;
else
ret->u.f *= pa2->u.i;
}
else
{
ret->type = INT;
ret->u.i = pa1->u.i * pa2->u.i;
}
break;
case DIVIDE:
if(is_float)
{
ret->type = FLOAT;
if(pa1->type == FLOAT)
ret->u.f = pa1->u.f;
else
ret->u.f = pa1->u.i;

if(pa2->type == FLOAT)
ret->u.f /= pa2->u.f;
else
ret->u.f /= pa2->u.i;
}
else
{
ret->type = INT;
ret->u.i = pa1->u.i / pa2->u.i;
}
break;
}
return 0;
}

void caculator()
{
char in_ch;
struct member result;
struct member pa1, pa2;
struct member opr;
char in_str[50] = {0}, *pstr;
char str_pa1[20] = {0}, *ppa1;
char str_pa2[20] = {0}, *ppa2;

CLS_MEM;

while(1)
{
if(0 == get_input(&in_ch))
{
*pstr++ = in_ch;
if(IS_DIGIT(in_ch))
{
if(result.type != NONE)
{
CLS_MEM;
*pstr++ = in_ch;
printf("%s", in_str);
}

if(opr.type != NONE)
{
if(pa2.type != NONE)
{
*ppa2++ = in_ch;
if(pa2.type == INT)
{
if(str2int(str_pa2, &pa2.u.i))
{
BACK_GRACEFUL(pstr);
ppa2--;
*ppa2 = 0;
str2int(str_pa2, &pa2.u.i);
continue;
}
}
else if(pa2.type == FLOAT)
{
if(str2float(str_pa2, &pa2.u.f))
{
BACK_GRACEFUL(pstr);
ppa2--;
*ppa2 = 0;
str2float(str_pa2, &pa2.u.f);
continue;
}
}
}
else
{
*ppa2++ = in_ch;

if(str2int(str_pa2, &pa2.u.i))
{
BACK_GRACEFUL(pstr);
ppa2--;
*ppa2 = 0;
str2int(str_pa2, &pa2.u.i);
continue;
}
pa2.type = INT;
}
}
else if(pa1.type == NONE)
{
pa1.type = INT;
*ppa1++ = in_ch;
if(str2int(str_pa1, &pa1.u.i))
{
BACK_GRACEFUL(pstr);
ppa1--;
*ppa1 = 0;
str2int(str_pa1, &pa1.u.i);
continue;
}
}
else if(pa1.type == INT)
{
*ppa1++ = in_ch;
if(str2int(str_pa1, &pa1.u.i))
{
BACK_GRACEFUL(pstr);
ppa1--;
*ppa1 = 0;
str2int(str_pa1, &pa1.u.i);
continue;
}
}
else if(pa1.type == FLOAT)
{
*ppa1++ = in_ch;
if(str2float(str_pa1, &pa1.u.f))
{
BACK_GRACEFUL(pstr);
ppa1--;
*ppa1 = 0;
str2float(str_pa1, &pa1.u.f);
continue;
}
}
else
{
BACK_GRACEFUL(pstr);
ppa1--;
*ppa1 = 0;
continue;
}
}

if(IS_OPR(in_ch))
{
if(pa1.type == NONE && result.type != NONE)
CPY_RST;

if(pa1.type == NONE)
{
BACK_GRACEFUL(pstr);
continue;
}

if(opr.type == NONE)
{
opr.type = OPR;
switch(in_ch)
{
case '+':
opr.u.o = PLUS;
break;
case '-':
opr.u.o = MINUS;
break;
case '*':
opr.u.o = MULTIP;
break;
case '/':
opr.u.o = DIVIDE;
break;
}
continue;
}
else if(pa2.type == NONE)
{
BACK_GRACEFUL(pstr);
continue;
}
else
{
if(pa2.u.i == 0 && opr.u.o == DIVIDE)
{
BACK_GRACEFUL(pstr);
continue;
}

if(0 == cal(&pa1, &opr, &pa2, &result))
{
opr.type = OPR;
switch(in_ch)
{
case '+':
opr.u.o = PLUS;
break;
case '-':
opr.u.o = MINUS;
break;
case '*':
opr.u.o = MULTIP;
break;
case '/':
opr.u.o = DIVIDE;
break;
}
memset(in_str, 0, strlen(in_str));
memset(str_pa1, 0, strlen(str_pa1));
memset(str_pa2, 0, strlen(str_pa2));
ppa1 = str_pa1;
ppa2 = str_pa2;
pstr = in_str;
pa1.type = NONE;
pa2.type = NONE;

if(result.type == INT)
sprintf(in_str, "%d", result.u.i);
else
sprintf(in_str, "%f", result.u.f);

system("cls");
display_menu();
strcat(in_str, opr_str[opr.u.o]);
pstr = in_str + strlen(in_str);
printf("%s", in_str);

CPY_RST; /*copy the result to pa1*/
}
}
}

if(IS_POINT(in_ch))
{
if(pa1.type == NONE)
{
BACK_GRACEFUL(pstr);
continue;
}

if(opr.type != NONE && pa2.type == NONE)
{
BACK_GRACEFUL(pstr);
continue;
}

if(opr.type == NONE)
{
*ppa1++ = in_ch;
pa1.type = FLOAT;
}
else
{
*ppa2++ = in_ch;
pa2.type = FLOAT;
}
}

if(IS_CLS(in_ch))
{
CLS_MEM;
continue;
}

if(IS_RTN(in_ch))
{
return;
}

if(IS_EQUAL(in_ch))
{
if(pa1.type != NONE && opr.type != NONE
&& pa2.type != NONE)
{
if(pa2.u.i == 0 && opr.u.o == DIVIDE)
{
BACK_GRACEFUL(pstr);
continue;
}

if(0 == cal(&pa1, &opr, &pa2, &result))
{
memset(in_str, 0, strlen(in_str));
memset(str_pa1, 0, strlen(str_pa1));
memset(str_pa2, 0, strlen(str_pa2));
ppa1 = str_pa1;
ppa2 = str_pa2;
pstr = in_str;
pa1.type = NONE;
pa2.type = NONE;
opr.type = NONE;
system("cls");
display_menu();

if(result.type == INT)
sprintf(in_str, "%d", result.u.i);
else
sprintf(in_str, "%f", result.u.f);

pstr = in_str + strlen(in_str);
printf("%s", in_str);
}
}
else
{
BACK_GRACEFUL(pstr);
continue;
}
}
}
}
}

void main()
{
while(1)
{
caculator();
}
}本回答被提问者采纳
相似回答