用c语言编程简单的加减乘除以及混合运算的计算器

2024年11月29日 23:37
有1个网友回答
网友(1):

#include
#include
#include
#include
#include
#include

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\nin: ");
}

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;
}

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);

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

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);
}
}
}
}
}
}

void main()
{
while(1)
{
caculator();
}
}