Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.69 KB | None | 0 0
  1. #include <stdio.h>
  2. void draw()
  3. {
  4.     int x;
  5.     int length;
  6.     int middleOfSquare;
  7.  
  8.     printf("Enter a number: ");
  9.     scanf("%d", &x);
  10.  
  11.     length = 2 * x + 1;
  12.     middleOfSquare = x + 1;
  13.  
  14.  
  15.     /*
  16.     this loop moves between
  17.     the "rows" of the square
  18.     */
  19.  
  20.  
  21.     for (int i = 1; i <= length; i++)
  22.     {
  23.  
  24.         /*
  25.         this loop moves between
  26.         the "columns" of the square
  27.         */
  28.  
  29.         for (int j = 1; j <= length; j++)
  30.         {
  31.  
  32.             //checks if this is the place to put X(the middle)
  33.             if (i == middleOfSquare && j == middleOfSquare)
  34.                 printf("X");
  35.  
  36.  
  37.             //checks if this is one of the corners
  38.             else if ((i == 1 && j == 1)
  39.                 || (i == 1 && j == length)
  40.                 || (i == length && j == 1)
  41.                 || (i == length && j == length))
  42.                 printf("+");
  43.  
  44.  
  45.             //checks if this is the main diagonal
  46.             else if (i == j)
  47.                 printf("\\");
  48.  
  49.  
  50.             //checks if this is the second diagonal
  51.             else if (i + j - 1 == length)
  52.                 printf("/");
  53.  
  54.  
  55.             //checks if this is the top or bottom side
  56.             else if (i == 1 || i == length)
  57.                 printf("-");
  58.  
  59.  
  60.             //checks if this is the left or right side
  61.             else if (j == 1 || j == length)
  62.                 printf("|");
  63.  
  64.  
  65.             //if its not all of the above, we need to print space
  66.             else printf(" ");
  67.  
  68.  
  69.         }
  70.  
  71.         //enter
  72.         printf("\n");
  73.  
  74.  
  75.     }
  76. }
  77.  
  78. void isEvenLength()
  79. {
  80.     //even=1,odd=0
  81.  
  82.     int isEven = 1;
  83.     char c;
  84.  
  85.     printf("Enter text: ");
  86.     scanf(" %c", &c);
  87.  
  88.  
  89.     while (c != '\n')
  90.     {
  91.         if (isEven)
  92.             isEven = 0;
  93.         else isEven = 1;
  94.  
  95.         scanf("%c", &c);
  96.     }
  97.  
  98.  
  99.     if (isEven)
  100.         printf("Your text's length is even ");
  101.  
  102.  
  103.     else printf("Your text's length is odd ");
  104.     printf("\n");
  105.  
  106. }
  107.  
  108. void identifyText()
  109. {
  110.  
  111.     //sets previousC to something(doesnt matter)
  112.  
  113.     char c,firstC,previousC=' ';
  114.     int isLegit = 1, isConst = 1,isIncreasing=0,isDecreasing=0;
  115.  
  116.     printf("Enter text: ");
  117.     scanf(" %c", &firstC);
  118.     c = firstC;
  119.  
  120.     while ((c!='\n'))
  121.     {
  122.         //checks if legit
  123.         if (c < 'a' || c > 'z')
  124.             isLegit = 0;
  125.  
  126.  
  127.         //check if current letter is like the first one, if not the word isnt const
  128.         if (c != firstC)
  129.             isConst = 0;
  130.  
  131.  
  132.         //checks if this letter is bigger than the last, if yes then there is an increasment in the word
  133.         if ((previousC != ' ')&&(c - previousC > 0))
  134.             isIncreasing = 1;
  135.  
  136.  
  137.         //checks if this letter is smaller than the last, if yes then there is a decreasment in the word
  138.         if ((previousC != ' ')&&(c - previousC < 0))
  139.             isDecreasing = 1;
  140.  
  141.  
  142.         previousC = c;
  143.         scanf("%c", &c);
  144.  
  145.     }
  146.  
  147.     if (!isLegit)
  148.         printf("your text is invalid ");
  149.  
  150.  
  151.     else if (isConst)
  152.         printf("your text is constant ");
  153.  
  154.  
  155.     //if there are increasment is decreasment, then the word is mixed
  156.     else if (isIncreasing && isDecreasing)
  157.         printf("your text is mixed ");
  158.  
  159.  
  160.     else if (isIncreasing)
  161.         printf("your text is increasing ");
  162.  
  163.  
  164.     else printf("your text is decreasing ");
  165.     printf("\n");
  166.  
  167.  
  168. }
  169.  
  170. void hexToDec()
  171. {
  172.     char c;
  173.     int tempNum;
  174.  
  175.     //num will be our number in dec form
  176.     int num = 0, isLegit = 1,placeInNum=0;
  177.     printf("Enter a reversed number in base 16: ");
  178.     scanf(" %c", &c);
  179.    
  180.     while ((c != '\n'))
  181.     {
  182.         tempNum = 0;
  183.  
  184.         //this ifs give the letter an int value
  185.         if (c >= 'A' && c <= 'F')
  186.             tempNum = c - 'A' + 10;
  187.  
  188.         else if (c >= 'a' && c <= 'f')
  189.             tempNum = c - 'a' + 10;
  190.  
  191.         else if (c >= '0' && c <= '9')
  192.             tempNum = c - '0';
  193.  
  194.         //if we couldnt find a good value, then the letter is invalid
  195.         else
  196.         {
  197.             isLegit = 0;
  198.             printf("Error! %c is not a valid digit in base 16\n", c);
  199.         }
  200.  
  201.  
  202.         //multyplies the number by 16^(index)
  203.         for (int i = 0; i < placeInNum; i++)
  204.         {
  205.             tempNum = tempNum * 16;
  206.         }
  207.  
  208.  
  209.         num += tempNum;
  210.         placeInNum++;
  211.         scanf("%c", &c);
  212.  
  213.     }
  214.  
  215.  
  216.     if (isLegit)
  217.         printf("%d \n", num);
  218.  
  219. }
  220.  
  221. void baseToDec()
  222. {
  223.  
  224.     char c;
  225.     int tempNum, base;
  226.  
  227.  
  228.     //num will be our number in dec form
  229.     int num = 0, isLegit = 1, placeInNum = 0;
  230.     printf("Enter a base (2-10): ");
  231.     scanf("%d", &base);
  232.     printf("Enter a reversed number in base %d: ",base);
  233.     scanf(" %c", &c);
  234.  
  235.  
  236.     while ((c != '\n'))
  237.     {
  238.         tempNum = 0;
  239.  
  240.         //this if gives the letter an int value
  241.         if (c >= '0' && c <= '0'+base-1)
  242.             tempNum = c - '0';
  243.  
  244.  
  245.         //if we couldnt find a good value, then the letter is invalid
  246.         else
  247.         {
  248.             isLegit = 0;
  249.             printf("Error! %c is not a valid digit in base %d\n", c,base);
  250.         }
  251.  
  252.  
  253.         //multyplies the number by (base)^(index)
  254.         for (int i = 0; i < placeInNum; i++)
  255.         {
  256.             tempNum = tempNum * base;
  257.         }
  258.  
  259.  
  260.         num += tempNum;
  261.         placeInNum++;
  262.         scanf("%c", &c);
  263.  
  264.  
  265.     }
  266.  
  267.     if (isLegit)
  268.         printf("%d \n", num);
  269.  
  270. }
  271.  
  272. void bitCount()
  273. {
  274.     int number, openBitCounter = 0, numOfBits = 32;
  275.  
  276.     //we want to save number, so we will opearte on tempNum
  277.     int tempNum;
  278.     int isNeg = 0;
  279.     printf("Enter a number: ");
  280.     scanf("%d", &number);
  281.     tempNum = number;
  282.  
  283.  
  284.     //if the num if neg, we will not the bits, and at the end we will substruct openBitCounter from the numOfBits
  285.     if (tempNum < 0)
  286.     {
  287.         tempNum = ~tempNum;
  288.         isNeg = 1;
  289.     }
  290.  
  291.  
  292.     while (tempNum != 0)
  293.     {
  294.         if (tempNum % 2 == 1)
  295.             openBitCounter += 1;
  296.         tempNum /= 2;
  297.     }
  298.  
  299.  
  300.     if (isNeg)
  301.         openBitCounter = numOfBits - openBitCounter;
  302.  
  303.  
  304.     printf("The bit count of %d is %d ", number, openBitCounter);
  305.     printf("\n");
  306. }
  307.  
  308. void main()
  309. {
  310.     int choice;
  311.     do
  312.     {
  313.  
  314.         printf("Choose an option:\n1: Draw\n2: Even or Odd\n3: Text type\
  315.                \n4: Hex to Dec\n5: Base to Dec\n6: Count bits\n0: Exit\n");
  316.  
  317.         scanf("%d", &choice);
  318.         switch (choice)
  319.         {
  320.  
  321.         case 1: draw();
  322.             break;
  323.  
  324.  
  325.         case 2: isEvenLength();
  326.             break;
  327.  
  328.  
  329.         case 3: identifyText();
  330.             break;
  331.  
  332.  
  333.         case 4: hexToDec();
  334.             break;
  335.  
  336.  
  337.         case 5: baseToDec();
  338.             break;
  339.  
  340.  
  341.         case 6: bitCount();
  342.         case 0: break;
  343.  
  344.  
  345.  
  346.         default: printf("Wrong option!\n");
  347.  
  348.         }
  349.  
  350.     }
  351.  
  352.  while (choice != 0);
  353.  
  354. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement