罗马数字的转换
本帖最后由 bin554385863 于 2019-7-26 17:10 编辑#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*根据罗马数字转换阿拉伯数字 */
//-I+73 1
//V86 5
//-X+88 10
//L92 50
//-C+67 100
//D68 500
//-M+77 1000
int romanToInt(char *s)
{
int slen = strlen(s);
int sum = 0;
/*--------------定义一个整数数组--------------- */
int *arr = (int *)malloc(sizeof(int) * (slen));
/*--------------转换成整数数组----------------- */
for (size_t i = 0; i < slen; i++)
{
if (s == 'I')
{
arr = 1;
}
else if (s == 'V')
{
arr = 5;
}
else if (s == 'X')
{
arr = 10;
}
else if (s == 'L')
{
arr = 50;
}
else if (s == 'C')
{
arr = 100;
}
else if (s == 'D')
{
arr = 500;
}
else if (s == 'M')
{
arr = 1000;
}
}
/*-----------调整数组元素的正负号--------------------------------- */
for (size_t i = 0, j = 1; i <slen - 1, j < slen; i++, j = i + 1)
{
if (arr == 1)
{
if (arr < arr)
{
arr = -1;
}
}
if (arr == 10)
{
if (arr < arr)
{
arr = -10;
}
}
if (arr == 100)
{
if (arr < arr)
{
arr = -100;
}
}
}
/*----------------求和---------------------------- */
for (size_t i = 0; i < slen; i++)
{
sum = sum + arr;
}
return sum;
free(arr);
}
int main(int argc, char const *argv[])
{
char str;
gets(str);
int a = romanToInt(str);
printf("%d", a);
}
---------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\C_Cpp_Language Study>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.24.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-tdhf0z3y.spf --stdout=Microsoft-MIEngine-Out-paepwm2z.rgr --stderr=Microsoft-MIEngine-Error-e5zryizh.evl --pid=Microsoft-MIEngine-Pid-vipggfv1.r1h --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
I
1
E:\Administrator\Documents\C_Cpp_Language Study>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.24.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-z5vqnfv0.1lu --stdout=Microsoft-MIEngine-Out-2trhilsp.xug --stderr=Microsoft-MIEngine-Error-5wndy4gq.nl5 --pid=Microsoft-MIEngine-Pid-k4l4qid4.5h4 --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
II
2
E:\Administrator\Documents\C_Cpp_Language Study>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.24.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-n1n2ebsa.xdv --stdout=Microsoft-MIEngine-Out-2jvaqbnc.uot --stderr=Microsoft-MIEngine-Error-z40nmd3p.qiq --pid=Microsoft-MIEngine-Pid-tm0rgura.vph --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
VI
6
E:\Administrator\Documents\C_Cpp_Language Study>
------------------------------------------------------------------------------------
看起来代码很短吧?我尼玛花了三天的时间才写出来(╯‵□′)╯︵┻━┻
这是人干的???
当然代码还不够严谨,算了也懒得改了,反正线上通过了 学个C语言,动脑子比我上十几年的学,死的脑细胞都多
页:
[1]