Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
char LETTERS[16];
int total (int, int, int, int, int, int, int, int, int, int, int, int, int);
int countless (int);
int counter (char, int);
int readletter2 (char, int);
int count (int, int);
void sort (char); // converts the NUMERALS into counts
void readkeyboard (); // reads the string from the keyboard
void ask () ; // asks for input type
int fork(int); // choose (F)ile or (K)eyboard
int total (int m, int d, int c, int l, int x, int v, int i, int cm, int cd, int xc, int xl, int ix, int iv)
{
return (m*1000) + (d*500) + (c*100) + (1*50) + (x*10) + (v*5) + (i) + (cm*900) + (cd*400) + (xc*90) + (xl*40) + (ix*9) + (iv*4);
}
int countless (int a)
{
a --;
return a;
}
int counter (char Tab[16], int i) // TALLIES AND ERROR CHECKS THE DOUBLE NUMERAL COUNTS
{
switch (Tab[i])
{
case '\0':
break;
case 'D':
REP = 1; countless(countC); count(countCM, REP); break;
case 'M':
REP = 1; countless(countC); count(countCD, REP); break;
case 'C':
REP = 1; countless(countX); count(countXC, REP); break;
case 'L':
REP = 1; countless(countX); count(countXL, REP); break;
case 'X':
REP = 1; countless(countI); count(countIX, REP); break;
case 'V':
REP = 1; countless(countI); count(countIV, REP); break;
default:
printf("You have input an invalid character");
RESTART ();
}
return i;
}
int readletter2 (char Tab[16], int i) // READS THE SECOND THE SECOND NUMERAL AFTER THE PRIMARIES C, X and I AND PERFORMS TALL
{
i ++;
counter (Tab[16], int i);
return i;
}
int count (int i, int rep) // CHECKS AND TALLIES THE SINGLE NUMERAL COUNTS
{
(i ++);
if (i > rep)
{
printf("You have input too many consecutive numerals");
RESTART();
}
return i;
}
char sort (char Tab[16])
{
int i;
int REP;
for (i = 0; Tab[i] != '\0'; i++);
{
switch (Tab[i])
{
case 'M':
REP = 4; count(countM, REP); break;
case 'D':
REP = 1; count(countD, REP); break;
case 'C':
REP = 3; count(countC, REP); readletter2(Tab[i], i); break;
case 'L':
REP = 1; count(countL, REP); break;
case 'X':
REP = 3; count(countX, REP); readletter2(Tab[i], i); break;
case 'V':
REP = 1; count(countV, REP); break;
case 'I':
REP = 3; count(countI, REP); readletter2(Tab[i], i); break;
default:
printf("You have input an invalid character"); RESTART ();
}
}
}
void readkeyboard (char Tab[16]) // FOR KEYBOARD MODE ASK FOR NUMERALS AND COPY INTO "LETTERS"
{ //and change string to upper case
int x;
printf("\n Please input your ROMAN NUMERALS from the keyboard: ");
scanf("%s", LETTERS);
for (x=0; x<strlen(LETTERS); ++x)
{
LETTERS[x] = toupper(LETTERS[x]);
}
}
void ask ()
{
printf("\n Choose 1 for FILE input");
printf("\n Choose 2 for KEYBOARD input: ");
}
int fork(int c)
{
scanf("%i", &c)
while ((c <=0) || (c >=3));
if (c == 1)
printf("You have chosen one - file input");
else if (c == 2)
{
printf("You have chosen two - file input"); // FOR KEYBOARD MODE ASK FOR NUMERALS AND COPY INTO "LETTERS"
readkeyboard ();
}
int main ()
{ // DECLARE VARIABLES ETC...
int countM = countD = countC = countL = countX = countV = countI = countCM = countCD = countXC = countXL = countIX = countIV = 0;
int answer = 0;
// CHOOSE BETWEEN KEYBOARD & BATCH MODE
ask();
fork ();
sort (LETTERS);
answer = total(countM, countD, countC, countL, countX, countV, countI, countCM, countCD, countXC, countXL, countIX, countIV);
printf("The value you input in ROMAN NUMERALS was: %s", LETTERS);
printf("The equivalent value in Arabic numbers is: %i", answer);
}