/*
Ottiene "(132-2*51)"
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define DEBUG 0
#define NUMERO 19 //numero max caratteri per a,b e k in k=ax+by e MCD(a,b)
long int powi(int num, int exp) {//10^0
long int ris=1;
while(exp--)
ris=ris*num;
return ris;
}
int intlen(int num) {
int i=1;
while(num/powi(10,i) != 0)
i++;
return i;
}
/*
* Delete characters from where starting from from_included position to to_included position.
* Positions included in the process of deleting.
*
* Example:
* where = +1*51-1*132+1*51/*
Ottiene "(132-2*51)"
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define DEBUG 0
#define NUMERO 19 //numero max caratteri per a,b e k in k=ax+by e MCD(a,b)
long int powi(int num, int exp) {//10^0
long int ris=1;
while(exp--)
ris=ris*num;
return ris;
}
int intlen(int num) {
int i=1;
while(num/powi(10,i) != 0)
i++;
return i;
}
/*
* Delete characters from where starting from from_included position to to_included position.
* Positions included in the process of deleting.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_included = 0, to_included=4
* OutPut: where = -1*132+1*51{parsed_message}
* */
void strdeleteAt(char where[], const int from_included, const int to_included) {
char temp[strlen(where)-(to_included-from_included)+1];
int i;
for(i=0; i<from_included;i++)
temp[i] = where[i];
strcpy(temp+i, where+to_included+1);
strcpy(where, temp);
}
/*
* Deletes characters in where starting from from_inc position to to_inc position.
* Positions included in the process of deleting.
*
* Warning: passing a NULL pointer causes where to be returned.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_inc = strstr(where, "+");
* to_inc=strstr(where, "*");
* OutPut: where = 51-1*132+1*51{parsed_message}
*/
char* strdelete(char where[], char *from_inc, char* to_inc) {
if(to_inc == NULL || from_inc == NULL || where == NULL) return where;
printf("\nwhere: %s %d\nto_inc: %s %d\nfrom_inc: %s %d", where, strlen(where), to_inc, strlen(to_inc), from_inc, strlen(from_inc));
short len = strlen(where)-(to_inc-from_inc)+1;//from inc è pos 1 => errore
int pos=0;
if(len <= 0) return where;
char temp[len];
for(;from_inc-(where+pos) != 0; pos++)
*(temp+pos) = *(where+pos);
for(int posTo_inc=1; *(to_inc+posTo_inc) != '{parsed_message}';posTo_inc++,pos++)
*(temp+pos) = *(to_inc+posTo_inc);
*(temp+pos)='{parsed_message}';
strcpy(where,temp);
return where;
}
char* int2string(const int toBeParsed, char* whereToPut) {//-1
char digits[] = "0123456789";
int pos = 0, len=intlen(toBeParsed);
if(toBeParsed<=0) {
*whereToPut = '-';
pos++;
}
/* Determine the integer and putting it into whereToPut.
* for 38:
* whereToPut[0] = digits[38/10 -10*(38/100)] = '3'
* whereToPut[1] = digits[38/1 - 10*(38/10)] = '8'
* whereToPut => "38{parsed_message}"
*/
while(len-- > 0)
*(whereToPut + pos++) = digits[abs(toBeParsed) / powi(10,len) - 10*(abs(toBeParsed) / powi(10,len+1))];
*(whereToPut+pos)='{parsed_message}';
#if DEBUG
printf("\n\tint2string. toBeParsed=%d; whereToPut=%s\n",toBeParsed, whereToPut);
#endif
return whereToPut;
}
char* substringAt(char where[], int from_included, int to_included, char whereToPut[]) {
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(where+from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
char* substringAtstrstr(const char where[], char* from_included, const char* to_included, char whereToPut[]) {
if(to_included == NULL)
return strcpy(whereToPut, from_included);
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
int countChars(const char where[], const char character) {
int cont = 0;
for(; *where;where++)
if(*where == character) cont++;
return cont;
}
_Bool strcontain(const char where[], const char character) {
return countChars(where, character);
}
_Bool strcontainArray(const char where[], const char characters[], const short charactersLen)
{
_Bool r=1;
for(int i=0;i<charactersLen && r; i++)
r = r && countChars(where, characters[i]);
return r;
}
char* strstrArray(const char where[], const char chars[], const short charsLeng) {
//restituire l'occorrenza più vicina a where di una delle stringhe in chars. Per esempio per 132-2*51 e 132+2*51
char* occ = NULL;
for(int i=0; i < charsLeng; i++)
for(int pos = 0; *(where+pos); pos++)
if(*(where+pos) == chars[i])
if(occ == NULL)
{
occ = where+pos;
pos = strlen(where)-1;
}
else
if(occ != NULL && where+pos < occ)
{
occ = where+pos;
pos = strlen(where)-1;
}
return occ;
}
void emptystring(char string[]) {
while(*string++ = '{parsed_message}');
}
char* strBetweenPointersReverse(const char* yourstring, char *pos1, char *pos2, char whereToPut[]) {
#if DEBUG
puts("\nstringBetweenReverse()");
printf("\nyourstring %s", yourstring);
#endif
strcpy(whereToPut, yourstring);
strdelete(whereToPut, whereToPut, pos1);
strdelete(whereToPut, pos2, whereToPut+strlen(whereToPut));
return whereToPut;
#if DEBUG
printf("\nwhereToPut = %s", whereToPut);
#endif
}
/*
* This function find the first addend's components of an expression.
*
* @Params:
* exp is the expression where you want to find the first addend;
* mul2 is the memory location where the multiplier will be put. Must be "" when passed;
* num is the memory location where the number will be put. Must be "" when passed.
*
* @returns:
* true if the first addend was found. false otherwise.
*
* Example:
* exp = "è?(["; mul2 = ""; num = ""
* Output: mul2 = ""; ncum = ""
*
* Example :
* exp = "2*132-51"; mul2 = ""; num = ""
* Output: mul2 = "2", num = "132
* */
_Bool findAddendCmpts(const register char exp[], char mul2[], char num[]) {
char signs[] = {'+','-'}, *endnum = strstrArray(exp+1, signs, 2);
if(endnum != NULL)
endnum--;
if(*(exp) == '-') //132-2*51; -132-2*51; 2*132-2*51; -2*132-2*51; 132; -132; -2*132; 2*132
if(strcontain(exp, '*') && strstrArray(exp, signs,2) < strstr(exp, "*"))
{ //-132-2*51; -2*132-2*51; -2*132
char r[9+1];
strcpy(r,strBetweenPointersReverse(exp, strstr(exp, "-"), strstr(exp, "*")-1, mul2));
if(strcontainArray(r, signs, 2)) //is there, next to + or -, a number?
{ //-132-2*51
strcpy(mul2, "-1");//sure that exp contains *
substringAtstrstr(exp, exp+1, endnum, num);
}
else
{ //-2*132-2*51; -2*132
substringAtstrstr(exp, mul2+2, strstrArray(mul2+2, signs, 2), num);
}
}
else //-132
{
strcpy(mul2, "-1");
substringAtstrstr(exp, exp+1, endnum, num);
}
else //132-2*51; 2*132-2*51; 132; 2*132
if(strcontain(exp+1, '-') || strcontain(exp+1, '+'))
{ //132-2*51; 2*132-2*51
if(strcontain(exp, '*') && strstr(exp, "*") < strstrArray(exp, signs, 2)) //2*132-2*51
{
substringAtstrstr(exp,exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp,"*")+1, endnum, num);
}
else //132-2*51
{
strcpy(mul2, "1");
substringAtstrstr(exp, exp, endnum, num);
}
}
else
if(strcontain(exp, '*'))
{ //2*132
substringAtstrstr(exp, exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp, "*")+1, endnum, num); //all right with NULL ;) (see substringAtstrstr
}
else
{//132 that must be multiplied per n
strcpy(mul2, "1");
if(*exp=='+')
exp++;
strcpy(num, exp);
}
if(strcmp(num, "") == 0 || strcmp(mul2, "") == 0)
return 0;
else
return 1;
}
char *distribute(const char sign, char mul1[], char exp[], char result[]){
char mul2[5+1], num[5+1], signs[] = {'+', '-'}, *occ=NULL;;
short lung = 0, pos = 0;
while(findAddendCmpts(exp+pos, mul2, num))
{
if((occ=strstrArray(exp+pos+1, signs, 2))!=NULL)
pos+=occ -(exp+pos);
else pos+=strlen(exp+pos);
int mulDone = atoi(mul2) * atoi(mul1);
if(mulDone > 0) {
strcat(result, "+");
lung++;
}
char wtp[intlen(mulDone)+1];
strcat(result, int2string(mulDone, wtp));
lung+=intlen(mulDone);
strcat(result, "*");
lung++;
strcat(result, num);
lung+=strlen(num);
*(result+lung+2) = '{parsed_message}';
*num = '{parsed_message}';
*mul2 = '{parsed_message}';
}
return result;
}
int main(void) {
char wtp[10] = "";
char exp[] ="-13+2*41";
printf("\ndistribuzione: 2* (%s) =\n%s",exp,distribute('+', "2", exp, wtp));
//dovrei ottenere "-2*13+4*41"
return 0;
}
* from_included = 0, to_included=4
* OutPut: where = -1*132+1*51/*
Ottiene "(132-2*51)"
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define DEBUG 0
#define NUMERO 19 //numero max caratteri per a,b e k in k=ax+by e MCD(a,b)
long int powi(int num, int exp) {//10^0
long int ris=1;
while(exp--)
ris=ris*num;
return ris;
}
int intlen(int num) {
int i=1;
while(num/powi(10,i) != 0)
i++;
return i;
}
/*
* Delete characters from where starting from from_included position to to_included position.
* Positions included in the process of deleting.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_included = 0, to_included=4
* OutPut: where = -1*132+1*51{parsed_message}
* */
void strdeleteAt(char where[], const int from_included, const int to_included) {
char temp[strlen(where)-(to_included-from_included)+1];
int i;
for(i=0; i<from_included;i++)
temp[i] = where[i];
strcpy(temp+i, where+to_included+1);
strcpy(where, temp);
}
/*
* Deletes characters in where starting from from_inc position to to_inc position.
* Positions included in the process of deleting.
*
* Warning: passing a NULL pointer causes where to be returned.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_inc = strstr(where, "+");
* to_inc=strstr(where, "*");
* OutPut: where = 51-1*132+1*51{parsed_message}
*/
char* strdelete(char where[], char *from_inc, char* to_inc) {
if(to_inc == NULL || from_inc == NULL || where == NULL) return where;
printf("\nwhere: %s %d\nto_inc: %s %d\nfrom_inc: %s %d", where, strlen(where), to_inc, strlen(to_inc), from_inc, strlen(from_inc));
short len = strlen(where)-(to_inc-from_inc)+1;//from inc è pos 1 => errore
int pos=0;
if(len <= 0) return where;
char temp[len];
for(;from_inc-(where+pos) != 0; pos++)
*(temp+pos) = *(where+pos);
for(int posTo_inc=1; *(to_inc+posTo_inc) != '{parsed_message}';posTo_inc++,pos++)
*(temp+pos) = *(to_inc+posTo_inc);
*(temp+pos)='{parsed_message}';
strcpy(where,temp);
return where;
}
char* int2string(const int toBeParsed, char* whereToPut) {//-1
char digits[] = "0123456789";
int pos = 0, len=intlen(toBeParsed);
if(toBeParsed<=0) {
*whereToPut = '-';
pos++;
}
/* Determine the integer and putting it into whereToPut.
* for 38:
* whereToPut[0] = digits[38/10 -10*(38/100)] = '3'
* whereToPut[1] = digits[38/1 - 10*(38/10)] = '8'
* whereToPut => "38{parsed_message}"
*/
while(len-- > 0)
*(whereToPut + pos++) = digits[abs(toBeParsed) / powi(10,len) - 10*(abs(toBeParsed) / powi(10,len+1))];
*(whereToPut+pos)='{parsed_message}';
#if DEBUG
printf("\n\tint2string. toBeParsed=%d; whereToPut=%s\n",toBeParsed, whereToPut);
#endif
return whereToPut;
}
char* substringAt(char where[], int from_included, int to_included, char whereToPut[]) {
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(where+from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
char* substringAtstrstr(const char where[], char* from_included, const char* to_included, char whereToPut[]) {
if(to_included == NULL)
return strcpy(whereToPut, from_included);
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
int countChars(const char where[], const char character) {
int cont = 0;
for(; *where;where++)
if(*where == character) cont++;
return cont;
}
_Bool strcontain(const char where[], const char character) {
return countChars(where, character);
}
_Bool strcontainArray(const char where[], const char characters[], const short charactersLen)
{
_Bool r=1;
for(int i=0;i<charactersLen && r; i++)
r = r && countChars(where, characters[i]);
return r;
}
char* strstrArray(const char where[], const char chars[], const short charsLeng) {
//restituire l'occorrenza più vicina a where di una delle stringhe in chars. Per esempio per 132-2*51 e 132+2*51
char* occ = NULL;
for(int i=0; i < charsLeng; i++)
for(int pos = 0; *(where+pos); pos++)
if(*(where+pos) == chars[i])
if(occ == NULL)
{
occ = where+pos;
pos = strlen(where)-1;
}
else
if(occ != NULL && where+pos < occ)
{
occ = where+pos;
pos = strlen(where)-1;
}
return occ;
}
void emptystring(char string[]) {
while(*string++ = '{parsed_message}');
}
char* strBetweenPointersReverse(const char* yourstring, char *pos1, char *pos2, char whereToPut[]) {
#if DEBUG
puts("\nstringBetweenReverse()");
printf("\nyourstring %s", yourstring);
#endif
strcpy(whereToPut, yourstring);
strdelete(whereToPut, whereToPut, pos1);
strdelete(whereToPut, pos2, whereToPut+strlen(whereToPut));
return whereToPut;
#if DEBUG
printf("\nwhereToPut = %s", whereToPut);
#endif
}
/*
* This function find the first addend's components of an expression.
*
* @Params:
* exp is the expression where you want to find the first addend;
* mul2 is the memory location where the multiplier will be put. Must be "" when passed;
* num is the memory location where the number will be put. Must be "" when passed.
*
* @returns:
* true if the first addend was found. false otherwise.
*
* Example:
* exp = "è?(["; mul2 = ""; num = ""
* Output: mul2 = ""; ncum = ""
*
* Example :
* exp = "2*132-51"; mul2 = ""; num = ""
* Output: mul2 = "2", num = "132
* */
_Bool findAddendCmpts(const register char exp[], char mul2[], char num[]) {
char signs[] = {'+','-'}, *endnum = strstrArray(exp+1, signs, 2);
if(endnum != NULL)
endnum--;
if(*(exp) == '-') //132-2*51; -132-2*51; 2*132-2*51; -2*132-2*51; 132; -132; -2*132; 2*132
if(strcontain(exp, '*') && strstrArray(exp, signs,2) < strstr(exp, "*"))
{ //-132-2*51; -2*132-2*51; -2*132
char r[9+1];
strcpy(r,strBetweenPointersReverse(exp, strstr(exp, "-"), strstr(exp, "*")-1, mul2));
if(strcontainArray(r, signs, 2)) //is there, next to + or -, a number?
{ //-132-2*51
strcpy(mul2, "-1");//sure that exp contains *
substringAtstrstr(exp, exp+1, endnum, num);
}
else
{ //-2*132-2*51; -2*132
substringAtstrstr(exp, mul2+2, strstrArray(mul2+2, signs, 2), num);
}
}
else //-132
{
strcpy(mul2, "-1");
substringAtstrstr(exp, exp+1, endnum, num);
}
else //132-2*51; 2*132-2*51; 132; 2*132
if(strcontain(exp+1, '-') || strcontain(exp+1, '+'))
{ //132-2*51; 2*132-2*51
if(strcontain(exp, '*') && strstr(exp, "*") < strstrArray(exp, signs, 2)) //2*132-2*51
{
substringAtstrstr(exp,exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp,"*")+1, endnum, num);
}
else //132-2*51
{
strcpy(mul2, "1");
substringAtstrstr(exp, exp, endnum, num);
}
}
else
if(strcontain(exp, '*'))
{ //2*132
substringAtstrstr(exp, exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp, "*")+1, endnum, num); //all right with NULL ;) (see substringAtstrstr
}
else
{//132 that must be multiplied per n
strcpy(mul2, "1");
if(*exp=='+')
exp++;
strcpy(num, exp);
}
if(strcmp(num, "") == 0 || strcmp(mul2, "") == 0)
return 0;
else
return 1;
}
char *distribute(const char sign, char mul1[], char exp[], char result[]){
char mul2[5+1], num[5+1], signs[] = {'+', '-'}, *occ=NULL;;
short lung = 0, pos = 0;
while(findAddendCmpts(exp+pos, mul2, num))
{
if((occ=strstrArray(exp+pos+1, signs, 2))!=NULL)
pos+=occ -(exp+pos);
else pos+=strlen(exp+pos);
int mulDone = atoi(mul2) * atoi(mul1);
if(mulDone > 0) {
strcat(result, "+");
lung++;
}
char wtp[intlen(mulDone)+1];
strcat(result, int2string(mulDone, wtp));
lung+=intlen(mulDone);
strcat(result, "*");
lung++;
strcat(result, num);
lung+=strlen(num);
*(result+lung+2) = '{parsed_message}';
*num = '{parsed_message}';
*mul2 = '{parsed_message}';
}
return result;
}
int main(void) {
char wtp[10] = "";
char exp[] ="-13+2*41";
printf("\ndistribuzione: 2* (%s) =\n%s",exp,distribute('+', "2", exp, wtp));
//dovrei ottenere "-2*13+4*41"
return 0;
}
* */
void strdeleteAt(char where[], const int from_included, const int to_included) {
char temp[strlen(where)-(to_included-from_included)+1];
int i;
for(i=0; i<from_included;i++)
temp[i] = where[i];
strcpy(temp+i, where+to_included+1);
strcpy(where, temp);
}
/*
* Deletes characters in where starting from from_inc position to to_inc position.
* Positions included in the process of deleting.
*
* Warning: passing a NULL pointer causes where to be returned.
*
* Example:
* where = +1*51-1*132+1*51/*
Ottiene "(132-2*51)"
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define DEBUG 0
#define NUMERO 19 //numero max caratteri per a,b e k in k=ax+by e MCD(a,b)
long int powi(int num, int exp) {//10^0
long int ris=1;
while(exp--)
ris=ris*num;
return ris;
}
int intlen(int num) {
int i=1;
while(num/powi(10,i) != 0)
i++;
return i;
}
/*
* Delete characters from where starting from from_included position to to_included position.
* Positions included in the process of deleting.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_included = 0, to_included=4
* OutPut: where = -1*132+1*51{parsed_message}
* */
void strdeleteAt(char where[], const int from_included, const int to_included) {
char temp[strlen(where)-(to_included-from_included)+1];
int i;
for(i=0; i<from_included;i++)
temp[i] = where[i];
strcpy(temp+i, where+to_included+1);
strcpy(where, temp);
}
/*
* Deletes characters in where starting from from_inc position to to_inc position.
* Positions included in the process of deleting.
*
* Warning: passing a NULL pointer causes where to be returned.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_inc = strstr(where, "+");
* to_inc=strstr(where, "*");
* OutPut: where = 51-1*132+1*51{parsed_message}
*/
char* strdelete(char where[], char *from_inc, char* to_inc) {
if(to_inc == NULL || from_inc == NULL || where == NULL) return where;
printf("\nwhere: %s %d\nto_inc: %s %d\nfrom_inc: %s %d", where, strlen(where), to_inc, strlen(to_inc), from_inc, strlen(from_inc));
short len = strlen(where)-(to_inc-from_inc)+1;//from inc è pos 1 => errore
int pos=0;
if(len <= 0) return where;
char temp[len];
for(;from_inc-(where+pos) != 0; pos++)
*(temp+pos) = *(where+pos);
for(int posTo_inc=1; *(to_inc+posTo_inc) != '{parsed_message}';posTo_inc++,pos++)
*(temp+pos) = *(to_inc+posTo_inc);
*(temp+pos)='{parsed_message}';
strcpy(where,temp);
return where;
}
char* int2string(const int toBeParsed, char* whereToPut) {//-1
char digits[] = "0123456789";
int pos = 0, len=intlen(toBeParsed);
if(toBeParsed<=0) {
*whereToPut = '-';
pos++;
}
/* Determine the integer and putting it into whereToPut.
* for 38:
* whereToPut[0] = digits[38/10 -10*(38/100)] = '3'
* whereToPut[1] = digits[38/1 - 10*(38/10)] = '8'
* whereToPut => "38{parsed_message}"
*/
while(len-- > 0)
*(whereToPut + pos++) = digits[abs(toBeParsed) / powi(10,len) - 10*(abs(toBeParsed) / powi(10,len+1))];
*(whereToPut+pos)='{parsed_message}';
#if DEBUG
printf("\n\tint2string. toBeParsed=%d; whereToPut=%s\n",toBeParsed, whereToPut);
#endif
return whereToPut;
}
char* substringAt(char where[], int from_included, int to_included, char whereToPut[]) {
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(where+from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
char* substringAtstrstr(const char where[], char* from_included, const char* to_included, char whereToPut[]) {
if(to_included == NULL)
return strcpy(whereToPut, from_included);
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
int countChars(const char where[], const char character) {
int cont = 0;
for(; *where;where++)
if(*where == character) cont++;
return cont;
}
_Bool strcontain(const char where[], const char character) {
return countChars(where, character);
}
_Bool strcontainArray(const char where[], const char characters[], const short charactersLen)
{
_Bool r=1;
for(int i=0;i<charactersLen && r; i++)
r = r && countChars(where, characters[i]);
return r;
}
char* strstrArray(const char where[], const char chars[], const short charsLeng) {
//restituire l'occorrenza più vicina a where di una delle stringhe in chars. Per esempio per 132-2*51 e 132+2*51
char* occ = NULL;
for(int i=0; i < charsLeng; i++)
for(int pos = 0; *(where+pos); pos++)
if(*(where+pos) == chars[i])
if(occ == NULL)
{
occ = where+pos;
pos = strlen(where)-1;
}
else
if(occ != NULL && where+pos < occ)
{
occ = where+pos;
pos = strlen(where)-1;
}
return occ;
}
void emptystring(char string[]) {
while(*string++ = '{parsed_message}');
}
char* strBetweenPointersReverse(const char* yourstring, char *pos1, char *pos2, char whereToPut[]) {
#if DEBUG
puts("\nstringBetweenReverse()");
printf("\nyourstring %s", yourstring);
#endif
strcpy(whereToPut, yourstring);
strdelete(whereToPut, whereToPut, pos1);
strdelete(whereToPut, pos2, whereToPut+strlen(whereToPut));
return whereToPut;
#if DEBUG
printf("\nwhereToPut = %s", whereToPut);
#endif
}
/*
* This function find the first addend's components of an expression.
*
* @Params:
* exp is the expression where you want to find the first addend;
* mul2 is the memory location where the multiplier will be put. Must be "" when passed;
* num is the memory location where the number will be put. Must be "" when passed.
*
* @returns:
* true if the first addend was found. false otherwise.
*
* Example:
* exp = "è?(["; mul2 = ""; num = ""
* Output: mul2 = ""; ncum = ""
*
* Example :
* exp = "2*132-51"; mul2 = ""; num = ""
* Output: mul2 = "2", num = "132
* */
_Bool findAddendCmpts(const register char exp[], char mul2[], char num[]) {
char signs[] = {'+','-'}, *endnum = strstrArray(exp+1, signs, 2);
if(endnum != NULL)
endnum--;
if(*(exp) == '-') //132-2*51; -132-2*51; 2*132-2*51; -2*132-2*51; 132; -132; -2*132; 2*132
if(strcontain(exp, '*') && strstrArray(exp, signs,2) < strstr(exp, "*"))
{ //-132-2*51; -2*132-2*51; -2*132
char r[9+1];
strcpy(r,strBetweenPointersReverse(exp, strstr(exp, "-"), strstr(exp, "*")-1, mul2));
if(strcontainArray(r, signs, 2)) //is there, next to + or -, a number?
{ //-132-2*51
strcpy(mul2, "-1");//sure that exp contains *
substringAtstrstr(exp, exp+1, endnum, num);
}
else
{ //-2*132-2*51; -2*132
substringAtstrstr(exp, mul2+2, strstrArray(mul2+2, signs, 2), num);
}
}
else //-132
{
strcpy(mul2, "-1");
substringAtstrstr(exp, exp+1, endnum, num);
}
else //132-2*51; 2*132-2*51; 132; 2*132
if(strcontain(exp+1, '-') || strcontain(exp+1, '+'))
{ //132-2*51; 2*132-2*51
if(strcontain(exp, '*') && strstr(exp, "*") < strstrArray(exp, signs, 2)) //2*132-2*51
{
substringAtstrstr(exp,exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp,"*")+1, endnum, num);
}
else //132-2*51
{
strcpy(mul2, "1");
substringAtstrstr(exp, exp, endnum, num);
}
}
else
if(strcontain(exp, '*'))
{ //2*132
substringAtstrstr(exp, exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp, "*")+1, endnum, num); //all right with NULL ;) (see substringAtstrstr
}
else
{//132 that must be multiplied per n
strcpy(mul2, "1");
if(*exp=='+')
exp++;
strcpy(num, exp);
}
if(strcmp(num, "") == 0 || strcmp(mul2, "") == 0)
return 0;
else
return 1;
}
char *distribute(const char sign, char mul1[], char exp[], char result[]){
char mul2[5+1], num[5+1], signs[] = {'+', '-'}, *occ=NULL;;
short lung = 0, pos = 0;
while(findAddendCmpts(exp+pos, mul2, num))
{
if((occ=strstrArray(exp+pos+1, signs, 2))!=NULL)
pos+=occ -(exp+pos);
else pos+=strlen(exp+pos);
int mulDone = atoi(mul2) * atoi(mul1);
if(mulDone > 0) {
strcat(result, "+");
lung++;
}
char wtp[intlen(mulDone)+1];
strcat(result, int2string(mulDone, wtp));
lung+=intlen(mulDone);
strcat(result, "*");
lung++;
strcat(result, num);
lung+=strlen(num);
*(result+lung+2) = '{parsed_message}';
*num = '{parsed_message}';
*mul2 = '{parsed_message}';
}
return result;
}
int main(void) {
char wtp[10] = "";
char exp[] ="-13+2*41";
printf("\ndistribuzione: 2* (%s) =\n%s",exp,distribute('+', "2", exp, wtp));
//dovrei ottenere "-2*13+4*41"
return 0;
}
* from_inc = strstr(where, "+");
* to_inc=strstr(where, "*");
* OutPut: where = 51-1*132+1*51/*
Ottiene "(132-2*51)"
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define DEBUG 0
#define NUMERO 19 //numero max caratteri per a,b e k in k=ax+by e MCD(a,b)
long int powi(int num, int exp) {//10^0
long int ris=1;
while(exp--)
ris=ris*num;
return ris;
}
int intlen(int num) {
int i=1;
while(num/powi(10,i) != 0)
i++;
return i;
}
/*
* Delete characters from where starting from from_included position to to_included position.
* Positions included in the process of deleting.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_included = 0, to_included=4
* OutPut: where = -1*132+1*51{parsed_message}
* */
void strdeleteAt(char where[], const int from_included, const int to_included) {
char temp[strlen(where)-(to_included-from_included)+1];
int i;
for(i=0; i<from_included;i++)
temp[i] = where[i];
strcpy(temp+i, where+to_included+1);
strcpy(where, temp);
}
/*
* Deletes characters in where starting from from_inc position to to_inc position.
* Positions included in the process of deleting.
*
* Warning: passing a NULL pointer causes where to be returned.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_inc = strstr(where, "+");
* to_inc=strstr(where, "*");
* OutPut: where = 51-1*132+1*51{parsed_message}
*/
char* strdelete(char where[], char *from_inc, char* to_inc) {
if(to_inc == NULL || from_inc == NULL || where == NULL) return where;
printf("\nwhere: %s %d\nto_inc: %s %d\nfrom_inc: %s %d", where, strlen(where), to_inc, strlen(to_inc), from_inc, strlen(from_inc));
short len = strlen(where)-(to_inc-from_inc)+1;//from inc è pos 1 => errore
int pos=0;
if(len <= 0) return where;
char temp[len];
for(;from_inc-(where+pos) != 0; pos++)
*(temp+pos) = *(where+pos);
for(int posTo_inc=1; *(to_inc+posTo_inc) != '{parsed_message}';posTo_inc++,pos++)
*(temp+pos) = *(to_inc+posTo_inc);
*(temp+pos)='{parsed_message}';
strcpy(where,temp);
return where;
}
char* int2string(const int toBeParsed, char* whereToPut) {//-1
char digits[] = "0123456789";
int pos = 0, len=intlen(toBeParsed);
if(toBeParsed<=0) {
*whereToPut = '-';
pos++;
}
/* Determine the integer and putting it into whereToPut.
* for 38:
* whereToPut[0] = digits[38/10 -10*(38/100)] = '3'
* whereToPut[1] = digits[38/1 - 10*(38/10)] = '8'
* whereToPut => "38{parsed_message}"
*/
while(len-- > 0)
*(whereToPut + pos++) = digits[abs(toBeParsed) / powi(10,len) - 10*(abs(toBeParsed) / powi(10,len+1))];
*(whereToPut+pos)='{parsed_message}';
#if DEBUG
printf("\n\tint2string. toBeParsed=%d; whereToPut=%s\n",toBeParsed, whereToPut);
#endif
return whereToPut;
}
char* substringAt(char where[], int from_included, int to_included, char whereToPut[]) {
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(where+from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
char* substringAtstrstr(const char where[], char* from_included, const char* to_included, char whereToPut[]) {
if(to_included == NULL)
return strcpy(whereToPut, from_included);
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
int countChars(const char where[], const char character) {
int cont = 0;
for(; *where;where++)
if(*where == character) cont++;
return cont;
}
_Bool strcontain(const char where[], const char character) {
return countChars(where, character);
}
_Bool strcontainArray(const char where[], const char characters[], const short charactersLen)
{
_Bool r=1;
for(int i=0;i<charactersLen && r; i++)
r = r && countChars(where, characters[i]);
return r;
}
char* strstrArray(const char where[], const char chars[], const short charsLeng) {
//restituire l'occorrenza più vicina a where di una delle stringhe in chars. Per esempio per 132-2*51 e 132+2*51
char* occ = NULL;
for(int i=0; i < charsLeng; i++)
for(int pos = 0; *(where+pos); pos++)
if(*(where+pos) == chars[i])
if(occ == NULL)
{
occ = where+pos;
pos = strlen(where)-1;
}
else
if(occ != NULL && where+pos < occ)
{
occ = where+pos;
pos = strlen(where)-1;
}
return occ;
}
void emptystring(char string[]) {
while(*string++ = '{parsed_message}');
}
char* strBetweenPointersReverse(const char* yourstring, char *pos1, char *pos2, char whereToPut[]) {
#if DEBUG
puts("\nstringBetweenReverse()");
printf("\nyourstring %s", yourstring);
#endif
strcpy(whereToPut, yourstring);
strdelete(whereToPut, whereToPut, pos1);
strdelete(whereToPut, pos2, whereToPut+strlen(whereToPut));
return whereToPut;
#if DEBUG
printf("\nwhereToPut = %s", whereToPut);
#endif
}
/*
* This function find the first addend's components of an expression.
*
* @Params:
* exp is the expression where you want to find the first addend;
* mul2 is the memory location where the multiplier will be put. Must be "" when passed;
* num is the memory location where the number will be put. Must be "" when passed.
*
* @returns:
* true if the first addend was found. false otherwise.
*
* Example:
* exp = "è?(["; mul2 = ""; num = ""
* Output: mul2 = ""; ncum = ""
*
* Example :
* exp = "2*132-51"; mul2 = ""; num = ""
* Output: mul2 = "2", num = "132
* */
_Bool findAddendCmpts(const register char exp[], char mul2[], char num[]) {
char signs[] = {'+','-'}, *endnum = strstrArray(exp+1, signs, 2);
if(endnum != NULL)
endnum--;
if(*(exp) == '-') //132-2*51; -132-2*51; 2*132-2*51; -2*132-2*51; 132; -132; -2*132; 2*132
if(strcontain(exp, '*') && strstrArray(exp, signs,2) < strstr(exp, "*"))
{ //-132-2*51; -2*132-2*51; -2*132
char r[9+1];
strcpy(r,strBetweenPointersReverse(exp, strstr(exp, "-"), strstr(exp, "*")-1, mul2));
if(strcontainArray(r, signs, 2)) //is there, next to + or -, a number?
{ //-132-2*51
strcpy(mul2, "-1");//sure that exp contains *
substringAtstrstr(exp, exp+1, endnum, num);
}
else
{ //-2*132-2*51; -2*132
substringAtstrstr(exp, mul2+2, strstrArray(mul2+2, signs, 2), num);
}
}
else //-132
{
strcpy(mul2, "-1");
substringAtstrstr(exp, exp+1, endnum, num);
}
else //132-2*51; 2*132-2*51; 132; 2*132
if(strcontain(exp+1, '-') || strcontain(exp+1, '+'))
{ //132-2*51; 2*132-2*51
if(strcontain(exp, '*') && strstr(exp, "*") < strstrArray(exp, signs, 2)) //2*132-2*51
{
substringAtstrstr(exp,exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp,"*")+1, endnum, num);
}
else //132-2*51
{
strcpy(mul2, "1");
substringAtstrstr(exp, exp, endnum, num);
}
}
else
if(strcontain(exp, '*'))
{ //2*132
substringAtstrstr(exp, exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp, "*")+1, endnum, num); //all right with NULL ;) (see substringAtstrstr
}
else
{//132 that must be multiplied per n
strcpy(mul2, "1");
if(*exp=='+')
exp++;
strcpy(num, exp);
}
if(strcmp(num, "") == 0 || strcmp(mul2, "") == 0)
return 0;
else
return 1;
}
char *distribute(const char sign, char mul1[], char exp[], char result[]){
char mul2[5+1], num[5+1], signs[] = {'+', '-'}, *occ=NULL;;
short lung = 0, pos = 0;
while(findAddendCmpts(exp+pos, mul2, num))
{
if((occ=strstrArray(exp+pos+1, signs, 2))!=NULL)
pos+=occ -(exp+pos);
else pos+=strlen(exp+pos);
int mulDone = atoi(mul2) * atoi(mul1);
if(mulDone > 0) {
strcat(result, "+");
lung++;
}
char wtp[intlen(mulDone)+1];
strcat(result, int2string(mulDone, wtp));
lung+=intlen(mulDone);
strcat(result, "*");
lung++;
strcat(result, num);
lung+=strlen(num);
*(result+lung+2) = '{parsed_message}';
*num = '{parsed_message}';
*mul2 = '{parsed_message}';
}
return result;
}
int main(void) {
char wtp[10] = "";
char exp[] ="-13+2*41";
printf("\ndistribuzione: 2* (%s) =\n%s",exp,distribute('+', "2", exp, wtp));
//dovrei ottenere "-2*13+4*41"
return 0;
}
*/
char* strdelete(char where[], char *from_inc, char* to_inc) {
if(to_inc == NULL || from_inc == NULL || where == NULL) return where;
printf("\nwhere: %s %d\nto_inc: %s %d\nfrom_inc: %s %d", where, strlen(where), to_inc, strlen(to_inc), from_inc, strlen(from_inc));
short len = strlen(where)-(to_inc-from_inc)+1;//from inc è pos 1 => errore
int pos=0;
if(len <= 0) return where;
char temp[len];
for(;from_inc-(where+pos) != 0; pos++)
*(temp+pos) = *(where+pos);
for(int posTo_inc=1; *(to_inc+posTo_inc) != '/*
Ottiene "(132-2*51)"
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define DEBUG 0
#define NUMERO 19 //numero max caratteri per a,b e k in k=ax+by e MCD(a,b)
long int powi(int num, int exp) {//10^0
long int ris=1;
while(exp--)
ris=ris*num;
return ris;
}
int intlen(int num) {
int i=1;
while(num/powi(10,i) != 0)
i++;
return i;
}
/*
* Delete characters from where starting from from_included position to to_included position.
* Positions included in the process of deleting.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_included = 0, to_included=4
* OutPut: where = -1*132+1*51{parsed_message}
* */
void strdeleteAt(char where[], const int from_included, const int to_included) {
char temp[strlen(where)-(to_included-from_included)+1];
int i;
for(i=0; i<from_included;i++)
temp[i] = where[i];
strcpy(temp+i, where+to_included+1);
strcpy(where, temp);
}
/*
* Deletes characters in where starting from from_inc position to to_inc position.
* Positions included in the process of deleting.
*
* Warning: passing a NULL pointer causes where to be returned.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_inc = strstr(where, "+");
* to_inc=strstr(where, "*");
* OutPut: where = 51-1*132+1*51{parsed_message}
*/
char* strdelete(char where[], char *from_inc, char* to_inc) {
if(to_inc == NULL || from_inc == NULL || where == NULL) return where;
printf("\nwhere: %s %d\nto_inc: %s %d\nfrom_inc: %s %d", where, strlen(where), to_inc, strlen(to_inc), from_inc, strlen(from_inc));
short len = strlen(where)-(to_inc-from_inc)+1;//from inc è pos 1 => errore
int pos=0;
if(len <= 0) return where;
char temp[len];
for(;from_inc-(where+pos) != 0; pos++)
*(temp+pos) = *(where+pos);
for(int posTo_inc=1; *(to_inc+posTo_inc) != '{parsed_message}';posTo_inc++,pos++)
*(temp+pos) = *(to_inc+posTo_inc);
*(temp+pos)='{parsed_message}';
strcpy(where,temp);
return where;
}
char* int2string(const int toBeParsed, char* whereToPut) {//-1
char digits[] = "0123456789";
int pos = 0, len=intlen(toBeParsed);
if(toBeParsed<=0) {
*whereToPut = '-';
pos++;
}
/* Determine the integer and putting it into whereToPut.
* for 38:
* whereToPut[0] = digits[38/10 -10*(38/100)] = '3'
* whereToPut[1] = digits[38/1 - 10*(38/10)] = '8'
* whereToPut => "38{parsed_message}"
*/
while(len-- > 0)
*(whereToPut + pos++) = digits[abs(toBeParsed) / powi(10,len) - 10*(abs(toBeParsed) / powi(10,len+1))];
*(whereToPut+pos)='{parsed_message}';
#if DEBUG
printf("\n\tint2string. toBeParsed=%d; whereToPut=%s\n",toBeParsed, whereToPut);
#endif
return whereToPut;
}
char* substringAt(char where[], int from_included, int to_included, char whereToPut[]) {
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(where+from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
char* substringAtstrstr(const char where[], char* from_included, const char* to_included, char whereToPut[]) {
if(to_included == NULL)
return strcpy(whereToPut, from_included);
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
int countChars(const char where[], const char character) {
int cont = 0;
for(; *where;where++)
if(*where == character) cont++;
return cont;
}
_Bool strcontain(const char where[], const char character) {
return countChars(where, character);
}
_Bool strcontainArray(const char where[], const char characters[], const short charactersLen)
{
_Bool r=1;
for(int i=0;i<charactersLen && r; i++)
r = r && countChars(where, characters[i]);
return r;
}
char* strstrArray(const char where[], const char chars[], const short charsLeng) {
//restituire l'occorrenza più vicina a where di una delle stringhe in chars. Per esempio per 132-2*51 e 132+2*51
char* occ = NULL;
for(int i=0; i < charsLeng; i++)
for(int pos = 0; *(where+pos); pos++)
if(*(where+pos) == chars[i])
if(occ == NULL)
{
occ = where+pos;
pos = strlen(where)-1;
}
else
if(occ != NULL && where+pos < occ)
{
occ = where+pos;
pos = strlen(where)-1;
}
return occ;
}
void emptystring(char string[]) {
while(*string++ = '{parsed_message}');
}
char* strBetweenPointersReverse(const char* yourstring, char *pos1, char *pos2, char whereToPut[]) {
#if DEBUG
puts("\nstringBetweenReverse()");
printf("\nyourstring %s", yourstring);
#endif
strcpy(whereToPut, yourstring);
strdelete(whereToPut, whereToPut, pos1);
strdelete(whereToPut, pos2, whereToPut+strlen(whereToPut));
return whereToPut;
#if DEBUG
printf("\nwhereToPut = %s", whereToPut);
#endif
}
/*
* This function find the first addend's components of an expression.
*
* @Params:
* exp is the expression where you want to find the first addend;
* mul2 is the memory location where the multiplier will be put. Must be "" when passed;
* num is the memory location where the number will be put. Must be "" when passed.
*
* @returns:
* true if the first addend was found. false otherwise.
*
* Example:
* exp = "è?(["; mul2 = ""; num = ""
* Output: mul2 = ""; ncum = ""
*
* Example :
* exp = "2*132-51"; mul2 = ""; num = ""
* Output: mul2 = "2", num = "132
* */
_Bool findAddendCmpts(const register char exp[], char mul2[], char num[]) {
char signs[] = {'+','-'}, *endnum = strstrArray(exp+1, signs, 2);
if(endnum != NULL)
endnum--;
if(*(exp) == '-') //132-2*51; -132-2*51; 2*132-2*51; -2*132-2*51; 132; -132; -2*132; 2*132
if(strcontain(exp, '*') && strstrArray(exp, signs,2) < strstr(exp, "*"))
{ //-132-2*51; -2*132-2*51; -2*132
char r[9+1];
strcpy(r,strBetweenPointersReverse(exp, strstr(exp, "-"), strstr(exp, "*")-1, mul2));
if(strcontainArray(r, signs, 2)) //is there, next to + or -, a number?
{ //-132-2*51
strcpy(mul2, "-1");//sure that exp contains *
substringAtstrstr(exp, exp+1, endnum, num);
}
else
{ //-2*132-2*51; -2*132
substringAtstrstr(exp, mul2+2, strstrArray(mul2+2, signs, 2), num);
}
}
else //-132
{
strcpy(mul2, "-1");
substringAtstrstr(exp, exp+1, endnum, num);
}
else //132-2*51; 2*132-2*51; 132; 2*132
if(strcontain(exp+1, '-') || strcontain(exp+1, '+'))
{ //132-2*51; 2*132-2*51
if(strcontain(exp, '*') && strstr(exp, "*") < strstrArray(exp, signs, 2)) //2*132-2*51
{
substringAtstrstr(exp,exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp,"*")+1, endnum, num);
}
else //132-2*51
{
strcpy(mul2, "1");
substringAtstrstr(exp, exp, endnum, num);
}
}
else
if(strcontain(exp, '*'))
{ //2*132
substringAtstrstr(exp, exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp, "*")+1, endnum, num); //all right with NULL ;) (see substringAtstrstr
}
else
{//132 that must be multiplied per n
strcpy(mul2, "1");
if(*exp=='+')
exp++;
strcpy(num, exp);
}
if(strcmp(num, "") == 0 || strcmp(mul2, "") == 0)
return 0;
else
return 1;
}
char *distribute(const char sign, char mul1[], char exp[], char result[]){
char mul2[5+1], num[5+1], signs[] = {'+', '-'}, *occ=NULL;;
short lung = 0, pos = 0;
while(findAddendCmpts(exp+pos, mul2, num))
{
if((occ=strstrArray(exp+pos+1, signs, 2))!=NULL)
pos+=occ -(exp+pos);
else pos+=strlen(exp+pos);
int mulDone = atoi(mul2) * atoi(mul1);
if(mulDone > 0) {
strcat(result, "+");
lung++;
}
char wtp[intlen(mulDone)+1];
strcat(result, int2string(mulDone, wtp));
lung+=intlen(mulDone);
strcat(result, "*");
lung++;
strcat(result, num);
lung+=strlen(num);
*(result+lung+2) = '{parsed_message}';
*num = '{parsed_message}';
*mul2 = '{parsed_message}';
}
return result;
}
int main(void) {
char wtp[10] = "";
char exp[] ="-13+2*41";
printf("\ndistribuzione: 2* (%s) =\n%s",exp,distribute('+', "2", exp, wtp));
//dovrei ottenere "-2*13+4*41"
return 0;
}
';posTo_inc++,pos++)
*(temp+pos) = *(to_inc+posTo_inc);
*(temp+pos)='/*
Ottiene "(132-2*51)"
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define DEBUG 0
#define NUMERO 19 //numero max caratteri per a,b e k in k=ax+by e MCD(a,b)
long int powi(int num, int exp) {//10^0
long int ris=1;
while(exp--)
ris=ris*num;
return ris;
}
int intlen(int num) {
int i=1;
while(num/powi(10,i) != 0)
i++;
return i;
}
/*
* Delete characters from where starting from from_included position to to_included position.
* Positions included in the process of deleting.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_included = 0, to_included=4
* OutPut: where = -1*132+1*51{parsed_message}
* */
void strdeleteAt(char where[], const int from_included, const int to_included) {
char temp[strlen(where)-(to_included-from_included)+1];
int i;
for(i=0; i<from_included;i++)
temp[i] = where[i];
strcpy(temp+i, where+to_included+1);
strcpy(where, temp);
}
/*
* Deletes characters in where starting from from_inc position to to_inc position.
* Positions included in the process of deleting.
*
* Warning: passing a NULL pointer causes where to be returned.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_inc = strstr(where, "+");
* to_inc=strstr(where, "*");
* OutPut: where = 51-1*132+1*51{parsed_message}
*/
char* strdelete(char where[], char *from_inc, char* to_inc) {
if(to_inc == NULL || from_inc == NULL || where == NULL) return where;
printf("\nwhere: %s %d\nto_inc: %s %d\nfrom_inc: %s %d", where, strlen(where), to_inc, strlen(to_inc), from_inc, strlen(from_inc));
short len = strlen(where)-(to_inc-from_inc)+1;//from inc è pos 1 => errore
int pos=0;
if(len <= 0) return where;
char temp[len];
for(;from_inc-(where+pos) != 0; pos++)
*(temp+pos) = *(where+pos);
for(int posTo_inc=1; *(to_inc+posTo_inc) != '{parsed_message}';posTo_inc++,pos++)
*(temp+pos) = *(to_inc+posTo_inc);
*(temp+pos)='{parsed_message}';
strcpy(where,temp);
return where;
}
char* int2string(const int toBeParsed, char* whereToPut) {//-1
char digits[] = "0123456789";
int pos = 0, len=intlen(toBeParsed);
if(toBeParsed<=0) {
*whereToPut = '-';
pos++;
}
/* Determine the integer and putting it into whereToPut.
* for 38:
* whereToPut[0] = digits[38/10 -10*(38/100)] = '3'
* whereToPut[1] = digits[38/1 - 10*(38/10)] = '8'
* whereToPut => "38{parsed_message}"
*/
while(len-- > 0)
*(whereToPut + pos++) = digits[abs(toBeParsed) / powi(10,len) - 10*(abs(toBeParsed) / powi(10,len+1))];
*(whereToPut+pos)='{parsed_message}';
#if DEBUG
printf("\n\tint2string. toBeParsed=%d; whereToPut=%s\n",toBeParsed, whereToPut);
#endif
return whereToPut;
}
char* substringAt(char where[], int from_included, int to_included, char whereToPut[]) {
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(where+from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
char* substringAtstrstr(const char where[], char* from_included, const char* to_included, char whereToPut[]) {
if(to_included == NULL)
return strcpy(whereToPut, from_included);
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
int countChars(const char where[], const char character) {
int cont = 0;
for(; *where;where++)
if(*where == character) cont++;
return cont;
}
_Bool strcontain(const char where[], const char character) {
return countChars(where, character);
}
_Bool strcontainArray(const char where[], const char characters[], const short charactersLen)
{
_Bool r=1;
for(int i=0;i<charactersLen && r; i++)
r = r && countChars(where, characters[i]);
return r;
}
char* strstrArray(const char where[], const char chars[], const short charsLeng) {
//restituire l'occorrenza più vicina a where di una delle stringhe in chars. Per esempio per 132-2*51 e 132+2*51
char* occ = NULL;
for(int i=0; i < charsLeng; i++)
for(int pos = 0; *(where+pos); pos++)
if(*(where+pos) == chars[i])
if(occ == NULL)
{
occ = where+pos;
pos = strlen(where)-1;
}
else
if(occ != NULL && where+pos < occ)
{
occ = where+pos;
pos = strlen(where)-1;
}
return occ;
}
void emptystring(char string[]) {
while(*string++ = '{parsed_message}');
}
char* strBetweenPointersReverse(const char* yourstring, char *pos1, char *pos2, char whereToPut[]) {
#if DEBUG
puts("\nstringBetweenReverse()");
printf("\nyourstring %s", yourstring);
#endif
strcpy(whereToPut, yourstring);
strdelete(whereToPut, whereToPut, pos1);
strdelete(whereToPut, pos2, whereToPut+strlen(whereToPut));
return whereToPut;
#if DEBUG
printf("\nwhereToPut = %s", whereToPut);
#endif
}
/*
* This function find the first addend's components of an expression.
*
* @Params:
* exp is the expression where you want to find the first addend;
* mul2 is the memory location where the multiplier will be put. Must be "" when passed;
* num is the memory location where the number will be put. Must be "" when passed.
*
* @returns:
* true if the first addend was found. false otherwise.
*
* Example:
* exp = "è?(["; mul2 = ""; num = ""
* Output: mul2 = ""; ncum = ""
*
* Example :
* exp = "2*132-51"; mul2 = ""; num = ""
* Output: mul2 = "2", num = "132
* */
_Bool findAddendCmpts(const register char exp[], char mul2[], char num[]) {
char signs[] = {'+','-'}, *endnum = strstrArray(exp+1, signs, 2);
if(endnum != NULL)
endnum--;
if(*(exp) == '-') //132-2*51; -132-2*51; 2*132-2*51; -2*132-2*51; 132; -132; -2*132; 2*132
if(strcontain(exp, '*') && strstrArray(exp, signs,2) < strstr(exp, "*"))
{ //-132-2*51; -2*132-2*51; -2*132
char r[9+1];
strcpy(r,strBetweenPointersReverse(exp, strstr(exp, "-"), strstr(exp, "*")-1, mul2));
if(strcontainArray(r, signs, 2)) //is there, next to + or -, a number?
{ //-132-2*51
strcpy(mul2, "-1");//sure that exp contains *
substringAtstrstr(exp, exp+1, endnum, num);
}
else
{ //-2*132-2*51; -2*132
substringAtstrstr(exp, mul2+2, strstrArray(mul2+2, signs, 2), num);
}
}
else //-132
{
strcpy(mul2, "-1");
substringAtstrstr(exp, exp+1, endnum, num);
}
else //132-2*51; 2*132-2*51; 132; 2*132
if(strcontain(exp+1, '-') || strcontain(exp+1, '+'))
{ //132-2*51; 2*132-2*51
if(strcontain(exp, '*') && strstr(exp, "*") < strstrArray(exp, signs, 2)) //2*132-2*51
{
substringAtstrstr(exp,exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp,"*")+1, endnum, num);
}
else //132-2*51
{
strcpy(mul2, "1");
substringAtstrstr(exp, exp, endnum, num);
}
}
else
if(strcontain(exp, '*'))
{ //2*132
substringAtstrstr(exp, exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp, "*")+1, endnum, num); //all right with NULL ;) (see substringAtstrstr
}
else
{//132 that must be multiplied per n
strcpy(mul2, "1");
if(*exp=='+')
exp++;
strcpy(num, exp);
}
if(strcmp(num, "") == 0 || strcmp(mul2, "") == 0)
return 0;
else
return 1;
}
char *distribute(const char sign, char mul1[], char exp[], char result[]){
char mul2[5+1], num[5+1], signs[] = {'+', '-'}, *occ=NULL;;
short lung = 0, pos = 0;
while(findAddendCmpts(exp+pos, mul2, num))
{
if((occ=strstrArray(exp+pos+1, signs, 2))!=NULL)
pos+=occ -(exp+pos);
else pos+=strlen(exp+pos);
int mulDone = atoi(mul2) * atoi(mul1);
if(mulDone > 0) {
strcat(result, "+");
lung++;
}
char wtp[intlen(mulDone)+1];
strcat(result, int2string(mulDone, wtp));
lung+=intlen(mulDone);
strcat(result, "*");
lung++;
strcat(result, num);
lung+=strlen(num);
*(result+lung+2) = '{parsed_message}';
*num = '{parsed_message}';
*mul2 = '{parsed_message}';
}
return result;
}
int main(void) {
char wtp[10] = "";
char exp[] ="-13+2*41";
printf("\ndistribuzione: 2* (%s) =\n%s",exp,distribute('+', "2", exp, wtp));
//dovrei ottenere "-2*13+4*41"
return 0;
}
';
strcpy(where,temp);
return where;
}
char* int2string(const int toBeParsed, char* whereToPut) {//-1
char digits[] = "0123456789";
int pos = 0, len=intlen(toBeParsed);
if(toBeParsed<=0) {
*whereToPut = '-';
pos++;
}
/* Determine the integer and putting it into whereToPut.
* for 38:
* whereToPut[0] = digits[38/10 -10*(38/100)] = '3'
* whereToPut[1] = digits[38/1 - 10*(38/10)] = '8'
* whereToPut => "38/*
Ottiene "(132-2*51)"
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define DEBUG 0
#define NUMERO 19 //numero max caratteri per a,b e k in k=ax+by e MCD(a,b)
long int powi(int num, int exp) {//10^0
long int ris=1;
while(exp--)
ris=ris*num;
return ris;
}
int intlen(int num) {
int i=1;
while(num/powi(10,i) != 0)
i++;
return i;
}
/*
* Delete characters from where starting from from_included position to to_included position.
* Positions included in the process of deleting.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_included = 0, to_included=4
* OutPut: where = -1*132+1*51{parsed_message}
* */
void strdeleteAt(char where[], const int from_included, const int to_included) {
char temp[strlen(where)-(to_included-from_included)+1];
int i;
for(i=0; i<from_included;i++)
temp[i] = where[i];
strcpy(temp+i, where+to_included+1);
strcpy(where, temp);
}
/*
* Deletes characters in where starting from from_inc position to to_inc position.
* Positions included in the process of deleting.
*
* Warning: passing a NULL pointer causes where to be returned.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_inc = strstr(where, "+");
* to_inc=strstr(where, "*");
* OutPut: where = 51-1*132+1*51{parsed_message}
*/
char* strdelete(char where[], char *from_inc, char* to_inc) {
if(to_inc == NULL || from_inc == NULL || where == NULL) return where;
printf("\nwhere: %s %d\nto_inc: %s %d\nfrom_inc: %s %d", where, strlen(where), to_inc, strlen(to_inc), from_inc, strlen(from_inc));
short len = strlen(where)-(to_inc-from_inc)+1;//from inc è pos 1 => errore
int pos=0;
if(len <= 0) return where;
char temp[len];
for(;from_inc-(where+pos) != 0; pos++)
*(temp+pos) = *(where+pos);
for(int posTo_inc=1; *(to_inc+posTo_inc) != '{parsed_message}';posTo_inc++,pos++)
*(temp+pos) = *(to_inc+posTo_inc);
*(temp+pos)='{parsed_message}';
strcpy(where,temp);
return where;
}
char* int2string(const int toBeParsed, char* whereToPut) {//-1
char digits[] = "0123456789";
int pos = 0, len=intlen(toBeParsed);
if(toBeParsed<=0) {
*whereToPut = '-';
pos++;
}
/* Determine the integer and putting it into whereToPut.
* for 38:
* whereToPut[0] = digits[38/10 -10*(38/100)] = '3'
* whereToPut[1] = digits[38/1 - 10*(38/10)] = '8'
* whereToPut => "38{parsed_message}"
*/
while(len-- > 0)
*(whereToPut + pos++) = digits[abs(toBeParsed) / powi(10,len) - 10*(abs(toBeParsed) / powi(10,len+1))];
*(whereToPut+pos)='{parsed_message}';
#if DEBUG
printf("\n\tint2string. toBeParsed=%d; whereToPut=%s\n",toBeParsed, whereToPut);
#endif
return whereToPut;
}
char* substringAt(char where[], int from_included, int to_included, char whereToPut[]) {
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(where+from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
char* substringAtstrstr(const char where[], char* from_included, const char* to_included, char whereToPut[]) {
if(to_included == NULL)
return strcpy(whereToPut, from_included);
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
int countChars(const char where[], const char character) {
int cont = 0;
for(; *where;where++)
if(*where == character) cont++;
return cont;
}
_Bool strcontain(const char where[], const char character) {
return countChars(where, character);
}
_Bool strcontainArray(const char where[], const char characters[], const short charactersLen)
{
_Bool r=1;
for(int i=0;i<charactersLen && r; i++)
r = r && countChars(where, characters[i]);
return r;
}
char* strstrArray(const char where[], const char chars[], const short charsLeng) {
//restituire l'occorrenza più vicina a where di una delle stringhe in chars. Per esempio per 132-2*51 e 132+2*51
char* occ = NULL;
for(int i=0; i < charsLeng; i++)
for(int pos = 0; *(where+pos); pos++)
if(*(where+pos) == chars[i])
if(occ == NULL)
{
occ = where+pos;
pos = strlen(where)-1;
}
else
if(occ != NULL && where+pos < occ)
{
occ = where+pos;
pos = strlen(where)-1;
}
return occ;
}
void emptystring(char string[]) {
while(*string++ = '{parsed_message}');
}
char* strBetweenPointersReverse(const char* yourstring, char *pos1, char *pos2, char whereToPut[]) {
#if DEBUG
puts("\nstringBetweenReverse()");
printf("\nyourstring %s", yourstring);
#endif
strcpy(whereToPut, yourstring);
strdelete(whereToPut, whereToPut, pos1);
strdelete(whereToPut, pos2, whereToPut+strlen(whereToPut));
return whereToPut;
#if DEBUG
printf("\nwhereToPut = %s", whereToPut);
#endif
}
/*
* This function find the first addend's components of an expression.
*
* @Params:
* exp is the expression where you want to find the first addend;
* mul2 is the memory location where the multiplier will be put. Must be "" when passed;
* num is the memory location where the number will be put. Must be "" when passed.
*
* @returns:
* true if the first addend was found. false otherwise.
*
* Example:
* exp = "è?(["; mul2 = ""; num = ""
* Output: mul2 = ""; ncum = ""
*
* Example :
* exp = "2*132-51"; mul2 = ""; num = ""
* Output: mul2 = "2", num = "132
* */
_Bool findAddendCmpts(const register char exp[], char mul2[], char num[]) {
char signs[] = {'+','-'}, *endnum = strstrArray(exp+1, signs, 2);
if(endnum != NULL)
endnum--;
if(*(exp) == '-') //132-2*51; -132-2*51; 2*132-2*51; -2*132-2*51; 132; -132; -2*132; 2*132
if(strcontain(exp, '*') && strstrArray(exp, signs,2) < strstr(exp, "*"))
{ //-132-2*51; -2*132-2*51; -2*132
char r[9+1];
strcpy(r,strBetweenPointersReverse(exp, strstr(exp, "-"), strstr(exp, "*")-1, mul2));
if(strcontainArray(r, signs, 2)) //is there, next to + or -, a number?
{ //-132-2*51
strcpy(mul2, "-1");//sure that exp contains *
substringAtstrstr(exp, exp+1, endnum, num);
}
else
{ //-2*132-2*51; -2*132
substringAtstrstr(exp, mul2+2, strstrArray(mul2+2, signs, 2), num);
}
}
else //-132
{
strcpy(mul2, "-1");
substringAtstrstr(exp, exp+1, endnum, num);
}
else //132-2*51; 2*132-2*51; 132; 2*132
if(strcontain(exp+1, '-') || strcontain(exp+1, '+'))
{ //132-2*51; 2*132-2*51
if(strcontain(exp, '*') && strstr(exp, "*") < strstrArray(exp, signs, 2)) //2*132-2*51
{
substringAtstrstr(exp,exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp,"*")+1, endnum, num);
}
else //132-2*51
{
strcpy(mul2, "1");
substringAtstrstr(exp, exp, endnum, num);
}
}
else
if(strcontain(exp, '*'))
{ //2*132
substringAtstrstr(exp, exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp, "*")+1, endnum, num); //all right with NULL ;) (see substringAtstrstr
}
else
{//132 that must be multiplied per n
strcpy(mul2, "1");
if(*exp=='+')
exp++;
strcpy(num, exp);
}
if(strcmp(num, "") == 0 || strcmp(mul2, "") == 0)
return 0;
else
return 1;
}
char *distribute(const char sign, char mul1[], char exp[], char result[]){
char mul2[5+1], num[5+1], signs[] = {'+', '-'}, *occ=NULL;;
short lung = 0, pos = 0;
while(findAddendCmpts(exp+pos, mul2, num))
{
if((occ=strstrArray(exp+pos+1, signs, 2))!=NULL)
pos+=occ -(exp+pos);
else pos+=strlen(exp+pos);
int mulDone = atoi(mul2) * atoi(mul1);
if(mulDone > 0) {
strcat(result, "+");
lung++;
}
char wtp[intlen(mulDone)+1];
strcat(result, int2string(mulDone, wtp));
lung+=intlen(mulDone);
strcat(result, "*");
lung++;
strcat(result, num);
lung+=strlen(num);
*(result+lung+2) = '{parsed_message}';
*num = '{parsed_message}';
*mul2 = '{parsed_message}';
}
return result;
}
int main(void) {
char wtp[10] = "";
char exp[] ="-13+2*41";
printf("\ndistribuzione: 2* (%s) =\n%s",exp,distribute('+', "2", exp, wtp));
//dovrei ottenere "-2*13+4*41"
return 0;
}
"
*/
while(len-- > 0)
*(whereToPut + pos++) = digits[abs(toBeParsed) / powi(10,len) - 10*(abs(toBeParsed) / powi(10,len+1))];
*(whereToPut+pos)='/*
Ottiene "(132-2*51)"
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define DEBUG 0
#define NUMERO 19 //numero max caratteri per a,b e k in k=ax+by e MCD(a,b)
long int powi(int num, int exp) {//10^0
long int ris=1;
while(exp--)
ris=ris*num;
return ris;
}
int intlen(int num) {
int i=1;
while(num/powi(10,i) != 0)
i++;
return i;
}
/*
* Delete characters from where starting from from_included position to to_included position.
* Positions included in the process of deleting.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_included = 0, to_included=4
* OutPut: where = -1*132+1*51{parsed_message}
* */
void strdeleteAt(char where[], const int from_included, const int to_included) {
char temp[strlen(where)-(to_included-from_included)+1];
int i;
for(i=0; i<from_included;i++)
temp[i] = where[i];
strcpy(temp+i, where+to_included+1);
strcpy(where, temp);
}
/*
* Deletes characters in where starting from from_inc position to to_inc position.
* Positions included in the process of deleting.
*
* Warning: passing a NULL pointer causes where to be returned.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_inc = strstr(where, "+");
* to_inc=strstr(where, "*");
* OutPut: where = 51-1*132+1*51{parsed_message}
*/
char* strdelete(char where[], char *from_inc, char* to_inc) {
if(to_inc == NULL || from_inc == NULL || where == NULL) return where;
printf("\nwhere: %s %d\nto_inc: %s %d\nfrom_inc: %s %d", where, strlen(where), to_inc, strlen(to_inc), from_inc, strlen(from_inc));
short len = strlen(where)-(to_inc-from_inc)+1;//from inc è pos 1 => errore
int pos=0;
if(len <= 0) return where;
char temp[len];
for(;from_inc-(where+pos) != 0; pos++)
*(temp+pos) = *(where+pos);
for(int posTo_inc=1; *(to_inc+posTo_inc) != '{parsed_message}';posTo_inc++,pos++)
*(temp+pos) = *(to_inc+posTo_inc);
*(temp+pos)='{parsed_message}';
strcpy(where,temp);
return where;
}
char* int2string(const int toBeParsed, char* whereToPut) {//-1
char digits[] = "0123456789";
int pos = 0, len=intlen(toBeParsed);
if(toBeParsed<=0) {
*whereToPut = '-';
pos++;
}
/* Determine the integer and putting it into whereToPut.
* for 38:
* whereToPut[0] = digits[38/10 -10*(38/100)] = '3'
* whereToPut[1] = digits[38/1 - 10*(38/10)] = '8'
* whereToPut => "38{parsed_message}"
*/
while(len-- > 0)
*(whereToPut + pos++) = digits[abs(toBeParsed) / powi(10,len) - 10*(abs(toBeParsed) / powi(10,len+1))];
*(whereToPut+pos)='{parsed_message}';
#if DEBUG
printf("\n\tint2string. toBeParsed=%d; whereToPut=%s\n",toBeParsed, whereToPut);
#endif
return whereToPut;
}
char* substringAt(char where[], int from_included, int to_included, char whereToPut[]) {
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(where+from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
char* substringAtstrstr(const char where[], char* from_included, const char* to_included, char whereToPut[]) {
if(to_included == NULL)
return strcpy(whereToPut, from_included);
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
int countChars(const char where[], const char character) {
int cont = 0;
for(; *where;where++)
if(*where == character) cont++;
return cont;
}
_Bool strcontain(const char where[], const char character) {
return countChars(where, character);
}
_Bool strcontainArray(const char where[], const char characters[], const short charactersLen)
{
_Bool r=1;
for(int i=0;i<charactersLen && r; i++)
r = r && countChars(where, characters[i]);
return r;
}
char* strstrArray(const char where[], const char chars[], const short charsLeng) {
//restituire l'occorrenza più vicina a where di una delle stringhe in chars. Per esempio per 132-2*51 e 132+2*51
char* occ = NULL;
for(int i=0; i < charsLeng; i++)
for(int pos = 0; *(where+pos); pos++)
if(*(where+pos) == chars[i])
if(occ == NULL)
{
occ = where+pos;
pos = strlen(where)-1;
}
else
if(occ != NULL && where+pos < occ)
{
occ = where+pos;
pos = strlen(where)-1;
}
return occ;
}
void emptystring(char string[]) {
while(*string++ = '{parsed_message}');
}
char* strBetweenPointersReverse(const char* yourstring, char *pos1, char *pos2, char whereToPut[]) {
#if DEBUG
puts("\nstringBetweenReverse()");
printf("\nyourstring %s", yourstring);
#endif
strcpy(whereToPut, yourstring);
strdelete(whereToPut, whereToPut, pos1);
strdelete(whereToPut, pos2, whereToPut+strlen(whereToPut));
return whereToPut;
#if DEBUG
printf("\nwhereToPut = %s", whereToPut);
#endif
}
/*
* This function find the first addend's components of an expression.
*
* @Params:
* exp is the expression where you want to find the first addend;
* mul2 is the memory location where the multiplier will be put. Must be "" when passed;
* num is the memory location where the number will be put. Must be "" when passed.
*
* @returns:
* true if the first addend was found. false otherwise.
*
* Example:
* exp = "è?(["; mul2 = ""; num = ""
* Output: mul2 = ""; ncum = ""
*
* Example :
* exp = "2*132-51"; mul2 = ""; num = ""
* Output: mul2 = "2", num = "132
* */
_Bool findAddendCmpts(const register char exp[], char mul2[], char num[]) {
char signs[] = {'+','-'}, *endnum = strstrArray(exp+1, signs, 2);
if(endnum != NULL)
endnum--;
if(*(exp) == '-') //132-2*51; -132-2*51; 2*132-2*51; -2*132-2*51; 132; -132; -2*132; 2*132
if(strcontain(exp, '*') && strstrArray(exp, signs,2) < strstr(exp, "*"))
{ //-132-2*51; -2*132-2*51; -2*132
char r[9+1];
strcpy(r,strBetweenPointersReverse(exp, strstr(exp, "-"), strstr(exp, "*")-1, mul2));
if(strcontainArray(r, signs, 2)) //is there, next to + or -, a number?
{ //-132-2*51
strcpy(mul2, "-1");//sure that exp contains *
substringAtstrstr(exp, exp+1, endnum, num);
}
else
{ //-2*132-2*51; -2*132
substringAtstrstr(exp, mul2+2, strstrArray(mul2+2, signs, 2), num);
}
}
else //-132
{
strcpy(mul2, "-1");
substringAtstrstr(exp, exp+1, endnum, num);
}
else //132-2*51; 2*132-2*51; 132; 2*132
if(strcontain(exp+1, '-') || strcontain(exp+1, '+'))
{ //132-2*51; 2*132-2*51
if(strcontain(exp, '*') && strstr(exp, "*") < strstrArray(exp, signs, 2)) //2*132-2*51
{
substringAtstrstr(exp,exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp,"*")+1, endnum, num);
}
else //132-2*51
{
strcpy(mul2, "1");
substringAtstrstr(exp, exp, endnum, num);
}
}
else
if(strcontain(exp, '*'))
{ //2*132
substringAtstrstr(exp, exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp, "*")+1, endnum, num); //all right with NULL ;) (see substringAtstrstr
}
else
{//132 that must be multiplied per n
strcpy(mul2, "1");
if(*exp=='+')
exp++;
strcpy(num, exp);
}
if(strcmp(num, "") == 0 || strcmp(mul2, "") == 0)
return 0;
else
return 1;
}
char *distribute(const char sign, char mul1[], char exp[], char result[]){
char mul2[5+1], num[5+1], signs[] = {'+', '-'}, *occ=NULL;;
short lung = 0, pos = 0;
while(findAddendCmpts(exp+pos, mul2, num))
{
if((occ=strstrArray(exp+pos+1, signs, 2))!=NULL)
pos+=occ -(exp+pos);
else pos+=strlen(exp+pos);
int mulDone = atoi(mul2) * atoi(mul1);
if(mulDone > 0) {
strcat(result, "+");
lung++;
}
char wtp[intlen(mulDone)+1];
strcat(result, int2string(mulDone, wtp));
lung+=intlen(mulDone);
strcat(result, "*");
lung++;
strcat(result, num);
lung+=strlen(num);
*(result+lung+2) = '{parsed_message}';
*num = '{parsed_message}';
*mul2 = '{parsed_message}';
}
return result;
}
int main(void) {
char wtp[10] = "";
char exp[] ="-13+2*41";
printf("\ndistribuzione: 2* (%s) =\n%s",exp,distribute('+', "2", exp, wtp));
//dovrei ottenere "-2*13+4*41"
return 0;
}
';
#if DEBUG
printf("\n\tint2string. toBeParsed=%d; whereToPut=%s\n",toBeParsed, whereToPut);
#endif
return whereToPut;
}
char* substringAt(char where[], int from_included, int to_included, char whereToPut[]) {
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(where+from_included);
*(whereToPut+pos) = '/*
Ottiene "(132-2*51)"
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define DEBUG 0
#define NUMERO 19 //numero max caratteri per a,b e k in k=ax+by e MCD(a,b)
long int powi(int num, int exp) {//10^0
long int ris=1;
while(exp--)
ris=ris*num;
return ris;
}
int intlen(int num) {
int i=1;
while(num/powi(10,i) != 0)
i++;
return i;
}
/*
* Delete characters from where starting from from_included position to to_included position.
* Positions included in the process of deleting.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_included = 0, to_included=4
* OutPut: where = -1*132+1*51{parsed_message}
* */
void strdeleteAt(char where[], const int from_included, const int to_included) {
char temp[strlen(where)-(to_included-from_included)+1];
int i;
for(i=0; i<from_included;i++)
temp[i] = where[i];
strcpy(temp+i, where+to_included+1);
strcpy(where, temp);
}
/*
* Deletes characters in where starting from from_inc position to to_inc position.
* Positions included in the process of deleting.
*
* Warning: passing a NULL pointer causes where to be returned.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_inc = strstr(where, "+");
* to_inc=strstr(where, "*");
* OutPut: where = 51-1*132+1*51{parsed_message}
*/
char* strdelete(char where[], char *from_inc, char* to_inc) {
if(to_inc == NULL || from_inc == NULL || where == NULL) return where;
printf("\nwhere: %s %d\nto_inc: %s %d\nfrom_inc: %s %d", where, strlen(where), to_inc, strlen(to_inc), from_inc, strlen(from_inc));
short len = strlen(where)-(to_inc-from_inc)+1;//from inc è pos 1 => errore
int pos=0;
if(len <= 0) return where;
char temp[len];
for(;from_inc-(where+pos) != 0; pos++)
*(temp+pos) = *(where+pos);
for(int posTo_inc=1; *(to_inc+posTo_inc) != '{parsed_message}';posTo_inc++,pos++)
*(temp+pos) = *(to_inc+posTo_inc);
*(temp+pos)='{parsed_message}';
strcpy(where,temp);
return where;
}
char* int2string(const int toBeParsed, char* whereToPut) {//-1
char digits[] = "0123456789";
int pos = 0, len=intlen(toBeParsed);
if(toBeParsed<=0) {
*whereToPut = '-';
pos++;
}
/* Determine the integer and putting it into whereToPut.
* for 38:
* whereToPut[0] = digits[38/10 -10*(38/100)] = '3'
* whereToPut[1] = digits[38/1 - 10*(38/10)] = '8'
* whereToPut => "38{parsed_message}"
*/
while(len-- > 0)
*(whereToPut + pos++) = digits[abs(toBeParsed) / powi(10,len) - 10*(abs(toBeParsed) / powi(10,len+1))];
*(whereToPut+pos)='{parsed_message}';
#if DEBUG
printf("\n\tint2string. toBeParsed=%d; whereToPut=%s\n",toBeParsed, whereToPut);
#endif
return whereToPut;
}
char* substringAt(char where[], int from_included, int to_included, char whereToPut[]) {
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(where+from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
char* substringAtstrstr(const char where[], char* from_included, const char* to_included, char whereToPut[]) {
if(to_included == NULL)
return strcpy(whereToPut, from_included);
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
int countChars(const char where[], const char character) {
int cont = 0;
for(; *where;where++)
if(*where == character) cont++;
return cont;
}
_Bool strcontain(const char where[], const char character) {
return countChars(where, character);
}
_Bool strcontainArray(const char where[], const char characters[], const short charactersLen)
{
_Bool r=1;
for(int i=0;i<charactersLen && r; i++)
r = r && countChars(where, characters[i]);
return r;
}
char* strstrArray(const char where[], const char chars[], const short charsLeng) {
//restituire l'occorrenza più vicina a where di una delle stringhe in chars. Per esempio per 132-2*51 e 132+2*51
char* occ = NULL;
for(int i=0; i < charsLeng; i++)
for(int pos = 0; *(where+pos); pos++)
if(*(where+pos) == chars[i])
if(occ == NULL)
{
occ = where+pos;
pos = strlen(where)-1;
}
else
if(occ != NULL && where+pos < occ)
{
occ = where+pos;
pos = strlen(where)-1;
}
return occ;
}
void emptystring(char string[]) {
while(*string++ = '{parsed_message}');
}
char* strBetweenPointersReverse(const char* yourstring, char *pos1, char *pos2, char whereToPut[]) {
#if DEBUG
puts("\nstringBetweenReverse()");
printf("\nyourstring %s", yourstring);
#endif
strcpy(whereToPut, yourstring);
strdelete(whereToPut, whereToPut, pos1);
strdelete(whereToPut, pos2, whereToPut+strlen(whereToPut));
return whereToPut;
#if DEBUG
printf("\nwhereToPut = %s", whereToPut);
#endif
}
/*
* This function find the first addend's components of an expression.
*
* @Params:
* exp is the expression where you want to find the first addend;
* mul2 is the memory location where the multiplier will be put. Must be "" when passed;
* num is the memory location where the number will be put. Must be "" when passed.
*
* @returns:
* true if the first addend was found. false otherwise.
*
* Example:
* exp = "è?(["; mul2 = ""; num = ""
* Output: mul2 = ""; ncum = ""
*
* Example :
* exp = "2*132-51"; mul2 = ""; num = ""
* Output: mul2 = "2", num = "132
* */
_Bool findAddendCmpts(const register char exp[], char mul2[], char num[]) {
char signs[] = {'+','-'}, *endnum = strstrArray(exp+1, signs, 2);
if(endnum != NULL)
endnum--;
if(*(exp) == '-') //132-2*51; -132-2*51; 2*132-2*51; -2*132-2*51; 132; -132; -2*132; 2*132
if(strcontain(exp, '*') && strstrArray(exp, signs,2) < strstr(exp, "*"))
{ //-132-2*51; -2*132-2*51; -2*132
char r[9+1];
strcpy(r,strBetweenPointersReverse(exp, strstr(exp, "-"), strstr(exp, "*")-1, mul2));
if(strcontainArray(r, signs, 2)) //is there, next to + or -, a number?
{ //-132-2*51
strcpy(mul2, "-1");//sure that exp contains *
substringAtstrstr(exp, exp+1, endnum, num);
}
else
{ //-2*132-2*51; -2*132
substringAtstrstr(exp, mul2+2, strstrArray(mul2+2, signs, 2), num);
}
}
else //-132
{
strcpy(mul2, "-1");
substringAtstrstr(exp, exp+1, endnum, num);
}
else //132-2*51; 2*132-2*51; 132; 2*132
if(strcontain(exp+1, '-') || strcontain(exp+1, '+'))
{ //132-2*51; 2*132-2*51
if(strcontain(exp, '*') && strstr(exp, "*") < strstrArray(exp, signs, 2)) //2*132-2*51
{
substringAtstrstr(exp,exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp,"*")+1, endnum, num);
}
else //132-2*51
{
strcpy(mul2, "1");
substringAtstrstr(exp, exp, endnum, num);
}
}
else
if(strcontain(exp, '*'))
{ //2*132
substringAtstrstr(exp, exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp, "*")+1, endnum, num); //all right with NULL ;) (see substringAtstrstr
}
else
{//132 that must be multiplied per n
strcpy(mul2, "1");
if(*exp=='+')
exp++;
strcpy(num, exp);
}
if(strcmp(num, "") == 0 || strcmp(mul2, "") == 0)
return 0;
else
return 1;
}
char *distribute(const char sign, char mul1[], char exp[], char result[]){
char mul2[5+1], num[5+1], signs[] = {'+', '-'}, *occ=NULL;;
short lung = 0, pos = 0;
while(findAddendCmpts(exp+pos, mul2, num))
{
if((occ=strstrArray(exp+pos+1, signs, 2))!=NULL)
pos+=occ -(exp+pos);
else pos+=strlen(exp+pos);
int mulDone = atoi(mul2) * atoi(mul1);
if(mulDone > 0) {
strcat(result, "+");
lung++;
}
char wtp[intlen(mulDone)+1];
strcat(result, int2string(mulDone, wtp));
lung+=intlen(mulDone);
strcat(result, "*");
lung++;
strcat(result, num);
lung+=strlen(num);
*(result+lung+2) = '{parsed_message}';
*num = '{parsed_message}';
*mul2 = '{parsed_message}';
}
return result;
}
int main(void) {
char wtp[10] = "";
char exp[] ="-13+2*41";
printf("\ndistribuzione: 2* (%s) =\n%s",exp,distribute('+', "2", exp, wtp));
//dovrei ottenere "-2*13+4*41"
return 0;
}
';
return whereToPut;
}
char* substringAtstrstr(const char where[], char* from_included, const char* to_included, char whereToPut[]) {
if(to_included == NULL)
return strcpy(whereToPut, from_included);
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(from_included);
*(whereToPut+pos) = '/*
Ottiene "(132-2*51)"
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define DEBUG 0
#define NUMERO 19 //numero max caratteri per a,b e k in k=ax+by e MCD(a,b)
long int powi(int num, int exp) {//10^0
long int ris=1;
while(exp--)
ris=ris*num;
return ris;
}
int intlen(int num) {
int i=1;
while(num/powi(10,i) != 0)
i++;
return i;
}
/*
* Delete characters from where starting from from_included position to to_included position.
* Positions included in the process of deleting.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_included = 0, to_included=4
* OutPut: where = -1*132+1*51{parsed_message}
* */
void strdeleteAt(char where[], const int from_included, const int to_included) {
char temp[strlen(where)-(to_included-from_included)+1];
int i;
for(i=0; i<from_included;i++)
temp[i] = where[i];
strcpy(temp+i, where+to_included+1);
strcpy(where, temp);
}
/*
* Deletes characters in where starting from from_inc position to to_inc position.
* Positions included in the process of deleting.
*
* Warning: passing a NULL pointer causes where to be returned.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_inc = strstr(where, "+");
* to_inc=strstr(where, "*");
* OutPut: where = 51-1*132+1*51{parsed_message}
*/
char* strdelete(char where[], char *from_inc, char* to_inc) {
if(to_inc == NULL || from_inc == NULL || where == NULL) return where;
printf("\nwhere: %s %d\nto_inc: %s %d\nfrom_inc: %s %d", where, strlen(where), to_inc, strlen(to_inc), from_inc, strlen(from_inc));
short len = strlen(where)-(to_inc-from_inc)+1;//from inc è pos 1 => errore
int pos=0;
if(len <= 0) return where;
char temp[len];
for(;from_inc-(where+pos) != 0; pos++)
*(temp+pos) = *(where+pos);
for(int posTo_inc=1; *(to_inc+posTo_inc) != '{parsed_message}';posTo_inc++,pos++)
*(temp+pos) = *(to_inc+posTo_inc);
*(temp+pos)='{parsed_message}';
strcpy(where,temp);
return where;
}
char* int2string(const int toBeParsed, char* whereToPut) {//-1
char digits[] = "0123456789";
int pos = 0, len=intlen(toBeParsed);
if(toBeParsed<=0) {
*whereToPut = '-';
pos++;
}
/* Determine the integer and putting it into whereToPut.
* for 38:
* whereToPut[0] = digits[38/10 -10*(38/100)] = '3'
* whereToPut[1] = digits[38/1 - 10*(38/10)] = '8'
* whereToPut => "38{parsed_message}"
*/
while(len-- > 0)
*(whereToPut + pos++) = digits[abs(toBeParsed) / powi(10,len) - 10*(abs(toBeParsed) / powi(10,len+1))];
*(whereToPut+pos)='{parsed_message}';
#if DEBUG
printf("\n\tint2string. toBeParsed=%d; whereToPut=%s\n",toBeParsed, whereToPut);
#endif
return whereToPut;
}
char* substringAt(char where[], int from_included, int to_included, char whereToPut[]) {
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(where+from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
char* substringAtstrstr(const char where[], char* from_included, const char* to_included, char whereToPut[]) {
if(to_included == NULL)
return strcpy(whereToPut, from_included);
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
int countChars(const char where[], const char character) {
int cont = 0;
for(; *where;where++)
if(*where == character) cont++;
return cont;
}
_Bool strcontain(const char where[], const char character) {
return countChars(where, character);
}
_Bool strcontainArray(const char where[], const char characters[], const short charactersLen)
{
_Bool r=1;
for(int i=0;i<charactersLen && r; i++)
r = r && countChars(where, characters[i]);
return r;
}
char* strstrArray(const char where[], const char chars[], const short charsLeng) {
//restituire l'occorrenza più vicina a where di una delle stringhe in chars. Per esempio per 132-2*51 e 132+2*51
char* occ = NULL;
for(int i=0; i < charsLeng; i++)
for(int pos = 0; *(where+pos); pos++)
if(*(where+pos) == chars[i])
if(occ == NULL)
{
occ = where+pos;
pos = strlen(where)-1;
}
else
if(occ != NULL && where+pos < occ)
{
occ = where+pos;
pos = strlen(where)-1;
}
return occ;
}
void emptystring(char string[]) {
while(*string++ = '{parsed_message}');
}
char* strBetweenPointersReverse(const char* yourstring, char *pos1, char *pos2, char whereToPut[]) {
#if DEBUG
puts("\nstringBetweenReverse()");
printf("\nyourstring %s", yourstring);
#endif
strcpy(whereToPut, yourstring);
strdelete(whereToPut, whereToPut, pos1);
strdelete(whereToPut, pos2, whereToPut+strlen(whereToPut));
return whereToPut;
#if DEBUG
printf("\nwhereToPut = %s", whereToPut);
#endif
}
/*
* This function find the first addend's components of an expression.
*
* @Params:
* exp is the expression where you want to find the first addend;
* mul2 is the memory location where the multiplier will be put. Must be "" when passed;
* num is the memory location where the number will be put. Must be "" when passed.
*
* @returns:
* true if the first addend was found. false otherwise.
*
* Example:
* exp = "è?(["; mul2 = ""; num = ""
* Output: mul2 = ""; ncum = ""
*
* Example :
* exp = "2*132-51"; mul2 = ""; num = ""
* Output: mul2 = "2", num = "132
* */
_Bool findAddendCmpts(const register char exp[], char mul2[], char num[]) {
char signs[] = {'+','-'}, *endnum = strstrArray(exp+1, signs, 2);
if(endnum != NULL)
endnum--;
if(*(exp) == '-') //132-2*51; -132-2*51; 2*132-2*51; -2*132-2*51; 132; -132; -2*132; 2*132
if(strcontain(exp, '*') && strstrArray(exp, signs,2) < strstr(exp, "*"))
{ //-132-2*51; -2*132-2*51; -2*132
char r[9+1];
strcpy(r,strBetweenPointersReverse(exp, strstr(exp, "-"), strstr(exp, "*")-1, mul2));
if(strcontainArray(r, signs, 2)) //is there, next to + or -, a number?
{ //-132-2*51
strcpy(mul2, "-1");//sure that exp contains *
substringAtstrstr(exp, exp+1, endnum, num);
}
else
{ //-2*132-2*51; -2*132
substringAtstrstr(exp, mul2+2, strstrArray(mul2+2, signs, 2), num);
}
}
else //-132
{
strcpy(mul2, "-1");
substringAtstrstr(exp, exp+1, endnum, num);
}
else //132-2*51; 2*132-2*51; 132; 2*132
if(strcontain(exp+1, '-') || strcontain(exp+1, '+'))
{ //132-2*51; 2*132-2*51
if(strcontain(exp, '*') && strstr(exp, "*") < strstrArray(exp, signs, 2)) //2*132-2*51
{
substringAtstrstr(exp,exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp,"*")+1, endnum, num);
}
else //132-2*51
{
strcpy(mul2, "1");
substringAtstrstr(exp, exp, endnum, num);
}
}
else
if(strcontain(exp, '*'))
{ //2*132
substringAtstrstr(exp, exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp, "*")+1, endnum, num); //all right with NULL ;) (see substringAtstrstr
}
else
{//132 that must be multiplied per n
strcpy(mul2, "1");
if(*exp=='+')
exp++;
strcpy(num, exp);
}
if(strcmp(num, "") == 0 || strcmp(mul2, "") == 0)
return 0;
else
return 1;
}
char *distribute(const char sign, char mul1[], char exp[], char result[]){
char mul2[5+1], num[5+1], signs[] = {'+', '-'}, *occ=NULL;;
short lung = 0, pos = 0;
while(findAddendCmpts(exp+pos, mul2, num))
{
if((occ=strstrArray(exp+pos+1, signs, 2))!=NULL)
pos+=occ -(exp+pos);
else pos+=strlen(exp+pos);
int mulDone = atoi(mul2) * atoi(mul1);
if(mulDone > 0) {
strcat(result, "+");
lung++;
}
char wtp[intlen(mulDone)+1];
strcat(result, int2string(mulDone, wtp));
lung+=intlen(mulDone);
strcat(result, "*");
lung++;
strcat(result, num);
lung+=strlen(num);
*(result+lung+2) = '{parsed_message}';
*num = '{parsed_message}';
*mul2 = '{parsed_message}';
}
return result;
}
int main(void) {
char wtp[10] = "";
char exp[] ="-13+2*41";
printf("\ndistribuzione: 2* (%s) =\n%s",exp,distribute('+', "2", exp, wtp));
//dovrei ottenere "-2*13+4*41"
return 0;
}
';
return whereToPut;
}
int countChars(const char where[], const char character) {
int cont = 0;
for(; *where;where++)
if(*where == character) cont++;
return cont;
}
_Bool strcontain(const char where[], const char character) {
return countChars(where, character);
}
_Bool strcontainArray(const char where[], const char characters[], const short charactersLen)
{
_Bool r=1;
for(int i=0;i<charactersLen && r; i++)
r = r && countChars(where, characters[i]);
return r;
}
char* strstrArray(const char where[], const char chars[], const short charsLeng) {
//restituire l'occorrenza più vicina a where di una delle stringhe in chars. Per esempio per 132-2*51 e 132+2*51
char* occ = NULL;
for(int i=0; i < charsLeng; i++)
for(int pos = 0; *(where+pos); pos++)
if(*(where+pos) == chars[i])
if(occ == NULL)
{
occ = where+pos;
pos = strlen(where)-1;
}
else
if(occ != NULL && where+pos < occ)
{
occ = where+pos;
pos = strlen(where)-1;
}
return occ;
}
void emptystring(char string[]) {
while(*string++ = '/*
Ottiene "(132-2*51)"
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define DEBUG 0
#define NUMERO 19 //numero max caratteri per a,b e k in k=ax+by e MCD(a,b)
long int powi(int num, int exp) {//10^0
long int ris=1;
while(exp--)
ris=ris*num;
return ris;
}
int intlen(int num) {
int i=1;
while(num/powi(10,i) != 0)
i++;
return i;
}
/*
* Delete characters from where starting from from_included position to to_included position.
* Positions included in the process of deleting.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_included = 0, to_included=4
* OutPut: where = -1*132+1*51{parsed_message}
* */
void strdeleteAt(char where[], const int from_included, const int to_included) {
char temp[strlen(where)-(to_included-from_included)+1];
int i;
for(i=0; i<from_included;i++)
temp[i] = where[i];
strcpy(temp+i, where+to_included+1);
strcpy(where, temp);
}
/*
* Deletes characters in where starting from from_inc position to to_inc position.
* Positions included in the process of deleting.
*
* Warning: passing a NULL pointer causes where to be returned.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_inc = strstr(where, "+");
* to_inc=strstr(where, "*");
* OutPut: where = 51-1*132+1*51{parsed_message}
*/
char* strdelete(char where[], char *from_inc, char* to_inc) {
if(to_inc == NULL || from_inc == NULL || where == NULL) return where;
printf("\nwhere: %s %d\nto_inc: %s %d\nfrom_inc: %s %d", where, strlen(where), to_inc, strlen(to_inc), from_inc, strlen(from_inc));
short len = strlen(where)-(to_inc-from_inc)+1;//from inc è pos 1 => errore
int pos=0;
if(len <= 0) return where;
char temp[len];
for(;from_inc-(where+pos) != 0; pos++)
*(temp+pos) = *(where+pos);
for(int posTo_inc=1; *(to_inc+posTo_inc) != '{parsed_message}';posTo_inc++,pos++)
*(temp+pos) = *(to_inc+posTo_inc);
*(temp+pos)='{parsed_message}';
strcpy(where,temp);
return where;
}
char* int2string(const int toBeParsed, char* whereToPut) {//-1
char digits[] = "0123456789";
int pos = 0, len=intlen(toBeParsed);
if(toBeParsed<=0) {
*whereToPut = '-';
pos++;
}
/* Determine the integer and putting it into whereToPut.
* for 38:
* whereToPut[0] = digits[38/10 -10*(38/100)] = '3'
* whereToPut[1] = digits[38/1 - 10*(38/10)] = '8'
* whereToPut => "38{parsed_message}"
*/
while(len-- > 0)
*(whereToPut + pos++) = digits[abs(toBeParsed) / powi(10,len) - 10*(abs(toBeParsed) / powi(10,len+1))];
*(whereToPut+pos)='{parsed_message}';
#if DEBUG
printf("\n\tint2string. toBeParsed=%d; whereToPut=%s\n",toBeParsed, whereToPut);
#endif
return whereToPut;
}
char* substringAt(char where[], int from_included, int to_included, char whereToPut[]) {
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(where+from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
char* substringAtstrstr(const char where[], char* from_included, const char* to_included, char whereToPut[]) {
if(to_included == NULL)
return strcpy(whereToPut, from_included);
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
int countChars(const char where[], const char character) {
int cont = 0;
for(; *where;where++)
if(*where == character) cont++;
return cont;
}
_Bool strcontain(const char where[], const char character) {
return countChars(where, character);
}
_Bool strcontainArray(const char where[], const char characters[], const short charactersLen)
{
_Bool r=1;
for(int i=0;i<charactersLen && r; i++)
r = r && countChars(where, characters[i]);
return r;
}
char* strstrArray(const char where[], const char chars[], const short charsLeng) {
//restituire l'occorrenza più vicina a where di una delle stringhe in chars. Per esempio per 132-2*51 e 132+2*51
char* occ = NULL;
for(int i=0; i < charsLeng; i++)
for(int pos = 0; *(where+pos); pos++)
if(*(where+pos) == chars[i])
if(occ == NULL)
{
occ = where+pos;
pos = strlen(where)-1;
}
else
if(occ != NULL && where+pos < occ)
{
occ = where+pos;
pos = strlen(where)-1;
}
return occ;
}
void emptystring(char string[]) {
while(*string++ = '{parsed_message}');
}
char* strBetweenPointersReverse(const char* yourstring, char *pos1, char *pos2, char whereToPut[]) {
#if DEBUG
puts("\nstringBetweenReverse()");
printf("\nyourstring %s", yourstring);
#endif
strcpy(whereToPut, yourstring);
strdelete(whereToPut, whereToPut, pos1);
strdelete(whereToPut, pos2, whereToPut+strlen(whereToPut));
return whereToPut;
#if DEBUG
printf("\nwhereToPut = %s", whereToPut);
#endif
}
/*
* This function find the first addend's components of an expression.
*
* @Params:
* exp is the expression where you want to find the first addend;
* mul2 is the memory location where the multiplier will be put. Must be "" when passed;
* num is the memory location where the number will be put. Must be "" when passed.
*
* @returns:
* true if the first addend was found. false otherwise.
*
* Example:
* exp = "è?(["; mul2 = ""; num = ""
* Output: mul2 = ""; ncum = ""
*
* Example :
* exp = "2*132-51"; mul2 = ""; num = ""
* Output: mul2 = "2", num = "132
* */
_Bool findAddendCmpts(const register char exp[], char mul2[], char num[]) {
char signs[] = {'+','-'}, *endnum = strstrArray(exp+1, signs, 2);
if(endnum != NULL)
endnum--;
if(*(exp) == '-') //132-2*51; -132-2*51; 2*132-2*51; -2*132-2*51; 132; -132; -2*132; 2*132
if(strcontain(exp, '*') && strstrArray(exp, signs,2) < strstr(exp, "*"))
{ //-132-2*51; -2*132-2*51; -2*132
char r[9+1];
strcpy(r,strBetweenPointersReverse(exp, strstr(exp, "-"), strstr(exp, "*")-1, mul2));
if(strcontainArray(r, signs, 2)) //is there, next to + or -, a number?
{ //-132-2*51
strcpy(mul2, "-1");//sure that exp contains *
substringAtstrstr(exp, exp+1, endnum, num);
}
else
{ //-2*132-2*51; -2*132
substringAtstrstr(exp, mul2+2, strstrArray(mul2+2, signs, 2), num);
}
}
else //-132
{
strcpy(mul2, "-1");
substringAtstrstr(exp, exp+1, endnum, num);
}
else //132-2*51; 2*132-2*51; 132; 2*132
if(strcontain(exp+1, '-') || strcontain(exp+1, '+'))
{ //132-2*51; 2*132-2*51
if(strcontain(exp, '*') && strstr(exp, "*") < strstrArray(exp, signs, 2)) //2*132-2*51
{
substringAtstrstr(exp,exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp,"*")+1, endnum, num);
}
else //132-2*51
{
strcpy(mul2, "1");
substringAtstrstr(exp, exp, endnum, num);
}
}
else
if(strcontain(exp, '*'))
{ //2*132
substringAtstrstr(exp, exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp, "*")+1, endnum, num); //all right with NULL ;) (see substringAtstrstr
}
else
{//132 that must be multiplied per n
strcpy(mul2, "1");
if(*exp=='+')
exp++;
strcpy(num, exp);
}
if(strcmp(num, "") == 0 || strcmp(mul2, "") == 0)
return 0;
else
return 1;
}
char *distribute(const char sign, char mul1[], char exp[], char result[]){
char mul2[5+1], num[5+1], signs[] = {'+', '-'}, *occ=NULL;;
short lung = 0, pos = 0;
while(findAddendCmpts(exp+pos, mul2, num))
{
if((occ=strstrArray(exp+pos+1, signs, 2))!=NULL)
pos+=occ -(exp+pos);
else pos+=strlen(exp+pos);
int mulDone = atoi(mul2) * atoi(mul1);
if(mulDone > 0) {
strcat(result, "+");
lung++;
}
char wtp[intlen(mulDone)+1];
strcat(result, int2string(mulDone, wtp));
lung+=intlen(mulDone);
strcat(result, "*");
lung++;
strcat(result, num);
lung+=strlen(num);
*(result+lung+2) = '{parsed_message}';
*num = '{parsed_message}';
*mul2 = '{parsed_message}';
}
return result;
}
int main(void) {
char wtp[10] = "";
char exp[] ="-13+2*41";
printf("\ndistribuzione: 2* (%s) =\n%s",exp,distribute('+', "2", exp, wtp));
//dovrei ottenere "-2*13+4*41"
return 0;
}
');
}
char* strBetweenPointersReverse(const char* yourstring, char *pos1, char *pos2, char whereToPut[]) {
#if DEBUG
puts("\nstringBetweenReverse()");
printf("\nyourstring %s", yourstring);
#endif
strcpy(whereToPut, yourstring);
strdelete(whereToPut, whereToPut, pos1);
strdelete(whereToPut, pos2, whereToPut+strlen(whereToPut));
return whereToPut;
#if DEBUG
printf("\nwhereToPut = %s", whereToPut);
#endif
}
/*
* This function find the first addend's components of an expression.
*
* @Params:
* exp is the expression where you want to find the first addend;
* mul2 is the memory location where the multiplier will be put. Must be "" when passed;
* num is the memory location where the number will be put. Must be "" when passed.
*
* @returns:
* true if the first addend was found. false otherwise.
*
* Example:
* exp = "è?(["; mul2 = ""; num = ""
* Output: mul2 = ""; ncum = ""
*
* Example :
* exp = "2*132-51"; mul2 = ""; num = ""
* Output: mul2 = "2", num = "132
* */
_Bool findAddendCmpts(const register char exp[], char mul2[], char num[]) {
char signs[] = {'+','-'}, *endnum = strstrArray(exp+1, signs, 2);
if(endnum != NULL)
endnum--;
if(*(exp) == '-') //132-2*51; -132-2*51; 2*132-2*51; -2*132-2*51; 132; -132; -2*132; 2*132
if(strcontain(exp, '*') && strstrArray(exp, signs,2) < strstr(exp, "*"))
{ //-132-2*51; -2*132-2*51; -2*132
char r[9+1];
strcpy(r,strBetweenPointersReverse(exp, strstr(exp, "-"), strstr(exp, "*")-1, mul2));
if(strcontainArray(r, signs, 2)) //is there, next to + or -, a number?
{ //-132-2*51
strcpy(mul2, "-1");//sure that exp contains *
substringAtstrstr(exp, exp+1, endnum, num);
}
else
{ //-2*132-2*51; -2*132
substringAtstrstr(exp, mul2+2, strstrArray(mul2+2, signs, 2), num);
}
}
else //-132
{
strcpy(mul2, "-1");
substringAtstrstr(exp, exp+1, endnum, num);
}
else //132-2*51; 2*132-2*51; 132; 2*132
if(strcontain(exp+1, '-') || strcontain(exp+1, '+'))
{ //132-2*51; 2*132-2*51
if(strcontain(exp, '*') && strstr(exp, "*") < strstrArray(exp, signs, 2)) //2*132-2*51
{
substringAtstrstr(exp,exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp,"*")+1, endnum, num);
}
else //132-2*51
{
strcpy(mul2, "1");
substringAtstrstr(exp, exp, endnum, num);
}
}
else
if(strcontain(exp, '*'))
{ //2*132
substringAtstrstr(exp, exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp, "*")+1, endnum, num); //all right with NULL ;) (see substringAtstrstr
}
else
{//132 that must be multiplied per n
strcpy(mul2, "1");
if(*exp=='+')
exp++;
strcpy(num, exp);
}
if(strcmp(num, "") == 0 || strcmp(mul2, "") == 0)
return 0;
else
return 1;
}
char *distribute(const char sign, char mul1[], char exp[], char result[]){
char mul2[5+1], num[5+1], signs[] = {'+', '-'}, *occ=NULL;;
short lung = 0, pos = 0;
while(findAddendCmpts(exp+pos, mul2, num))
{
if((occ=strstrArray(exp+pos+1, signs, 2))!=NULL)
pos+=occ -(exp+pos);
else pos+=strlen(exp+pos);
int mulDone = atoi(mul2) * atoi(mul1);
if(mulDone > 0) {
strcat(result, "+");
lung++;
}
char wtp[intlen(mulDone)+1];
strcat(result, int2string(mulDone, wtp));
lung+=intlen(mulDone);
strcat(result, "*");
lung++;
strcat(result, num);
lung+=strlen(num);
*(result+lung+2) = '/*
Ottiene "(132-2*51)"
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define DEBUG 0
#define NUMERO 19 //numero max caratteri per a,b e k in k=ax+by e MCD(a,b)
long int powi(int num, int exp) {//10^0
long int ris=1;
while(exp--)
ris=ris*num;
return ris;
}
int intlen(int num) {
int i=1;
while(num/powi(10,i) != 0)
i++;
return i;
}
/*
* Delete characters from where starting from from_included position to to_included position.
* Positions included in the process of deleting.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_included = 0, to_included=4
* OutPut: where = -1*132+1*51{parsed_message}
* */
void strdeleteAt(char where[], const int from_included, const int to_included) {
char temp[strlen(where)-(to_included-from_included)+1];
int i;
for(i=0; i<from_included;i++)
temp[i] = where[i];
strcpy(temp+i, where+to_included+1);
strcpy(where, temp);
}
/*
* Deletes characters in where starting from from_inc position to to_inc position.
* Positions included in the process of deleting.
*
* Warning: passing a NULL pointer causes where to be returned.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_inc = strstr(where, "+");
* to_inc=strstr(where, "*");
* OutPut: where = 51-1*132+1*51{parsed_message}
*/
char* strdelete(char where[], char *from_inc, char* to_inc) {
if(to_inc == NULL || from_inc == NULL || where == NULL) return where;
printf("\nwhere: %s %d\nto_inc: %s %d\nfrom_inc: %s %d", where, strlen(where), to_inc, strlen(to_inc), from_inc, strlen(from_inc));
short len = strlen(where)-(to_inc-from_inc)+1;//from inc è pos 1 => errore
int pos=0;
if(len <= 0) return where;
char temp[len];
for(;from_inc-(where+pos) != 0; pos++)
*(temp+pos) = *(where+pos);
for(int posTo_inc=1; *(to_inc+posTo_inc) != '{parsed_message}';posTo_inc++,pos++)
*(temp+pos) = *(to_inc+posTo_inc);
*(temp+pos)='{parsed_message}';
strcpy(where,temp);
return where;
}
char* int2string(const int toBeParsed, char* whereToPut) {//-1
char digits[] = "0123456789";
int pos = 0, len=intlen(toBeParsed);
if(toBeParsed<=0) {
*whereToPut = '-';
pos++;
}
/* Determine the integer and putting it into whereToPut.
* for 38:
* whereToPut[0] = digits[38/10 -10*(38/100)] = '3'
* whereToPut[1] = digits[38/1 - 10*(38/10)] = '8'
* whereToPut => "38{parsed_message}"
*/
while(len-- > 0)
*(whereToPut + pos++) = digits[abs(toBeParsed) / powi(10,len) - 10*(abs(toBeParsed) / powi(10,len+1))];
*(whereToPut+pos)='{parsed_message}';
#if DEBUG
printf("\n\tint2string. toBeParsed=%d; whereToPut=%s\n",toBeParsed, whereToPut);
#endif
return whereToPut;
}
char* substringAt(char where[], int from_included, int to_included, char whereToPut[]) {
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(where+from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
char* substringAtstrstr(const char where[], char* from_included, const char* to_included, char whereToPut[]) {
if(to_included == NULL)
return strcpy(whereToPut, from_included);
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
int countChars(const char where[], const char character) {
int cont = 0;
for(; *where;where++)
if(*where == character) cont++;
return cont;
}
_Bool strcontain(const char where[], const char character) {
return countChars(where, character);
}
_Bool strcontainArray(const char where[], const char characters[], const short charactersLen)
{
_Bool r=1;
for(int i=0;i<charactersLen && r; i++)
r = r && countChars(where, characters[i]);
return r;
}
char* strstrArray(const char where[], const char chars[], const short charsLeng) {
//restituire l'occorrenza più vicina a where di una delle stringhe in chars. Per esempio per 132-2*51 e 132+2*51
char* occ = NULL;
for(int i=0; i < charsLeng; i++)
for(int pos = 0; *(where+pos); pos++)
if(*(where+pos) == chars[i])
if(occ == NULL)
{
occ = where+pos;
pos = strlen(where)-1;
}
else
if(occ != NULL && where+pos < occ)
{
occ = where+pos;
pos = strlen(where)-1;
}
return occ;
}
void emptystring(char string[]) {
while(*string++ = '{parsed_message}');
}
char* strBetweenPointersReverse(const char* yourstring, char *pos1, char *pos2, char whereToPut[]) {
#if DEBUG
puts("\nstringBetweenReverse()");
printf("\nyourstring %s", yourstring);
#endif
strcpy(whereToPut, yourstring);
strdelete(whereToPut, whereToPut, pos1);
strdelete(whereToPut, pos2, whereToPut+strlen(whereToPut));
return whereToPut;
#if DEBUG
printf("\nwhereToPut = %s", whereToPut);
#endif
}
/*
* This function find the first addend's components of an expression.
*
* @Params:
* exp is the expression where you want to find the first addend;
* mul2 is the memory location where the multiplier will be put. Must be "" when passed;
* num is the memory location where the number will be put. Must be "" when passed.
*
* @returns:
* true if the first addend was found. false otherwise.
*
* Example:
* exp = "è?(["; mul2 = ""; num = ""
* Output: mul2 = ""; ncum = ""
*
* Example :
* exp = "2*132-51"; mul2 = ""; num = ""
* Output: mul2 = "2", num = "132
* */
_Bool findAddendCmpts(const register char exp[], char mul2[], char num[]) {
char signs[] = {'+','-'}, *endnum = strstrArray(exp+1, signs, 2);
if(endnum != NULL)
endnum--;
if(*(exp) == '-') //132-2*51; -132-2*51; 2*132-2*51; -2*132-2*51; 132; -132; -2*132; 2*132
if(strcontain(exp, '*') && strstrArray(exp, signs,2) < strstr(exp, "*"))
{ //-132-2*51; -2*132-2*51; -2*132
char r[9+1];
strcpy(r,strBetweenPointersReverse(exp, strstr(exp, "-"), strstr(exp, "*")-1, mul2));
if(strcontainArray(r, signs, 2)) //is there, next to + or -, a number?
{ //-132-2*51
strcpy(mul2, "-1");//sure that exp contains *
substringAtstrstr(exp, exp+1, endnum, num);
}
else
{ //-2*132-2*51; -2*132
substringAtstrstr(exp, mul2+2, strstrArray(mul2+2, signs, 2), num);
}
}
else //-132
{
strcpy(mul2, "-1");
substringAtstrstr(exp, exp+1, endnum, num);
}
else //132-2*51; 2*132-2*51; 132; 2*132
if(strcontain(exp+1, '-') || strcontain(exp+1, '+'))
{ //132-2*51; 2*132-2*51
if(strcontain(exp, '*') && strstr(exp, "*") < strstrArray(exp, signs, 2)) //2*132-2*51
{
substringAtstrstr(exp,exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp,"*")+1, endnum, num);
}
else //132-2*51
{
strcpy(mul2, "1");
substringAtstrstr(exp, exp, endnum, num);
}
}
else
if(strcontain(exp, '*'))
{ //2*132
substringAtstrstr(exp, exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp, "*")+1, endnum, num); //all right with NULL ;) (see substringAtstrstr
}
else
{//132 that must be multiplied per n
strcpy(mul2, "1");
if(*exp=='+')
exp++;
strcpy(num, exp);
}
if(strcmp(num, "") == 0 || strcmp(mul2, "") == 0)
return 0;
else
return 1;
}
char *distribute(const char sign, char mul1[], char exp[], char result[]){
char mul2[5+1], num[5+1], signs[] = {'+', '-'}, *occ=NULL;;
short lung = 0, pos = 0;
while(findAddendCmpts(exp+pos, mul2, num))
{
if((occ=strstrArray(exp+pos+1, signs, 2))!=NULL)
pos+=occ -(exp+pos);
else pos+=strlen(exp+pos);
int mulDone = atoi(mul2) * atoi(mul1);
if(mulDone > 0) {
strcat(result, "+");
lung++;
}
char wtp[intlen(mulDone)+1];
strcat(result, int2string(mulDone, wtp));
lung+=intlen(mulDone);
strcat(result, "*");
lung++;
strcat(result, num);
lung+=strlen(num);
*(result+lung+2) = '{parsed_message}';
*num = '{parsed_message}';
*mul2 = '{parsed_message}';
}
return result;
}
int main(void) {
char wtp[10] = "";
char exp[] ="-13+2*41";
printf("\ndistribuzione: 2* (%s) =\n%s",exp,distribute('+', "2", exp, wtp));
//dovrei ottenere "-2*13+4*41"
return 0;
}
';
*num = '/*
Ottiene "(132-2*51)"
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define DEBUG 0
#define NUMERO 19 //numero max caratteri per a,b e k in k=ax+by e MCD(a,b)
long int powi(int num, int exp) {//10^0
long int ris=1;
while(exp--)
ris=ris*num;
return ris;
}
int intlen(int num) {
int i=1;
while(num/powi(10,i) != 0)
i++;
return i;
}
/*
* Delete characters from where starting from from_included position to to_included position.
* Positions included in the process of deleting.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_included = 0, to_included=4
* OutPut: where = -1*132+1*51{parsed_message}
* */
void strdeleteAt(char where[], const int from_included, const int to_included) {
char temp[strlen(where)-(to_included-from_included)+1];
int i;
for(i=0; i<from_included;i++)
temp[i] = where[i];
strcpy(temp+i, where+to_included+1);
strcpy(where, temp);
}
/*
* Deletes characters in where starting from from_inc position to to_inc position.
* Positions included in the process of deleting.
*
* Warning: passing a NULL pointer causes where to be returned.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_inc = strstr(where, "+");
* to_inc=strstr(where, "*");
* OutPut: where = 51-1*132+1*51{parsed_message}
*/
char* strdelete(char where[], char *from_inc, char* to_inc) {
if(to_inc == NULL || from_inc == NULL || where == NULL) return where;
printf("\nwhere: %s %d\nto_inc: %s %d\nfrom_inc: %s %d", where, strlen(where), to_inc, strlen(to_inc), from_inc, strlen(from_inc));
short len = strlen(where)-(to_inc-from_inc)+1;//from inc è pos 1 => errore
int pos=0;
if(len <= 0) return where;
char temp[len];
for(;from_inc-(where+pos) != 0; pos++)
*(temp+pos) = *(where+pos);
for(int posTo_inc=1; *(to_inc+posTo_inc) != '{parsed_message}';posTo_inc++,pos++)
*(temp+pos) = *(to_inc+posTo_inc);
*(temp+pos)='{parsed_message}';
strcpy(where,temp);
return where;
}
char* int2string(const int toBeParsed, char* whereToPut) {//-1
char digits[] = "0123456789";
int pos = 0, len=intlen(toBeParsed);
if(toBeParsed<=0) {
*whereToPut = '-';
pos++;
}
/* Determine the integer and putting it into whereToPut.
* for 38:
* whereToPut[0] = digits[38/10 -10*(38/100)] = '3'
* whereToPut[1] = digits[38/1 - 10*(38/10)] = '8'
* whereToPut => "38{parsed_message}"
*/
while(len-- > 0)
*(whereToPut + pos++) = digits[abs(toBeParsed) / powi(10,len) - 10*(abs(toBeParsed) / powi(10,len+1))];
*(whereToPut+pos)='{parsed_message}';
#if DEBUG
printf("\n\tint2string. toBeParsed=%d; whereToPut=%s\n",toBeParsed, whereToPut);
#endif
return whereToPut;
}
char* substringAt(char where[], int from_included, int to_included, char whereToPut[]) {
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(where+from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
char* substringAtstrstr(const char where[], char* from_included, const char* to_included, char whereToPut[]) {
if(to_included == NULL)
return strcpy(whereToPut, from_included);
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
int countChars(const char where[], const char character) {
int cont = 0;
for(; *where;where++)
if(*where == character) cont++;
return cont;
}
_Bool strcontain(const char where[], const char character) {
return countChars(where, character);
}
_Bool strcontainArray(const char where[], const char characters[], const short charactersLen)
{
_Bool r=1;
for(int i=0;i<charactersLen && r; i++)
r = r && countChars(where, characters[i]);
return r;
}
char* strstrArray(const char where[], const char chars[], const short charsLeng) {
//restituire l'occorrenza più vicina a where di una delle stringhe in chars. Per esempio per 132-2*51 e 132+2*51
char* occ = NULL;
for(int i=0; i < charsLeng; i++)
for(int pos = 0; *(where+pos); pos++)
if(*(where+pos) == chars[i])
if(occ == NULL)
{
occ = where+pos;
pos = strlen(where)-1;
}
else
if(occ != NULL && where+pos < occ)
{
occ = where+pos;
pos = strlen(where)-1;
}
return occ;
}
void emptystring(char string[]) {
while(*string++ = '{parsed_message}');
}
char* strBetweenPointersReverse(const char* yourstring, char *pos1, char *pos2, char whereToPut[]) {
#if DEBUG
puts("\nstringBetweenReverse()");
printf("\nyourstring %s", yourstring);
#endif
strcpy(whereToPut, yourstring);
strdelete(whereToPut, whereToPut, pos1);
strdelete(whereToPut, pos2, whereToPut+strlen(whereToPut));
return whereToPut;
#if DEBUG
printf("\nwhereToPut = %s", whereToPut);
#endif
}
/*
* This function find the first addend's components of an expression.
*
* @Params:
* exp is the expression where you want to find the first addend;
* mul2 is the memory location where the multiplier will be put. Must be "" when passed;
* num is the memory location where the number will be put. Must be "" when passed.
*
* @returns:
* true if the first addend was found. false otherwise.
*
* Example:
* exp = "è?(["; mul2 = ""; num = ""
* Output: mul2 = ""; ncum = ""
*
* Example :
* exp = "2*132-51"; mul2 = ""; num = ""
* Output: mul2 = "2", num = "132
* */
_Bool findAddendCmpts(const register char exp[], char mul2[], char num[]) {
char signs[] = {'+','-'}, *endnum = strstrArray(exp+1, signs, 2);
if(endnum != NULL)
endnum--;
if(*(exp) == '-') //132-2*51; -132-2*51; 2*132-2*51; -2*132-2*51; 132; -132; -2*132; 2*132
if(strcontain(exp, '*') && strstrArray(exp, signs,2) < strstr(exp, "*"))
{ //-132-2*51; -2*132-2*51; -2*132
char r[9+1];
strcpy(r,strBetweenPointersReverse(exp, strstr(exp, "-"), strstr(exp, "*")-1, mul2));
if(strcontainArray(r, signs, 2)) //is there, next to + or -, a number?
{ //-132-2*51
strcpy(mul2, "-1");//sure that exp contains *
substringAtstrstr(exp, exp+1, endnum, num);
}
else
{ //-2*132-2*51; -2*132
substringAtstrstr(exp, mul2+2, strstrArray(mul2+2, signs, 2), num);
}
}
else //-132
{
strcpy(mul2, "-1");
substringAtstrstr(exp, exp+1, endnum, num);
}
else //132-2*51; 2*132-2*51; 132; 2*132
if(strcontain(exp+1, '-') || strcontain(exp+1, '+'))
{ //132-2*51; 2*132-2*51
if(strcontain(exp, '*') && strstr(exp, "*") < strstrArray(exp, signs, 2)) //2*132-2*51
{
substringAtstrstr(exp,exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp,"*")+1, endnum, num);
}
else //132-2*51
{
strcpy(mul2, "1");
substringAtstrstr(exp, exp, endnum, num);
}
}
else
if(strcontain(exp, '*'))
{ //2*132
substringAtstrstr(exp, exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp, "*")+1, endnum, num); //all right with NULL ;) (see substringAtstrstr
}
else
{//132 that must be multiplied per n
strcpy(mul2, "1");
if(*exp=='+')
exp++;
strcpy(num, exp);
}
if(strcmp(num, "") == 0 || strcmp(mul2, "") == 0)
return 0;
else
return 1;
}
char *distribute(const char sign, char mul1[], char exp[], char result[]){
char mul2[5+1], num[5+1], signs[] = {'+', '-'}, *occ=NULL;;
short lung = 0, pos = 0;
while(findAddendCmpts(exp+pos, mul2, num))
{
if((occ=strstrArray(exp+pos+1, signs, 2))!=NULL)
pos+=occ -(exp+pos);
else pos+=strlen(exp+pos);
int mulDone = atoi(mul2) * atoi(mul1);
if(mulDone > 0) {
strcat(result, "+");
lung++;
}
char wtp[intlen(mulDone)+1];
strcat(result, int2string(mulDone, wtp));
lung+=intlen(mulDone);
strcat(result, "*");
lung++;
strcat(result, num);
lung+=strlen(num);
*(result+lung+2) = '{parsed_message}';
*num = '{parsed_message}';
*mul2 = '{parsed_message}';
}
return result;
}
int main(void) {
char wtp[10] = "";
char exp[] ="-13+2*41";
printf("\ndistribuzione: 2* (%s) =\n%s",exp,distribute('+', "2", exp, wtp));
//dovrei ottenere "-2*13+4*41"
return 0;
}
';
*mul2 = '/*
Ottiene "(132-2*51)"
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define DEBUG 0
#define NUMERO 19 //numero max caratteri per a,b e k in k=ax+by e MCD(a,b)
long int powi(int num, int exp) {//10^0
long int ris=1;
while(exp--)
ris=ris*num;
return ris;
}
int intlen(int num) {
int i=1;
while(num/powi(10,i) != 0)
i++;
return i;
}
/*
* Delete characters from where starting from from_included position to to_included position.
* Positions included in the process of deleting.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_included = 0, to_included=4
* OutPut: where = -1*132+1*51{parsed_message}
* */
void strdeleteAt(char where[], const int from_included, const int to_included) {
char temp[strlen(where)-(to_included-from_included)+1];
int i;
for(i=0; i<from_included;i++)
temp[i] = where[i];
strcpy(temp+i, where+to_included+1);
strcpy(where, temp);
}
/*
* Deletes characters in where starting from from_inc position to to_inc position.
* Positions included in the process of deleting.
*
* Warning: passing a NULL pointer causes where to be returned.
*
* Example:
* where = +1*51-1*132+1*51{parsed_message}
* from_inc = strstr(where, "+");
* to_inc=strstr(where, "*");
* OutPut: where = 51-1*132+1*51{parsed_message}
*/
char* strdelete(char where[], char *from_inc, char* to_inc) {
if(to_inc == NULL || from_inc == NULL || where == NULL) return where;
printf("\nwhere: %s %d\nto_inc: %s %d\nfrom_inc: %s %d", where, strlen(where), to_inc, strlen(to_inc), from_inc, strlen(from_inc));
short len = strlen(where)-(to_inc-from_inc)+1;//from inc è pos 1 => errore
int pos=0;
if(len <= 0) return where;
char temp[len];
for(;from_inc-(where+pos) != 0; pos++)
*(temp+pos) = *(where+pos);
for(int posTo_inc=1; *(to_inc+posTo_inc) != '{parsed_message}';posTo_inc++,pos++)
*(temp+pos) = *(to_inc+posTo_inc);
*(temp+pos)='{parsed_message}';
strcpy(where,temp);
return where;
}
char* int2string(const int toBeParsed, char* whereToPut) {//-1
char digits[] = "0123456789";
int pos = 0, len=intlen(toBeParsed);
if(toBeParsed<=0) {
*whereToPut = '-';
pos++;
}
/* Determine the integer and putting it into whereToPut.
* for 38:
* whereToPut[0] = digits[38/10 -10*(38/100)] = '3'
* whereToPut[1] = digits[38/1 - 10*(38/10)] = '8'
* whereToPut => "38{parsed_message}"
*/
while(len-- > 0)
*(whereToPut + pos++) = digits[abs(toBeParsed) / powi(10,len) - 10*(abs(toBeParsed) / powi(10,len+1))];
*(whereToPut+pos)='{parsed_message}';
#if DEBUG
printf("\n\tint2string. toBeParsed=%d; whereToPut=%s\n",toBeParsed, whereToPut);
#endif
return whereToPut;
}
char* substringAt(char where[], int from_included, int to_included, char whereToPut[]) {
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(where+from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
char* substringAtstrstr(const char where[], char* from_included, const char* to_included, char whereToPut[]) {
if(to_included == NULL)
return strcpy(whereToPut, from_included);
int pos = 0;
for(; to_included >= from_included; from_included++, pos++)
*(whereToPut+pos) = *(from_included);
*(whereToPut+pos) = '{parsed_message}';
return whereToPut;
}
int countChars(const char where[], const char character) {
int cont = 0;
for(; *where;where++)
if(*where == character) cont++;
return cont;
}
_Bool strcontain(const char where[], const char character) {
return countChars(where, character);
}
_Bool strcontainArray(const char where[], const char characters[], const short charactersLen)
{
_Bool r=1;
for(int i=0;i<charactersLen && r; i++)
r = r && countChars(where, characters[i]);
return r;
}
char* strstrArray(const char where[], const char chars[], const short charsLeng) {
//restituire l'occorrenza più vicina a where di una delle stringhe in chars. Per esempio per 132-2*51 e 132+2*51
char* occ = NULL;
for(int i=0; i < charsLeng; i++)
for(int pos = 0; *(where+pos); pos++)
if(*(where+pos) == chars[i])
if(occ == NULL)
{
occ = where+pos;
pos = strlen(where)-1;
}
else
if(occ != NULL && where+pos < occ)
{
occ = where+pos;
pos = strlen(where)-1;
}
return occ;
}
void emptystring(char string[]) {
while(*string++ = '{parsed_message}');
}
char* strBetweenPointersReverse(const char* yourstring, char *pos1, char *pos2, char whereToPut[]) {
#if DEBUG
puts("\nstringBetweenReverse()");
printf("\nyourstring %s", yourstring);
#endif
strcpy(whereToPut, yourstring);
strdelete(whereToPut, whereToPut, pos1);
strdelete(whereToPut, pos2, whereToPut+strlen(whereToPut));
return whereToPut;
#if DEBUG
printf("\nwhereToPut = %s", whereToPut);
#endif
}
/*
* This function find the first addend's components of an expression.
*
* @Params:
* exp is the expression where you want to find the first addend;
* mul2 is the memory location where the multiplier will be put. Must be "" when passed;
* num is the memory location where the number will be put. Must be "" when passed.
*
* @returns:
* true if the first addend was found. false otherwise.
*
* Example:
* exp = "è?(["; mul2 = ""; num = ""
* Output: mul2 = ""; ncum = ""
*
* Example :
* exp = "2*132-51"; mul2 = ""; num = ""
* Output: mul2 = "2", num = "132
* */
_Bool findAddendCmpts(const register char exp[], char mul2[], char num[]) {
char signs[] = {'+','-'}, *endnum = strstrArray(exp+1, signs, 2);
if(endnum != NULL)
endnum--;
if(*(exp) == '-') //132-2*51; -132-2*51; 2*132-2*51; -2*132-2*51; 132; -132; -2*132; 2*132
if(strcontain(exp, '*') && strstrArray(exp, signs,2) < strstr(exp, "*"))
{ //-132-2*51; -2*132-2*51; -2*132
char r[9+1];
strcpy(r,strBetweenPointersReverse(exp, strstr(exp, "-"), strstr(exp, "*")-1, mul2));
if(strcontainArray(r, signs, 2)) //is there, next to + or -, a number?
{ //-132-2*51
strcpy(mul2, "-1");//sure that exp contains *
substringAtstrstr(exp, exp+1, endnum, num);
}
else
{ //-2*132-2*51; -2*132
substringAtstrstr(exp, mul2+2, strstrArray(mul2+2, signs, 2), num);
}
}
else //-132
{
strcpy(mul2, "-1");
substringAtstrstr(exp, exp+1, endnum, num);
}
else //132-2*51; 2*132-2*51; 132; 2*132
if(strcontain(exp+1, '-') || strcontain(exp+1, '+'))
{ //132-2*51; 2*132-2*51
if(strcontain(exp, '*') && strstr(exp, "*") < strstrArray(exp, signs, 2)) //2*132-2*51
{
substringAtstrstr(exp,exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp,"*")+1, endnum, num);
}
else //132-2*51
{
strcpy(mul2, "1");
substringAtstrstr(exp, exp, endnum, num);
}
}
else
if(strcontain(exp, '*'))
{ //2*132
substringAtstrstr(exp, exp, strstr(exp, "*")-1, mul2);
substringAtstrstr(exp, strstr(exp, "*")+1, endnum, num); //all right with NULL ;) (see substringAtstrstr
}
else
{//132 that must be multiplied per n
strcpy(mul2, "1");
if(*exp=='+')
exp++;
strcpy(num, exp);
}
if(strcmp(num, "") == 0 || strcmp(mul2, "") == 0)
return 0;
else
return 1;
}
char *distribute(const char sign, char mul1[], char exp[], char result[]){
char mul2[5+1], num[5+1], signs[] = {'+', '-'}, *occ=NULL;;
short lung = 0, pos = 0;
while(findAddendCmpts(exp+pos, mul2, num))
{
if((occ=strstrArray(exp+pos+1, signs, 2))!=NULL)
pos+=occ -(exp+pos);
else pos+=strlen(exp+pos);
int mulDone = atoi(mul2) * atoi(mul1);
if(mulDone > 0) {
strcat(result, "+");
lung++;
}
char wtp[intlen(mulDone)+1];
strcat(result, int2string(mulDone, wtp));
lung+=intlen(mulDone);
strcat(result, "*");
lung++;
strcat(result, num);
lung+=strlen(num);
*(result+lung+2) = '{parsed_message}';
*num = '{parsed_message}';
*mul2 = '{parsed_message}';
}
return result;
}
int main(void) {
char wtp[10] = "";
char exp[] ="-13+2*41";
printf("\ndistribuzione: 2* (%s) =\n%s",exp,distribute('+', "2", exp, wtp));
//dovrei ottenere "-2*13+4*41"
return 0;
}
';
}
return result;
}
int main(void) {
char wtp[10] = "";
char exp[] ="-13+2*41";
printf("\ndistribuzione: 2* (%s) =\n%s",exp,distribute('+', "2", exp, wtp));
//dovrei ottenere "-2*13+4*41"
return 0;
}