Aborting when trying to reading and writing bmp files c++ - c++

this is my first post in stackoverflow.
I write a program that should take a bmp file in input, and black and white it and then write it into the out.bmp.
when I started to write the code, i delete the bmp file format at the end of the name of input and then open it with text editor, then write the code, and the output style is like the input.
when I type ./a.out <in.bmp>out.bmp in terminal, I get an Abort error (Aborted (core dumped)) and when I give the ./a.out < in > out.bmp gimp say to me it is not a bmp file.
here is the code:
// In the Name of God
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <math.h>
#include <sstream>
using namespace std;
bool TypeIsTrue (string type){
if (type == "424d")
return true;
return false;
}
int GrayIt (int b , int g , int r){
return (b + g + r) / 3 ;
}
int ConvertToDec(string hex){
long int dec = 0;
reverse(hex.begin(), hex.end());
for (int i = 0 ; i <hex.size();i++){
if (hex[i] == 'a')
dec = dec + 10 *(pow(16,i));
else if (hex[i] == 'b')
dec = dec + 11 *(pow(16,i));
else if (hex[i] == 'c')
dec = dec + 12 *(pow(16,i));
else if (hex[i] == 'd')
dec = dec + 13 *(pow(16,i));
else if (hex[i] == 'e')
dec = dec + 14 *(pow(16,i));
else if (hex[i] == 'f')
dec = dec + 15 *(pow(16,i));
else
dec = dec + ((hex[i] - '0')*(pow(16,i)));
}
return dec;
}
string ConvertToHex(int dec){
string hex;
int reminded,Divided;
reminded = dec % 16 ;
Divided = dec / 16;
if (Divided == 10){
hex = "a";
}
else if (Divided == 11){
hex = "b";
}
else if (Divided == 12){
hex = "c";
}
else if (Divided == 13){
hex = "d";
}
else if (Divided == 14){
hex = "e";
}
else if (Divided == 15){
hex = "f";
}
else if (Divided == 0){
hex = "0";
}
else if (Divided == 1){
hex = "1";
}
else if (Divided == 2){
hex = "2";
}
else if (Divided == 3){
hex = "3";
}
else if (Divided == 4){
hex = "4";
}
else if (Divided == 5){
hex = "5";
}
else if (Divided == 6){
hex = "6";
}
else if (Divided == 7){
hex = "7";
}
else if (Divided == 8){
hex = "8";
}
else if (Divided == 9){
hex = "9";
}
if (reminded == 10){
hex = hex+"a";
}
else if (reminded == 11){
hex = hex+"b";
}
else if (reminded == 12){
hex = hex+"c";
}
else if (reminded == 13){
hex = hex+"d";
}
else if (reminded == 14){
hex = hex+"e";
}
else if (reminded == 15){
hex = hex+"f";
}
else if (reminded == 0){
hex = hex+"0";
}
else if (reminded == 1){
hex = hex+"1";
}
else if (reminded == 2){
hex = hex+"2";
}
else if (reminded == 3){
hex = hex+"3";
}
else if (reminded == 4){
hex = hex+"4";
}
else if (reminded == 5){
hex = hex+"5";
}
else if (reminded == 6){
hex = hex+"6";
}
else if (reminded == 7){
hex = hex+"7";
}
else if (reminded == 8){
hex = hex+"8";
}
else if (reminded == 9){
hex = hex+"9";
}
return hex;
}
int main (){
vector <string> a;
vector <string> r;
vector <string> g;
vector <string> b;
vector <string> out;
string temp;
int red,green,blue;
while(cin >> temp){
a.push_back (temp);
}
if(!TypeIsTrue(a[0])){
cout<<"The file is not bmp\nRerun program"<<endl;
abort();
}
int phase = 1;
for (int i = 27 ; i < a.size(); i++){ //int i = 27
string first;
string last;
first = a[i].substr(0,2);
last = a[i].substr(2,3);
if(phase == 4)
phase = 1;
if(phase == 1){
b.push_back(first);
g.push_back(last);
phase ++;
// cout<<"push_backed"<<endl;
}
else if(phase == 2){
r.push_back(first);
b.push_back(last);
phase ++;
// cout<<"push_backed"<<endl;
}
else if(phase == 3){
g.push_back(first);
r.push_back(last);
phase ++;
// cout<<"push_backed"<<endl;
}
}
for (int i = 0 ; i <27 ; i++){
out.push_back(a[i]);
}
for(int i = 27 ; i<b.size() ; i++){
blue = ConvertToDec(b[i]);
green = ConvertToDec(g[i]);
red = ConvertToDec(r[i]);
out.push_back ( ConvertToHex( GrayIt (blue , green , red)));
out.push_back ( ConvertToHex( GrayIt (blue , green , red)));
out.push_back ( ConvertToHex( GrayIt (blue , green , red)));
}
int j = 1 ;
for (int i = 0 ; i < 27 ; i++){
cout<< out[i] << " ";
if (j == 8){
cout<<endl;
j = 0;
}
j++;
}
j=1;
bool space = false;
for (int i = 27 ; i < out.size(); i++){
if( i == 27 + 10){
cout<<endl;
j = 1;
}
cout<<out[i];
if (space)
cout<<" ";
j++;
if(j == 17){
cout<<endl;
j = 1 ;
}
space=!space;
}
return 0;
}

You're getting an abort error because you asked for one.
if(!TypeIsTrue(a[0])){
cout<<"The file is not bmp\nRerun program"<<endl;
abort();
}
If your program is designed to have its output redirected, it is very important to send error messages to stderr (and std::cerr or std::clog) and not stdout.
BTW, the type test is failing because BMP files are not text, it makes no sense to read them using cin >> variable.
In addition, there's no guarantee that a[0] even exists. You need to test a.size() first.
if(a.size() < 1 || !TypeIsTrue(a[0])){
cerr << "The file is not bmp\nRerun program\n";
abort();
}

Related

How do I get this binary addition program in c++ to work?

I am trying to create a program that takes in 2 arrays in binary form and calculates the addition of said arrays, although I keep getting the error:
"munmap_chunk(): invalid pointer
Aborted (core dumped) "
The code is below
#include <iostream>
#include <string>
using namespace std;
string binaryAddition(int num1[], int num2[]) {
// BINARY 1: 111011101011
// BINARY 2: 000000000011
int carry = 0;
string res = "";
for (int i = 11; i >= 0; i--) {
if (num1[i] == 0 && num2 == 0 && carry == 0) {
res += "1";
} else if (num1[i] == 0 && num2[i] == 1 && carry == 0) {
res += "1";
} else if (num1[i] == 1 && num2[i] == 0 && carry == 0) {
res += "1";
} else if (num1[i] == 1 && num2[i] == 1 && carry == 0) {
res += "0";
carry = 1;
} else if (num1[i] == 0 && num2[i] == 0 && carry == 1) {
res += "1";
carry = 0;
} else if (num1[i] == 0 && num2[i] == 1 && carry == 1) {
res += "0";
carry = 1;
} else if (num1[i] == 1 && num2[i] == 0 && carry == 1) {
res += "0";
carry = 1;
} else if (num1[i] == 1 && num2[i] == 1 && carry == 1) {
res += "1";
carry = 1;
}
}
cout << res;
}
int main() {
int num1[12] = {1,1,1,0,1,1,1,0,1,0,1,1};
int num2[12] = {0,0,0,0,0,0,0,0,0,0,1,1};
binaryAddition(num1, num2);
}
Would appreciate some help, Thankyou!
You can have to calculate the result least significant bit to most significant bit to carry the bit forward. When you print it then you want go the opposite direction:
#include <iostream>
#include <string>
using namespace std;
string binaryAddition(int num1[], int num2[]) {
int num3[12] = {};
int carry = 0;
for(int i = 11; i >= 0; i--) {
int sum = num1[i] + num2[i] + carry;
num3[i] = sum % 2;
carry = sum / 2;
}
string res = "";
for(int i = 0; i < 12; i++) {
res += num3[i] ? "1" : "0";
}
return res;
}
int main() {
int num1[] = {1,1,1,0,1,1,1,0,1,0,1,1};
int num2[] = {0,0,0,0,0,0,0,0,0,0,1,1};
cout << binaryAddition(num1, num2) << "\n";
}
I suggest to use bitsets instead. Here is a good example how it could be done
https://alikhuram.wordpress.com/2013/05/15/performing-arithmetic-on-bitsets-in-c/
You set your binaryAddition function to be returning a string but it never returns. So, there are two ways you can do. Either return your res and then cout it in the main:
string binaryAddition(int num1[], int num2[]) {
...
return res;
}
int main() {
...
string res = binaryAddition(num1, num2);
cout << res;
...
}
Or, you can change its return type to void:
void binaryAddition(int num1[], int num2[]) {
... // keep the rest
cout << res;
}
Well, you'll need to debug your binaryAddition function and reverse the string too.

long conversion C++ arduino

I have this error, and I can't figure this out.
cannot convert 'String' to 'long int' in assignment
I want to send all RC5 codes beetween 0x00 and 0xFF by IR led and arduino.
I am using IRremote
Here is my code :
for(int i = 0;i < 16 ;i++){
value = i;
if(i == 10){
value = "a";
}
if(i == 11){
value = 'b';
}
if(i == 12){
value = 'c';
}
if(i == 13){
value = 'd';
}
if(i == 14){
value = 'e';
}
if(i == 15){
value = 'f';
}
for(int j = 0;j < 16 ;j++){
value2 = j;
if(j == 10){
value2 = "a";
}
if(j == 11){
value2 = 'b';
}
if(j == 12){
value2 = 'c';
}
if(j == 13){
value2 = 'd';
}
if(j == 14){
value2 = 'e';
}
if(j == 15){
value2 = 'f';
}
valueTotal = "0x" + value + value2;
toSend = valueTotal;
irsend.sendRC5(toSend , 12);
delay(20);
} }
Assuming you're using this library https://github.com/z3t0/Arduino-IRremote/blob/master/IRremote.h , sendRC5 takes an unsigned long argument. You either seem to have got confused because of examples using hexadecimal literals into thinking it requires a string, or need to send your string as multiple words.
Assuming the former, something like this sends all the codes:
for (int i = 0x0; i <= 0xff ; ++i) {
irsend.sendRC5(i, 12);
delay(20);
}

C++ Arabic numbers to Roman Numeral's & vice versa

I am writing a C++ program that converts between Arabic and Roman numbering systems. I wrote one program that converts Arabic to Roman and have another program that converts Roman to Arabic.
The problem is that I can't figure out how to merge them into one single program so that the user can input an Arabic or Roman number and as a result, the program would convert said number to the other.
My question is HOW can I merge these two programs into one?
Code for Roman to Arabic
#include <iostream>
using namespace std;
int main()
{
char roman_Numeral;
int arabic_Numeral = 0;
cout << "Enter the Roman Numeral in Capital letters (e.g. CCXIX) : ";
while (cin.get(roman_Numeral))
{
if (arabic_Numeral > 100)
{
cout << "\nInvalid Value. Number must be between I and C" << endl;
return 0;
}
else if (roman_Numeral == 'C')
{
roman_Numeral = cin.peek();
if (roman_Numeral == 'M' || roman_Numeral == 'D')
{
arabic_Numeral = arabic_Numeral - 100;
}
else
{
arabic_Numeral = arabic_Numeral + 100;
}
}
else if (roman_Numeral == 'L')
{
roman_Numeral = cin.peek();
if (roman_Numeral == 'M' || roman_Numeral == 'D'
|| roman_Numeral == 'C')
{
arabic_Numeral = arabic_Numeral - 50;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 50;
continue;
}
}
else if (roman_Numeral == 'X')
{
roman_Numeral = cin.peek();
if (roman_Numeral == 'M' || roman_Numeral == 'D'
|| roman_Numeral == 'C' || roman_Numeral == 'L')
{
arabic_Numeral = arabic_Numeral - 10;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 10;
continue;
}
}
else if (roman_Numeral == 'V')
{
roman_Numeral = cin.peek();
if (roman_Numeral == 'M' || roman_Numeral == 'D'
|| roman_Numeral == 'C' || roman_Numeral == 'L'
|| roman_Numeral == 'X')
{
arabic_Numeral = arabic_Numeral - 5;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 5;
continue;
}
}
else if (roman_Numeral == 'I')
{
roman_Numeral = cin.peek();
if (roman_Numeral == 'M' || roman_Numeral == 'D'
|| roman_Numeral == 'C' || roman_Numeral == 'L'
|| roman_Numeral == 'X' || roman_Numeral == 'V')
{
arabic_Numeral = arabic_Numeral - 1;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 1;
continue;
}
}
else
break;
}
cout << arabic_Numeral << endl;
return 0;
}
Code for Arabic to Roman
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
#define MAX_INPUT 100 // These constants hold high and low integer numbers,
#define MIN_INPUT 1
#define ARRAY_SIZE 4 // and the array size declarator.
string convert(int digit, string low, string mid, string high);
int main()
{
string answers[ARRAY_SIZE] = { "", "", "", "" }; //An array of string to hold the output from the convert function.
int accumulator = 0; // Variable to hold number of arabic numbers converted.
int userNum = 0;
string strUserNum;
do
{
cout << "";
cout << "Enter an arabic number between 1 and 100: ";
cin >> strUserNum;
userNum = std::stoi(strUserNum);
if (userNum == 0 || userNum > MAX_INPUT)
{
cout << "\nInvalid Value. Number must be between 1 and 100" << endl;
return 0;
}
else if (userNum == 'end')
{
cout << "Exiting program:";
break;
}
int thous = userNum / 1000;
int hund = userNum % 1000 / 100;
int tens = userNum % 100 / 10;
int ones = userNum % 10 / 1;
answers[0] = convert(thous, "M", "M", "M");
answers[1] = convert(hund, "C", "D", "M");
answers[2] = convert(tens, "X", "L", "C");
answers[3] = convert(ones, "I", "V", "X");
cout << answers[0] << answers[1] << answers[2];
cout << answers[3] << endl;
cout << endl;
break;
} while (userNum != 'end');
system("PAUSE");
return 0;
}
string convert(int digit, string low, string mid, string high)
{
if (digit == 1)
{
return low;
}
if (digit == 2)
{
return low + low;
}
if (digit == 3)
{
return low + low + low;
}
if (digit == 4)
{
return low + mid;
}
if (digit == 5)
{
return mid;
}
if (digit == 6)
{
return mid + low;
}
if (digit == 7)
{
return mid + low + low;
}
if (digit == 8)
{
return mid + low + low + low;
}
if (digit == 9)
{
return low + high;
}
if (digit == 0)
{
return "";
}
}
Put code (that inside the main block) for Roman to Arabic into void procedureRomanToArabic()
The same for Roman to Arabic. Put it into void procedureArabicToRoman().
Remove remove ArabicToRoman do-while and move it to main.
void procedureRomanToArabic(){
char roman_Numeral;
int arabic_Numeral = 0;
//...
cout << arabic_Numeral << endl;
}
void procedureArabicToRoman()
{
string answers[ARRAY_SIZE] = { "", "", "", "" }; //An array of string to hold the output from the convert function.
int accumulator = 0; // Variable to hold number of arabic numbers converted.
int userNum = 0;
string strUserNum;
//remove do while here
cout << "";
cout << "Enter an arabic number between 1 and 100: ";
cin >> strUserNum;
//...
answers[2] = convert(tens, "X", "L", "C");
answers[3] = convert(ones, "I", "V", "X");
cout << answers[0] << answers[1] << answers[2];
cout << answers[3] << endl;
cout << endl;
}
int main(){
char type;
do{
cout<<"2 for Arabic to Roman, 1 for Roman to Arabic, or anything else for exit";
cin>>type;
if(type=='1')
procedureArabicToRoman();
else if(type =='2')
procedureRomanToArabic();
else break; //end program
} while(true);
}

Having issues converting numbers into roman numerals

for class I have to create a code so that it could ask the user for an integer between 1-99 and be able to print that integer in roman numerals. Issue is after creating my code it would only print the numbers fully up to 39. Once it hits 40 it give's no Roman Numeral output and then from 41-99 it wont print the tenth place value(Ex. 45 will come out as V). Here's my code at the moment.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int num;
int tenum;
int remnum;
string romnum;
string fromnum;
cout << "Enter a number between 1 and 99. \n";
cin >> num;
tenum = num / 10;
remnum = num % 10;
if (tenum == 9)
{
fromnum == "XC";
}
else if (tenum == 8)
{
fromnum == "LXXX";
}
else if (tenum == 7)
{
fromnum == "LXX";
}
else if (tenum == 6)
{
fromnum == "LX";
}
else if (tenum == 5)
{
fromnum == "L";
}
else if (tenum == 4)
{
fromnum == "XL";
}
else if (tenum == 3)
{
fromnum = "XXX";
}
else if (tenum == 2)
{
fromnum == "XX";
}
else if (tenum == 1)
{
fromnum == "X";
}
if (remnum == 9)
{
romnum = "IX";
}
else if (remnum == 8)
{
romnum = "VIII";
}
else if (remnum == 7)
{
romnum = "VII";
}
else if (remnum == 6)
{
romnum = "VI";
}
else if (remnum == 5)
{
romnum = "V";
}
else if (remnum == 4)
{
romnum = "IV";
}
else if (remnum == 3)
{
romnum = "III";
}
else if (remnum == 2)
{
romnum = "II";
}
else if (remnum == 1)
{
romnum = "I";
}
cout << tenum << endl;
cout << remnum << endl;
cout << fromnum;
cout << romnum << endl;
return 0;
}
You've mixed up == and =
change lines like
fromnum == "XL"
to
fromnum = "XL"
Demo
(I used a switch statement for brevity)
same answer with: Converting integer to Roman Numeral
#include <iostream>
#include <string>
std::string to_roman(int value)
{
struct romandata_t { int value; char const* numeral; };
static romandata_t const romandata[] =
{ 1000, "M",
900, "CM",
500, "D",
400, "CD",
100, "C",
90, "XC",
50, "L",
40, "XL",
10, "X",
9, "IX",
5, "V",
4, "IV",
1, "I",
0, NULL }; // end marker
std::string result;
for (romandata_t const* current = romandata; current->value > 0; ++current)
{
while (value >= current->value)
{
result += current->numeral;
value -= current->value;
}
}
return result;
}
int main()
{
for (int i = 1; i <= 4000; ++i)
{
std::cout << to_roman(i) << std::endl;
}
}
this code converts up to 1000. just take what you need and you are good to go!
from http://rosettacode.org/wiki/Roman_numerals/Encode#C.2B.2B

c++ Converting roman numerals to decimals

This program is a part of an exam I just took, that I had to write. I only got this far and couldn't get anywhere. Here is the prompt:"Write a Test Function toDecimal() that converts a roman numeral such as MMLXVII to it's decimal number representation. Use Main() to test the function. The toDecimal() function should have 2 arguments, the string array of roman numerals and a helper function. This helper function will return the numeric value of each of the letters used in roman numbers. Then convert the string arguments as so: Look at the first two characters,if the first is larger, convert the first and add it to the summation, then call the conversion function again with the second value and add both. IF the first character is lesser than the second subtract the first from the second, and add the result to the conversion of the string. without validation it will also convert strings like "IC". VAlidate the string arguement, if there is an error, call the error processing function. Provide at least two error processing functions and test toDecimal() with each. One could be adking the user to correct, the other may correct it."
I,X,C,M cannot be repeated more than 3 times in succession, D,L,V, can never be repeated in succession.I can only be subtracted from V and X,X can only be subtracted from L and C, C can only be subtracted from D and M. V, L, and D can never be subtracted.
I've lost about 2 days worth of sleep on this, tried writing it hundreds of different ways using and breaking the rules. This is the closest I've got on it.
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <cstring>
using namespace std;
bool checker(string roman);
// Adds each value of the roman numeral together
int toDecimal(string, bool* (*function)(string));
int convert(string roman, int i);
int main(){
string roman;
cout << "This program takes a roman numeral the user enters then converts it to decimal notation." << endl;
cout << "Enter a roman numeral: ";
cin >> roman;
transform(roman.begin(), roman.end(), roman.begin(), toupper);
cout << roman << " is equal to " << toDecimal(roman, *checker(roman)) << endl;
}
bool checker(string roman){
int length = roman.length();
for (int count = 0; count < length; count++){
string sub = roman.substr(count, count);
if(sub != "I" || sub != "V" || sub != "X" || sub != "L" || sub != "C" || sub != "D" || sub != "M"){
cout << "Error. Try Again"<< endl;
return false;
}
else if(convert(roman, count) == convert(roman, count-1) && convert(roman, count) == convert(roman, count+1)){
if (convert(roman,count) == 1 || convert(roman,count) == 10 || convert(roman,count) == 100 || convert(roman,count) == 1000)
if(convert(roman, count-1) == convert(roman, count-2) || convert(roman, count+1) == convert(roman, count+2)){
cout << "Error Try again" << endl;
return false;
}
else if (convert(roman,count) == 5 || convert(roman,count) == 50 || convert(roman,count) == 500){
cout << "Error Try again" << endl;
return false;
}
else return true;
}
}
return true;
}
int toDecimal(string s, bool*(checker) (string roman)){
/**map<char, int> roman;
roman['M'] = 1000;
roman['D'] = 500;
roman['C'] = 100;
roman['L'] = 50;
roman['X'] = 10;
roman['V'] = 5;
roman['I'] = 1;*/
checker(s);
int res = 0;
for (int i = 0; i < s.length() - 1; ++i){
int num = convert(s,i);
res += num;
/**if (roman[s[i]] < roman[s[i+1]])
res -= roman[s[i]];
else
res += roman[s[i]];
}
res += roman[s[s.size()-1]];*/}
return res;
}
int convert(string roman, int i){
enum romans {I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000};
int num = 0;
char c = roman[0];
switch(c){
case 'M':
num = M; break;
case 'D':
if(i + 1 != roman.size() && roman[i+1] == 'M'){
num = M - D;break;
}
else
num = D; break;
case 'C':
if(i + 1 != roman.size() && roman[i+1] == 'M' || roman[i+1] == 'D'){
if(roman[i+1] == 'M') num = M - C; break;
if(roman[i+1] == 'D') num = D - C; break;
}
else
num = C; break;
case 'L':
if(i + 1 != roman.size() && roman[i+1] == 'M' || roman[i+1] == 'D' || roman[i+1] == 'C'){
if(roman[i+1] == 'M') num = M - L; break;
if(roman[i+1] == 'D') num = D - L; break;
if(roman[i+1] == 'C') num = C - L; break;
}
else
num = L; break;
case 'X':
if(i + 1 != roman.size() && roman[i+1] == 'M' || roman[i+1] == 'D' || roman[i+1] == 'C'|| roman[i+1] == 'L'){
if(roman[i+1] == 'M') num = M - X; break;
if(roman[i+1] == 'D') num = D - X; break;
if(roman[i+1] == 'C') num = C - X; break;
if(roman[i+1] == 'L') num = C - X; break;
}
num = X; break;
case 'V':
if(i + 1 != roman.size() && roman[i+1] == 'M' || roman[i+1] == 'D' || roman[i+1] == 'C'|| roman[i+1] == 'L' || roman[i+1] == 'X'){
if(roman[i+1] == 'M') num = M - V; break;
if(roman[i+1] == 'D') num = D - V; break;
if(roman[i+1] == 'C') num = C - V; break;
if(roman[i+1] == 'L') num = L - V; break;
if(roman[i+1] == 'X') num = X - V; break;
}
num = V; break;
case 'I':
if ( i + 1 != roman.size() && roman[i + 1] != 'I'){
if(roman[i+1] == 'M') num = M - I; break;
if(roman[i+1] == 'D') num = D - I; break;
if(roman[i+1] == 'C') num = C - I; break;
if(roman[i+1] == 'L') num = L - I; break;
if(roman[i+1] == 'X') num = X - I; break;
}
num =1; break;
}
return num;
}
** I have added the help of people on here. This is an edit to show an progress/congress.
This is the code that I use to convert Roman (smaller than 3999) to Integer. You may check if it works for larger numbers.
int romanToInt(string s) {
map<char, int> roman;
roman['M'] = 1000;
roman['D'] = 500;
roman['C'] = 100;
roman['L'] = 50;
roman['X'] = 10;
roman['V'] = 5;
roman['I'] = 1;
int res = 0;
for (int i = 0; i < s.size() - 1; ++i)
{
if (roman[s[i]] < roman[s[i+1]])
res -= roman[s[i]];
else
res += roman[s[i]];
}
res += roman[s[s.size()-1]];
return res;
}
Hope this could help you.
The solution provided by Annie Kim works, but it uses a std::map, querying it several times for the same character, and I fail to see a reason for it.
int convert_roman_digit(char d)
{
switch (d)
{
case 'M': return 1000;
case 'D': return 500;
case 'C': return 100;
case 'L': return 50;
case 'X': return 10;
case 'V': return 5;
case 'I': return 1;
default: throw std::invalid_argument("Invalid digit");
}
}
int roman_to_int(const std::string& roman)
{
int result = 0, last_added = 0;
for (auto it = roman.rbegin(); it != roman.rend(); ++it)
{
const int value = convert_roman_digit(*it);
if (value >= last_added)
{
result += value;
last_added = value;
}
else
{
result -= value;
}
}
return result;
}
Caveat: the function happily accepts some invalid inputs (e.g. IMM) including "negative" numbers (e.g. IIIIIIIIIIIIIX), there are no overflow checks, and it throws. Feel free to improve it.
int romanToInt(string s)
{
unordered_map<char, int> roman;
roman['I'] = 1;
roman['V'] = 5;
roman['X'] = 10;
roman['L'] = 50;
roman['C'] = 100;
roman['D'] = 500;
roman['M'] = 1000;
int num = 0, prev = 0, curr;
for (int i = s.length() - 1; i >= 0; i--)
{
curr = roman[s[i]];
num += (curr >= prev ? 1 : -1) * curr;
prev = curr;
}
return num;
}