clrscr();
ramka();
screen_input();
screen_output();
message();
info();
dva_x_dva();
_setcursortype(_NORMALCURSOR);
textcolor(15);
gotoxy(6,4);
func_name = (char*) Malloc(5);
while (1)
get_token();
if (curr_tok == END) break;
if (curr_tok != PRINT && curr_tok != NEXT)
pr=printf("%f\n",expr());
if(pr!=0)
gotoxy(6,4);
printf(" "
" ");
gotoxy(6,4);
free(func_name);
break;
case 3:
clrscr(); if((about=fopen(put="d:\univer\langs\bc\work\Credits.txt","r"))==NULL)
printf("Imposible open file %s!\n",put);
getch();
break;
return;
i=0;
while(!feof(about))
putchar(getc(about));
i++;
_setcursortype(_NOCURSOR);
getch();
break;
case 4:
sound_exit();
loop = 0;break;
default:
continue;
/*=========================Проверканехваткипамяти========================*/
void *Malloc(size_t size)
void *p;
if((p=malloc(size))==NULL)
printf(" No memory\n");
exit(1);
return p;
/*==================Функция синтаксического разбора выражения==============*/
token_value get_token()
char ch;
do
if ((ch = getch()) == ESC) return curr_tok = END;
while (ch == ' ');
switch(ch)
case ';':
putch(ch);
ps = '\x0';
return curr_tok = PRINT;
case '\r':
ps = '\x0';
gotoxy(1,wherey()+1);
return curr_tok = NEXT;
case '*': case '/': case '+': case '-':
case '(': case ')': case '=': case '^':
dupl_oper_verify(ps, ch);
return curr_tok = ch;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': case '.':
ps = '\x0';
ungetch(ch);
get_number();
return curr_tok = NUMBER;
default:
ps = '\x0';
if (isalpha(ch))
ungetch(ch);
get_name();
return curr_tok = NAME;
error("Invalid symbol");
return curr_tok = PRINT;
/*=====================Проверкадублированиязнакаоперации================*/
void dupl_oper_verify(char ps, char ch)
if (ps=='*' || ps=='/' || ps=='+' || ps=='-' || ps=='^')
error("Operation is duplicated");
ch = ps;
else
putch(ch);
if (ps=='*' || ps=='/' || ps=='+' || ps=='-' || ps=='^') ps = ch;
/*====================================Вводчисла===========================*/
void get_number()
char ch, dec_flag=0;
char *anum;
int i=0;
anum = (char*) Malloc(32);
do
ch=getch();
if (isdigit(ch))
putch(ch);
*(anum+i) = ch;
i++;
else
if (ch == '.')
if (dec_flag) error("Second decimal point is forbidden");
else
dec_flag = 1;
putch(ch);
*(anum+i) = ch;
i++;
else
if (ch == BACKSPACE) back_space(&i);
else
if (!term_sym(ch))
error("Invalid symbol");
ch = BACKSPACE;
while (i < 31 && (isdigit(ch) || ch == '.' || ch == BACKSPACE));
anum[i] = '\x0';
number_value = _atold(anum);
ungetch(ch);
free(anum);
/*=====================Проверка на терминальный символ=====================*/
int term_sym(char ch)
if (ch == '*' || ch == '/' || ch == '+' || ch == '-' ||
ch == '(' || ch == ')' || ch == '=' || ch == '^' ||
ch == ESC || ch == ';' || ch == '\r') return 1;
else return 0;
/*================Исправлениеошибкиклавишей BACKSPACE====================*/
void back_space(int *i)
if (i)
gotoxy(wherex()-1,wherey());
putch(' ');
gotoxy(wherex()-1,wherey());
i--;
/*========================Вводименифункции==============================*/
void get_name()
char ch;
int i=0, j;
do
ch = getch();
if (isalnum(ch))
putch(ch);
*(func_name+i) = ch;
i++;
for (j = 0; j < 42 && strncmp(funcs+j,func_name,i); j += 6);
if (j >= 42)
error("Invalid function");
i--;
else
if (ch == BACKSPACE) back_space(&i);
else
if (!term_sym(ch))
error("Invalid symbol");
ch = BACKSPACE;
while (i < 5 && (isalnum(ch) || ch == BACKSPACE));
*(func_name+i) = '\x0';
function_number = j/6;
ungetch(ch);
/*=====================Вычисление полного выражения========================*/
double expr()
double left = term();
while(1)
switch(curr_tok)
case PLUS:
get_token();
left += term();
break;
case MINUS:
get_token();
left -= term();
break;
default:
if (curr_tok != RP)
// gotoxy(31,8);
gotoxy(23,8);
printf(" ");
gotoxy(23,8);
return left;
/*===========================Вычислениеслагаемого=========================*/
double term()
double left = prim();
while(1)
switch(curr_tok)
case MUL:
get_token();
left *= term();
break;
case DIV:
get_token();
float d = prim();
if (!d) return error("Division by zero");
left /= d;
break;
case POWER:
get_token();
left = pow(left,term());
default:
return left;
/*==================Вычисление первичной части выражения===================*/
double prim()
switch(curr_tok)
case NUMBER:
get_token();
return number_value;
case NAME:
get_token();
return function_value();
case MINUS:
get_token();
return -prim();
case LP:
get_token();
double e = expr();
if (curr_tok != RP) return error("Rigth parentsis expected");
get_token();
return e;
case END:
return 1;
default:
return number_value;
/*=====================Вычисление значения функции========================*/
double function_value()
switch(function_number)
case 0:
return sin(expr());
case 1:
return cos(expr());
case 2:
return log(expr());
case 3:
return asin(expr());
case 4:
return acos(expr());
case 5:
return E;
case 6:
return PI;
/*========================Вывод сообщения об ошибке========================*/
double error(char *s)
int sx, sy;
char *empty_str, *err_message;
err_message = (char*) Malloc(strlen(s)+29);
strcpy(err_message,s);
strcat(err_message,". Press any key to continue!");
empty_str = (char*) Malloc(strlen(s)+29);
memset(empty_str,' ',strlen(s)+28);
empty_str[strlen(s)+28] = '\x0';
sx = wherex();
sy = wherey();
gotoxy(16,22);
textcolor(10);
cprintf("%s",err_message);
sound(440);
delay(550);
nosound();
getch();
gotoxy(16,22);
cprintf("%s",empty_str);
gotoxy(sx,sy);
textcolor(15);
free(err_message);
free(empty_str);
/*==============================Окносообщения============================*/
void message()
textcolor(RED);
gotoxy(15,21);
cprintf("-");
for(int x=16;x<70;x++)
cprintf("-");
cprintf("-");
gotoxy(15,22);
cprintf("|");
gotoxy(70,22);
cprintf("|");
gotoxy(15,23);
cprintf("-");
for(int x1=16;x1<70;x1++)
cprintf("-");
cprintf("-");
gotoxy(3,22);
cprintf("Message:");
/*==================================Рамка=================================*/
void ramka()
textcolor(10);
cprintf("=");
for(int x=2;x<80;x++)
cprintf("=");
cprintf("=");
for(int y=2;y<24;y++)
cprintf("|\n\b");
cprintf("=");
for(int x1=2;x1<80;x1++)
cprintf("=");
cprintf("=");
gotoxy(80,2);
for(int y1=2;y1<24;y1++)
gotoxy(80,y1);
cprintf("|");
/*=================================Окноввода=============================*/
void screen_input()
textcolor(10);
gotoxy(5,3);
cprintf("-");
for(int x=6;x<76;x++)
cprintf("-");
cprintf("-");
gotoxy(5,4);
cprintf("|");
gotoxy(76,4);
cprintf("|");
gotoxy(5,5);
cprintf("-");
for(int x1=6;x1<76;x1++)
cprintf("-");
cprintf("-");
textcolor(15);
gotoxy(35,2);
cprintf("Input expression");
/*=================================Окновывода=============================*/
void screen_output()
textcolor(10);
gotoxy(22,7);
cprintf("-");
for(int x=22;x<58;x++)
cprintf("-");
cprintf("-");
gotoxy(22,8);
cprintf("|");
gotoxy(59,8);
cprintf("|");
gotoxy(22,9);
cprintf("-");
for(int x1=22;x1<58;x1++)
cprintf("-");
cprintf("-");
textcolor(15);
gotoxy(39,6);
cprintf("Answer");
/*================================Информация===============================*/
void info()
textcolor(10);
gotoxy(13,11);
cprintf("Input expression, used +-*/()^ sin,cos,asin,acos,ln,pi,e");
gotoxy(22,12);
cprintf("Enter ; = output answer Esc exit");
/*====================================Меню================================*/
void menu(int n)
clrscr();
textcolor(3); gotoxy(31,9);
cprintf("Program calculator\n");
textcolor(15);
if (n == 1) textcolor(12); else
textcolor(15);
gotoxy(37,12);
cprintf("Help\n");
if (n == 2) textcolor(12); else
textcolor(15);
gotoxy(34,14);
cprintf("Calculator\n");
if (n == 3) textcolor(12); else
textcolor(15);
gotoxy(33,16);
cprintf("About author\n");
if (n == 4) textcolor(12); else
textcolor(15);
gotoxy(37,18);
cprintf("Exit\n");
_setcursortype(_NOCURSOR);
/*===========================Музыка при выходе===========================*/
void sound_exit()
sound(659.3);
delay(310);
sound(784);
delay(310);
sound(721.65);
delay(310);
sound(659.3);
delay(310);
sound(495.9);
delay(310);
sound(587.3);
delay(310);
sound(513.65);
delay(310);
sound(440);
delay(310);
nosound();
/*=================================Два на два=============================*/
void dva_x_dva()
textcolor(1);
gotoxy(32,15);
cprintf("---- ----");
gotoxy(34,16);
cprintf("-- --- -- ----");
gotoxy(32,17);
cprintf("-- - - -- ----");
gotoxy(32,18);
cprintf("---- ----");