C Primer Plus (Fifth Edition) 15th chapter position Operation programming Exercise

Source: Internet
Author: User

1. Write a function that converts a binary string into a numeric value. In other words, if you have the following statement:

char * pbin = "01001001";

Then you can pass Pbin as a parameter to the function, which returns the value of an int 25

#include <stdio.h>int bitoi (char *), int main (void) {char * Pbin = "00011001";p rintf ("Binary:%s equals 10 in Suppression:%d\n", Pbin, Bito I (Pbin));} int Bitoi (char * str) {int val = 0;while (*str! = ')//(2*val) 2 is incremented for its number of Val per run once each time the screen is added. val = 2 * val + (*str++-' 0 '); return Val;}


2. Write a program that reads two binary strings with command-line arguments and prints the result of using the ~ operator for each number, and the result of using the &, |, and ^ operators for these two numbers. Displays the result using a binary string.

#include  <stdio.h> #include  <string.h>//use 8-bit to test the value of INTSIZE to 9#define intsize 9int  bitoi (char *); char * itobi (int); Int main (int argc, char * argv[] {Int n1, n2;n1 = bitoi (argv[1]), N2 = bitoi (argv[2]);p rintf ("Take anti \ n");p rintf ("%s\n %s\n ",  argv[1], itobi (~N1));p rintf (" \n%s\n%s\n ",  argv[2], itobi (~N2));p rintf (" \ n and \ n "); printf (" %s\n %s\n %s\n",  argv[1], argv[2], itobi (N1&NBSP;&AMP;&NBSP;N2));p rintf ("\ n or \ n");p rintf (" %s\n %s\n %s\n",  argv[1], argv[2], itobi (N1&NBSP;|&NBSP;N2)); printf ("\ n xor \ n");p rintf (" %s\n %s\n %s\n",  argv[1], argv[2], itobi (n1 ^ &NBSP;N2));} Int bitoi (CHAR&NBSP;*&NBSP;STR) {int val = 0;while  (*str !=  ')//(2*val) 2 for its number Val each time the run is incremented by a curtain. val = 2 * val +  (*str++ -  ' 0 '); return val;} Char *&nbSp;itobi (int n) {int i = 1;static char str[intsize];str[intsize - 1]  =  ';int j = intsize - 2;while  ' (j >= 0) {str[j--]  =  (n & 1)  +  ' 0 '; n >>= 1;} Return str;}


3. Write a function that takes an int parameter and returns the number of bits opened in this parameter. Test the function in the program

#include <stdio.h>int open_count (int n); char * Itobi (int n); int main (void) {int i = 118191;printf ("Binary form of%d:%s\n", I , Itobi (i));p rintf (number of bits opened in "%d binary form:%d\n", I, Open_count (i)); return 0;} int open_count (int n) {int temp = 1;int I, Count = 0;for (i = 0; i < 8 * sizeof (int); i++) {if (N & temp) Count++;n &G T;>= 1;} return count;} char * Itobi (int n) {int i = 1;static char str[8 * sizeof (int)];str[8 * sizeof (int)-1] = ' + '; int j = 8 * sizeof (int) -2;whi Le (J >= 0) {str[j--] = (N & 1) + ' 0 '; n >>= 1;} return str;}


15_4. Write a function that accepts two int parameters: a value and the position of a bit. If the value on the specified bit is 1, the

The function returns 1, otherwise 0 is returned. Test the function in the program.

#include  <stdio.h> #include  <stdlib.h> #define  INTSIZE  (sizeof (int)  *  8) Int fun (int n, int b); Char * itobi (int n); Int main (void) {int  num = 123234;int bit = 13;if  (bit > intsize | |  bit <= 0) {printf ("the number of digits out of range \ n"); exit (exit_failure);} printf ("Binary form of%d:%s\n",  num, itobi (num));p rintf (%d bits in%d binary form:%d\n ",  num, bit, fun ( Num, bit)); return 0;} Int fun (int n, int b) {int i = 1;n >>= b - 1;if   (N & i) return 1;return 0;} Char * itobi (int n) {int i = 1;static char str[8 * sizeof (int )];str[8 * sizeof (int) -1] =  ' + '; int j = 8 * sizeof (int) -2;while   (j >= 0) {str[j--] =  (n & 1)  +  ' 0 '; n > >= 1;} Return str;}


5. Writes a function that rotates all bits in a unsigned int to the left by the specified number of bits.

For example, rotate_l (x,4) moves all the bits in X to the left by 4 positions, and the missing bits from the left end appear again at the right end. In other words, the bits removed from the high position are placed low. Test the function in the program.

#include <stdio.h> #define UINTSIZE (sizeof (unsigned int) * 8) unsigned int rotate_l (unsigned int x, int n); char * Itob I (int n); int main (void) {unsigned int num = 4123456;int bit = 6;printf ("%s\n", Itobi (num));p rintf ("%s\n", Itobi (rotate_l (n (um, bit)));} char * Itobi (int n) {int i = 1;static char str[uintsize+1];str[uintsize] = ' n '; int j = uintsize-1;while (J >= 0) {str[j- -] = (n & 1) + ' 0 '; n >>= 1;} return str;} unsigned int rotate_l (unsigned int x, int n) {unsigned int y;y = x >> (uintsize-n); x <<= N;return x | y;}


6. Design a bit field structure to store the following information:

A number of Font id:0 to 255

A number of Font size:0 to 127

Bold:off (0) or on (1)

Italic:off (0) or on (L)

Underline:off (0) or on (1)

Use this structure in your program to display font parameters, and use a looping menu to let the user change the parameters. For example, a running example of a program is as follows:

ID SIZE ALIGNMENT B I U

1 left off off


f) Change font s) change size a) change alignment

b) Toggle Bold i) Toggle italic u) Toggle Underline

Q) quit

S

Enter font Size (0-127): 36


ID SIZE ALIGNMENT B I U

1. Left off off

f) Change font s) change size a) change alignment

b) Toggle Bold i) Toggle italic u) Toggle Underline

Q) quit

A

Select Alignment:

L) left C) Center R) Right

R


ID SIZE ALIGNMENT B I U

1 + right off off

f) Change font s) change size a) change alignment

b) Toggle Bold i) Toggle italic u) Toggle Underline

Q) quit

I


ID SIZE ALIGNMENT B I U

1 + right off on off

f) Change font s) change size a) change alignment

b) Toggle Bold i) Toggle italic u) Toggle Underline

Q) quit

Q

bye!

A program should use the & operator and the appropriate mask to ensure that the font ID and font size information are converted to the specified range.

#include  <stdio.h> #include  <string.h> #define  left 0#define center 1# Define right 2#define off 0#define on 1#define fontid 0xff#define  fontsize 0x7ftypedef struct box{unsigned int font_id : 8;// A number between 0 and 255   font idunsigned int font_size : 7;//0 to 127 for a number   font size Unsigned int  align : 2;//left (0) or right (1)   align Unsigned int bold : 1;//off (0) or on (1)   Blackbody Unsigned int italic : 1;//off (0) or on (l)   Italic unsigned int underline  : 1;//off (0) or on (1)   underline}box;void show_box (Const box * pbox); Int menu ( void); Int get_ch (CHAR&NBSP;*&NBSP;STR); void get_id (Box * pbox); Void get_size (BOX  * pbox); void set_align (Box * pbox); Int main (void) {box box_info =  { 1, 12, left, off,  off, off };char ch;show_box (&box_info);while  ((Ch = menu ())  !=   ' Q ') {switch  (CH) {case  ' F ': get_id (&box_info);break;case  ' s ': get_size (&box_info); break;case  ' a ': Set_align (&box_info);break;case  ' B ': Box_info. Bold = ~box_info. bold;break;case  ' i ': box_info. Italic = ~box_info. italic;break;case  ' u ': box_info. Underline = ~box_info. underline;break;default:printf ("\nerror!\n");} printf ("\ n"); Show_box (&box_info);} printf ("bye!\n"); return 0;} Void show_box (Const box * pbox) {char *a[] = {  "LETF",  "center", " Right " };char *b[] = { " off ", " on " };char *i[] = { " off ", " on " };char *u[] = { " Off ", " on "&NBSP;};p rintf ("  ID SIZE  alignment  b   i   u \n ");p rintf ("%3d %4d %9s %3s %3s %3s\n ", pbox->font_id, pbox->font_size,a[pbox->align], b[pbox-> Bold], i[pbox->italic], u[pbox->underline]);} Int menu (void) {puts ("f) change font    s) change size     A) change alignment ");p UTS (" b) toggle bold    i) toggle italic  u) Toggle underline ");p UTS (" Q) Quit "); Return get_ch (" Fsabiuq ");} Int get_ch (CHAR&NBSP;*&NBSP;STR) {int ch;while  (Ch = getchar (), &NBSP;STRCHR (str,  CH)  == null) {while  (GetChar ()  !=  ' \ n ');p rintf ("error! please enter  (%s ): ",  str);} while  (GetChar ()  !=  ' \ n '); return ch;} VOID&NBSP;GET_ID (Box * pbox) {int f;do{printf ("Enter font id (0-255):"); scanf ("%d", &NBSP;&AMP;F);}  while  (f > 255 | |  f < 0);p Box->font_id = f & fontid;while  (GetChar ()  !=  ' \ n '); Void get_size (Box * pbox) {int s;do{printf ("Enter font id (0-255):"); scanf ("%d",  &s);}  while  (s > 127 | |  s < 0);p box->font_size = s & fontsize;while  (GetChar ()  !=  ' \ n ');} Void set_align (Box * pbox) {puts ("SELECT&NBSP;ALIGNMENT&NBSP;:");p UTS ("L) left   c ) center   r);switch  (get_ch ("LCR") {case  ' l ':p box->align = left;  break;case  ' C ':p box->align = center; break;case  ' r ':p box->align =  right; break;default:printf ("\nerror\n");}}


7. Write a program that is identical to the functionality described in Exercise 6. Use a unsigned long to save the font information, and use the bitwise operator instead of the bit member to manage the information.

#include  <stdio.h> #include  <string.h> #define  FONTID 0x00FF#define  fontsize 0x7f00#define align 0x18000#define left 0x00000#define center  0x08000#define right 0x10000#define bold 0x20000#define italic 0x40000#define  underline 0x80000void show_box (unsigned long f); Int menu (void); Int get_ch ( CHAR&NBSP;*&NBSP;STR); void get_id (Unsigned long * pfont); Void get_size (unsigned  Long * pfont); void set_align (Unsigned long * pfont); Int main (void) {unsigned  long font = 1 |  (12<<8)  | LEFT | BOLD |  Italic | underline;char ch;show_box (font);while  ((Ch = menu ())  !=  ' Q ') {switch  (CH) {case  ' F ': get_id (&font);break;case  ' s ': Get_size (&font);break;case  ' a ': Set_align (&font);case  ' B ':font ^= bold;break;case  ' I ':font ^= italic;break;case  ' u ': Font  ^= underline;break;default:printf ("\nerror!\n");} printf ("\ n"); Show_box (font);} printf ("bye!\n"); return 0;} Void show_box (unsigned long f) {printf ("  id size alignment  b    i   u \n ");p rintf ("%4d %4d  ",  f & fontid,   (f & fontsize) >>8);switch  (f & align) {case left: printf ("%9s",  "left"),  break;case right: printf ("%9s",  "right"); break;case  center: printf ("%9s",  "center")  break;default: printf ("%9s",  "Unknown");  break;} printf (" %3s %3s %3s\n\n",  (f & bold)  == BOLD ?  "Off"  :  "On", (f & italic)  == ITALIC ?  "Off"  :  "on", (f  & underliNE)  == UNDERLINE ?  "Off"  :  "on";} Int menu (void) {puts ("f) change font    s) change size     A) change alignment ");p UTS (" b) toggle bold    i) toggle italic  u) Toggle underline ");p UTS (" Q) Quit "); Return get_ch (" Fsabiuq ");} Int get_ch (CHAR&NBSP;*&NBSP;STR) {int ch;while  (Ch = getchar (), &NBSP;STRCHR (str,  CH)  == null) {while  (GetChar ()  !=  ' \ n ');p rintf ("error! please enter  (%s ): ",  str);} while  (GetChar ()  !=  ' \ n '); return ch;} VOID&NBSP;GET_ID (Unsigned long * pfont) {int f;do{printf ("Enter font id (0-255):") ; scanf ("%d",  &f);}  while  (f > 255 | | &NBSP;F&NBSP;&LT;&NBSP;0); f = f & fontid;*pfont &= ~fontid;*pfont | = f;while  (GetChar ()  !=  ' \ n '); Void gEt_size (Unsigned long * pfont) {int s;do{printf ("Enter font id (0-255):"); scanf ("% D ",  &s);}  while  (s > 127 | |  s < 0);s =  (s << 8)  & fontsize;*pfont &=  ~FONTSIZE;*pfont |= s;while  (GetChar ()  !=  ' \ n ');} Void set_align (Unsigned long * pfont) {puts ("SELECT&NBSP;ALIGNMENT&NBSP;:");p UTS ("L) left &NBSP;&NBSP;&NBSP;C) center   r);switch  (get_ch ("LCR")) {case  ' l ':* pfont  &= ~ALIGN; *pfont |= LEFT; break;case  ' C ':* pfont &= ~align;  *pfont |= CENTER; break;case  ' R ':* pfont &= ~align; *pfont |=  right; break;default:printf ("\nerror\n");}}


This article is from the "30 years old school Programming" blog, please be sure to keep this source http://xiongyi85.blog.51cto.com/8756903/1660958

C Primer Plus (Fifth Edition) 15th chapter position Operation programming Exercise

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.