admin 管理员组文章数量: 887007
15.8 编程练习
1.编写一个将二进制字符串转化为数字值的函数。也就是说,如果您有一下语句:
char *pbin="01001001";
那么您将可以把pbin作为参数传递给函数,使该函数返回一个int值25.
(题目是不是出错了,01001001拿计算器算是等于73)
#include <stdio.h>
#include <string.h>
int tointeger(char * str);
int POW(int p);
int main(void)
{char pbin[9]="01001001";printf("to integer is %d\n",tointeger(pbin));
}
int POW(int p)
{int result=1;int i;for(i=0;i<p;i++){result=result*2;} return result;
}
int tointeger(char * str)
{int i;int p;int num;int size=strlen(str);for(i=size-1,p=0,num=0;i>=0;i--,p++){num+=((str[i]-'0')*POW(p));}return num;
}
2.编写一个程序,该程序用命令行参数读取两个二进制字符串,并且打印对每个数使用~运算符的结果。以及对这两个数使用&,|,^运算符的结果。使用二进制字符串形式显示结果。
#include <stdio.h>
#include <string.h>
int StrToNum(const char * str);
int Pow(int p);
void NumToStr(int num,char *argv[],int index);int main(int argc,char *argv[])
{int num1,num2;num1=StrToNum(argv[1]);num2=StrToNum(argv[2]);printf("argv[1]=%d ",num1);printf("argv[2]=%d\n",num2);NumToStr(~num1,argv,1);NumToStr(~num2,argv,1);NumToStr((num1 & num2),argv,1);NumToStr((num1 | num2),argv,1);NumToStr((num1 ^ num2),argv,1);
}int StrToNum(const char * str)
{int size=strlen(str);int i,j;int num=0;for(i=size-1,j=0;i>=0;i--,j++){num=num+(str[i]-'0')*Pow(j);}return num;
}int Pow(int p)
{int result=1;int i;for(i=0;i<p;i++)result=result*2;return result;
}void NumToStr(int num,char *argv[],int index)
{char ps[100];int size=strlen(argv[index]);int i;for(i=size-1;i>=0;i--){ps[i]=(num & 01)+'0';num>>=1;}ps[size]='\0';printf("%s\n",ps);
}
3.编写一个函数,该函数接受一个int参数,并返回这个参数中打开的位的数量。在程序中测试该函数。
#include <stdio.h>
#include <stdbool.h>
#define MASK 01
int AmountOfOpenBit(int n);
int main(void)
{int number;printf("Please enter a number to find how many bits are opened.\n");while(scanf("%d",&number)==1){printf("There are %d bits are opened.\n",AmountOfOpenBit(number));printf("Enter another number.(q to quit:)\n");}
}
int AmountOfOpenBit(int n)
{int total=0;int size=sizeof(int)*8;int i;for(i=0;i<size-1;i++,n>>=1){if(n & MASK)total++; }return total;
}
4.编写一个函数,该函数接受两个int参数,一个值和一个位的位置。如果指定的位上的值是1.则该函数返回1,否则返回0.在程序中测试该函数。
#include <stdio.h>
#include <stdbool.h>
#define MASK 01
bool isOpened(int number,int code);
int main(void)
{int number,code;printf("Please enter two number to see is it opened.\n");while(scanf("%d %d",&number,&code)==2){isOpened(number,code);if(isOpened(number,code))printf("It is opened.\n");else printf("It is closed.\n");printf("Enter another number.q to quit:\n");}
}
bool isOpened(int number,int code)
{int i;bool isopen=false;for(i=0;i<code;i++)number>>=1;if(number & MASK)isopen=true;return isopen;
}
5.编写一个函数,该函数将一个unsigned int 中的所有位向左旋指定数量的位。例如,rotate_1(x,4)将x中的所有位向左移动4个位置,而且左端丢失的位会重新出现在右端。也就是说,把高位移动出的为放入低位。在程序中测试该函数。
#include <stdio.h>
#include <string.h>
#define MASK 01
void MoveBits(int num,int code);
void show(char *str,int size);
int main(void)
{unsigned int number;int code;printf("Please enter two number to operat.\n");while(scanf("%d %d",&number,&code)==2){MoveBits(number,code); printf("Enter another number.q to quit:\n");}
}void MoveBits(int num,int code)
{int size=sizeof(unsigned int)*8;char source[size+1];char AfterMove[size+1];int i,j;/*把数字转换成字符数组*/for(i=size-1;i>=0;i--,num>>=1){source[i]=(num & MASK)+'0';}source[size]='\0';show(source,size);/*把code位后面的二进制拷贝进AfterMove字符数组*/strcpy(AfterMove,&source[code]);/*把code位之前的二进制接入AfterMove字符数组*/strncat(AfterMove,source,code);show(AfterMove,size);}void show(char *str,int size)
{int i;for(i=0;i<size;i++){putchar(str[i]);if((i+1)%8==0)putchar(' ');}printf("\n");
}
6.设计一个字段结构用来存储以下信息
Font ID:0到255之间的一个数
Font Size:0到127之间的一个数
Bold:Off 或者 On
Italic:Off 或者On
Underlin:Off或者On
在程序中使用这个结构来现实字体参数,并使用循环的菜单来让用户改变参数。例如,程序的一个运行示例如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MASK 01
#define ON 01
#define OFF 00typedef struct font{unsigned int id : 8 ;unsigned int size : 7 ;unsigned int bold : 1 ;unsigned int italic : 1 ;unsigned int underline : 1 ;unsigned int : 14 ;
}FONT;const char *ALIGNMENT[3]={"left","center","right"};
const char *onoff[2]={"off","on"};
const char *TITLE="ID SIZE ALIGNMENT B I U";
const char *BLANK=" ";
const int TITLELEN=strlen(TITLE); void ShowStructure(FONT a,char alignment);
void Menu(void);
void endline(void);int main(void)
{FONT a={1,12,OFF,OFF,OFF,};char alignment='l';char choice;ShowStructure(a,alignment);Menu();printf("Enter a label to perform process (q to quit):\n");choice=getchar();endline();while(choice!='q'){switch(choice){int value;case 'f':printf("Enter Font ID (0~255):\n");/*检查输入合法性*/while(scanf("%d",&value)!=1 || value<0 || value>255){endline();printf("Illegal input.Please enter a number (0~255):\n");}endline();a.id=value;break;case 's':printf("Enter Font size (0~127):\n");/*检查输入合法性*/while(scanf("%d",&value)!=1 || value<0 || value>127){endline();printf("Illegal input.Please enter a number (0~127):\n");}endline();a.size=value;break;case 'a':printf("Select alignment:\n");printf("l) left c)center r)right\n");alignment=getchar();endline();while(alignment!='l'&&alignment!='c' &&alignment!='r'){printf("Illegal input.Please enter a character like l or c or r:\n");alignment=getchar();endline();}break;case 'b':a.bold=(a.bold ^ MASK);break;case 'i':a.italic=(a.italic ^ MASK);break;case 'u':a.underline=(a.underline ^ MASK);break;default :printf("Enter right labels.\n");}ShowStructure(a,alignment);Menu();printf("Enter a label to perform process (q to quit):\n");choice=getchar();endline();printf("\n");}}
void ShowStructure(FONT a,char alignment)
{char Before[50];char After[50]={'\0',};char temp[4];int sourcelen;int D_value;int i;/*把结构体ID中的数据转换成字符串写入数组*/strcpy(Before,itoa(a.id,temp,10));/*追加一个空格*/strcat(Before,BLANK);/*把结构体size中的数据转换成字符串写入数组*/strcat(Before,itoa(a.size,temp,10));strcat(Before,BLANK);/*把结构体bold数据写入下标*/strcat(Before,onoff[a.bold]);strcat(Before,BLANK);/*把结构体italic数据写入下标*/strcat(Before,onoff[a.italic]);strcat(Before,BLANK); /*把结构体underline数据写入下标*/strcat(Before,onoff[a.underline]);strcat(Before,BLANK); sourcelen=strlen(Before);D_value=TITLELEN-sourcelen;/*打印标题*/printf("%s\n",TITLE);/*调整对齐方式*/if(alignment=='l'){/*左对齐直接打印*/strcpy(After,Before); }if(alignment=='c'){/*居中对齐,前面填充一半空格*/for(i=0;i<D_value/2;i++){strcat(After,BLANK);}strcat(&After[i],Before);}if(alignment=='r'){/*右对其方式,前面填充空格*/for(i=0;i<D_value;i++){strcat(After,BLANK);}strcat(&After[i],Before); }printf("%s\n",After);printf("\n");
}void Menu(void)
{printf("f) change font s) change size a) changge alignment\n");printf("b) toggle bold i) toggle italic u) toggle underline\n");printf("q) quit\n");
}void endline(void)
{int ch;while((ch=getchar())!='\n')continue;
}
7.编写一个与练习6所描述的功能相同的程序,使用一个unsigned long来保存字体信息,使用位运算符而不是位成员来管理这些信息。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>#define MASK_ID 0x3ff00
#define MASK_SIZE 0x380ff const char *ALIGNMENT[3]={"left ","center ","right "};
const char *onoff[2]={"off ","on "};
const char *TITLE="ID SIZE ALIGNMENT B I U";
const char *BLANK=" ";
const int TITLELEN=strlen(TITLE); void Menu(void);
void endline(void);
void show(unsigned long set,char alignment);int main(void)
{int mask=01;unsigned long set=3073;char alignment='l';char choice;show(set,alignment);Menu();printf("Enter a label to perform process (q to quit):\n");choice=getchar();endline();while(choice!='q'){switch(choice){int value;case 'f':printf("Enter Font ID (0~255):\n");/*检查输入合法性*/while(scanf("%d",&value)!=1 || value<0 || value>255){endline();printf("Illegal input.Please enter a number (0~255):\n");}endline();/*ID位于0~7位。先清零*/set=set&MASK_ID;/*设置ID的值*/set=set|value;break;case 's':printf("Enter Font size (0~127):\n");/*检查输入合法性*/while(scanf("%d",&value)!=1 || value<0 || value>127){endline();printf("Illegal input.Please enter a number (0~127):\n");}endline();/*SIZE位置0*/set=set&MASK_SIZE;/*设置SIZE位的值*/value<<=8;set=set|value; break;case 'a':printf("Select alignment:\n");printf("l) left c)center r)right\n");alignment=getchar();endline();while(alignment!='l'&&alignment!='c' &&alignment!='r'){printf("Illegal input.Please enter a character like l or c or r:\n");alignment=getchar();endline();}break;case 'b':mask=01;mask=mask<<15;set=set^mask;break;case 'i':mask=01;mask=mask<<16;set=set^mask;break;case 'u':mask=01;mask=mask<<17;set=set^mask;break;default :printf("Enter right labels.\n");}show(set,alignment);Menu();printf("Enter a label to perform process (q to quit):\n");choice=getchar();endline();printf("%ld\n",set);}}void Menu(void)
{printf("f) change font s) change size a) changge alignment\n");printf("b) toggle bold i) toggle italic u) toggle underline\n");printf("q) quit\n");
}void endline(void)
{int ch;while((ch=getchar())!='\n')continue;
}
void show(unsigned long set,char alignment)
{}
本文标签: 158 编程练习
版权声明:本文标题:15.8 编程练习 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1732357298h1534801.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论