kxcoze

satie__ars

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