Advertisement
Guest User

TM1

a guest
Nov 14th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.55 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define MAX 101
  4. #define MIN 0
  5. #define STR_LEN 256
  6.  
  7. void main(void)
  8. {
  9.    int inferiore = MIN, superiore=  MAX, medio;
  10.    char risposta[STR_LEN];
  11.    
  12.    printf("Pensa ad un numero tra %d e %d\n", MIN, MAX-1);
  13.    
  14.    do
  15.    {
  16.       medio = (superiore + inferiore )/2;
  17.       printf("E' %d ? [y/n] ", medio);
  18.       scanf("%s", risposta);
  19.      
  20.       switch (risposta[0])
  21.       {
  22.          case 'n':
  23.          case 'N':
  24.             printf("Il tuo numero è più grande di %d? [y/n] ", medio);
  25.             scanf("%s", risposta);
  26.          
  27.             switch (risposta[0])  
  28.             {
  29.                case 'y':
  30.                case 's':
  31.                case 'Y':
  32.                case 'S':
  33.                   inferiore=medio;
  34.                   risposta[0] = 'n';
  35.                   break;
  36.                  
  37.                case 'n':
  38.                case 'N':
  39.                   superiore=medio;
  40.                   risposta[0] = 'n';
  41.                   break;
  42.                  
  43.                default:
  44.                   printf("Per favore rispondi con [y/n]\n");
  45.                   risposta[0]='n';
  46.                   break;
  47.             }
  48.             break;      
  49.              
  50.          case 'y':
  51.          case 'Y':
  52.          case 's':
  53.          case 'S':
  54.             break;
  55.            
  56.          default:
  57.             printf("Per favore rispondi con [y/n]\n");
  58.             risposta[0] = 'n';
  59.             break;
  60.        }  
  61.    }
  62.    
  63.    while (risposta[0] == 'n');
  64.    
  65.    printf("Ho indovinato! Il tuo numero è %d\n", medio);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement