Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <limits.h>
  4.  
  5. int len(int x) {
  6.     int d = 0;
  7.     if (x == 0) return 1;
  8.     while (x > 0) {
  9.         x /= 10;
  10.         d++;
  11.     }
  12.     return d;
  13. }
  14.  
  15. void spaces(int x) {
  16.     int i = 0;
  17.     for (i = 0; i < x; i++)
  18.         printf(" ");
  19. }
  20.  
  21. void tire(int x) {
  22.     int i = 0;
  23.     for (i = 0; i < x; i++)
  24.         printf("-");
  25.     printf("\n");
  26. }
  27.  
  28. int max(int x, int y) {
  29.     if (x > y) return x;
  30.     else return y;
  31. }
  32.  
  33. int version2() {
  34.     int x, y;
  35.     printf("Vvedite dva natural'nih chisla\n");
  36.     scanf("%d %d", &x, &y);
  37.     if (x <= 0 || y <= 0) {
  38.         printf("\nNe vnaturi");
  39.         return 0;
  40.     }
  41.     if (x > INT_MAX / y || y > INT_MAX / x) {
  42.         printf("\nPerestaralsya malex");
  43.         return 0;
  44.     }
  45.     int dx, dy, ans = x * y;
  46.     dx = len(x);
  47.     spaces(20 - dx);
  48.     printf("%d\n", x);
  49.     dy = len(y);
  50.     spaces(20 - max(dx, dy) - 1);
  51.     printf("x\n");
  52.     spaces(20 - dy);
  53.     printf("%d\n", y);
  54.     int da = len(ans);
  55.     spaces(20 - da - dy + 1);
  56.     tire(da + dy - 1);
  57.     int i = 0, pr = -1, st = 1;
  58.     while (1) {
  59.         int v = y % 10, d = len(v * x);
  60.         spaces(20 - d - i);
  61.         printf("%d", v * x);
  62.         spaces(i);
  63.         printf("\n");
  64.  
  65.         if (pr == -1) {
  66.             pr = v * x;
  67.             spaces(20 - d - i - 2);
  68.             printf("+\n");
  69.         } else {
  70.             spaces(20 - da - dy + 1);
  71.             tire(da + dy - 1);
  72.             st *= 10;
  73.             pr += v * x * st;
  74.             int dp = len(pr);
  75.             spaces(20 - dp);
  76.             printf("%d\n", pr);
  77.             if (y / 10 == 0) break;
  78.             spaces(20 - dp - 2);
  79.             printf("+\n");
  80.  
  81.  
  82.         }
  83.         y /= 10;
  84.         i++;
  85.     }
  86.  
  87.  
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement