[問題] C語言 二進位轉十進位
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
Dev C++
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
No
問題(Question):
二進位轉十進位 輸入字元數超過10位數會變成負的
餵入的資料(Input):
11111111111
預期的正確結果(Expected Output):
2047
錯誤結果(Wrong Output):
-1015
程式碼(Code):(請善用置底文網頁, 記得排版)
#include<stdio.h>
#include<math.h>
int main()
{
long int input;
long int i,bit;
long int C_result=0;
printf("Input an integer containing only 0s and 1s: ");
scanf("%ld",&input);
for(i=0;input!=0;input/=10)
{
bit=input%2;
C_result=C_result + bit*pow(2,i);
i++;
}
printf("Result of convertion is %ld \n",C_result);
system("Pause");
return(0);
}
補充說明(Supplement):
我覺得是int宣告的長度問題 但是我換成long int 還是無法
有請各位幫忙了~~
--
推
03/12 22:04,
03/12 22:04
→
03/12 22:04,
03/12 22:04
→
03/12 22:04,
03/12 22:04
→
03/12 22:05,
03/12 22:05
→
03/12 22:05,
03/12 22:05
→
03/12 22:06,
03/12 22:06
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.118.232.71
→
03/23 23:48, , 1F
03/23 23:48, 1F
→
03/23 23:58, , 2F
03/23 23:58, 2F
→
03/24 00:20, , 3F
03/24 00:20, 3F
→
03/24 00:25, , 4F
03/24 00:25, 4F
推
03/24 00:38, , 5F
03/24 00:38, 5F
→
03/24 00:49, , 6F
03/24 00:49, 6F
→
03/24 01:00, , 7F
03/24 01:00, 7F
推
03/24 01:05, , 8F
03/24 01:05, 8F
→
03/24 01:41, , 9F
03/24 01:41, 9F
→
03/24 01:43, , 10F
03/24 01:43, 10F
→
03/24 01:59, , 11F
03/24 01:59, 11F
→
03/24 10:05, , 12F
03/24 10:05, 12F
→
03/24 10:11, , 13F
03/24 10:11, 13F
→
03/24 10:12, , 14F
03/24 10:12, 14F
→
03/24 10:30, , 15F
03/24 10:30, 15F
→
03/24 11:04, , 16F
03/24 11:04, 16F
→
03/24 13:33, , 17F
03/24 13:33, 17F
→
03/24 14:24, , 18F
03/24 14:24, 18F
→
03/24 14:26, , 19F
03/24 14:26, 19F
→
03/24 14:27, , 20F
03/24 14:27, 20F
→
03/24 21:36, , 21F
03/24 21:36, 21F
→
03/24 21:37, , 22F
03/24 21:37, 22F
→
03/24 21:47, , 23F
03/24 21:47, 23F
→
03/24 21:48, , 24F
03/24 21:48, 24F
→
03/24 21:49, , 25F
03/24 21:49, 25F
→
03/24 21:50, , 26F
03/24 21:50, 26F
→
03/24 22:01, , 27F
03/24 22:01, 27F
→
03/25 00:22, , 28F
03/25 00:22, 28F
→
03/26 01:10, , 29F
03/26 01:10, 29F
→
03/26 01:11, , 30F
03/26 01:11, 30F
→
03/26 01:11, , 31F
03/26 01:11, 31F