Advertisement
Guest User

calculator

a guest
Dec 22nd, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.19 KB | None | 0 0
  1. // лаба_2.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4.  
  5.  
  6. #include "stdafx.h"
  7. #include "conio.h"
  8. #include "stdlib.h"
  9. #include "math.h"
  10. #include "iostream"
  11.  
  12. // для того чтобы учесть переполнение типа int была подключена библиотека limits.h
  13. #include "limits.h"
  14.  
  15. int _tmain(int argc, _TCHAR* argv[])
  16. {
  17.     int ar1, ar2, a;
  18.     char sig=NULL, sht=NULL, sht1=NULL, zero;
  19.     // чтобы приложение не закрывалось сразу же после выполнения выражения, был введен бесконечный цикл while c условием выхода при нажатии клавиши q
  20.     while(true)
  21.     {
  22.         ar1=0; ar2=0; a=0; sig, zero=0;
  23.         // для удобства экран очищается каждый раз перед следующей итерацией
  24.         system("cls");
  25.         // с помощью условного оператора if задание было разделено на две части, доступ к которым осуществляется при нажатии "1" либо "2"
  26.         printf("choose part of exercise, press 1 or 2, for exit press q\n");
  27.         scanf("%c",&zero);
  28.         if(zero=='q') break;
  29.         system("cls");
  30.         // начало первой части задания
  31.         if(zero==49)
  32.         {
  33.             printf("Enter expression 1\n");
  34.             flushall();
  35.             scanf("%d%c", &ar1, &sht);
  36.             if(sht!=NULL)
  37.                 {
  38.                 printf("you have entered another sign, please enter number \n");
  39.                 flushall();
  40.                 sht=NULL;
  41.                 printf("ar1=%d",ar1);
  42.                 printf(" sht=%d",sht);
  43.                 scanf("%c", &zero);
  44.                 if(zero==10) continue;
  45.                 }
  46.             // проверка для исключения переполнения типов
  47.             if ((ar1>=SHRT_MAX)||(ar1<=SHRT_MIN))
  48.             {
  49.                 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");
  50.                 flushall();
  51.                 getchar();
  52.                 // system("cls"); очищает экран перед следующей итерацией цикла
  53.                 system("cls");
  54.                 continue;
  55.             }
  56.             printf("Enter sign\n");
  57.             flushall();
  58.             scanf("%s", &sig);
  59.             // проверка на правильность ввода знака
  60.             if ((sig!='+')&&(sig!='-')&&(sig!='*')&&(sig!='/'))
  61.             {
  62.                 printf("you entered an incorrect character please one of the following characters: +, - , *, /; press enter to continue \n");
  63.                 flushall();
  64.                
  65.                 scanf("%c", &zero);
  66.                 if(zero==10) continue;
  67.             }
  68.             printf("Enter expression 2\n");
  69.             flushall();
  70.             scanf("%d%c", &ar2, &sht1);
  71.             if(sht1!=NULL)
  72.                 {
  73.                 printf("you have entered another sign, please enter number \n");
  74.                 flushall();
  75.                 sht=NULL;
  76.                 sht1=NULL;
  77.                 scanf("%c", &zero);
  78.                 if(zero==10) continue;
  79.                 }
  80.             if ((ar2>=SHRT_MAX)||(ar2<=SHRT_MIN))
  81.             {
  82.                 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");
  83.                 flushall();
  84.                
  85.                 // system("cls"); очищает экран перед следующей итерацией цикла
  86.                 system("cls");
  87.                 continue;
  88.             }
  89.             else
  90.             {
  91.                 switch(sig)
  92.             {
  93.                 case '+':
  94.                     a=ar1+ar2;
  95.                     break;
  96.                 case '-':
  97.                     a=ar1-ar2;
  98.                     break;
  99.                 case '*':
  100.                     a=ar1*ar2;
  101.                     break;
  102.                 case '/':
  103.                 {
  104.                     // для операции деления была введена проверка с целью исключения деления на ноль
  105.                     if (ar2==0)
  106.                     {
  107.                         printf("deviding by zero is not allowed in this world, press Enter to continue");
  108.                         flushall();
  109.                    
  110.                         scanf("%c", &zero);
  111.                         if(zero==0) continue;
  112.                         continue;
  113.                     }
  114.                 }
  115.             }
  116.                 system("cls");
  117.                 // вывод ответа
  118.                 printf("\n");
  119.                 printf("%d%c%d", ar1,sig,ar2);
  120.                 printf("=%d\n\n", a);
  121.                 // условие выхода либо продолжения работы с приложением
  122.                 printf("To continue, press Enter , to exit press q \n");
  123.                 flushall();
  124.  
  125.                 scanf("%c", &sig);
  126.                 if(sig=='q') break;
  127.                 if(sig==10) continue;
  128.             }
  129.         }
  130.         // начало второй части задания
  131.         if(zero==50)
  132.         {
  133.             printf("Enter expression \n");
  134.             // оператор flushall() очищает буфер, из которого берет данные scanf
  135.             flushall();
  136.             scanf("%d %c %d", &ar1, &sig, &ar2);
  137.            
  138.             system("cls");
  139.             // условный оператор для учета переполнения типов
  140.             if ((ar1>=SHRT_MAX)||(ar1<=SHRT_MIN)||(ar2>=SHRT_MAX)||(ar2<=SHRT_MIN))
  141.             {
  142.                 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");
  143.                 flushall();
  144.                 getchar();
  145.                 system("cls");
  146.                 continue;
  147.             }
  148.             // проверка на правильность ввода знака
  149.             if ((sig!='+')&&(sig!='-')&&(sig!='*')&&(sig!='/'))
  150.             {
  151.                 printf("you entered an incorrect character please one of the following characters: +, - , *, /, or incorrect argument; press enter to continue \n");
  152.                 flushall();
  153.                
  154.                 scanf("%c", &zero);
  155.                 if(zero==10) continue;
  156.             }
  157.        
  158.             else
  159.             {
  160.                 // если знак введен правильно, то в игру вступает оператор switch case
  161.                 switch(sig)
  162.                 {
  163.                     case '+':
  164.                         a=ar1+ar2;
  165.                         break;
  166.                     case '-':
  167.                         a=ar1-ar2;
  168.                         break;
  169.                     case '*':
  170.                         a=ar1*ar2;
  171.                         break;
  172.                     case '/':
  173.                     {
  174.                         if (ar2==0)
  175.                         {
  176.                             printf("deviding by zero is not allowed in this world, press Enter to continue");
  177.                             flushall();
  178.  
  179.                             scanf("%c", &zero);
  180.                             if(zero==0) continue;
  181.                             continue;
  182.                         }
  183.  
  184.                         a=ar1/ar2;
  185.                     }
  186.                     break;
  187.                 }
  188.             }
  189.             // печатаем ответ
  190.             printf("\n");
  191.             printf("%d%c%d", ar1,sig,ar2);
  192.             printf("=%d\n\n", a);
  193.            
  194.             printf("To continue, press Enter , to exit press q \n");
  195.             flushall();
  196.             scanf("%c", &sig);
  197.             if(sig=='q') break;
  198.             if(sig==10) continue;
  199.         }
  200.  
  201.     }
  202. return 0;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement