nutclump
nutclump

Reputation: 71

Array manipulation C

I have a file with several lines of input, in the form

6 7 3 -4 5 6 7 8 * + % ( / ) -

where 6 is the number of numbers in the expression, 7 is the number of operands, followed by the actual operands and operators. So far I have the below code, which stores 6 and 7. I am restricted to using fscanf(). I must produce output that looks like:

3 * -4 + 5 % ( 6 / 7 ) - 8

There's a lot more to the project but this is the part where i'm stuck. I have no idea how to print out the statement like that.

I'm also printing the expression to another file, and am restricted to using fprintf() for that.

while (fscanf (input, "%d %d", &OPN, &OPR)!=EOF);
{

Upvotes: 0

Views: 128

Answers (3)

r3mainer
r3mainer

Reputation: 24557

I am restricted to using fscanf()

Homework, right?

You would, I think, be better off reading in a whole line at a time into a line buffer. Use, e.g., strtok() to split the string apart wherever a space occurs, and store each return value (a string pointer) in an array of string pointers (e.g., char *tokens[20];). If you're going to use the line buffer for something else, you'll have to use strdup() to create a copy of each token before storing it in the table, otherwise you'll lose this data.

The simplest way to read in a whole line is by using fgets(), but if you must use fscanf(), then you could use something like scanf(" %99[^\n]\n",str); (replacing 99 with one less than the size of the buffer you are reading the line into).

One last thing:

while (fscanf (input, "%d %d", &OPN, &OPR)!=EOF);
{

There's a semicolon at the end of the while() statement. You'll need to get rid of that.

Upvotes: 1

BLUEPIXY
BLUEPIXY

Reputation: 40145

#include <stdio.h>

int main(void){
    FILE *fi = fopen("data.txt", "r");
    int OPN, OPR;

    fscanf(fi, "%d %d", &OPN, &OPR);
    int nums[OPN];
    char ops[OPR];
    int ni, oi;
    for(ni=0; ni < OPN; ++ni)
        fscanf(fi, "%d", &nums[ni]);
    for(oi=0; oi < OPR; ++oi)
        fscanf(fi, " %c", &ops[oi]);
    fclose(fi);

    FILE *fo = fopen("out.txt", "w");
    ni = oi = 0;
    if(ops[0] != '(')
        fprintf(fo, "%d", nums[ni++]);
    while(oi < OPR){
        if(ops[oi] != '(') {
            fprintf(fo, " %c ", ops[oi]);
            if(ops[oi++] == ')' && oi < OPR)
                fprintf(fo, "%c ", ops[oi++]);
        }
        if(oi < OPR && ops[oi] == '(')
            fprintf(fo, "%c ", ops[oi++]);
        if(ni < OPN)
            fprintf(fo, "%d", nums[ni++]);
    }
    fprintf(fo, "\n");
    fclose(fo);
    return 0;
}

Upvotes: 0

UncleO
UncleO

Reputation: 8449

Is there a limit to the number of numbers and operators? Then you could use static arrays, otherwise you will have to dynamically allocate arrays of sufficient size.

Basically, you need to store the values in arrays and then process them. Read the operators array to determine if you want to print a number or the operator next. ( '(' gets printed before, the others after, for example. A ')' is followed by an operator or the end of the line.)

fscanf() moves the file pointer along as it reads, so you can do something like

int numnums = 0;
int numops = 0;
int nums[MAXNUMS];
char ops[MAXOPS];

while (fscanf (input, "%d %d", &numnums, &numops)!=EOF)
{
    for(i = 0; i < numnums; ++i)
    {
        fscanf (input, "%d", nums[i]);
    }
    for(i = 0; i < numops; ++i)
    {
        fscanf (input, "%c", nuopsms[i]);
    }

    // use fprintf to print out the ops and nums (and spaces) one at a time.
}

Upvotes: 0

Related Questions