模擬計算器程序 設(shè)計一個程序來模擬一個簡單的手持計算器。程序支持算術(shù)運算+、-、*、/、=、以及C(清除) 6.模擬計算器程序 設(shè)計要求:設(shè)計一個程序來模擬一個簡單的手...
/*
一個計算器小程序
1. 支持加減乘除。可以擴展,只要修改cal()函數(shù)和增加運算符判斷。
2. 回車和等于號或者下一個運算符都可以作為求值號,
結(jié)果可以作為下一個運算對象,也可以不作為運算對象,程序自行判斷。
3. 支持容錯功能,整數(shù)和實數(shù)任意可以運算,程序自行判斷。
4. 一直在循環(huán)接受輸入,每一個輸入都認為是字符,由程序自
己判斷為運算數(shù)或者運算符號并且智能轉(zhuǎn)化,非法輸入會被拒絕,
不會顯示在終端上。
*/
#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);}
/*判斷是否是數(shù)字*/
#define IS_DIGIT(x) \
((x) <= '9' && (x) >= '0')
/*判斷是否是運算符*/
#define IS_OPR(x) \
(((x) == '+') \
|| ((x) == '-') \
|| ((x) == '*') \
|| ((x) == '/'))
/*判斷是否是小數(shù)點*/
#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();
}
}
友莫15214593471: 獲取文本框中的文本的方法是什么 - 上學(xué)吧普法考試
常寧市外齒: ______ #includemain() { double a,b,c; char ch,ch1; printf("這是一個簡單的計算程序,只支持±整數(shù)和小數(shù)的加減乘除運算,請輸入運算表達式,如:1+2,按回車鍵.\n"); scanf("%lf%c%lf%c",&a,&ch,&b,&ch1); switch(ch) { case '+': c=a+b; printf("...
友莫15214593471: c語言設(shè)計一個簡單的計算器程序
常寧市外齒: ______ /* 2013年12月23日 12:43:46 目的:計算器的實現(xiàn) */ # include <stdio.h> # include <ctype.h> # include <math.h> char get_choice(void); //獲取用戶輸入的選項,并建立目 char get_first(void); //獲取用戶輸入的選項,并剔除錯誤輸入 float get_int(...
友莫15214593471: 模擬計算器程序 -
常寧市外齒: ______ var c:char; sym:array[0..30]of char; num:array[0..30]of longint; ts,tn:shortint; x:longword; procedure calcu; var p:char; x,x2:longint; x1:real; begin p:=sym[ts];dec(ts); x2:=num[tn];dec(tn); x1:=num[tn];dec(tn); case p of '+':x:=trunc(x1)+x2; '-':x:=trunc(x1)-x2; ...
友莫15214593471: 設(shè)計一個簡單的計算器程序
常寧市外齒: ______ #include <iostream> using namespace std; int main() { double x,y,s1,s2,s3,s4; char z; cout<<"請輸入運算公式:"; cin>>x>>z>>y; s1=x+y; s2=x-y; s3=x*y; s4=x/y; if(z='+') { cout<<"運算結(jié)果為:"<<s1<<endl; } else if(z='-') { cout<<"運算結(jié)果為...
友莫15214593471: 用C++設(shè)計一簡單的計算器模擬程序 -
常寧市外齒: ______ 前幾天寫過這個,6-1. 簡單計算器(20) 時間限制400 ms 內(nèi)存限制32000 kB 代碼長度限制8000 B 判題程序 Standard 作者 張彤彧(浙江大學(xué)) 模擬簡單運算器的工作.假設(shè)計算器只能進行加減乘除運算,運算數(shù)和結(jié)果都是整數(shù),4種運算符...
友莫15214593471: C語言編程~關(guān)于設(shè)計模擬計算器程序
常寧市外齒: ______ 是用win32 SDK來做吧?
友莫15214593471: C++模擬計算器程序 -
常寧市外齒: ______ Javascript的行不?模擬計算器程序 問題描述:設(shè)計一個程序來模擬一個簡單的手持計算器.程序支持算術(shù)運算+、-、*、/、=、以及C(清除)、A(全清除)操作. 基本要求:程序運行時,顯示一個窗口,等待用戶輸入,用戶可以從鍵盤輸入要計算的表達式,輸入的表達式顯示在窗口中,用戶鍵入'=' 符號后,窗口顯示出結(jié)果. 測試數(shù)據(jù):程序輸入不少于5種不同的表達式進行測試. 實現(xiàn)提示:可定義一個計算器類,該類包括兩個組件對象,一個計算引擎和一個用戶接口,用戶接口對象處理接受的鍵盤輸入信息,并顯示答案,計算引擎對象對給出的數(shù)據(jù)執(zhí)行相應(yīng)操作,并存儲操作的結(jié)果. 選作內(nèi)容:如果用戶輸入的表達式不合法,可以判別出來并給出相應(yīng)的錯誤提示
友莫15214593471: C語言編程~關(guān)于設(shè)計模擬計算器程序 -
常寧市外齒: ______ 一個簡單的二元運算很簡單 但要寫出能處理任何長度表達式的計算器就很麻煩了
友莫15214593471: 計算器程序的C語言程序設(shè)計 -
常寧市外齒: ______ 簡單的說下思路 輸入一個數(shù) 輸入一個符號 while(符號不是=) { switch 符號 case +,-,*,/...的處理 輸入一個數(shù) 輸入一個符號 } 顯示結(jié)果
一個計算器小程序
1. 支持加減乘除。可以擴展,只要修改cal()函數(shù)和增加運算符判斷。
2. 回車和等于號或者下一個運算符都可以作為求值號,
結(jié)果可以作為下一個運算對象,也可以不作為運算對象,程序自行判斷。
3. 支持容錯功能,整數(shù)和實數(shù)任意可以運算,程序自行判斷。
4. 一直在循環(huán)接受輸入,每一個輸入都認為是字符,由程序自
己判斷為運算數(shù)或者運算符號并且智能轉(zhuǎn)化,非法輸入會被拒絕,
不會顯示在終端上。
*/
#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);}
/*判斷是否是數(shù)字*/
#define IS_DIGIT(x) \
((x) <= '9' && (x) >= '0')
/*判斷是否是運算符*/
#define IS_OPR(x) \
(((x) == '+') \
|| ((x) == '-') \
|| ((x) == '*') \
|| ((x) == '/'))
/*判斷是否是小數(shù)點*/
#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();
}
}
相關(guān)評說:
常寧市外齒: ______ #includemain() { double a,b,c; char ch,ch1; printf("這是一個簡單的計算程序,只支持±整數(shù)和小數(shù)的加減乘除運算,請輸入運算表達式,如:1+2,按回車鍵.\n"); scanf("%lf%c%lf%c",&a,&ch,&b,&ch1); switch(ch) { case '+': c=a+b; printf("...
常寧市外齒: ______ /* 2013年12月23日 12:43:46 目的:計算器的實現(xiàn) */ # include <stdio.h> # include <ctype.h> # include <math.h> char get_choice(void); //獲取用戶輸入的選項,并建立目 char get_first(void); //獲取用戶輸入的選項,并剔除錯誤輸入 float get_int(...
常寧市外齒: ______ var c:char; sym:array[0..30]of char; num:array[0..30]of longint; ts,tn:shortint; x:longword; procedure calcu; var p:char; x,x2:longint; x1:real; begin p:=sym[ts];dec(ts); x2:=num[tn];dec(tn); x1:=num[tn];dec(tn); case p of '+':x:=trunc(x1)+x2; '-':x:=trunc(x1)-x2; ...
常寧市外齒: ______ #include <iostream> using namespace std; int main() { double x,y,s1,s2,s3,s4; char z; cout<<"請輸入運算公式:"; cin>>x>>z>>y; s1=x+y; s2=x-y; s3=x*y; s4=x/y; if(z='+') { cout<<"運算結(jié)果為:"<<s1<<endl; } else if(z='-') { cout<<"運算結(jié)果為...
常寧市外齒: ______ 前幾天寫過這個,6-1. 簡單計算器(20) 時間限制400 ms 內(nèi)存限制32000 kB 代碼長度限制8000 B 判題程序 Standard 作者 張彤彧(浙江大學(xué)) 模擬簡單運算器的工作.假設(shè)計算器只能進行加減乘除運算,運算數(shù)和結(jié)果都是整數(shù),4種運算符...
常寧市外齒: ______ 是用win32 SDK來做吧?
常寧市外齒: ______ Javascript的行不?模擬計算器程序 問題描述:設(shè)計一個程序來模擬一個簡單的手持計算器.程序支持算術(shù)運算+、-、*、/、=、以及C(清除)、A(全清除)操作. 基本要求:程序運行時,顯示一個窗口,等待用戶輸入,用戶可以從鍵盤輸入要計算的表達式,輸入的表達式顯示在窗口中,用戶鍵入'=' 符號后,窗口顯示出結(jié)果. 測試數(shù)據(jù):程序輸入不少于5種不同的表達式進行測試. 實現(xiàn)提示:可定義一個計算器類,該類包括兩個組件對象,一個計算引擎和一個用戶接口,用戶接口對象處理接受的鍵盤輸入信息,并顯示答案,計算引擎對象對給出的數(shù)據(jù)執(zhí)行相應(yīng)操作,并存儲操作的結(jié)果. 選作內(nèi)容:如果用戶輸入的表達式不合法,可以判別出來并給出相應(yīng)的錯誤提示
常寧市外齒: ______ 一個簡單的二元運算很簡單 但要寫出能處理任何長度表達式的計算器就很麻煩了
常寧市外齒: ______ 簡單的說下思路 輸入一個數(shù) 輸入一個符號 while(符號不是=) { switch 符號 case +,-,*,/...的處理 輸入一個數(shù) 輸入一個符號 } 顯示結(jié)果