Re: Help Badly needed- replacing rules & strings
- From: "Arthur J. O'Dwyer" <ajonospam@xxxxxxxxxxxxxx>
- Date: Tue, 28 Feb 2006 00:56:58 -0500 (EST)
On Mon, 27 Feb 2006, Willing 2 Learn wrote:
I'm working on this program below but im stuck.
What's wrong with it? I mean, other than that it's ugly and half
of it is commented out? Here's a clean copy for you. You should really
look up "function" in your C++ textbook, and figure out how to define
functions other than 'main'; for example,
/* Returns a new state number, dependent on input_character. */
int state1(int input_character);
int state2(int input_character);
int state3(int input_character);
/* Returns a parse tree of the given sentence. */
mytree *parse_sentence(const char *sentence);
mytree *parse_sentence(const std::string & sentence);
My suspicions are confirmed; you /really/ need to seek help from your teacher, rather than trying to get random strangers on Usenet to solve
your problems.
-Arthur
#include <cstdio>
#include <iostream>
using namespace std;
int main(void)
{
const char sentence[] = "E+ND+T";
const char non_terminal[] = "ETND";
int sidx, nidx;
int rule;
for (sidx=0; sentence[sidx] != '\0'; ++sidx) {
for (nidx=0; non_terminal[nidx] != '\0'; ++nidx) {
/*
Output the non-terminals, and their position in the
sentence and in the non-terminals.
*/
if (sentence[sidx] == non_terminal[nidx])
{
printf("Found %c at index %d in \"%s\"\n",
sentence[sidx], sidx, sentence);
printf("Found %c at index %d in \"%s\"\n",
non_terminal[nidx], nidx, non_terminal);
/* Output the rule relating to this non-terminal. */
switch (non_terminal[nidx]) {
case 'E':
puts("Rule: E = E+T (Enter 1)");
puts("Rule: E = E-T (Enter 2)");
puts("Rule: E = T (Enter 3)");
break;
case 'T':
puts("Rule: T = N (Enter 4)");
puts("Rule: T = (E) (Enter 5)");
break;
case 'N':
puts("Rule: N = ND (Enter 6)");
puts("Rule: N = D (Enter 7)");
break;
case 'D':
for (int i=0; i <= 9; ++i)
printf("Rule: D = %d (Enter %d)", i, i+8);
break;
}
break;
}
}
}
/* This section is probably in the wrong place. */
cout << "Please enter number for rule selected: ";
cin >> rule;
/* A bunch of commented-out code snipped here. */
return 0;
}
.
- References:
- Help Badly needed- replacing rules & strings
- From: Willing 2 Learn
- Help Badly needed- replacing rules & strings
- Prev by Date: Re: hex help
- Next by Date: Re: hex help
- Previous by thread: Help Badly needed- replacing rules & strings
- Index(es):