Holey_yan

20140610_KUAS_VisualC++_Exersice01

Jun 13th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 KB | None | 0 0
  1. /************************************/
  2. /** 20140613 Exercise 01           **/
  3. /** Maker  : Yan                   **/
  4. /** E-Mail : [email protected] **/
  5. /** Date   : 2014/06/13 pm.06:24   **/
  6. /************************************/
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12.  
  13. void main()
  14. {
  15.     fflush(stdin);
  16.     system("cls");
  17.  
  18.     char input[3][10] = {EOF};
  19.     int counter_of_input = 0;
  20.     for(int i = 0; i < 3; i++)
  21.     {
  22.         counter_of_input++;
  23.         if(i != 0)
  24.         {
  25.             printf("\n");
  26.         }
  27.         printf("Enter a string %1d: ", counter_of_input);
  28.         fflush(stdin);
  29.         scanf("%[^\n]", input[i]);
  30.  
  31.         printf("Do you want to enter next string (Y/N): ", counter_of_input);
  32.         char select = EOF;
  33.         fflush(stdin);
  34.         scanf("%c", &select);
  35.         if((select == 'Y') || (select == 'y'))
  36.         {
  37.             continue;
  38.         }
  39.         else
  40.         {
  41.             break;
  42.         }
  43.     }
  44.  
  45.     printf("\nAnswer 1\n======================================\n");
  46.     for(int i = 0; i < counter_of_input; i++)
  47.     {
  48.         printf("String %1d is: %s.\n", i + 1, input[i]);
  49.     }
  50.  
  51.  
  52.     printf("\nAnswer 2\n=======================================\n");
  53.     int counter_of_number = 0;
  54.     char number_of_input[3][10] = {EOF};
  55.     int tmpN = 0;
  56.     for(int i = 0; i < counter_of_input; i++)
  57.     {
  58.         tmpN = 0;
  59.         for(int j = 0; j < strlen(input[i]); j++)
  60.         {
  61.             if(isdigit(input[i][j]))
  62.             {
  63.                 counter_of_number++;
  64.                 number_of_input[i][tmpN] = ((int)input[i][j]) - 48;
  65.                 tmpN++;
  66.             }
  67.         }
  68.     }
  69.     printf("Number's number of times in all string is: %d.\n", counter_of_number);
  70.     for(int i = 0; i < counter_of_input; i++)
  71.     {
  72.         printf("String %1d number is: ", i + 1);
  73.         for(int j = 0; j < strlen(number_of_input[i]); j++)
  74.         {
  75.             switch(j)
  76.             {
  77.                 case 0:
  78.                     printf("%d", number_of_input[i][j]);
  79.                     break;
  80.  
  81.                 default:
  82.                     printf(", %d", number_of_input[i][j]);
  83.                     break;
  84.             }
  85.         }
  86.         printf(".\n");
  87.     }
  88.  
  89.  
  90.     printf("\nAnswer 3\n=======================================\n");
  91.     char letter_of_input[2][30] = {EOF}; //0 is upper, 1 is lower.
  92.     int tmpUpper = 0, tmpLower = 0;
  93.     for(int i = 0; i < counter_of_input; i++)
  94.     {
  95.         tmpN = 0;
  96.         for(int j = 0; j < strlen(input[i]); j++)
  97.         {
  98.             if(!isdigit(input[i][j]))
  99.             {
  100.                 if(isupper(input[i][j]))
  101.                 {
  102.                     letter_of_input[0][tmpUpper] = input[i][j];
  103.                     tmpUpper++;
  104.                 }
  105.                 else if(islower(input[i][j]))
  106.                 {
  107.                     letter_of_input[1][tmpLower] = input[i][j];
  108.                     tmpLower++;
  109.                 }
  110.             }
  111.         }
  112.     }
  113.     for(int i = 0; i < 2; i++)
  114.     {
  115.         switch(i)
  116.         {
  117.             case 0:
  118.                 printf("In all string, capital letter have: ");
  119.                 break;
  120.             case 1:
  121.                 printf("In all string, small letter have: ");
  122.                 break;
  123.         }
  124.  
  125.         for(int j = 0; j < strlen(letter_of_input[i]); j++)
  126.         {
  127.             switch(j)
  128.             {
  129.                 case 0:
  130.                     printf("%c", letter_of_input[i][j]);
  131.                     break;
  132.                 default:
  133.                     printf(", %c", letter_of_input[i][j]);
  134.                     break;
  135.             }
  136.         }
  137.         printf(".\n");
  138.     }
  139.  
  140.  
  141.     printf("\nAnswer 4\n=======================================\n");
  142.     char ascii_of_input[30] = {EOF};
  143.     int tmpLetter = 0;
  144.     for(int i = 0; i < counter_of_input; i++)
  145.     {
  146.         for(int j = 0; j < strlen(input[i]); j++)
  147.         {
  148.             ascii_of_input[tmpLetter] = input[i][j];
  149.             tmpLetter++;
  150.  
  151.         }
  152.     }
  153.  
  154.     printf("In all input string, letter's ASCII descending order of: ");
  155.     int typesetting = 0;
  156.     for(int i = 255; i >= 0; i--)
  157.     {
  158.         for(int j = 0; j < strlen(ascii_of_input); j++)
  159.         {
  160.             if((int)ascii_of_input[j] == i)
  161.             {
  162.                 switch(typesetting)
  163.                 {
  164.                     case 0:
  165.                         printf("%c", ascii_of_input[j]);
  166.                         typesetting++;
  167.                         break;
  168.                     default:
  169.                         printf(", %c", ascii_of_input[j]);
  170.                         break;
  171.                 }
  172.             }
  173.         }
  174.     }
  175.     printf(".\n\n");
  176.  
  177.     printf("Do you want to restart this program(0/1): ");
  178.     while(1)
  179.     {
  180.         fflush(stdin);
  181.         char select_of_restart = 0;
  182.         scanf("%c", &select_of_restart);
  183.         if(isdigit(select_of_restart))
  184.         {
  185.             switch((int)select_of_restart - 48)
  186.             {
  187.                 case 0:
  188.                     exit(1);
  189.                     break;
  190.  
  191.                 case 1:
  192.                     main();
  193.                     break;
  194.  
  195.                 default:
  196.                     printf("Press 0 to exit, 1 to restart: ");
  197.                     break;
  198.             }
  199.         }
  200.         else
  201.         {
  202.             printf("Press 0 to exit, 1 to restart: ");
  203.         }
  204.     }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment