48225 (Программа - калькулятор), страница 2
Описание файла
Документ из архива "Программа - калькулятор", который расположен в категории "". Всё это находится в предмете "информатика" из 1 семестр, которые можно найти в файловом архиве . Не смотря на прямую связь этого архива с , его также можно найти и в других разделах. Архив можно найти в разделе "курсовые/домашние работы", в предмете "информатика, программирование" в общих файлах.
Онлайн просмотр документа "48225"
Текст 2 страницы из документа "48225"
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("---- ----");