Программа - калькулятор

Курсовой проект - Компьютеры, программирование

Другие курсовые по предмету Компьютеры, программирование

:

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

 

 

/*=================================Окно ввода=============================*/<