Write a program using C language by using the function get_opnd() whose prototyp
ID: 653035 • Letter: W
Question
Write a program using C language by using the function
get_opnd() whose prototype is in opnd.h
/* File: opnd.h */
/* This file contains the prototypes for funcitons in opnd.c */
int get_opnd(char ch);
/* This function is given the first character of an operand. It
reads the remaining characters of the operand and converts
it to internal integer form. Operands may be a sequence of
digit characters or a sequence of digits (the base) followed
by a sequence of exponent characters, in which case get_opnd()
computes the base to the exponent as its result. All characters
are echoed to the display using Display Module routines.
*/
You can start by copying and modifying get_int() from chrutil.c. Write a test driver that reads integer operands and writes them to the screen using write_debug(). Then add the exponent operand types using the functions in your exponent.c.
Compile this code with
This program will be called driver2.
/* File: chrutil.c */
/* This file contains various utility functions for processing characters */
#include <stdio.h>
#include "tfdef.h"
#include "chrutil.h"
/* Function converts ch to an integer if it is a digit. Otherwise, it
prints an error message.
*/
int dig_to_int(char ch)
{
if (IS_DIGIT(ch))
return ch - '0';
printf("ERROR:dig_to_int: %c is not a digit ", ch);
return ERROR;
}
/* Function converts a positive integer less than 10 to a corresponding
digit character.
*/
char int_to_dig(int n)
{
if (n >= 0 && n < 10)
return n + '0';
printf("ERROR:int_to_dig: %d is not in the range 0 to 9 ", n);
return NULL;
}
/* Function reads the next integer from the input */
int getint()
{ int n = 0;
int got_dig = FALSE;
signed char ch;
ch = getchar(); /* read next char */
while (IS_WHITE_SPACE(ch)) /* skip white space */
ch = getchar();
while (IS_DIGIT(ch)) { /* repeat as long as ch is a digit */
n = n * 10 + dig_to_int(ch); /* accumulate value in n */
got_dig = TRUE;
#ifdef DEBUG
printf("debug:getint: ch = %c ", ch); /* debug statement */
printf("debug:getint: n = %d ", n); /* debug statement */
#endif
ch = getchar(); /* read next char */
}
if(ch == EOF) return EOF; /* test for end of file */
if(!got_dig) return ERROR; /* test for no digits read */
return n; /* otherwise return the result */
}
/* Function tests if c is an alphabetic letter. */
int letterp(char c)
{
if (IS_LOWER(c) || IS_UPPER(c))
return TRUE;
return FALSE;
}
/* Function returns TRUE if c is a delimiter, i.e., it is a white space
or a punctuation. Otherwise, it returns FALSE.
*/
int delimitp(char c)
{
if (whitep(c) || punctp(c))
return TRUE;
return FALSE;
}
/* Function returns TRUE if c is white space; returns FALSE otherwise. */
int whitep(char c)
{
if (c == ' ' || c == ' ' || c == ' ')
return TRUE;
return FALSE;
}
/* Function returns TRUE if c is a punctuation; returns FALSE otherwise. */
int punctp(char c)
{
if (c == '.' || c == ',' || c == ';' || c == ':'
|| c == '?' || c == '!')
return TRUE;
return FALSE;
}
/* Function checks if c is a vowel. */
int vowelp(char c)
{
switch(c) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': return TRUE;
default: return FALSE;
}
}
/* Function tests if c is printable. */
int illegal(char c)
{
if (IS_PRINT(c) || IS_WHITE_SPACE(c))
return FALSE;
return TRUE;
}
Explanation / Answer
/* Function reads the next integer from the input */
int getint()
{ int n = 0;
int got_dig = FALSE;
signed char ch;
ch = getchar(); /* read next char */
while (IS_WHITE_SPACE(ch)) /* skip white space */
ch = getchar();
while (IS_DIGIT(ch)) { /* repeat as long as ch is a digit */
n = n * 10 + dig_to_int(ch); /* accumulate value in n */
got_dig = TRUE;
#ifdef DEBUG
printf("debug:getint: ch = %c ", ch); /* debug statement */
printf("debug:getint: n = %d ", n); /* debug statement */
#endif
ch = getchar(); /* read next char */
}
if(ch == EOF) return EOF; /* test for end of file */
if(!got_dig) return ERROR; /* test for no digits read */
return n; /* otherwise return the result */
}
#include "stdio.h"
void main()
{
int op1,op2;
printf("Enter the integer operands :");
scanf("%d",op1);
scanf("%d",op2);
printf("The integer operands are %d and %d",op1,op2);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.