In C language please Pseudocode provided Write an interactiv
In C language please!!!! Pseudocode provided!!!
Write an interactive program that can be used as a Roman numeral calculator. The program will accept two Roman numerals and an arithmetic operation from the keyboard and will print out the result of the operation. The values of the Roman numeral digits are as follows:
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
Each Roman numeral must start with a digit of highest value and must end with a digit of lowest value. That is, 9 will be represented as VIIII instead of IX. The recognizable arithmetic operations are integer addition, integer subtraction, integer multiplication, and modulus.
Internally, the program will convert the Roman numerals into integers, perform the required operation, and then convert the result back to Roman numerals for printing. Input and output characters one at a time. Do not use strings, arrays, functions, or anything else we have not covered in class.
Negative numbers are not acceptable as input. The only instance of negative numbers is when the first number is smaller than the second and the operation is subtraction, in which case a negative Roman numeral will result and be printed.
If division by zero is attempted by the user, alert the user, and accept a different arithmetic operation.
Much of the code is given below in a version that does not use functions, and again below that is a version that does use functions. This use of functions to make main look simpler is sometimes called parameterized. Your final program should be parameterized.
Sample Output:
Welcome to Roman Calculator. Hail Caesar!
Enter first number: MMXI
The first number is : 2011
Enter second number: VIII
The second number is : 8
Enter operation (* / + - ): +
Answer: 2019
In Roman: MMXVIIII
Press Q to quit, any other key to continue.
Enter first number: XXII
The first number is : 22
Enter second number: VII
The second number is : 7
Enter operation (* / + - ): /
Answer: 3
In Roman: III
Press Q to quit, any other key to continue. Thanks for playing! Press any key to continue . . .
Psuedocode:
// This is a mix of psuedocode and actual C code
// display welcome message
// MAIN PROCESSING LOOP
do{
// prompt user for first number
// zero num1
// loop until <enter>, ignore bad chars
do {
// get user input
switch (ch) {
case \'M\':
putch(ch);
num1 += 1000;
break;
// etc...
// invalid entres will be ignored by default (no actual default)
}
} while (ch != \'\ \');
// display first number in arabic numerals
// REPEAT FOR NUM2
// loop, looking for valid operation input
do {
// prompt for user operation
// get user input
// set flag to true
// check for valid operation and perform operation
switch (ch) {
case \'+\':
ans = num1 + num2;
break;
//...
case \'/\':
if (!num2){
printf(\"\ Can\'t Divide by zero. Select another operation.\ \");
opOK = false;
break;
}
ans = num1 / num2;
break;
//...
default:
printf(\"\ Unidentfied operation. Select another operation.\ \");
opOK = false;
}
} while (!opOK);
// Convert and display roman numerals
if (ans<0){
printf(\"-\");
ans *= -1;
}
while (ans >= 1000){
printf(\"M\");
ans -= 1000;
}
// etc...
// ask user if they want to go again
// get user’s response into a character
} while again is not \'Q\' // END MAIN LOOP
// display farewell message
Parameterized Version of Roman Calculator – USE THIS PLEASE
Edit and re-write the previous program as necessary to use functions. Create functions that can use the following main program.
void main char chi char agains int opOK, int display welcome message printf Welcome to Roman Calculator. Hail Caesar An main loop dod //prompt user for first number lnEnter first number: /l loop until enter ignore bad chars num1 Oman O, display first number in arabic numerals printf mnahefirst number is: d numm1); //prompt user for second number printf nEnter second number: loop until enter ignore bad chars num2 a get Romano, display second numberinarabic numerals printf( InThe second number is: %d nuun2); ans dooperation accesses global nun1 nun print Romanian ask user if they want to go again printf InAnPress Q to quit, any other key to continue. get users response again getcho, while (again display farewell message printf Thanks for playingSolution
#include<stdio.h>
#include<string.h>
//declare num1 and num2 to hold arabic number
int num1, num2;
//derclare functions
int getRoman();
int doOperation(char ch);
void printRoman(int ans);
void predigit(char num1, char num2);
void postdigit(char c, int n);
char romanval[1000];
int i = 0;
int main()
{
//declare local variable
char ch;
char again;
int opOk;
int ans;
//display welcome message
printf(\"Welcome to-Roman calculator. \ \");
//mainloop
do
{
//prompt user for first number
fflush(stdout);
fflush(stdin);
printf(\"\ Enter first number: \");
num1 =getRoman();
//display first number in arabic number
printf(\"\ First number is :%d\ \",num1);
//prompt user for second number
fflush(stdin);
printf(\"\ Enter second number: \");
num2 =getRoman();
//display first number in arabic number
printf(\"\ second number is :%d\ \",num2);
printf(\"Enter the operation : \");
//not allow other than +, - , /, * in operation
fflush(stdin);
//Ask for operation till user enters valid operations +, - , * and/
while(1)
{
fflush(stdin);
scanf(\"%c\",&ch);
if( ch== \'+\' || ch==\'-\' || ch==\'*\' || ch==\'/\')
break;
else
{
printf(\"Not valid operation ,enter valid operations + , -, * or/ \ \");
continue;
}
}
//it performs the operation use enters on num1 and num2
ans= doOperation(ch);
i = 0 ;
//print number in Arabic
if (ans<0)
{
//printf(\"-\");
ans *= -1;
printRoman(ans);
printf(\"Ans = -%d\ \",ans);
printf(\"Ans in Roman : -%s\ \",romanval);
}
else
{
printRoman(ans);
printf(\"Ans = %d\ \",ans);
}
//call printing number in Roman
printf(\"Ans in Roman : %s\ \",romanval);
printf(\"\ \ Press Q to quit ,any other key to continue \ \");
//get user response
fflush(stdin);
fflush(stdout);
again =getchar();
}while(again != \'Q\');
//display farewell message
printf(\"Thanks forplaying\ \");
}
int getRoman()
{
int num= 0,i =0;
char str[20];
// prompt user for first number
// zero num1
// loop until <enter>, ignore bad chars
printf(\"Enter first number in Roman : \");
// get user input
scanf(\"%s\",str);
do
{
switch (str[i])
{
case \'M\':
putchar(str[i++]);
num += 1000;
continue;
break;
case \'D\':
putchar(str[i++]);
if(str[i] == \'M\' )
{
num-=500;
}
else
num += 500;
continue;
break;
case \'C\':
putchar(str[i++]);
if(str[i] == \'D\' || str[i] == \'M\')
num-=100;
else
num += 100;
continue;
break;
case \'L\':
putchar(str[i++]);
if(str[i] == \'C\' || str[i] == \'D\' || str[i] == \'M\' )
num-=50;
else
num += 50;
continue;
break;
case \'V\':
putchar(str[i++]);
if(str[i] == \'X\' || str[i] == \'L\' || str[i] == \'C\' || str[i] ==\'D\' || str[i] == \'M\')
num -= 5;
else
num += 5;
continue;
break;
case \'X\':
putchar(str[i++]);
if(str[i] == \'L\' || str[i] == \'C\' || str[i] == \'D\' || str[i] ==\'M\' )
num -= 10;
else
num += 10;
continue;
break;
case \'I\':
putchar(str[i++]);
if(str[i] == \'L\' || str[i] == \'C\' || str[i] == \'D\' || str[i] ==\'M\' || str[i] == \'V\' || str[i] == \'X\')
num -= 1;
else
num += 1;
continue;
break;
default:
//not valid charecter continue
//printf(\"not valid character ,ignoring\ \");
break;
}
// etc...
// invalid entres will be ignored by default (no actual default)
} while (i < strlen(str));
return num;
}
int doOperation(char ch)
{
int ans=0;
bool opOK = true;
switch (ch)
{
case \'+\':
ans = num1 + num2;
opOK = true;
break;
case \'-\':
ans = num1 - num2;
opOK = true;
break;
case \'*\':
ans = num1 * num2;
opOK = true;
break;
case \'/\':
if (!num2){
printf(\"\ Can\'t Divide by zero. Select another operation.\ \");
opOK = false;
break;
}
ans = num1 / num2;
opOK = true;
break;
//...
default:
printf(\"\ Unidentfied operation. Select another operation.\ \");
opOK = false;
}
return ans;
}
void printRoman(int number)
{
int num =number;
while (number != 0)
{
if (number >= 1000)
{
postdigit(\'M\', number / 1000);
number = number - (number / 1000) * 1000;
}
else if (number >= 500)
{
if (number < (500 + 4 * 100))
{
postdigit(\'D\', number / 500);
number = number - (number / 500) * 500;
}
else
{
predigit(\'C\',\'M\');
number = number - (1000-100);
}
}
else if (number >= 100)
{
if (number < (100 + 3 * 100))
{
postdigit(\'C\', number / 100);
number = number - (number / 100) * 100;
}
else
{
predigit(\'L\', \'D\');
number = number - (500 - 100);
}
}
else if (number >= 50 )
{
if (number < (50 + 4 * 10))
{
postdigit(\'L\', number / 50);
number = number - (number / 50) * 50;
}
else
{
predigit(\'X\',\'C\');
number = number - (100-10);
}
}
else if (number >= 10)
{
if (number < (10 + 3 * 10))
{
postdigit(\'X\', number / 10);
number = number - (number / 10) * 10;
}
else
{
predigit(\'X\',\'L\');
number = number - (50 - 10);
}
}
else if (number >= 5)
{
if (number < (5 + 4 * 1))
{
postdigit(\'V\', number / 5);
number = number - (number / 5) * 5;
}
else
{
predigit(\'I\', \'X\');
number = number - (10 - 1);
}
}
else if (number >= 1)
{
if (number < 4)
{
postdigit(\'I\', number / 1);
number = number - (number / 1) * 1;
}
else
{
predigit(\'I\', \'V\');
number = number - (5 - 1);
}
}
}
if( num < 0 )
printf(\"Number in Roman:-%s\ \",romanval);
}
void postdigit(char c, int n)
{
int j;
for (j = 0; j < n; j++)
romanval[i++] = c;
}
void predigit(char num1, char num2)
{
romanval[i++] = num1;
romanval[i++] = num2;
}







