Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // лаба_2.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include "conio.h"
- #include "stdlib.h"
- #include "math.h"
- #include "iostream"
- // для того чтобы учесть переполнение типа int была подключена библиотека limits.h
- #include "limits.h"
- int _tmain(int argc, _TCHAR* argv[])
- {
- int ar1, ar2, a;
- char sig=NULL, sht=NULL, sht1=NULL, zero;
- // чтобы приложение не закрывалось сразу же после выполнения выражения, был введен бесконечный цикл while c условием выхода при нажатии клавиши q
- while(true)
- {
- ar1=0; ar2=0; a=0; sig, zero=0;
- // для удобства экран очищается каждый раз перед следующей итерацией
- system("cls");
- // с помощью условного оператора if задание было разделено на две части, доступ к которым осуществляется при нажатии "1" либо "2"
- printf("choose part of exercise, press 1 or 2, for exit press q\n");
- scanf("%c",&zero);
- if(zero=='q') break;
- system("cls");
- // начало первой части задания
- if(zero==49)
- {
- printf("Enter expression 1\n");
- flushall();
- scanf("%d%c", &ar1, &sht);
- if(sht!=NULL)
- {
- printf("you have entered another sign, please enter number \n");
- flushall();
- sht=NULL;
- printf("ar1=%d",ar1);
- printf(" sht=%d",sht);
- scanf("%c", &zero);
- if(zero==10) continue;
- }
- // проверка для исключения переполнения типов
- if ((ar1>=SHRT_MAX)||(ar1<=SHRT_MIN))
- {
- printf("You have gone beyond a value of type int, please enter the arguments are not outside the range [−2,147,483,648 ; +2,147,483,647] press enter to continue \n");
- flushall();
- getchar();
- // system("cls"); очищает экран перед следующей итерацией цикла
- system("cls");
- continue;
- }
- printf("Enter sign\n");
- flushall();
- scanf("%s", &sig);
- // проверка на правильность ввода знака
- if ((sig!='+')&&(sig!='-')&&(sig!='*')&&(sig!='/'))
- {
- printf("you entered an incorrect character please one of the following characters: +, - , *, /; press enter to continue \n");
- flushall();
- scanf("%c", &zero);
- if(zero==10) continue;
- }
- printf("Enter expression 2\n");
- flushall();
- scanf("%d%c", &ar2, &sht1);
- if(sht1!=NULL)
- {
- printf("you have entered another sign, please enter number \n");
- flushall();
- sht=NULL;
- sht1=NULL;
- scanf("%c", &zero);
- if(zero==10) continue;
- }
- if ((ar2>=SHRT_MAX)||(ar2<=SHRT_MIN))
- {
- printf("You have gone beyond a value of type int, please enter the arguments are not outside the range [−2,147,483,648 ; +2,147,483,647] press enter to continue \n");
- flushall();
- // system("cls"); очищает экран перед следующей итерацией цикла
- system("cls");
- continue;
- }
- else
- {
- switch(sig)
- {
- case '+':
- a=ar1+ar2;
- break;
- case '-':
- a=ar1-ar2;
- break;
- case '*':
- a=ar1*ar2;
- break;
- case '/':
- {
- // для операции деления была введена проверка с целью исключения деления на ноль
- if (ar2==0)
- {
- printf("deviding by zero is not allowed in this world, press Enter to continue");
- flushall();
- scanf("%c", &zero);
- if(zero==0) continue;
- continue;
- }
- }
- }
- system("cls");
- // вывод ответа
- printf("\n");
- printf("%d%c%d", ar1,sig,ar2);
- printf("=%d\n\n", a);
- // условие выхода либо продолжения работы с приложением
- printf("To continue, press Enter , to exit press q \n");
- flushall();
- scanf("%c", &sig);
- if(sig=='q') break;
- if(sig==10) continue;
- }
- }
- // начало второй части задания
- if(zero==50)
- {
- printf("Enter expression \n");
- // оператор flushall() очищает буфер, из которого берет данные scanf
- flushall();
- scanf("%d %c %d", &ar1, &sig, &ar2);
- system("cls");
- // условный оператор для учета переполнения типов
- if ((ar1>=SHRT_MAX)||(ar1<=SHRT_MIN)||(ar2>=SHRT_MAX)||(ar2<=SHRT_MIN))
- {
- printf("You have gone beyond a value of type int, please enter the arguments are not outside the range [−2,147,483,648 ; +2,147,483,647] press enter to continue \n");
- flushall();
- getchar();
- system("cls");
- continue;
- }
- // проверка на правильность ввода знака
- if ((sig!='+')&&(sig!='-')&&(sig!='*')&&(sig!='/'))
- {
- printf("you entered an incorrect character please one of the following characters: +, - , *, /, or incorrect argument; press enter to continue \n");
- flushall();
- scanf("%c", &zero);
- if(zero==10) continue;
- }
- else
- {
- // если знак введен правильно, то в игру вступает оператор switch case
- switch(sig)
- {
- case '+':
- a=ar1+ar2;
- break;
- case '-':
- a=ar1-ar2;
- break;
- case '*':
- a=ar1*ar2;
- break;
- case '/':
- {
- if (ar2==0)
- {
- printf("deviding by zero is not allowed in this world, press Enter to continue");
- flushall();
- scanf("%c", &zero);
- if(zero==0) continue;
- continue;
- }
- a=ar1/ar2;
- }
- break;
- }
- }
- // печатаем ответ
- printf("\n");
- printf("%d%c%d", ar1,sig,ar2);
- printf("=%d\n\n", a);
- printf("To continue, press Enter , to exit press q \n");
- flushall();
- scanf("%c", &sig);
- if(sig=='q') break;
- if(sig==10) continue;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement