測試結果:
系統呼叫了 abort 函式! *** Error in `/4129990/code_4129990.exe': free(): invalid pointer: 0x00007f67e97639a0 *** ======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f67e8b737e5] /lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7f67e8b7c37a] /lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f67e8b8053c] /4129990/code_4129990.exe[0x400fb6] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f67e8b1c830] /4129990/code_4129990.exe[0x401259] ======= Memory map: ======== 00400000-00402000 r-xp 00000000 fc:00 3677354 /4129990/code_4129990.exe 00601000-00602000 r--p 00001000 fc:00 3677354 /4129990/code_4129990.exe 00602000-00603000 rw-p 00002000 fc:00 3677354 /4129990/code_4129990.exe 0258b000-025bd000 rw-p 00000000 00:00 0 [heap] 7f67e8afc000-7f67e8cbc000 r-xp 00000000 fc:00 14029440 /lib/x86_64-linux-gnu/libc-2.23.so 7f67e8cbc000-7f67e8ebc000 ---p 001c0000 fc:00 14029440 /lib/x86_64-linux-gnu/libc-2.23.so 7f67e8ebc000-7f67e8ec0000 r--p 001c0000 fc:00 14029440 /lib/x86_64-linux-gnu/libc-2.23.so 7f67e8ec0000-7f67e8ec2000 rw-p 001c4000 fc:00 14029440 /lib/x86_64-linux-gnu/libc-2.23.so 7f67e8ec2000-7f67e8ec6000 rw-p 00000000 00:00 0 7f67e8ec6000-7f67e8edc000 r-xp 00000000 fc:00 14029454 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f67e8edc000-7f67e90db000 ---p 00016000 fc:00 14029454 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f67e90db000-7f67e90dc000 rw-p 00015000 fc:00 14029454 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f67e90dc000-7f67e91e4000 r-xp 00000000 fc:00 14029463 /lib/x86_64-linux-gnu/libm-2.23.so 7f67e91e4000-7f67e93e3000 ---p 00108000 fc:00 14029463 /lib/x86_64-linux-gnu/libm-2.23.so 7f67e93e3000-7f67e93e4000 r--p 00107000 fc:00 14029463 /lib/x86_64-linux-gnu/libm-2.23.so 7f67e93e4000-7f67e93e5000 rw-p 00108000 fc:00 14029463 /lib/x86_64-linux-gnu/libm-2.23.so 7f67e93e5000-7f67e9557000 r-xp 00000000 fc:00 14158321 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21 7f67e9557000-7f67e9757000 ---p 00172000 fc:00 14158321 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21 7f67e9757000-7f67e9761000 r--p 00172000 fc:00 14158321 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21 7f67e9761000-7f67e9763000 rw-p 0017c000 fc:00 14158321 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21 7f67e9763000-7f67e9767000 rw-p 00000000 00:00 0 7f67e9767000-7f67e978d000 r-xp 00000000 fc:00 14029428 /lib/x86_64-linux-gnu/ld-2.23.so 7f67e997c000-7f67e9982000 rw-p 00000000 00:00 0 7f67e998a000-7f67e998c000 rw-p 00000000 00:00 0 7f67e998c000-7f67e998d000 r--p 00025000 fc:00 14029428 /lib/x86_64-linux-gnu/ld-2.23.so 7f67e998d000-7f67e998e000 rw-p 00026000 fc:00 14029428 ...訊息太長省略。
------------------------------------------------------------------------------------------------------------------------------------------------------------
程式碼:
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int roma_n(char ch) //羅馬轉數字
{
switch(ch)
{
case 'I':
return 1;
break;
case 'V':
return 5;
break;
case 'X':
return 10;
break;
case 'L':
return 50;
break;
case 'C':
return 100;
break;
case 'D':
return 500;
break;
case 'M':
return 1000;
break;
}
}
string roma_r(int a)//數字轉羅馬
{
switch(a)
{
case 1:
return "I";
break;
case 2:
return "II";
break;
case 3:
return "III";
break;
case 4:
return "IV";
break;
case 5:
return "V";
break;
case 6:
return "VI";
break;
case 7:
return "VII";
break;
case 8:
return "VIII";
break;
case 9:
return "IX";
break;
case 10:
return "X";
break;
case 20:
return "XX";
break;
case 30:
return "XXX";
break;
case 40:
return "XL";
break;
case 50:
return "L";
break;
case 60:
return "LX";
break;
case 70:
return "LXX";
break;
case 80:
return "LXXX";
break;
case 90:
return "XC";
break;
case 100:
return "C";
break;
case 200:
return "CC";
break;
case 300:
return "CCC";
break;
case 400:
return "CD";
break;
case 500:
return "D";
break;
case 600:
return "DC";
break;
case 700:
return "DCC";
break;
case 800:
return "DCCC";
break;
case 900:
return "CM";
break;
case 1000:
return "M";
break;
case 2000:
return "MM";
break;
case 3000:
return "MMM";
break;
}
}
int main()
{
string a[2];//兩個羅馬數字
while(cin >> a[0])
{
if(a[0][0]!='#')
{
cin >> a[1];
int sum[2];//兩個羅馬數字轉數字的結果
int n;
int ans;//兩個羅馬數字轉數字後相減的結果(必為正)
int t=0;//計算ans位數
int x;
sum[0]=0;
sum[1]=0;
for(int i=0;i<2;i++)
{
n=0;
while(a[i][n]!='\0')//輸入的數字中 從左開始兩數為一組 當一組數字中的左數為空字元則停(表示到底了)
{
int x,y;
x=roma_n(a[i][n]);//左數
y=roma_n(a[i][n+1]);//右數
if(a[i][n+1]=='\0')//當輸入羅馬數字為奇數時 左數會有數字 右數為空字元 要把左數加入
{
sum[i]+=x;
}
if(x>=y)//左數大於右數或左數等於右數 兩數相加
{
sum[i]+=(x+y);
}
else if(x<y)//左數小於右數 大數(右數)減小數(左數)
{
sum[i]+=(y-x);
}
n+=2;//輸入的羅馬數字中 從左開始兩數為一組
}
}
ans= sum[0]-sum[1];
if(ans==0)
{
cout << "ZERO" << endl;
}
else if(ans!=0)
{
if(ans<0)
{
ans= -ans;
}
x=ans;
while(x!=0)
{
x/=10;
t++;
}
string str[t];
int j=0;
while(ans!=0)//相減後的數字轉羅馬
{
str[j]=roma_r((ans%10)*pow(10,j));
ans/=10;
j++;
//cout << "j=" << j << "str=" << str[j-1] << endl;
}
for(int k=t-1;k>=0;k--)
{
cout << str[k];
}
cout << endl;
}
}
else if(a[0][0]=='#')//輸入#終止程式
{
break;
}
}
return 0 ;
}