Thread: add value at any number in string

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    2

    add value at any number in string

    Hy all!
    I build a automatic foam cutter machine ,and for semplicity i modify potrace program that transform raster to vectorial ... to make gcode ...is javascript ... but is not important the language.
    For curiosity you can test here <<snipped>>
    So... you make a image in paint or copy for internet , and with this program van transformate quickly in gcode a and can cut!
    But motors of my machine is with wheel teets , and is not precise ... so .. when inverse the movement ... the motor begin rotate after few steps ... and the cut is not accurately!

    MY QUESTION!

    I want to insert a function ... when value change ... add a small value to actual value...

    for example ,i have:

    G01 X0
    G01 X7
    G01 X3
    G01 X1
    G01 X20
    G01 x27
    G01 X14

    So i have 0,7,3,1,20,27,14

    so 0 ... 7 motor motor begin rotation to one sense
    7...3 motor change sense rotation ... but rimain stopped until wheels teets begin traction

    for example when change direction the motor rimain stopped 1 step ... so ... the cnc not arrive a 3 ... arrive a 4 ...

    i not shure that you understand!

    And need to find a formula to compensate this by program! I not shure that function in practice ... but i can try!
    Thanks in advance!
    Last edited by Salem; 11-14-2018 at 02:31 AM. Reason: snipped

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    What does any of that have to do with C++?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2018
    Posts
    2
    Quote Originally Posted by Salem View Post
    What does any of that have to do with C++?
    How make with c this?

    in string ... need to change value ... only when value is greater or lower that first value in string...

    For example i have this string...

    1,7,9,4,2,5,9,8...

    i need to make

    1,7,9,3,2,6,9,7
    Last edited by costycnc; 11-14-2018 at 02:50 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Explain in more detail how 4 becomes 3.

    Parse into integers separated by commas would be a start.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    66
    Hi costycnc!

    I think you need to parse the string by using the adress (X,Y,Z,I).
    Here a little example with gcc written on Linux with codeblocks:
    first the header "cngnum.h":
    Code:
    #ifndef CNGNUM_H_INCLUDED
    #define CNGNUM_H_INCLUDED
    
    #include <stdio.h>
    #include <stdlib.h> /* strncpy, strtol */
    #include <string.h>
    #include <math.h>
    
    
    int Get_LetterPos(char tosearch[], char satz[], int *letter, int *letterpos);
    int Get_CharPos(char tosearch, char satz[], int *letterpos);
    int GetnumPos(char satz[], int startpos);
    void STRITEIL(char *zfolge, char *erge, int beginn, int ende);
    char *itoa(int zahl, char puffer[], int basis);
    void initstrings(char letters[], int ende);
    void addstring(char fertig[], char teila[], char dummy[], char teilb[]);
    void IncNum(char satz[], char fertig[], char adletr, double aenderung);
    
    
    
    #endif // CNGNUM_H_INCLUDED
    then the file cngnum.c:
    Code:
    #include "cngnum.h"
    
    /// Rückgabewert ist Laenge des Satzes (argument 2)
    int Get_LetterPos(char tosearch[], char satz[], int *letter, int *letterpos)
    {
     int rewer = -1;
     int satzlaenge = 0;
     int i, j;
     satzlaenge = strlen(satz);
    
     for (i = 0; i < satzlaenge; i++)
      {
       for (j = 0; j < 3; j++)
       if(satz[i] == tosearch[j])
        {
         rewer = i;
         *letter = j;
         *letterpos = i;
         break;
         }
        if (rewer != -1)
         break;
      }
    
     return satzlaenge;
    }
    
    /** Rückgabewert ist Laenge des Satzes (argument 2)*/
    int Get_CharPos(char tosearch, char satz[], int *letterpos)
    {
     int rewer = -1;
     int satzlaenge = 0;
     int i;
     satzlaenge = strlen(satz);
    
     for (i = 0; i < satzlaenge; i++)
      {
       if(satz[i] == tosearch)
        {
         rewer = i;
         *letterpos = i;
         break;
         }
        if (rewer != -1)
         break;
      }
    
     return satzlaenge;
    }
    
    /// findet die Position des ersten Leerzeichens nach startpos
    int GetnumPos(char satz[], int startpos)
    {
     int rewer = -1;
     int i;
     int satzlaenge = 0;
     satzlaenge = strlen(satz);
    
     for (i = startpos; i < satzlaenge; i++)
      {
       if(satz[i] == ' ')
        {
         rewer = i;
         break;
        }
        if (rewer != -1)
         break;
      }
    
     return rewer;
    }
    
    
    /// Kopiert einen Stringteil von zfolge nach erge von beginn bis ende
    void STRITEIL(char *zfolge, char *erge, int beginn, int ende)
    {
     int i, j = 0;
     int satzlaenge = 0;
    
     satzlaenge = strlen(zfolge);
    
    
     if((beginn <= ende) && (beginn <= (satzlaenge - 1)) && (ende <= satzlaenge) )
      for (i = beginn; i <= ende; i++)
       {
        erge[j++] = zfolge[i];
        erge[j] = 0;
       }
    }
    
    
      char *itoa(int zahl, char puffer[], int basis)
     {
      int slei = 0, start;
      int produkt = 0, dummy = 0, sleib = 0, nullsteller;
      char nummern[36] = {'0','1','2','3','4','5','6','7','8','9','A','b','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
        sleib = nullsteller = 0;
      /// behandelt maximal basis hoch 16 große ganze Dezimalzahlen (natürliche Zahlen)
      /// zuerst wird geprüft, ob eine negative Zahl vorliegt*/
        if (zahl == 0 )
         {
          sleib = 1;
          puffer[0] = '0';
          ///printf("\nzahl=%d", zahl);
         }
        if (zahl < 0 )
         {
          sleib = 1;
          puffer[0] = '-';
          zahl *= -1;
          ///printf("\nzahl=%d", zahl);
        }
      /// Prüft, wann die erste Zahl vorliegt
       for (slei = 32; slei >= 0; slei--)
         {
          produkt = (int)pow(basis, slei);
          if (produkt != 0)
           {
            start = slei;
            break;
           }
         }
       for (slei = start; slei >= 0; slei--)
         {
          produkt = (int)pow(basis, slei);
          dummy   = zahl / produkt;
          zahl   -= dummy * produkt;
    
          //printf("\nslei: %2d produkt= %lu dummy=%lu", slei, produkt, dummy);
          if ((dummy == 0) && (nullsteller == 1))
           puffer[sleib++] = '0';
          if ( dummy != 0)
           {
            if (dummy <= 9)
             puffer[sleib++] = nummern[dummy];
    
            nullsteller = 1;
           }
          puffer[sleib] = '\0';
         }
     return puffer;
     }
    
    /// Initialisiert eindimensionale char-arrays mit String-Ende-Zeichen
     void initstrings(char letters[], int ende)
     {
      int i;
      for (i = 0; i < ende; i++)
       letters = '\0';
     }
    
    /// Setzt aus drei Strings einen zusammen
    void addstring(char fertig[], char teila[], char dummy[], char teilb[])
     {
     strcpy(fertig, teila);
     strcat(fertig, dummy);
     strcat(fertig, teilb);
     }
    
    ///Inkrementiert die Zahl nach dem angegebenen Adressbuchstaben
    void IncNum(char satz[], char fertig[], char adletr, double aenderung)
    {
     char teila[100];
     char nummer[100];
     char teilb[100];
     char dummy[100];
    
     int lpos = -1;
     int endpos = -1;
     int satzlaenge = 0;
     double zahl = 0;
    
     initstrings(teila, 100);
     initstrings(nummer, 100);
     initstrings(teilb, 100);
     initstrings(dummy, 100);
    
    
     satzlaenge = Get_CharPos(adletr, satz, &lpos);     /// Stelle Position von Adressbuchstabe fest
     endpos = GetnumPos(satz, (lpos + 1));              /// Stelle Laenge der Zahl fest durch Positionsbestimmung des Leerzeichens
     STRITEIL(satz, teila, 0, lpos);                    /// String vor Zahl separieren
     STRITEIL(satz, nummer, (lpos + 1), (endpos - 1));  /// Zahl in String separieren
     STRITEIL(satz, teilb, endpos, (satzlaenge - 1));   /// String nach Zahl separieren
    
    
     /// Zahl-String zu int umformen inkrementieren und zu String zurueck formen
     zahl = atof(nummer);
     zahl+= aenderung;
     sprintf(dummy, "%.3lf", zahl);
      //itoa(zahl, dummy, 10);
    
    addstring(fertig, teila, dummy, teilb);
    
     ///Zur internen Kontrolle der Verarbeitung
     printf("%c  Pos: %d\n", adletr, lpos);
     printf("start: %d   endpos: %d\n", (lpos + 1), (endpos - 1));
     printf("Beginn: %d    Ende: %d    Teila: %s\n", 0, lpos, teila);
     printf("nummer: %s   Zahl: %.3lf   dummy: %s\n", nummer, zahl, dummy);
     printf("Beginn: %d    Ende: %d    Teilb:%s\n", endpos, satzlaenge, teilb);
    }
    now main.c:
    Code:
    #include "cngnum.h"
    
    
    int main()
    {
     //int i, j;
     char satz[150];
     //char tocng[3] = { 'X','Y','Z'};
    
     char fertig[200];
     char adletr;
     int satzlaenge = 0;
    
     initstrings(satz, 150);
     initstrings(fertig, 200);
    
    
     strcpy(satz, "N35 G02 X32 Y38 I26 J-10.29"); //Seite 410 Tabellenbuch Europa
     satzlaenge = strlen(satz);
    
     printf("Satz vorher:\n");
     printf("012345678901234567890123456789\n");
     printf("%s\n", satz);
     printf("Satzlaenge: %d\n", satzlaenge);
    
     adletr = 'Y';
    
     IncNum(satz, fertig, adletr, -14);
    
     IncNum(satz, fertig, 'X', 33);
     printf("fertig: %s\n", fertig);
    
     strcpy(satz, fertig);
    
     IncNum(satz, fertig, 'I', 41);
    
     strcpy(satz, fertig);
    
     IncNum(satz, fertig, 'Y', 11.234);
    
     strcpy(satz, fertig);
    
     printf("fertig: %s\n", fertig);
    
     return EXIT_SUCCESS;
    }
    Sorry it is written in german, but may be it helps you.

  6. #6
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    @rusyoldguy -
    Interessantes Programm. Es analysiert Adressen - ziemlich kompliziert, ich muß mir das mal in Ruhe ansehen; versuch es gerade mit Listen.

    Wenn ich @costycnc richtig verstanden habe, dann sucht er nach einem Programm das seine Maschine korrigiert. Die Maschine arbeitet nach einer Weile offenbar nicht mehr korrekt bzw. exakt, vielleicht durch "Schlupf", und das Programm soll das korrigieren. Das ist das, was ich auf Grund seines Textes verstanden habe.

    Interesting program. It analyzes addresses - quite complicated, I have to take a look at it; currently I just try it with lists.

    If I understood @ costycnc correctly, then he is looking for a program that corrects his machine. The machine does not seem to work correctly after a while, maybe due to "slippage", and the program is supposed to correct that. That is what I have understood.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-24-2012, 12:31 AM
  2. Replies: 12
    Last Post: 03-07-2011, 01:24 AM
  3. Plz help...number string!!
    By symbee in forum C++ Programming
    Replies: 5
    Last Post: 11-23-2006, 02:53 AM
  4. number to string
    By peking1983 in forum C++ Programming
    Replies: 5
    Last Post: 02-08-2003, 05:08 PM
  5. number in string
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 12-11-2002, 04:54 AM

Tags for this Thread