Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

大家可以帮忙看看这个问题是怎么回事吗?一直error

Posted by wangzhanbiwei at 2009-03-19 01:06:50 on Problem 1001
#include <iostream>
#include <cstring>

using namespace std;

#define SIZE_OF_BUF 512
#define SIZE_OF_R 8


int ctoi(char ch)
{
	if (0==ch)
	{
		return 0;
	}
	else
	{
		return ch - 48;
	}
}

char * mul(char *buf, char *A, char *B) {
	int tmp = 0;
	int add = 0;
	char * ret = NULL;
	int i=0,j=0;
	char *p = A;

	while(1)
	{
		if('\0'==*B)
		{
			break;
		}
		if('.'==*B)
		{
			B--;
			continue;
		}
		add = 0;
		i=0;
		A = p;
		while(1)
		{
			if('.'==*A)
			{
				A--;
				continue;
			}
			if ('\0' == *A && 0 == add) {
				break;
			}
			tmp = ctoi(*(buf-i-j)) + ctoi(*A) * (*B - 48) + add;
			*(buf-i-j) = tmp%10 + 48;
			add = tmp / 10;
			A--;
			i++;
		}
		ret = buf-i-j+1;
		B--;
		j++;
	}
	return ret;
}

char * insert_ch(char *p, char ch, int pos)
{
	char buf[SIZE_OF_BUF]={0};
	strncpy(buf, p, pos);
	strcat(buf,".");
	strncpy(p-1,buf,strlen(buf));
	return p-1;
}

/*return E*/
int get_E(char *R)
{
	for(int i=0; ; i++)
	{
		if(0==R[i])
		{
			break;
		}

		if('.'==R[i])
		{
			return -(strlen(R)-1-i);
		}
	}
	return 0;
}

char * process_str(char *str, int E)
{
	char *p = str+strlen(str)-1;
	while(1)
	{
		if('0'==*p && (p > str+strlen(str)-1-E))
		{
			*p = 0;
		}
		else
		{
			break;
		}
		p--;
	}
	while('0'==*str)
	{
		*str=0;
		str++;
	}
	if('.' == *(str+strlen(str)-1))
	{
		*(str+strlen(str)-1) = 0;
	}
	if(strlen(str)==0)
	{
		*str = '0';
	}
	return str;
}

char *calc(char *buf_rear, char *R, const int n) {
	char *p = NULL;
	char tb[SIZE_OF_BUF];


	memset( tb, 0, SIZE_OF_BUF);
	strcpy( tb, R);

	if('.'==*R)
	{
		R--;
		*R = '0';
	}

	if('.'==*(R+1)&&0==*(R+2))
	{
		*buf_rear = '0';
		return buf_rear;
	}

	int e=-get_E(R);
	int E = e;

	if(0==n)
	{
		*buf_rear = '1';
		return buf_rear;
	}
	if(1==n)
	{
		strcpy(buf_rear-strlen(R), R);
		p = process_str(buf_rear-strlen(R), E);
		return p;
	}

	for(int i=2; ; i++)
	{
		E = i*e;
		p = mul( buf_rear, tb+(strlen(tb)-1), R+(strlen(R)-1));
		if(n == i)
		{
			break;
		}
		memset( tb, 0, SIZE_OF_BUF);
		strcpy(tb, p);
		memset( p, 0, strlen(p));
	}
	if(0!=E)
	{
		p = insert_ch( p, '.', strlen(p)-E);
	}

	p = process_str(p, E);
	return p;
}

int main() {
	char *buf 	=	(char *) malloc(SIZE_OF_BUF);
	char *R		=	(char *) malloc(SIZE_OF_R);
	int n = 0;
	while (	cin >> R+2 >> n) {
		cout << calc(buf + SIZE_OF_BUF - 2, R+2, n);
		cout << endl;
		memset(buf, 0, SIZE_OF_BUF);
		memset(R, 0, SIZE_OF_R);
	}

	free(R);
	free(buf);
	return 0;
}



Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator