本文共 4515 字,大约阅读时间需要 15 分钟。
#include#include //肖战,nmslint main(){ printf("Domain of signed char is [%d, %d].\n",SCHAR_MIN,SCHAR_MAX); printf("Domain of unsigned char is [%u, %u].\n",0,UCHAR_MAX); printf("Domain of char is [%d, %d].\n",CHAR_MIN,CHAR_MAX); printf("Domain of signed short int is [%hd, %hd].\n",SHRT_MIN,SHRT_MAX); printf("Domain of unsigned short int is [%hu, %hu].\n",0,USHRT_MAX); printf("Domain of signed int is [%d, %d].\n",INT_MIN,INT_MAX); printf("Domain of unsigned int is [%u, %u].\n",0,UINT_MAX); printf("Domain of signed long int is [%ld, %ld].\n",LONG_MIN,LONG_MAX); printf("Domain of unsigned long int is [%lu, %lu].\n",0,ULLONG_MAX); return 0;}
#include#define MAXLINE 1024int main(){ char line[MAXLINE]; int c,i; i=0; while(i
#include#define MAXLINE 1024int get_line(char s[],int lim){ int c,i,l; for(i=0,l=0;(c=getchar())!=EOF&&c!='\n';++i) if(i ='0'&&c<='9') n+=c-'0'; else if(c>='a'&&c<='f') n+=c-'a'; else if(c>='A'&&c<='F') n+=c-'A'; } return n;}int main(){ int len; char line[MAXLINE]; while((len=get_line(line,MAXLINE))>0) printf("%lu\n",htoi(line)); return 0;}
#include#define MAXLINE 1024int get_line(char line[], int maxline);void squeeze(char s1[], char s2[]);int main(void){ int len; char s1[MAXLINE]; char s2[MAXLINE]; printf("Input string s1:\n"); while ((len = get_line(s1, MAXLINE)) == 0) ; printf("Input string s2:\n"); while ((len = get_line(s2, MAXLINE)) == 0) ; squeeze(s1, s2); printf("Result is %s\n", s1); return 0;}int get_line(char s[], int lim){ int c, i, l; for (i = 0, l = 0; (c = getchar()) != EOF && c != '\n'; ++i) if (i < lim - 1) s[l++] = c; s[l] = '\0'; return l;}void squeeze(char s1[], char s2[]){ int i=0;//s1 ptr char tmp[MAXLINE]; int k=0;//tmp ptr while(s1[i]!='\0'){ int find_flag = 0; int j=0;//s2 ptr while(s2[j]!='\0'){ if(s1[i]==s2[j]){ //match! find_flag=1; break; } j++; } if(find_flag==0){ tmp[k]=s1[i]; k++; } i++; } tmp[k]='\0'; for(int ii=0;ii
#include#define MAXLINE 1024int get_line(char line[], int maxline);void squeeze(char s1[], char s2[]);int main(void){ int len; char s1[MAXLINE]; char s2[MAXLINE]; printf("Input string s1:\n"); while ((len = get_line(s1, MAXLINE)) == 0) ; printf("Input string s2:\n"); while ((len = get_line(s2, MAXLINE)) == 0) ; squeeze(s1, s2); printf("Result is %s\n", s1); return 0;}int get_line(char s[], int lim){ int c, i, l; for (i = 0, l = 0; (c = getchar()) != EOF && c != '\n'; ++i) if (i < lim - 1) s[l++] = c; s[l] = '\0'; return l;}int any(char s1[], char s2[]){ int i, j; i = 0; while (s2[i] != '\0') { j = 0; while (s1[j] != '\0') { if (s1[j] == s2[i]) return j; ++j; } ++i; } return -1;}
#includeunsigned setbits(unsigned x,int p,int n,unsigned y){ unsigned maskA = x&~(~(~0U< <<(p+1-n)); unsigned maskB = (y&~(~0U< <<(p+1-n); return maskA|maskB;}int main(){ printf("%u\n",setbits(114514,1,9,19810)); return 0;}
#includeunsigned invert(unsigned x,int p,int n){ unsigned mask = ~(~0U< <<(p+1-n); return x^mask;}int main(){ printf("%u\n",invert(1145,19,14)); return 0;}
#includeunsigned rightrot(unsigned x,int n){ while(n-- >0) if(x&1) x = (x>>1)|~(~0U>>1); else x = x>>1; return x;}int main(){ printf("%u\n",rightrot(1145,1)); return 0;}
原理:x-1在二进制上相当于把最右边为1的位置零,然后把这一位之后的所有位置1
#includeint bitcount(unsigned x){ int bits; for(bits=0;x!=0;x&=(x-1)) bits++; return bits;}int main(){ printf("%d\n",bitcount(114514)); return 0;}
#includeint lower(int c){ return (c>='A'&&c<='Z')?c+'a'-'A':c;}int main(){ char jj[] = "YESHOUXIANBEI"; int i = 0; while(jj[i]!='\0'){ printf("%c--->%c\n",jj[i],lower(jj[i])); i++; } return 0;}
转载地址:http://nfwai.baihongyu.com/