Find the answer to your Linux question:
Results 1 to 2 of 2
Hi, guys I just cannot solve this little problem I have this text file: 4 a += 2 b -= 5 a += 4 b += a How do I ...
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Posts
    12

    Simple program help

    Hi, guys I just cannot solve this little problem

    I have this text file:
    4
    a += 2
    b -= 5
    a += 4
    b += a

    How do I solve these equations in C language?

    I would like to get this:
    a += 2 : 2
    b -= 5 : -5
    a += 4 : 6
    b += a : 1
    a = 6
    b = 1

    Here is my code so far, I tried to do something and it seems everything was read, but I just cannot do anything with it
    ./this -f filename

    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int main(int argccharargv[]) {

      
    char loadfile[32] = "text.txt";

      
    unsigned long i;
     
     if (
    argc == 3) { strcpy(loadfileargv[2]); }

     
    FILE *file;
     
    file fopen(loadfile"r");
     if (
    file == NULL) { printf("ERROR LOADING FILE"); }

     if (
    fseek(file0SEEK_END)) { printf("ERROR FILE SIZE"); }
     
    unsigned long filesize ftell(file);
     
    fseek(file0SEEK_SET);

     
    charinit malloc(filesize sizeof(char));
     if (
    init == NULL) { printf("ALLOC ERROR"); }
     
    int equations_counter;
     
    fscanf(file,"%i", &equations_counter);
     
    unsigned int items 0;

     for (
    filesize i++) {
         
    init[i] = fgetc(file);
       if (
    init[i] == '\n') { items++; }
      }
     
    fclose(file);

      
    char *LeftSide[items];
      
    char *operation[items];
      
    char *RightSide[items];

      
    LeftSide[0] = strtok (init," ");
      
    operation[0] = strtok (NULL," ");
      
    RightSide[0] = strtok (NULL," \n");

     for (
    items-i++) {
        
    LeftSide[i] = strtok (NULL," ");
        
    operation[i] = strtok (NULL," ");
        
    RightSide[i] = strtok (NULL," \n");
      }
     
    /**for (i = 0 ; i < items-1 ; i++) {
       printf ("%s ",LeftSide[i]);
       printf ("%s ",operation[i]);
       printf ("%s\n",RightSide[i]);
      }*/

     
    char check[3];
     
    char plus[3] = "+=";
     
    char minus[3] = "-=";
     
    char LSa,PSa;
     
    char op;
      
    //printf ("%s\n",plus);
      //printf ("%s\n",minus);

     
    for (0items-i++) {
     
    LSa strtok (init," ");
     
    op strtok (NULL," ");
     
    PSa strtok (NULL," \n");
       
    //printf ("%s ",operation[i]);
     
    if ( op == plus ) {
       
    printf ("%s ",operation[i]);
     }
     else if ( 
    op == minus ) {
       
    printf ("%s ",operation[i]);
     }
     else 
    printf ("ERROR\n");
     }


  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    The terms of use on this forum preclude us from doing your school work for you. Please don't ask us to. We can only help point you in the right direction at most. That is why you are in school, to learn by doing.

    That said, I can only ask where have you initialized your variables? Remember that in the C language, just declaring a variable does not set a value. It contains whatever was in memory there before. Ie,
    Code:
    int a, b, c;   /* These are not initialized so the following will generate unknown results */
    
    a += 2;
    b -= 5;
    c += a;
    However, if you initialize the variables to 0, a known value, then your results will make sense. Ie,
    Code:
    int a = 0, b = 0, c = 0;
    a += 2;
    b -= 5;
    c += a;
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...