Compile Error

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
snar
New poster
Posts: 44
Joined: Thu Sep 01, 2005 12:14 pm
Location: Yerevan, Armenia

Compile Error

Post by snar » Wed Jul 19, 2006 1:06 pm

Hi,

I have some problems with my C code. Usually I prefer C compiler, but when I have to use STL, I'm using C++. So C is faster than C++ and I've been thinking to write something like a big integer class in C.

I have a problem with my code.
I compiled it successfully in MVS2003 (I haven't got GNU compiler).
OJ says Compile Error (I send it as p100 in problemset). I'm not so good at C standarts and I really need a help.

Code: Select all

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define MAX 100000

int fscan    ( FILE*, char*& );
void fprint  ( FILE*, char*  );
void fprintln( FILE*, char*  );
int scan   ( char*&        );
void print   ( char*         );
void println ( char*         );

void assign             ( char*, char* );

int equalto             ( char*, char* );
int lessthan            ( char*, char* );
int lessthanorequalto   ( char*, char* );
int greaterthan         ( char*, char* );
int greaterthanoreqaulto( char*, char* );

char* abs ( char*         );

char* add     ( char*, char* );
char* subtract( char*, char* );
char* multiply( char*, char* );

int fscan ( FILE* in, char* &A )
{
	A = (char*)malloc(MAX);
	int i, n = 0;
	char c = getc (in);

    while (isspace (c))
		c = getc (in);
	if (c=='-')
	{
		A[n++] = '1';
		c = getc (in);
	}
	else
		if (c=='+')
		{
			A[n++] = '0';
			c = getc (in);
		}
		else
            A[n++] = '0';
	while (isdigit (c))
	{
		A[n++] = c;
		c = getc (in);
	}
	for (i=1; i<=n/2; i++) 
	{
		c = A[i];
		A[i] = A[n-i];
		A[n-i] = c;
	}
	A[n] = '\0';	
	i = strlen (A) - 1;
	while (A[i]=='0' && i>1) A[i--] = '\0';
	return strlen(A)>1 ? 1:0;
}

void fprint ( FILE* out, char* A )
{
	int i, n = strlen (A);
	if (A[0]=='1') putc ('-', out);
	for (i=1; A[i]; i++)
		fprintf (out, "%c", A[n-i]);
}

void fprintln ( FILE* out, char* A )
{
	fprint (out, A);
	putc ('\n', out);
}

int scan ( char* &A )
{
	return fscan (stdin, A);
}

void print ( char* A )
{
	fprint (stdout, A);
}

void println ( char* A )
{
	fprintln (stdout, A);
}

char* assign ( char* A )
{
    char* B = (char*)malloc(MAX);
	strcpy (B, A);
	return B;
}

int equalto ( char* A, char* B )
{
	return strcmp (A, B) == 0;
}

int lessthan ( char* A, char *B )
{
	if (A[0]=='0' && B[0]=='1')
		return 0;	
	if (A[0]=='1' && B[0]=='0')
		return 1;	
	int i, lA = strlen (A), lB = strlen (B);
	if (A[0]=='0' && B[0]=='0')
	{
		if (lA < lB)
			return 1;
		if (lA > lB)
			return 0;
		for (i=lA-1; i; i--)
			if (A[i] < B[i])
				return 1;
			else
				if (A[i] > B[i])
					return 0;		
	}
	else
	{
        if (lA < lB)
			return 0;
		if (lA > lB)
			return 1;
		for (i=lA-1; i; i--)
			if (A[i] < B[i])
				return 0;
			else
				if (A[i] > B[i])
					return 1;		
	}
	return 0;
}

int lessthanorequalto ( char* A, char* B )
{
    return lessthan (A, B) || equalto (A, B);
}

int greaterthan ( char* A, char* B )
{
    if (A[0]=='0' && B[0]=='1')
		return 1;
	if (A[0]=='1' && B[0]=='0')
		return 0;
	int i, lA = strlen (A), lB = strlen (B);
	if (A[0]=='0' && B[0]=='0')
	{
		if (lA < lB)
			return 0;
		if (lA > lB)
			return 1;
		for (i=lA; i; i--)
			if (A[i] < B[i])
				return 0;
			else
				if (A[i] > B[i])
					return 1;		
	}
	else
	{
        if (lA < lB)
			return 1;
		if (lA > lB)
			return 0;
		for (i=0; A[i]; i++)
			if (A[i] < B[i])
				return 1;
			else
				if (A[i] > B[i])
					return 0;		
	}
	return 0;
}

int greaterthanorequalto ( char* A, char* B )
{
	return greaterthan (A, B) || equalto (A, B);
}

char* abs ( char* A )
{
	char* B = assign ( A );
	if (B[0] == '1') B[0] = '0';
	return B;
}

char* add ( char* A, char* B )
{
    char *tA = assign (abs (A)), *tB = assign ( abs(B)), *C = (char*)malloc(MAX) ;	
	int i, j, c, lA = strlen (tA), lB = strlen (tB);

	if (A[0]==B[0])
	{
        i = 1; 
		c = 0;
		while (tA[i] && tB[i])
		{
			C[i] = (tA[i]-'0'+tB[i]-'0'+c)%10+'0';
			c = (tA[i]-'0'+tB[i]-'0'+c)/10;
			i++;
		}
		if (tA[i])
			while (tA[i])
			{
				C[i] = (tA[i]-'0'+c)%10+'0';
				c = (tA[i]-'0'+c)/10;
				i++;
			}
		else
			while (tB[i])
			{
				C[i] = (tB[i]-'0'+c)%10+'0';
				c = (tB[i]-'0'+c)/10;
				i++;
			}
		if (c)
			C[i++] = '1';
		C[i] = '\0';
		C[0] = A[0];		
	}
	else	
	{
		if (greaterthanorequalto (tA, tB))
		{
			for (i=lB; tA[i]; i++)
				tB[i] = '0';
			tB[lA] = '\0';
			for (i=1; tA[i]; i++)
			{
				if (tA[i] >= tB[i])
					C[i] = (tA[i]-tB[i])%10+'0';
				else
				{
					j = i+1;
					while (tA[j]=='0')
						tA[j++] = '9';
					tA[j]--;
					C[i] = (tA[i]-tB[i]+10)%10+'0';										
				}
			}	
			C[lA] = '\0';
			i = lA-1;
			while (C[i]=='0' && i>1) C[i--] = '\0';
			C[0] = A[0];
		}
		else
		{
			for (i=lA; tB[i]; i++)
				tA[i] = '0';
			tA[lB] = '\0';
			for (i=1; tB[i]; i++)
			{
				if (tB[i] >= tA[i])
					C[i] = (tB[i]-tA[i])%10+'0';
				else
				{
					j = i+1;
					while (tB[j]=='0')
						tB[j++] = '9';
					tB[j]--;
					C[i] = (tB[i]-tA[i]+10)%10+'0';										
				}
			}		
			C[lB] = '\0';
			i = lB-1;
			while (C[i]=='0' && i>1) C[i--] = '\0';
            C[0] = B[0];
		}		
		
	}
	free ( tA );
	free ( tB );
	return C;
}

char* subtract ( char* A, char* B )
{	
	B[0] = B[0]=='0' ? '1':'0';
	char *C = add (A, B);
	B[0] = B[0]=='0' ? '1':'0';
	return C;
}

char* multiply ( char* A, char* B )
{
	char *C;	

	if (equalto (abs(A), "00") || equalto (abs(B), "00"))
		C = assign ("00");	
	else
	{
        int  i, j, c, p, q, lA = strlen (A), lB = strlen (B);
			
		C = (char*)malloc(MAX);
        C[0] = A[0]==B[0] ? '0':'1';
		for (i=1; i<lA+lB-1; i++) C[i] = '0';	
		C[lA+lB-1] = '\0';
		for (i=1; A[i]; i++)
		{
			c = 0;
			for (j=1; B[j]; j++)
			{    
				p = C[i+j-1]-'0';
				q = (A[i]-'0')*(B[j]-'0');
				C[i+j-1] = (p+q+c)%10+'0';
				c = (p+q+c)/10;
			}
			if (c)
				C[i+j-1] = c+'0';
		}			
		i = lA+lB-2;
		while (C[i]=='0' && i>1) C[i--] = '\0';	
	}
		return C;
}

int main()
{
	return 0;
}
Thanks,
Narek Saribekyan

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar » Wed Jul 19, 2006 2:14 pm

Fix the syntax errors first.
1. assign: function declaration/definition doesn't match.
check # of parameters in prototype declaration and usage.
2. C doesn't have references: so no *& stuff
3. abs is predefined in stdlib.h -- can't do that.

snar
New poster
Posts: 44
Joined: Thu Sep 01, 2005 12:14 pm
Location: Yerevan, Armenia

Post by snar » Wed Jul 19, 2006 3:59 pm

Got it! (WA), thanks very much.
--
Narek Saribekyan

Post Reply

Return to “C”