ANSI C syntax to PHP

convert C language to PHP

Page 1 of 1

4 Replies - 6387 Views - Last Post: 05 April 2008 - 11:25 AM Rate Topic: -----

#1 Grom   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 31-January 08

ANSI C syntax to PHP

Posted 05 April 2008 - 07:32 AM

Hi, pls .. is there any body , who knows how convert metod "memset" from ANSI C to PHP ??
there is the code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define MAXCT 20

int dim1[MAXCT][MAXCT];
int dim2[MAXCT][MAXCT];
int l1[MAXCT][MAXCT];
int e[MAXCT][MAXCT];

#define NOO(A,B,C,D) (dim1[A][B]*dim2[A][B]*dim2[C][D] + e[A][B] + e[C][D])

void Print(int i, int j)
{
  if (i)
  {
	printf("(");
	Print(l1[i][j], j);
	printf(" x ");
	Print(i - l1[i][j] - 1, j + l1[i][j] + 1);
	printf(")");
  }
  else printf("A%d", j + 1);
}

int main(int argc, char *argv[])
{
  int n, m, i, j, k, l;
  scanf("%d", &n);
  while (n--)
  {
	scanf("%d", &m);
	for (i = 0; i < m; ++i) scanf("%d%d", &dim1[0][i], &dim2[0][i]);
	memset(l1[0], -1, sizeof(l1[0]));
	memset(e[0], 0, sizeof(e[0]));
	for (i = 1; i < m; ++i)
	  for (j = 0; j < m - i; ++j)
	  {
		e[i][j] =  NOO(0, j, i - 1, j + 1);
		l1[i][j] = 0;
		for (k = 1; k < i; ++k)
		{
		  l = NOO(k, j, i - k - 1, j + k + 1);
		  if (l <= e[i][j])
		  {
			e[i][j] = l;
			l1[i][j] = k;
		  }
		}
		dim1[i][j] = dim1[0][j];
		dim2[i][j] = dim2[0][i + j];
	  }
	Print(m - 1, 0);
	printf("\n");
  }
  system("PAUSE");	
  return 0;
}


This post has been edited by Grom: 05 April 2008 - 08:09 AM


Is This A Good Question/Topic? 0
  • +

Replies To: ANSI C syntax to PHP

#2 Martyr2   User is online

  • Programming Theoretician
  • member icon

Reputation: 5552
  • View blog
  • Posts: 14,558
  • Joined: 18-April 07

Re: ANSI C syntax to PHP

Posted 05 April 2008 - 08:51 AM

There is no equivalent. PHP doesn't deal in pointers like C nor does it directly manipulate memory like a full blown language like C. Remember PHP is an interpreted language which means it isn't compiled it is translated during runtime. This also means it doesn't have a lot of the same functionality as a low level computer programming desktop language. It is more of a scripting language than a compiled language.

However you can mimic some of memsets abilities by classic string manipulation functions...

Example in C...

char str[] = "almost every programmer should know memset!";
memset (str,'-',6);
puts (str);

// Returns "------ every programmer should know memset!"



You could do the same with the following...

$variable = "almost every programmer should know memset!";
echo substr_replace($variable, str_repeat("-",6),0, 6);

// Prints "------ every programmer should know memset!"



But this is for manipulating variables and not memory directly. Since PHP is designed to be a web language available to the public through the web, you wouldn't want it to directly manipulate memory anyways for security reasons.

Hope this helps. :)
Was This Post Helpful? 0
  • +
  • -

#3 Grom   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 31-January 08

Re: ANSI C syntax to PHP

Posted 05 April 2008 - 10:04 AM

And , how do memset for INT ? .. in my code .. a use memset for array arrays of ints.
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2   User is online

  • Programming Theoretician
  • member icon

Reputation: 5552
  • View blog
  • Posts: 14,558
  • Joined: 18-April 07

Re: ANSI C syntax to PHP

Posted 05 April 2008 - 10:25 AM

I think you missed the point of my original answer... PHP doesn't manipulate memory directly. You don't have a memset equivalent for manipulating data types like int.

Perhaps you can explain what you are attempting to do and we can give you some options for accomplishing that.

:)
Was This Post Helpful? 0
  • +
  • -

#5 Grom   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 31-January 08

Re: ANSI C syntax to PHP

Posted 05 April 2008 - 11:25 AM

i know that , but your resolution is for string .. NO for array with numbers. Or your resolution is for array of numbers too ?

example :
$numbres=Array(0,1,2,3,4,5,6,7);



How can i do that with numbers ???

This post has been edited by Grom: 05 April 2008 - 11:28 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1