Advertisement
kxcoze

satie15_1*

Jun 25th, 2020
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3.  
  4. using namespace std;
  5.  
  6. int findoperindex(char* a);
  7.  
  8. int main() {
  9.     setlocale(LC_ALL, "Rus");
  10.  
  11.     FILE* input, *output;
  12.     char str[100];
  13.     double x, y;
  14.  
  15.     fopen_s(&input, "input.txt", "r");
  16.     fopen_s(&output, "output.txt", "w");
  17.  
  18.     while (!feof(input)) {
  19.         fgets(str, 40, input);
  20.         printf("%s", str);
  21.         char number1[3], number2[3];
  22.         int index = findoperindex(str);
  23.         int j = 0, g = 0;
  24.  
  25.         if (index) {
  26.             for (int i = 0; i < strlen(str); i++) {
  27.                 if (i < index && str[i] > 47 && str[i] < 58)
  28.                         number1[j++] = str[i];
  29.  
  30.                 if (i > index && str[i] > 47 && str[i] < 58)
  31.                         number2[g++] = str[i];
  32.             }
  33.         }
  34.  
  35.         x = atof(number1);
  36.         y = atof(number2);
  37.         switch (str[index]) {
  38.             case '+':
  39.                 fprintf(output, "%1.0f %s %1.0f %s %1.0f \n", x, "+", y, "=", x + y);
  40.                 break;
  41.             case '-':
  42.                 fprintf(output, "%1.0f %s %1.0f %s %1.0f \n", x, "-", y, "=", x - y);
  43.                 break;
  44.             case '*':
  45.                 fprintf(output, "%1.0f %s %1.0f %s %1.0f \n", x, "*", y, "=", x * y);
  46.                 break;
  47.             case '/':
  48.                 fprintf(output, "%1.0f %s %1.0f %s %1.2f \n", x, "/", y, "=", x / y);
  49.                 break;
  50.         }
  51.     }
  52.  
  53.     fclose(input);
  54.     fclose(output);
  55.     return 0;
  56. }
  57.  
  58. int findoperindex(char* a) {
  59.     for (int i = 0; i < strlen(a); i++) {
  60.         switch (a[i]) {
  61.         case '+':
  62.             return i;
  63.  
  64.         case '-':
  65.             return i;
  66.  
  67.         case '*':
  68.             return i;
  69.  
  70.         case '/':
  71.             return i;
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement