急用! 。
解决方案:30分
void main() { int a,b; cin>>a; cin>>b; cout<< (a | b) <<endl; //或 cout<< (~a)<<endl;//非 cout<<hex<<a<<endl; //十六进制 cout<<oct<<a<<endl; //八进制 char binary[100]; itoa(a, binary, 2); //转换,这里的2表示2进制(可以是其他) cout<<binary<<endl; }
解决方案:50分
仅供参考:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <locale.h> int main() { int i,v; char bs[33]; char b[33]; char hs[9]; char h[9]; char s[4]; char *e; // 十进制整数转二进制串; i=1024; ltoa(i,b,2); sprintf(bs,"%032s",b); printf("i=%d,bs=%s\n",i,bs); // 十进制整数转十六进制串; i=1024; ltoa(i,h,16); sprintf(hs,"%08s",h); printf("i=%d,hs=%s\n",i,hs); // 十六进制字符串转成十进制数 strcpy(hs,"00000400"); sscanf(hs,"%x",&i); printf("hs=%s,i=%d\n",hs,i); // 二进制字符串转化为十六进制字符串; strcpy(bs,"00000000000000000000010000000000"); i=strtol(bs,&e,2); ltoa(i,h,16); sprintf(hs,"%08s",h); printf("bs=%s,hs=%s\n",bs,hs); // 二进制字符串转化为十进制数; strcpy(bs,"00000000000000000000010000000000"); i=strtol(bs,&e,2); printf("bs=%s,i=%d\n",bs,i); // 十六进制字符串转成二进制串 strcpy(hs,"00000400"); sscanf(hs,"%x",&i); ltoa(i,b,2); sprintf(bs,"%032s",b); printf("hs=%s,bs=%s\n",hs,bs); // ASC\GBK字符串转十六进制串 strcpy(s,"a汉"); i=0; while (1) { if (0==s[i]) break; sprintf(hs+i*2,"%02X",(unsigned char)s[i]); i++; } setlocale(LC_ALL,"chs"); printf("s=%s,hs=%s\n",s,hs); // 十六进制字符串转成汉字(GBK)及字符(ASC) strcpy(hs,"61BABA"); i=0; while (1) { if (1!=sscanf(hs+i*2,"%2x",&v)) break; s[i]=(char)v; i++; } s[i]=0; printf("hs=%s,s=%s\n",hs,s); return 0; } //i=1024,bs=00000000000000000000010000000000 //i=1024,hs=00000400 //hs=00000400,i=1024 //bs=00000000000000000000010000000000,hs=00000400 //bs=00000000000000000000010000000000,i=1024 //hs=00000400,bs=00000000000000000000010000000000 //s=a汉,hs=61BABA //hs=61BABA,s=a汉
解决方案:20分
再供参考:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <locale.h> const int DIGIT=1; const int MAXLEN=16384; char s[MAXLEN],*endss; int calc(char *expr,char **addr) { int ST[10]={0}; //数字栈 char op[10]={""+""}; //运算符栈 char c,*pp; int ist=1,iop=1,last; char cc; pp=expr; last = !DIGIT; if ((c=*pp)==""!"") { op[0] = c; pp++; } while (1) { c=*pp; if (c==0) break; if (c==""("") { ST[ist++]=calc(++pp,addr); pp = *addr; last = DIGIT; } else if (c=="")"") { pp++; break; } else if (c==""+""||c==""*"") { if (last != DIGIT) { printf("运算符粘连\n");exit(5); } pp++; if (c==""+"") { do { cc = op[--iop]; --ist; switch (cc) { case ""+"": ST[ist-1] |= ST[ist];break; case ""*"": ST[ist-1] &= ST[ist];break; case ""!"": ST[ist-1] =1-ST[ist];break; } } while (iop); op[iop++] = c; } else if (c==""*"") { cc = op[iop-1]; if (cc==""+"") { op[iop++] = c; } else { --ist; op[iop-1] = c; switch (cc) { case ""*"": ST[ist-1] &= ST[ist];break; case ""!"": ST[ist-1] =1-ST[ist];break; } } } last = !DIGIT; } else if (c==""!"") { op[iop++] = c; ST[ist++] =0; pp++; } else { if (last == DIGIT) { printf("两数字粘连\n");exit(7); } if (!(pp[0]==""0""||pp[0]==""1"")) { printf("非法字符\n");exit(8); } ST[ist++]=pp[0]-""0""; pp++; last = DIGIT; } } *addr=pp; if (iop>=ist) { printf("表达式有误\n");exit(9); } while (iop) { --ist; switch (op[--iop]) { case ""+"": ST[ist-1] |= ST[ist];break; case ""*"": ST[ist-1] &= ST[ist];break; case ""!"": ST[ist-1] =1-ST[ist];break; } } return ST[0]; } int main() { setlocale(LC_ALL,"chs"); while (1) { printf("请输入布尔表达式,例如!(1+1)*1:");fflush(stdout); rewind(stdin); gets(s); if (s[0]==0) break;// printf("%d\n",calc(s,&endss)); } return 0; }