Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- void draw()
- {
- int x;
- int length;
- int middleOfSquare;
- printf("Enter a number: ");
- scanf("%d", &x);
- length = 2 * x + 1;
- middleOfSquare = x + 1;
- /*
- this loop moves between
- the "rows" of the square
- */
- for (int i = 1; i <= length; i++)
- {
- /*
- this loop moves between
- the "columns" of the square
- */
- for (int j = 1; j <= length; j++)
- {
- //checks if this is the place to put X(the middle)
- if (i == middleOfSquare && j == middleOfSquare)
- printf("X");
- //checks if this is one of the corners
- else if ((i == 1 && j == 1)
- || (i == 1 && j == length)
- || (i == length && j == 1)
- || (i == length && j == length))
- printf("+");
- //checks if this is the main diagonal
- else if (i == j)
- printf("\\");
- //checks if this is the second diagonal
- else if (i + j - 1 == length)
- printf("/");
- //checks if this is the top or bottom side
- else if (i == 1 || i == length)
- printf("-");
- //checks if this is the left or right side
- else if (j == 1 || j == length)
- printf("|");
- //if its not all of the above, we need to print space
- else printf(" ");
- }
- //enter
- printf("\n");
- }
- }
- void isEvenLength()
- {
- //even=1,odd=0
- int isEven = 1;
- char c;
- printf("Enter text: ");
- scanf(" %c", &c);
- while (c != '\n')
- {
- if (isEven)
- isEven = 0;
- else isEven = 1;
- scanf("%c", &c);
- }
- if (isEven)
- printf("Your text's length is even ");
- else printf("Your text's length is odd ");
- printf("\n");
- }
- void identifyText()
- {
- //sets previousC to something(doesnt matter)
- char c,firstC,previousC=' ';
- int isLegit = 1, isConst = 1,isIncreasing=0,isDecreasing=0;
- printf("Enter text: ");
- scanf(" %c", &firstC);
- c = firstC;
- while ((c!='\n'))
- {
- //checks if legit
- if (c < 'a' || c > 'z')
- isLegit = 0;
- //check if current letter is like the first one, if not the word isnt const
- if (c != firstC)
- isConst = 0;
- //checks if this letter is bigger than the last, if yes then there is an increasment in the word
- if ((previousC != ' ')&&(c - previousC > 0))
- isIncreasing = 1;
- //checks if this letter is smaller than the last, if yes then there is a decreasment in the word
- if ((previousC != ' ')&&(c - previousC < 0))
- isDecreasing = 1;
- previousC = c;
- scanf("%c", &c);
- }
- if (!isLegit)
- printf("your text is invalid ");
- else if (isConst)
- printf("your text is constant ");
- //if there are increasment is decreasment, then the word is mixed
- else if (isIncreasing && isDecreasing)
- printf("your text is mixed ");
- else if (isIncreasing)
- printf("your text is increasing ");
- else printf("your text is decreasing ");
- printf("\n");
- }
- void hexToDec()
- {
- char c;
- int tempNum;
- //num will be our number in dec form
- int num = 0, isLegit = 1,placeInNum=0;
- printf("Enter a reversed number in base 16: ");
- scanf(" %c", &c);
- while ((c != '\n'))
- {
- tempNum = 0;
- //this ifs give the letter an int value
- if (c >= 'A' && c <= 'F')
- tempNum = c - 'A' + 10;
- else if (c >= 'a' && c <= 'f')
- tempNum = c - 'a' + 10;
- else if (c >= '0' && c <= '9')
- tempNum = c - '0';
- //if we couldnt find a good value, then the letter is invalid
- else
- {
- isLegit = 0;
- printf("Error! %c is not a valid digit in base 16\n", c);
- }
- //multyplies the number by 16^(index)
- for (int i = 0; i < placeInNum; i++)
- {
- tempNum = tempNum * 16;
- }
- num += tempNum;
- placeInNum++;
- scanf("%c", &c);
- }
- if (isLegit)
- printf("%d \n", num);
- }
- void baseToDec()
- {
- char c;
- int tempNum, base;
- //num will be our number in dec form
- int num = 0, isLegit = 1, placeInNum = 0;
- printf("Enter a base (2-10): ");
- scanf("%d", &base);
- printf("Enter a reversed number in base %d: ",base);
- scanf(" %c", &c);
- while ((c != '\n'))
- {
- tempNum = 0;
- //this if gives the letter an int value
- if (c >= '0' && c <= '0'+base-1)
- tempNum = c - '0';
- //if we couldnt find a good value, then the letter is invalid
- else
- {
- isLegit = 0;
- printf("Error! %c is not a valid digit in base %d\n", c,base);
- }
- //multyplies the number by (base)^(index)
- for (int i = 0; i < placeInNum; i++)
- {
- tempNum = tempNum * base;
- }
- num += tempNum;
- placeInNum++;
- scanf("%c", &c);
- }
- if (isLegit)
- printf("%d \n", num);
- }
- void bitCount()
- {
- int number, openBitCounter = 0, numOfBits = 32;
- //we want to save number, so we will opearte on tempNum
- int tempNum;
- int isNeg = 0;
- printf("Enter a number: ");
- scanf("%d", &number);
- tempNum = number;
- //if the num if neg, we will not the bits, and at the end we will substruct openBitCounter from the numOfBits
- if (tempNum < 0)
- {
- tempNum = ~tempNum;
- isNeg = 1;
- }
- while (tempNum != 0)
- {
- if (tempNum % 2 == 1)
- openBitCounter += 1;
- tempNum /= 2;
- }
- if (isNeg)
- openBitCounter = numOfBits - openBitCounter;
- printf("The bit count of %d is %d ", number, openBitCounter);
- printf("\n");
- }
- void main()
- {
- int choice;
- do
- {
- printf("Choose an option:\n1: Draw\n2: Even or Odd\n3: Text type\
- \n4: Hex to Dec\n5: Base to Dec\n6: Count bits\n0: Exit\n");
- scanf("%d", &choice);
- switch (choice)
- {
- case 1: draw();
- break;
- case 2: isEvenLength();
- break;
- case 3: identifyText();
- break;
- case 4: hexToDec();
- break;
- case 5: baseToDec();
- break;
- case 6: bitCount();
- case 0: break;
- default: printf("Wrong option!\n");
- }
- }
- while (choice != 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement