- #1
Jamin2112
- 986
- 12
I started making a program with the following prompt (For privacy purposes, I'm posting it as an image).
Yes, it's a useless program; I'm making it for fun.
I started out with what I thought were some nice implementations of helper functions:
After that it went all downhill ... I realized I'm doing something wrong after I go halfway through writing my master function.
Anyways, can someone please criticize me in the most condescending way possible; and if you're feeling nice, maybe give me an outline of how to better approach this problem?
Yes, it's a useless program; I'm making it for fun.
I started out with what I thought were some nice implementations of helper functions:
Code:
int index_of (char * str, char c)
{
/* Returns the index of the first instance of
character c in the character array str. If
none is found, returns -1.
*/
int i = 0;
while (str != '\0')
{
if (*str == c)
{
break;
}
else
{
++str;
++i;
}
}
return str == '\0' ? -1 : i;
}
int parse_int (char * cp, int * i)
{
/* cp: Pointer to a character array that is a base-10 string representation of an integer
i: Pointer to an integer which will store the output of the parsing of cp
Returns the number of characters parsed.
*/
int n = 0;
*i = 0;
while (cp!= '\0')
{
char c = *cp;
if (c >= '0' && c <= '9')
{
n = n * 10 + (c - '0');
}
else
{
break;
}
++cp;
}
return n;
}
int bin_op (int a, char oper, int b)
{
/* Returns
a oper b
where oper is defined as in the specs.
*/
int c;
switch (oper)
{
case '|':
c = a | b; break;
case '*':
c = a & b; break;
case '^':
c = a & b; break;
default: /* Never happens */
;
}
return c;
}
After that it went all downhill ... I realized I'm doing something wrong after I go halfway through writing my master function.
Code:
int parse_input (char ** args, int args_len, int * output, char * err)
{
/* args: Command-line parameters
args_len: Number of command-line parameters
output: Evaluation of the equation
err: Error message
*/
if (args_len < 2)
{
str_cpy("No equation provided.", err);
return -1;
}
char * pcc = args[1]; /* Pointer to current character */
char cc = *pcc; /* Current character */
char * plo = NULL; /* Pointer to last operator */
int nnrp = 0; /* Net number of right parantheses */
while (cc != '\0')
{
if (cc >= '0' && cc <= '9') /* If current character is a digit */
{
int pi = 0; /* Parameter index */
pcc += parse_int(pcc, &pi);
if (pi > (args_len - 2))
{
sprintf(err, "Index %d is out-of-bounds", pi);
return -1;
}
int pv; /* Value at parameter pn */
if (parse_int(args[pi], &pv) > 0)
{
if (plo == NULL)
{
*output = pv;
}
else
{
*output = bin_op(*output, *plo, pv);
}
}
else
{
sprintf(err, "Parameter %d is not an integer", pv);
return -1;
}
}
elseif (index_of(ops, cc) > -1) /* If current character is an operator */
{
if (pcc == args[1]) /* If the current character is the first character */
{
sprintf(err, "Expression cannot begin with %c", cc);
return -1;
}
elseif (pcc == (args[1] + sizeof(args[1])/sizeof(char *))) /* If the current character is the final character */
{
sprintf(err, "Expression cannot end with %c", cc);
return -1;
}
else if ((*(pcc - 1)) < '0') && (*(pcc - 1) > '9')) /* If the previous character is not a digit */
{
sprintf(err, "Operator %c must be preceded by a digit, cannot be preceded by %c", cc, *(pcc - 1)));
return -1;
}
else/* The current character is an operator preceded by a digit */
{
*plo = cc;
}
}
else if (cc == '(')
{
if (pcc == (args[1] + sizeof(args[1])/sizeof(char *))) /* If the current character is the final character */
{
sprintf(err, "Expression cannot end with %c", cc);
return -1;
}
else if (
{
}
else
{
++nnrp;
}
}
else if (cc == ')')
{
// ...
--nnrp;
}
++pcc;
}