我是靠谱客的博主 唠叨裙子,最近开发中收集的这篇文章主要介绍CPrimer Plus第6版第十五章编程练习答案(C语言)(自学记录),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

第一题

#define _CRT_SECURE_NO_WARNINGS
//第十五章,编程练习题,第一题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int printStrInt(char* str);
char* fget_s(char* st, int n);
int main(void)
{
int ch;
char str[100];
printf("请输入一个二进制字符串:");
fget_s(str, 100);
ch=printStrInt(str);
printf("输入的字符串%sn值为:%dn", str, ch);
return 0;
}
//求值函数
int printStrInt(char* str)
{
int i;
int len;
int wei = 1;	//值
int sum=0;
//总值
if (!(len = strlen(str)))
{
fprintf(stderr, "长度为0");
return 0;
}
for (i = len-1; i >= 0; i--, wei <<= 1)
{
if (str[i] == '1')
sum += wei;
else if (str[i] == '0')
;
else
{
fprintf(stderr, "输入的是二进制字符串,不符合规则");
exit(EXIT_FAILURE);
}
}
//sum^=80;
return sum;
}
char* fget_s(char* st,int n)
{
char* find;
char* str_val;
str_val = fgets(st, n, stdin);
if (str_val)
{
find = strchr(st, 'n');
if (find)
*find = '';
else
while (getchar() != 'n')
continue;
}
return str_val;
}

这题按照例子中的传递"01001001"(73)应该在函数中可以用一个按位取反的运算与"01010000"(80)运算可的返回值25,

第二题

#define _CRT_SECURE_NO_WARNINGS
//第十五章,编程练习题,第二题
//命令行参数读取两个二进制字符串,使用运算符,并以二进制打印结果
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ZERO '0'
#define ONE '1'
int main(int argc, char* argv[])
{
int count, i;
unsigned int
wei = 1;
unsigned int
num;
unsigned int
sum = 0;
unsigned int zongSum[2];
if (argc != 3)
{
fprintf(stderr, "命令行参数不正确,请从新输入:");
exit(EXIT_FAILURE);
}
for (count = 1; count < argc; count++)
{
num = strlen(argv[count]);
for (i = num - 1, sum = 0, wei = 1; i >= 0; i--)
{
if (argv[count][i] == ZERO)
;
else if (argv[count][i] == ONE)
sum += wei;
else
continue;
wei <<= 1;
}
zongSum[count - 1] = sum;
}
printf("输入的字符串为"%s","%s",转换为数值为%u,%u,n使用~运算符为%u,%un",
argv[1], argv[2], zongSum[0], zongSum[1], ~zongSum[0], ~zongSum[1]);
printf("使用&运算符为%un", zongSum[0] & zongSum[1]);
printf("使用|运算符为%un", zongSum[0] | zongSum[1]);
printf("使用^运算符为%un", zongSum[0] ^ zongSum[1]);
return 0;
}

第三题

#define _CRT_SECURE_NO_WARNINGS
//第十五章,编程练习题,第三题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int puts_wei(int num);
int main(void)
{
int a = 15;
int num;
num=puts_wei(a);
printf("%d", num);
return 0;
}
int puts_wei(int num)
{
int
wei = 1;
int i;
for (i = 0; num >= 1; num >>= 1)
{
(wei& num) == 1 ? i++ : i;
}
return i;
}

第四题

#define _CRT_SECURE_NO_WARNINGS
//第十五章,编程练习题,第四题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int puts_wei(int num, int wei);
int main(void)
{
int a, b;
int i;
printf("请输入值:");
scanf("%d", &a);
printf("请输入位数(0-31)之间:");
scanf("%d", &b);
i = puts_wei(a, b);
printf("值%d的第%d位是%dn", a, b, i);
return 0;
}
int puts_wei(int num, int wei)
{
int count = 1;
int i;
for (i = 0; i < wei; i++)
num >>= 1;
if (num & count)
return 1;
return 0;
}

第五题

#define _CRT_SECURE_NO_WARNINGS
//第十五章,编程练习题,第五题
//左旋转一个unsigned int 类型的值,编写一个可以指定左移指定位数的函数,左侧移除的位会出现在最右侧
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 2147483648
unsigned int putsUInt(unsigned int n, int ch);
int main(void)
{
int coun;
unsigned int com;
unsigned int wei;
printf("输入一个大于等于0的数字:");
while (scanf("%u", &com) == 1)
{
while (getchar() != 'n')
continue;
printf("请输入将向左位移几个位置(输入的值要大于等于0):");
if (scanf("%d", &coun) != 1 || coun < 0)
{
printf("输入一个大于等于0的数字:");
continue;
}
wei=putsUInt(com, coun);
printf("输入的数字%u向左位移%d个数字之后生成新的数字是%un", com, coun, wei);
printf("输入一个大于等于0的数字:");
}
return 0;
}
unsigned int putsUInt(unsigned int n, int ch)
{
int count;
unsigned int wei = MAX;
unsigned int linshi=n;
for (count = 1; count <= ch; count++)
{
if ((wei & n))
linshi = ((n <<= 1) | (unsigned int)1);
else
linshi = (n <<= 1);
n = linshi;
}
return linshi;
}

第六题

#define _CRT_SECURE_NO_WARNINGS
//第十五章,编程练习题,第六题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//位字段结构
typedef struct {
unsigned int id : 8;
unsigned int size : 7;
unsigned int : 1;
unsigned int duiqi : 2;
unsigned int cu : 1;
unsigned int italic : 1;
unsigned int xie : 1;
}kongzhi;
//函数指针
typedef void (*XuanZe)(kongzhi* font);
//函数声明
void putsCaidan(const kongzhi* font);
XuanZe putsXuanZe(kongzhi * font);
void putsChangeID(kongzhi* font);
void putsChangeFont(kongzhi* font);
void putsDuiQi(kongzhi* font);
void putsCuXi(kongzhi* font);
void putsItalic(kongzhi* font);
void putsUnderLine(kongzhi* font);
int main(void)
{
kongzhi fontgo;
XuanZe fontshuru;
memset(&fontgo, 0, sizeof(fontgo));
putsCaidan(&fontgo);
while ((fontshuru = putsXuanZe(&fontgo)) != NULL)
{
fontshuru(&fontgo);
putsCaidan(&fontgo);
}
return 0;
}
//菜单函数
void putsCaidan(const kongzhi* font)
{
printf("nnt ID SIZE ALIGNMENT
B
I
U
n");
printf("t%3u %4u ", font->id, font->size);
if (font->duiqi == 1)
printf("
center
");
else if (font->duiqi == 2)
printf("
right
");
else
printf("
left
");
if (font->cu == 1)
printf(" no ");
else
printf("off	");
if (font->italic == 1)
printf(" no ");
else
printf("off ");
if (font->xie == 1)
printf(" no ");
else
printf("off ");
putchar('n');
printf("nnf)change fontts)change size
ta)change alignmentn");
printf("b)toggle boldti)toggle italictu)toggle underlinen");
printf("q)quitn");
}
//输入选择函数
XuanZe putsXuanZe(kongzhi *font)
{
char ch;
XuanZe liuxuanyi=NULL;
while ((ch = getchar()) != 'n')
{
switch (ch)
{
case 'F':
case 'f':liuxuanyi = putsChangeID; break;
case 'S':
case 's':liuxuanyi=putsChangeFont; break;
case 'A':
case 'a':liuxuanyi=putsDuiQi; break;
case 'B':
case 'b':liuxuanyi=putsCuXi; break;
case 'I':
case 'i':liuxuanyi = putsItalic; break;
case 'U':
case 'u':liuxuanyi = putsUnderLine; break;
case 'Q':
case 'q':
fprintf(stdout, "谢谢!");
exit(EXIT_FAILURE);
break;
default:
printf("请输入有效字母n");
break;
}
while (getchar() != 'n')
continue;
if (liuxuanyi)
break;
}
return liuxuanyi;
}
//更改字体(字体id)
void putsChangeID(kongzhi* font)
{
char chr;
unsigned int ch;
printf("Enter font ID (0-255):");
while (scanf("%u", &ch) == 1)
{
if (ch <= 255 && ch >= 0)
{
font->id = ch;
break;
}
else
{
printf("Enter font ID (0-255):");
while (getchar() != 'n')
continue;
}
}
if ((chr = getchar()) == EOF)
putsChangeID(font);
else
while (getchar() != 'n')
continue;
}
//更改字体(字体大小)
void putsChangeFont(kongzhi* font)
{
char chr;
unsigned int ch;
printf("Enter font size (0-127):");
while (scanf("%u", &ch) == 1)
{
if (ch <= 127 && ch >= 0)
{
font->size = ch;
break;
}
else
{
printf("Enter font size (0-127):");
while (getchar() != 'n')
continue;
}
}
if ((chr = getchar()) == EOF)
putsChangeFont(font);
else
while (getchar() != 'n')
continue;
}
//更改对齐
void putsDuiQi(kongzhi* font)
{
char ch;
printf("Select alignment:n");
printf("l)lefttc)centertr)rightn");
while ((ch = getchar()) != EOF)
{
if (ch == 'l')
{
font->duiqi = 0;
break;
}
else if (ch == 'c')
{
font->duiqi = 1;
break;
}
else if (ch == 'r')
{
font->duiqi = 2;
break;
}
else
{
printf("Select alignment:n");
printf("l)lefttc)centertr)rightn");
while (getchar() != 'n')
continue;
}
}
while (getchar() != 'n')
continue;
}
//更改粗细
void putsCuXi(kongzhi* font)
{
char ch;
printf("Select toggle bold:n");
printf("c)cutx)xin");
while ((ch = getchar()) != EOF)
{
if (ch == 'c')
{
font->cu = 1;
break;
}
else if (ch == 'x')
{
font->cu = 0;
break;
}
else
{
printf("Select toggle bold:n");
printf("c)cutx)xin");
while (getchar() != 'n')
continue;
}
}
while (getchar() != 'n')
continue;
}
//更改?
void putsItalic(kongzhi* font)
{
char ch;
printf("Select toggle italic:n");
printf("k)kaitg)guann");
while ((ch = getchar()) != EOF)
{
if (ch == 'k')
{
font->italic = 1;
break;
}
else if (ch == 'g')
{
font->italic = 0;
break;
}
else
{
printf("Select toggle italic:n");
printf("k)kaitg)guann");
while (getchar() != 'n')
continue;
}
}
while (getchar() != 'n')
continue;
}
//更改倾斜
void putsUnderLine(kongzhi* font)
{
char ch;
printf("Select toggle underline:n");
printf("x)xieto)offn");
while ((ch = getchar()) != EOF)
{
if (ch == 'x')
{
font->xie = 1;
break;
}
else if (ch == 'o')
{
font->xie = 0;
break;
}
else
{
printf("Select toggle underline:n");
printf("x)xieto)offn");
while (getchar() != 'n')
continue;
}
}
while (getchar() != 'n')
continue;
}

第七题

//第十五章,编程练习题,第六题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//存储字体信息
//字体id
#define ID 0xFF
//字体大小
#define SIZE 0x7F00
//字体对齐
#define DUIQI 0x3000000
//字体粗细
#define CUXI 0x4000000
//italic 
#define ITALIC 0x8000000
//字体倾斜
#define XIE 0x10000000
//函数指针
typedef void (*XuanZe)(unsigned int* font);
//函数声明
void putsCaidan(const unsigned int* font);
XuanZe putsXuanZe(unsigned int* font);
void putsChangeID(unsigned int* font);
void putsChangeFont(unsigned int* font);
void putsDuiQi(unsigned int* font);
void putsCuXi(unsigned int* font);
void putsItalic(unsigned int* font);
void putsUnderLine(unsigned int* font);
int main(void)
{
unsigned int fontgo=0;
XuanZe fontshuru;
putsCaidan(&fontgo);
while ((fontshuru = putsXuanZe(&fontgo)) != NULL)
{
fontshuru(&fontgo);
putsCaidan(&fontgo);
}
return 0;
}
//菜单函数
void putsCaidan(const unsigned int* font)
{
printf("nnt ID SIZE ALIGNMENT
B
I
U
n");
printf("t%3u %4u ", ((*font) & ID), ((*font) & SIZE)>>8);
if (((*font) & DUIQI) == 0x1000000)
printf("
center
");
else if (((*font) & DUIQI) == 0x2000000)
printf("
right
");
else
printf("
left
");
if (((*font) & CUXI) == 0x4000000)
printf(" no ");
else
printf("off	");
if (((*font) & ITALIC) == 0x8000000)
printf(" no ");
else
printf("off ");
if (((*font) & XIE) == 0x10000000)
printf(" no ");
else
printf("off ");
putchar('n');
printf("nnf)change fontts)change size
ta)change alignmentn");
printf("b)toggle boldti)toggle italictu)toggle underlinen");
printf("q)quitn");
}
//输入选择函数
XuanZe putsXuanZe(unsigned int* font)
{
char ch;
XuanZe liuxuanyi=NULL;
while ((ch = getchar()) != 'n')
{
switch (ch)
{
case 'F':
case 'f':liuxuanyi = putsChangeID; break;
case 'S':
case 's':liuxuanyi=putsChangeFont; break;
case 'A':
case 'a':liuxuanyi=putsDuiQi; break;
case 'B':
case 'b':liuxuanyi=putsCuXi; break;
case 'I':
case 'i':liuxuanyi = putsItalic; break;
case 'U':
case 'u':liuxuanyi = putsUnderLine; break;
case 'Q':
case 'q':
fprintf(stdout, "谢谢!");
exit(EXIT_FAILURE);
break;
default:
printf("请输入有效字母n");
break;
}
while (getchar() != 'n')
continue;
if (liuxuanyi)
break;
}
return liuxuanyi;
}
//更改字体(字体id)
void putsChangeID(unsigned int* font)
{
char chr;
unsigned int ch;
printf("Enter font ID (0-255):");
while (scanf("%u", &ch) == 1)
{
if (ch <= 255 && ch >= 0)
{
ch |= (~ID);
*font |= ID;
*font &= ch;
break;
}
else
{
printf("Enter font ID (0-255):");
while (getchar() != 'n')
continue;
}
}
if ((chr = getchar()) == EOF)
putsChangeID(font);
else
while (getchar() != 'n')
continue;
}
//更改字体(字体大小)
void putsChangeFont(unsigned int* font)
{
char chr;
unsigned int ch;
printf("Enter font size (0-127):");
while (scanf("%u", &ch) == 1)
{
if (ch <= 127 && ch >= 0)
{
ch <<= 8;
ch |= (~SIZE);
*font |= SIZE;
*font &= ch;
break;
break;
}
else
{
printf("Enter font size (0-127):");
while (getchar() != 'n')
continue;
}
}
if ((chr = getchar()) == EOF)
putsChangeFont(font);
else
while (getchar() != 'n')
continue;
}
//更改对齐
void putsDuiQi(unsigned int* font)
{
char ch;
unsigned int chr;
printf("Select alignment:n");
printf("l)lefttc)centertr)rightn");
while ((ch = getchar()) != EOF)
{
if (ch == 'l')
{
chr = (~DUIQI);
*font &= chr;
break;
}
else if (ch == 'c')
{
chr = (~DUIQI) ;
*font &= chr;
*font |= 0x1000000;
break;
}
else if (ch == 'r')
{
chr = (~DUIQI) ;
*font &= chr;
*font |= 0x2000000;
break;
}
else
{
printf("Select alignment:n");
printf("l)lefttc)centertr)rightn");
while (getchar() != 'n')
continue;
}
}
while (getchar() != 'n')
continue;
}
//更改粗细
void putsCuXi(unsigned int* font)
{
char ch;
unsigned int chr;
printf("Select toggle bold:n");
printf("c)cutx)xin");
while ((ch = getchar()) != EOF)
{
if (ch == 'c')
{
chr = (~CUXI);
*font &= chr;
*font |= CUXI;
break;
}
else if (ch == 'x')
{
chr = (~CUXI);
*font &= chr;
break;
}
else
{
printf("Select toggle bold:n");
printf("c)cutx)xin");
while (getchar() != 'n')
continue;
}
}
while (getchar() != 'n')
continue;
}
//更改?
void putsItalic(unsigned int* font)
{
char ch;
unsigned int chr;
printf("Select toggle italic:n");
printf("k)kaitg)guann");
while ((ch = getchar()) != EOF)
{
if (ch == 'k')
{
chr = (~ITALIC);
*font &= chr;
*font |= ITALIC;
break;
}
else if (ch == 'g')
{
chr = (~ITALIC);
*font &= chr;
break;
}
else
{
printf("Select toggle italic:n");
printf("k)kaitg)guann");
while (getchar() != 'n')
continue;
}
}
while (getchar() != 'n')
continue;
}
//更改倾斜
void putsUnderLine(unsigned int* font)
{
char ch;
unsigned int chr;
printf("Select toggle underline:n");
printf("x)xieto)offn");
while ((ch = getchar()) != EOF)
{
if (ch == 'x')
{
chr = (~XIE);
*font &= chr;
*font |= XIE;
break;
}
else if (ch == 'o')
{
chr = (~XIE);
*font &= chr;
break;
}
else
{
printf("Select toggle underline:n");
printf("x)xieto)offn");
while (getchar() != 'n')
continue;
}
}
while (getchar() != 'n')
continue;
}

最后

以上就是唠叨裙子为你收集整理的CPrimer Plus第6版第十五章编程练习答案(C语言)(自学记录)的全部内容,希望文章能够帮你解决CPrimer Plus第6版第十五章编程练习答案(C语言)(自学记录)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部