ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c primer plus(第六版) 第十五章答案(vscode编译运行通过)

2021-09-03 15:04:26  阅读:144  来源: 互联网

标签:case break vscode 第六版 int num plus printf include


在这里插入图片描述
在这里插入图片描述
1.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
int strToNum(char *str)
{
    int num = 0;
    while(*str)
    {
        num *= 2;
        num += *str - '0';
        str++;
    }

    return num;
}
int main(void)
{
    char * pbin = "01001001";

    printf("The %s's result is %d\n", pbin, strToNum(pbin));

    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#define SIZE 33

void showBstr(const char *str)
{
    int i = 0;
    while(str[i])
    {
        putchar(str[i]);
        if((++i % 4 == 0) && str[i])
            putchar(' ');
    }
}

void prinBinUseStr(int num)
{
    char temp[SIZE];

    for(int i = SIZE - 2; i >= 0; i--, num >>= 1)
    {
        temp[i] = (01 & num) + '0';
    }
    temp[SIZE - 1] = '\0';
    showBstr(temp);
}

void strToNum(char *str1, char *str2)
{
    int num1 = 0;
    int num2 = 0;
    int temp;

    while(*str1)
    {
        num1 *= 2;
        num1 += *str1 - '0';
        str1++;
    }
     while(*str2)
    {
        num2 *= 2;
        num2 += *str2 - '0';
        str2++;
    }
    printf("The ~%s is: ", str1);
    prinBinUseStr(~num1);
    putchar('\n');
    printf("The ~%s is: ", str2);
    prinBinUseStr(~num2);
    putchar('\n');
    printf("The %s & %s is: ", str1, str2);
    prinBinUseStr(num1 & num2);
    putchar('\n');
    printf("The %s | %s is: ", str1, str2);
    prinBinUseStr(num1 | num2);
    putchar('\n');
    printf("The %s ^ %s is: ", str1, str2);
    prinBinUseStr(num1 ^ num2);
    putchar('\n');
}

int main(int argc, char *argv[])
{
    char num1[SIZE];
    char num2[SIZE];
    if(argc < 3)
    {
        puts("The parameter number is too small. Check the parameter number");
        exit(EXIT_FAILURE);
    }
    else
    {
        strcpy(num1, argv[1]);
        strcpy(num2, argv[2]);
    }
    strToNum(num1, num2);

    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#define SIZE 33
void showOpenBit(int num)
{
    int count = 0;
    int temp = num;

    for(int i = 31; i >= 0; i--, temp >>= 1)
    {
        if(temp & 01 == 1)
        {
            count++;
        }
    }
    printf("There are %d open bits in %d\n", count, num);
}

void numTostr(int num, char *str)
{
    for(int i = 31; i >= 0; i--, num >>= 1)
    {
        str[i] = (num & 01) + '0';
    }
    str[32] = '\0';
}

int main(int argc, char *argv[])
{
    int num;
    char num1[SIZE];

    printf("Please input a num:(and input q to quit)");
    while(scanf("%d", &num) == 1)
    {
        numTostr(num, num1);
        printf("%s\n", num1);
        showOpenBit(num);
        printf("Please input a num:(and input q to quit)");
    }

    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#define SIZE 33

void numTostr(int num, char *str)
{
    for(int i = 31; i >= 0; i--, num >>= 1)
    {
        str[i] = (num & 01) + '0';
    }
    str[32] = '\0';
}

int TheNumPos(int num, int pos)
{
    int mask = 1 << pos;
    if((num & mask) == mask)
        return 1;
    else
        return 0;
}

int main(int argc, char *argv[])
{
    int num1, pos;
    char num2[SIZE];

    printf("Please input two num:(and input q to quit)");
    while(scanf("%d %d", &num1, &pos) == 2)
    {
        numTostr(num1, num2);
        printf("%s\n", num2);
        if(TheNumPos(num1, pos) == 1)
        {
            printf("In the %d, at %d pos is 1\n", num1, pos);
        }
        else
        {
            printf("In the %d, at %d pos is 0\n", num1, pos);
        }
        printf("Please input two num:(and input q to quit)");
    }

    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#define SIZE 33

void numTostr(int num, char *str)
{
    for(int i = 31; i >= 0; i--, num >>= 1)
    {
        str[i] = (num & 01) + '0';
    }
    str[32] = '\0';
}

unsigned int rotate_1(unsigned int x, int pos)
{
    size_t size = CHAR_BIT * sizeof(unsigned int);
    unsigned int num;

    pos = pos % size;
    num = x >> (size - pos);
    num |= x << pos;
    return num;
}

int main(int argc, char *argv[])
{
    int  pos;
    unsigned int num1, num2;
    char temp1[SIZE], temp2[SIZE];

    printf("Please input two num:(and input q to quit)");
    while(scanf("%u %d", &num1, &pos) == 2)
    {
        num2 = rotate_1(num1, pos);
        numTostr(num1, temp1);
        numTostr(num2, temp2);
        printf("The %s rotate %d is %s\n", temp1, pos, temp2);
        printf("Please input two num:(and input q to quit)");
    }

    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#define SIZE 33

typedef struct fontFormat{
    unsigned int id: 8;
    unsigned int size: 7;
    unsigned int allgnment: 2;
    bool overstriking: 1;
    bool italic: 1;
    bool underline: 1;
}seeting;

#define left 0
#define center 1
#define right 2

#define on true
#define off false

void showInfor(seeting front)
{
    printf("%3s %4s %9s   %3s   %3s   %3s\n","ID", "SIZE", "ALIGNMENT", "B", "I", "U");
    printf("%3d %4d ",front.id,front.size); 
    switch(front.allgnment)
    {
        case left: printf("   %-9s", "left");break;
        case center: printf("   %-9s", "center");break;
        case right: printf("   %-9s", "right");break;
    }
    switch(front.overstriking)
    {
        case on: printf(" %3s","on");break;
        case off: printf(" %3s","off");break;
    }
    switch(front.italic)
    {
        case on: printf("   %3s","on");break;
        case off: printf("   %3s","off");break;
    }
    switch(front.underline)
    {
        case on: printf("   %3s","on");break;
        case off: printf("   %3s","off");break;
    }
    
}

void eatLine(void)
{
    while(getchar() != '\n')
        continue;
}

int getChoice(char *arr)
{
    int ch;

    ch = getchar();
    eatLine();
    while(strchr(arr,ch) == NULL)
    {
        printf("Please input %s\n", arr);
        ch = getchar();
        eatLine();
    }

    return ch;
}

int menu(void)
{
    int ch;
    printf("\n");
    printf("f)change font     s)change size      a)change alignment\n");
    printf("b)toggle bold     i)toggle italic    u)toggle underline\n");
    printf("q)quit\n");
    ch = getChoice("fsabiuq");

    return ch;
}

void changeAlignment(seeting *pt)
{
    int ch;

    printf("Select alignment:\n");
    printf("l)left  c)center    r)right\n");
    ch = getChoice("lcr");
    switch(ch)
    {
        case 'l':pt->allgnment = left; break;
        case 'c':pt->allgnment = center; break;
        case 'r':pt->allgnment = right; break;
    }
}

void showMenu(seeting *pt)
{
    int ch;

    unsigned int temp;

    showInfor(*pt);
    putchar('\n');
    putchar('\n');
    
    while((ch = menu()) != 'q')
    {
        switch(ch)
        {
            case 'f':printf("Enter font ID(0-255):"); scanf("%u",&temp);eatLine();temp &= 0xff;pt->id = temp; break;
            case 's':printf("Enter font size(0-127):"); scanf("%u", &temp);eatLine();temp &= 0x7f;pt->size = temp; break;
            case 'a':changeAlignment(pt); break;
            case 'b': pt->overstriking = !pt->overstriking; break;
            case 'i': pt->italic = !pt->italic; break;
            case 'u': pt->underline = !pt->underline; break;
        }
        showInfor(*pt);
        putchar('\n');
        putchar('\n');
    }
}

int main(int argc, char *argv[])
{
    seeting front = {1, 12, left, off, off, off};
    showMenu(&front);
    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <limits.h>

#define LEFT 0x00000
#define CENTER 0x08000
#define RIGHT 0x10000
#define SIZE_SHIFT 8
#define ID_MASK 0xff
#define SIZE_MASK 0x7f00
#define AllIGN_MASK 0x18000
#define OVERSTRIKING_ON 0x20000
#define ITALIC_ON 0x40000
#define UNDERLINE_ON 0x80000
#define OFF 0x00000

typedef struct{
    unsigned int id:8;
    unsigned int size:7;        
    unsigned int allign:2;
    bool overstriking:1;
    bool italic:1;
    bool underline:1;
}setting;

void show_result(unsigned long);
int show_menu(void);
void change_font(unsigned long *);
void change_size(unsigned long *);
void change_alignment(unsigned long *);
void toggle_bold(unsigned long *);
void toggle_italic(unsigned long *);
void toggle_underline(unsigned long *);

int main(int argc,char *argv[])
{
  int ch;
  
  unsigned long font = 1 | (12) << SIZE_SHIFT | LEFT; 
  show_result(font);
  while((ch = show_menu()) != 'q')
  {
      switch(ch)
      {
          case 'f':change_font(&font);break;
          case 's':change_size(&font);break;
          case 'a':change_alignment(&font);break;
          case 'b':toggle_bold(&font);break;
          case 'i':toggle_italic(&font);break;
          case 'u':toggle_underline(&font);break;
      }
      putchar('\n');
      show_result(font);
  }
  printf("end");
  printf("\n");
  system("pause");
  
    return 0;
}

void show_result(unsigned long font)
{
    printf("ID SIZE ALIGNMENT    B    I    U\n");
    printf("%2d %4d",font & ID_MASK, ((font & SIZE_MASK) >> 8));
    switch(font & AllIGN_MASK)
    {
        case LEFT:printf("   left");break;
        case CENTER:printf("  center");break;
        case RIGHT:printf("  right");break;
        default:break; 
    }
    switch(font & OVERSTRIKING_ON)
    {
        case OVERSTRIKING_ON:printf("       on");break;
        case OFF:printf("      off");break;
        default:break; 
    }
    switch(font & ITALIC_ON)
    {
        case ITALIC_ON:printf("   on");break;
        case OFF:printf("  off");break;
        default:break; 
    }
    switch(font & UNDERLINE_ON)
    {
        case UNDERLINE_ON:printf("   on\n");break;
        case OFF:printf("  off\n");break;
        default:break; 
    }

}

void eatline(void)
{
    while(getchar() != '\n')
        continue;
}

int get_choose(const char *p)
{
    int ch;

    ch = getchar();
    if(strchr(p,ch) == NULL)
    {
        printf("Please input right option(%s)\n",p);
        eatline();
        ch = getchar();
    }
    eatline();
    return ch;
}

#define SIZE 7
int show_menu(void)
{
    const char *p[SIZE] = {
        "change foot","change size","change alignment","toggle bold","toggle italic","toggle underline","quit",
    };
    const char data[SIZE + 1] = "fsabiuq";
    for(int i = 0; i < SIZE; i++)
    {
        i % 3 == 0?printf("\n"):printf("    ");
        printf("%c)%-17s",data[i],p[i]);
    }
    putchar('\n');

    return get_choose(data);
}

void change_font(unsigned long *p)
{
    int font_id;

    printf("Enter font ID:(0~255):");
    while(scanf("%d",&font_id) != 1)
    {
        printf("Please input a number(0-255):");
        scanf("%*s");
    }
    eatline();
    font_id &= 0xff;
    *p &= 0xfff00;
    *p |= font_id;
}

void change_size(unsigned long *p)
{
    int font_size;

    printf("Enter font size:(0~127):");
    while(scanf("%d",&font_size) != 1)
    {
        printf("Please input a number(0-127):");
        scanf("%*s");
    }
    eatline();
    font_size &= 0x7f;
    *p &= 0xf80ff;
    *p |= font_size << SIZE_SHIFT;
}
#define SIZE2 3
int show_alignment_menu(void)
{
    const char *data1[SIZE2] = {"left","center","right"};
    const char data2[SIZE2 + 1] = "lcr";
    for(int i = 0; i < SIZE2; i++)
    {
        printf("%c)%-8s",data2[i],data1[i]);
    }
    putchar('\n');
    return get_choose(data2);
}

void change_alignment(unsigned long *p)
{
    int ch;

    ch = show_alignment_menu();
    switch(ch)
    {
        case 'l':*p &= 0xE7fff;*p |= LEFT;break;
        case 'c':*p &= 0xE7fff;*p |= CENTER;break;
        case 'r':*p &= 0xE7fff;*p |= RIGHT;break;
        default:break;
    }
}

void toggle_bold(unsigned long *p)
{
  *p ^= OVERSTRIKING_ON;
}

void toggle_italic(unsigned long *p)
{
   *p ^= ITALIC_ON;
}

void toggle_underline(unsigned long *p)
{
   *p ^= UNDERLINE_ON;
}

标签:case,break,vscode,第六版,int,num,plus,printf,include
来源: https://blog.csdn.net/qq_41701950/article/details/120081866

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有