谁能看看呀,哪错了?
Description
Suppose you are reading byte streams from any device, representing IP addresses. Your task is to convert a 32 characters long sequence of '1s' and '0s' (bits) to a dotted decimal format. A dotted decimal format for an IP address is form by grouping 8 bits at a time and converting the binary representation to decimal representation. Any 8 bits is a valid part of an IP address.
To convert binary numbers to decimal numbers remember that both are positional numerical systems, where the first 8 positions of the binary systems are:
2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
128 64 32 16 8 4 2 1
Input
The input will have a number N (1<=N<=9) in its first line representing the number of streams to convert. N lines will follow.
Output
The output must have N lines with a doted decimal IP address. A dotted decimal IP address is formed by grouping 8 bit at the time and converting the binary representation to decimal representation.
Sample Input
4
00000000000000000000000000000000
00000011100000001111111111111111
11001011100001001110010110000000
01010000000100000000000000000001
Sample Output
0.0.0.0
3.128.255.255
203.132.229.128
80.16.0.1
我写的:
#include<stdio.h>
#include<math.h>
int main()
{
int a,i,j,sum,p;
sum=0;
char b[32];
scanf("%d",&a);
for(;a>0;a--)
{
scanf("%s",b);
for(j=0;j<4;j++)
{
for(i=8;i>0;i--)
{
p=pow(2,i-1);
sum=((int)b[8-i+4*j]-48)*p+sum;
}
printf("%d",sum);
sum=0;
if(j=3)
printf("\n");
else
printf(".");
}
}
return 0;
}
报错信息:
Compiler Error Message
D:EcnuOnlineJudgeJudgefoo.cpp:58:1: error: stray '243' in program
D:EcnuOnlineJudgeJudgefoo.cpp:58:1: error: stray '250' in program
D:EcnuOnlineJudgeJudgefoo.cpp:58:1: error: stray '243' in program
D:EcnuOnlineJudgeJudgefoo.cpp:58:1: error: stray '250' in program
D:EcnuOnlineJudgeJudgefoo.cpp:58:1: error: stray '243' in program
D:EcnuOnlineJudgeJudgefoo.cpp:58:1: error: stray '251' in program
D:EcnuOnlineJudgeJudgefoo.cpp:58:1: error: stray '243' in program
D:EcnuOnlineJudgeJudgefoo.cpp:58:1: error: stray '251' in program
D:EcnuOnlineJudgeJudgefoo.cpp: In function 'int main()':
D:EcnuOnlineJudgeJudgefoo.cpp:58:9: error: expected primary-expression before 'int'
D:EcnuOnlineJudgeJudgefoo.cpp:58:9: error: expected ';' before 'int'
......................................
[解决办法]
估计楼主用了中文字符。。- -
;和;是不同的
[解决办法]
sum=((int)b[8-i+4*j]-48)*p+sum;
不要使用全角的括号
[解决办法]
sum=((int)b[8-i+4*j]-48)*p+sum;//这句括号要用英文的。、。、、
[解决办法]
关于自己是否适合编程的很简单的测试:
在报纸或杂志上随便找一段约1000字的文章,在Word中输入一遍。输完后再参考下面答案:
A里面有10处以上文字或标点错误
B里面没有文字或标点错误并敢为此跟人打赌
C里面没有文字或标点错误并且字体和排版完全与原稿一致
D打印在半透明的纸上和原稿重叠在一起检查一模一样,且自我感觉很有成就感
A不适合编程(理由:打字准确度偏低、粗心大意)
B初级程序员(理由:打字准确度很高、认真细致、自信、理解全角半角概念)
C高级程序员(理由:在B的基础上理解字体和排版也是电脑打印的重要因素、但相比D还不够偏执、精益求精、结果可验证)
D软件项目经理(理由:能针对项目给出令人信服的细致到极点的需求说明和典型测试用例。用户几乎挑不出毛病。专业!)
如果想从A变成B的话,到我的资源http://zhao4zhong1.download.csdn.net/里面下载“适合程序员的键盘练习”