Thursday, September 9, 2010

Y2S2 sharing 3 ( Roman Converter for C )

In Engineering computing assignment. We had asked to do the roman converter program as one of the 3 selection of our assignment. I had took few weeks for roman check function. Roman check function used to make sure the roman input entered was correct. If wrong roman input detected error message will displayed.

So, in this part i just contributed effort in roman check function, others function were done by my group member KAREN. ^_^

Below were my codding for roman converter:

# include <>
# include <>
# include <>


#define NUM 100
int roman_check(const char * roman);
void romankarren(void);
int romantodec (const char * roman);
int loop2();

int main()//Main function

{ int m;
do
{
romankarren();
m=loop2();
}
while(m==1);
{
printf("\nThank you for using my program!\n");
printf("\n--System off--\n\n");
}
return 0;

}//end of main function



void romankarren(void)
{
char roman [BUFSIZ] = {0}; //BUFSIZ is the size of the stdio buffers
//I=1; V=5; X=10; L=50; C=100; D=500; M=1000;
int kr,kr2,i;

printf("\nSelection 3: Roman Number\n");
printf("Welcome to the Selection 3!\n");
printf("Please enter a number in Roman numerals to be converted: ");
fgets(roman, BUFSIZ, stdin);

for(i=0;roman[i];i++)//convert all the input of string go lower case since check_roman used lower case character
{
roman[i]=tolower(roman[i]);}

kr2=roman_check(roman);// call for roman_check() function for checking valid roman input

if(kr2!=0)// if check function return true it will enter the bellow section
{
roman[strlen(roman)-1] = '\0';

kr=romantodec (roman);
if(kr!=0)
{
printf("The Roman Number: %s = %d", roman, romantodec(roman));
printf("\n\n");
}
else
printf("\nInvalid input!\n");
}
}
// In romantodec() function is used to convert roman character to integer.
int romantodec (const char * roman)
{
int result = 0;
int temp = 0;
int prev = 5000; //higher than any single Roman numeral digit
char ans;
int count[100]={0,0,0,0,0,0,0,0};

while ( * roman)
{
switch ( * roman)
{
case 'i':
temp = 1;
count[0]+=1;
break;
case 'v':
temp = 5;
count[1]+=1;
break;
case 'x':
temp = 10;
count[2]+=1;
break;
case 'l':
temp = 50;
count[3]+=1;
break;
case 'c':
temp = 100;
count[4]+=1;
break;
case 'd':
temp = 500;
count[5]+=1;
break;
case 'm':
count[6]+=1;
temp = 1000; break;
}
//below are fomular and calculation for roman number
if (prev >= temp)
{
result += temp;
}
else //prev <>
{
result += (temp - (prev + prev));
}
prev = temp;
++roman;
}
return result;
}
int loop2()
{ int con4;
char con2[5],con3[5]={'Y'},con5[5]={'N'};// set and initialize the con3=Y and con5=N
do{
printf("Do you want to continue entering new value(Y/N):");//ask user enter Y or N
gets(con2);//string input and save in con2

con2[0]=toupper(con2[0]);//convert con2 to uppercaps
if(strcmp(con2,con3)==0)//compare con2 with con3=Y if true return integer con4=1
{
con4=1;
return con4;
}
else//if compare con2 and con 3 above false will enter pahse below
{
if(strcmp(con2,con5)==-1||strcmp(con2,con5)==1)//compare con2 and con5=N if false display error message
//and con4 will equal to 111 to make do-while condtion=true and loop back
{
printf("\nInput error...\n");
con4=111;
}
else//else is con2=N will return 0
{
con4=0;
return con4;
}
}
}
while(con4==111);
}
// This roman check function is created to check the roman input. By setting up 6 section in this function.
//In this function its will start checking roman character from first character.
int roman_check(const char * roman)
{ int a,b=0,c=0,d=0,e=0,l=0,f=0,g=0,cnt=0,cnt2=0,i,cnt3=0,cnt4=0,cnt5=0,cnt6=0,cnt7=0;

//------------------------------------------------------------------------------------------------------------------------------
//section 1:for first section is used to check the every bit of roman input. If not i,v,x,i,c,d and m will return false.
for(i=0;g!=10;i++)
{
switch(roman[i])
{
case 'i':
break;
case 'v':
break;
case 'x':
break;
case 'l':
break;
case 'c':
break;
case 'd':
break;
case 'm':
break;
default: printf("\nInvalid roman input\n");return 0; break;// return false when it's not i,v,x,i,c,d and m
}
g=roman[i+1];
}
//------------------------------------------------------------------------------------------------------------
// section 2:In this section we will do the first bit checking. And this section used to enable the other check section for second bit and above.
// 'a' here was used to direct the compiler to start do checking on second bit on next section
if(roman[0]=='l'){
a=1;
cnt4=4;//enable section check 'l'

}
else{
if(roman[0]=='x'){
a=1;
cnt=1;//enable section check 'x'
}
else{
if(roman[0]=='i')
{
cnt2=1;//enable section check 'i'
a=1;
}
else{
if(roman[0]=='v'){
cnt3=2;//enable section check 'v'
a=1;}
else{
if(roman[0]=='c'){
cnt5=5;//enable section check 'c'
a=1;
}
else{
if(roman[0]=='d'){
cnt6=6;//enable section check 'd'
a=1;
}else{
if(roman[0]=='m'){
cnt7=7;//enable section check 'm'
a=1;
}
}}}}}}
//------------------------------------------------------------------------------------------------------------------------
// section 3:For check 'm'
//code used for prompt roman input will have 'LF' character at the end of every input.If we convert it into integer we will get value of 10
if(cnt7==7)
{

for(i=a;i<10;i++)//>
{
if(roman[i]=='m'){e++;}// e used as a counter for counting 'm'. just after entering this check c section

cnt7=roman[i];
if(cnt7==10)
{
if(e<4){i=11;return>
else {
printf("\nInvalid roman input\n");return 0;}
i=11;
}else
//a=i+1 used below is used to direct compiler will able start to bit checking on following bit
//i=11 is used to stop the for looping
if(roman[i]=='i'){cnt2=1;a=i+1;i=11;}
else
if(roman[i]=='v'){cnt3=2;a=i+1;i=11;}
else
if (roman[i]=='x'){cnt=1;a=i+1;i=11;}
else
if(roman[i]=='l'){cnt4=4;a=i+1;i=11;}
else
if(roman[i]=='c'){cnt5=5;a=i+1;i=11;}
else
if(roman[i]=='d'){cnt6=6;a==i+1;i=11;}

}


}




//------------------------------------------------------------------------------------------------------------------------
// section 4:For check 'd'
//code used for prompt roman input will have 'LF' character at the end of every input.If we convert it into integer we will get value of 10
if(cnt6==6)
{

for(i=a;i<10;i++)
{
if(roman[i]=='d'){e++;}// e used as a counter for counting 'd'. just after entering this check c function
cnt6=roman[a];
if(cnt6==10)// if is NL
{
if(e<1){return>
else {printf("\nInvalid roman input\n");i=11;return 0;}
}else
//a=i+1 used below is used to direct compiler will able start to bit checking on following bit
//i=11 is used to stop the for looping
if(roman[i]=='i'){cnt2=1;a=i+1;i=11;}
else
if(roman[i]=='v'){cnt3=2;a=i+1;i=11;}
else
if (roman[i]=='x'){cnt=1;a=i+1;i=11;}
else
if(roman[i]=='l'){cnt4=4;a=i+1;i=11;}
else
if(roman[i]=='c'){cnt5=5;a=i+1;i=11;}



}
}
//-------------------------------------------------------------------------------------------------------------------------
// section 5:For check 'c'
//code used for prompt roman input will have 'LF' character at the end of every input.If we convert it into integer we will get value of 10
if(cnt5==5)// for check c
{
for(i=a;i<10;i++)
{
if(roman[i]=='c'){e++;}// e used as a counter for counting 'c'. just after entering this check c function
cnt5=roman[i];
if(cnt5==10)// if is NL
{
if(e<3){i=11;return>
else {
printf("\nInvalid roman input\n");
return 0;
}
}else
//a=i+1 used below is used to direct compiler will able start to bit checking on following bit
//i=11 is used to stop the for looping
if(roman[i]=='i'){cnt2=1;a=i+1;i=11;}
else
if(roman[i]=='v'){cnt3=2;a=i+1;i=11;}
else
if (roman[i]=='x'){cnt=1;a=i+1;i=11;}
else
if(roman[i]=='l'){cnt4=4;a=i+1;i=11;}

}
}
//-----------------------------------------------------------------------------------------------------------------
//section 5:For check 'L'
//code used for prompt roman input will have 'LF' character at the end of every input.If we convert it into integer we will get value of 10
if(cnt4==4)
{
if(roman[a]=='l'){printf("\nInvalid roman input\n"); return 0;}
//else if(roman)
else{
if(roman[a]=='x'){
cnt=1;//enable check x function
a+=1;}
else{
if(roman[a]=='i'){
cnt2=1;//enable check i function
a+=1;}
else{
if(roman[a]=='v'){
cnt3=2;//enable check v function
a+=1;}
}}}


}
//------------------------------------------------------------------------------------------------------
//section 6:For check 'x'
//code used for prompt roman input will have 'LF' character at the end of every input.If we convert it into integer we will get value of 10
if(cnt==1)// for first bit is 'x'
{
for(i=a;i<10;i++)
{
// three 'if' below used to do count for x,c,l
if(roman[i]=='x'){
b++;
}//count x
if(roman[i]=='c'){
c++;
}//count c
if(roman[i]=='l'){
l++;
}// count l
d=roman[i];
if(d==10)
{ //Since for roman number we should not have more than 3 x continuesly. So this condition below set
if(b<3){return>
if(b>2){printf("\nInvalid roman input\n"); return 0;}

}


if(roman[i]=='l')
{ d=roman[i+1];
if(d==10)//if second bit is equal 'LF'
{
// We should not have more than one x infront l so we set the condition below...
if(b>0){printf("\nInvalid roman input\n");return 0;}
else
if(l>1){printf("\nInvalid roman input\n");return 0;}
else return 1;
}
else
{
if(b>0)
{printf("\nInvalid roman input\n");return 0;i=11;}
else{
if(roman[i]=='i'){cnt2=1;a=i+1;i=11;}
if(roman[i]=='v'){cnt3=2;a=i+1;i=11;}
}
}
}//2st=L
else{
if(roman[i]=='c')
{ if(b>0)
{printf("\nInvalid roman input\n");return 0; }
else
if(c>1)
{printf("\nInvalid roman input\n");return 0;}
else
if(roman[i+1]=='i')
{printf("\nin check c third bit is i\n");a=i+2;i=11;cnt2=1;}
else
if(roman[i+1]=='v')
{printf("\nin check c third bit is v\n");a=i+2;i=11;cnt3=2;}

}//2st=c
else{
if(b<4){
if(roman[i]=='i'){cnt2=1;a=i+1;i=11;}
if(roman[i]=='v'){cnt3=2;a=i+1;i=11;}}
else
if(b>3)
{ printf("\nInvalid roman input\n");
return 0;}
}// check for i and v
}
}//}for ; for loop
}
//--------------------------------------------------------------------------------------------
//section 6:For check 'i'
//code used for prompt roman input will have 'LF' character at the end of every input.If we convert it into integer we will get value of 10
if(cnt2==1)// f0r 1st bit is i
{
for(i=a;i<10;i++)
{
if(roman[i]=='i'){
f+=1;
}
cnt2=roman[i];
if(cnt2==10)// if NL detected
{return 1;}

if(roman[i]=='c'){printf("\nInvalid roman input\n");return 0;};if(roman[i]=='l'){printf("\nInvalid roman input\n");return 0;};if(roman[i]=='m'){printf("\nInvalid roman input\n");return 0;}

if(roman[i]=='x')// when x detected
{
cnt2=roman[i+1];
if(cnt2==10){
if(f<1){return>
if(f>0){printf("\nInvalid roman input\n");return 0;}
}
else{
cnt2=roman[i+1];
if(cnt2!=10){
printf("\nInvalid roman input\n");
return 0;}}}

if(roman[i]=='v')// when v detected
{
cnt2=roman[i+1];
if(cnt2!=10){
printf("\nInvalid roman input\n");
return 0;}
if(f>0){printf("\nInvalid roman input\n");return 0;}}
else
{ cnt2=roman[i+1];
if(cnt2==10){
if(f>2)
{
printf("\nInvalid roman input\n");
return 0;
}
else{
{return 1;}}}


}}
}
//--------------------------------------------------------------------------------
//section 6:For check 'v'
//code used for prompt roman input will have 'LF' character at the end of every input.If we convert it into integer we will get value of 10
if(cnt3==2)
{
for(i=a;i<10;i++){
cnt2=roman[i];
if(cnt2==10){return 1;}
else{
cnt2=roman[i+1];
if(roman[i]!='i')
{printf("\nInvalid roman input\n");return 0;}
else{
if(cnt2==10){
if(i>3)
{printf("\nInvalid roman input\n");return 0;}
else return 1;
}}}

}
}




}

No comments: