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

代码写的有点乱。打了很多注释。将就看吧。。。。

Posted by 274856653 at 2020-12-18 17:12:10 on Problem 1467
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define  MAX_N 120 

char istr[MAX_N];
char ostr[MAX_N];

void init()
{
				memset(istr, 0, sizeof(istr));
				memset(ostr, 0, sizeof(ostr));

}

int findright(char * str, int pos)
{
				int i;
				int left = 0, right = 0;
				int len = strlen(str);

			//	printf("findright str = %s, pos = %d\n", str, pos);
				for(i = pos; i<len; i++)
				{
								if(str[i] == '(')
								{
												left++;
								}else if(str[i] == ')' )
								{
												right++;
								}
								if(left == right)
												return i;
				}
}

int findop(char *str, int start, char *op, int flag, char *ostr)
{
				int i, pos = 0;
				int len = strlen(str);

			//	printf("findop begin str = %s, flag = %d, pos = %d\n", str, flag, start);
				for(i = start; i<len; i++)
				{
								if(str[i] =='(')
								{

												pos = findright(str  , i);
												i = pos;


												//找出右端点
								}else if(str[i] == 'l')
								{
												pos = findright(str+i+2, i);
												i = pos;

												//找出右端点
								}else if(str[i] == '+'|| str[i] == '-'||str[i] == '*'|| str[i] == '/')
								{
												if(flag == 0 && i == 0)
																continue;
												memcpy(ostr, str+start, i-start);
												*op = str[i];
												return i;
								}
				}
				memcpy(ostr, str+start, i-start);
				return i;
}


void Derivation(char * in, char *out)
{
				char astr[MAX_N] = {0};
				char bstr[MAX_N] = {0};
				char cstr[MAX_N] = {0};
				char dstr[MAX_N] = {0};

				char mstr[MAX_N] = {0};//寻找乘法串。。 
				char fstr[MAX_N] = {0};
				char temp[MAX_N] = {0};

				char lop = 0, op = 0, opy;
				int i = 0, j = 0, begin = 0;
				int flag = 0, pos;
				int len1;
				int len = strlen(in);
//				printf("Derivation in = %s, len = %d\n", in, len);
				for(i = 0; i<len; i++)
				{
								memset(temp, 0, sizeof(temp));
								lop = op;

								op = 0;
								pos = findop(in, i, &op, flag, temp );

	//							printf("findop results in = %s, temp = %s, op = %c, pos = %d\n", in, temp, op, pos);

								i = pos;
		//						printf("which in = %s, op = %c, lop = %c \n", in, op, lop);
								if(((op == '+' || op == '-') && (lop == '*' || lop == '/' )) || (op == 0  &&(lop == '*' || lop == '/' )) )
								{
															//找到了最外层的*, /
																len1 = strlen(fstr);	
																fstr[len1-1] = '\0';
																memset(astr, 0, sizeof(astr));
																memset(bstr, 0, sizeof(bstr));
			//													printf("multiply and divide, fstr = %s\n", fstr);
																Derivation(fstr, astr); 
																Derivation(temp, bstr);

																opy = op;
																if(op == 0)
																				op = lop;
				//												printf("fstr = %s, temp = %s\n", fstr, temp);
					//											printf("in = %s, astr = %s, bstr = %s, op = %c\n", in, astr, bstr, op);

																memset(cstr, 0, sizeof(cstr));

																if(lop == '*' )
																{
																				if(fstr[0] == '-')
																								sprintf(cstr, "(%s*%s%s*%s)", astr, temp, fstr, bstr);
																				else
																								sprintf(cstr, "(%s*%s+%s*%s)", astr, temp, fstr, bstr);

																}else if(lop == '/' )
																{
																				if(fstr[0] == '-')
																				{
																								fstr[0] = '+';
																								sprintf(cstr, "(%s*%s%s*%s)/%s^2", astr, temp, fstr, bstr, temp);
																				}
																				else
																								sprintf(cstr, "(%s*%s-%s*%s)/%s^2", astr, temp, fstr, bstr, temp);
																}
																strcat(dstr, cstr);


																memset(cstr, 0, sizeof(cstr));
																if(opy != 0)
																{
																				sprintf(cstr, "%c", opy);
																}
																strcat(dstr, cstr);

//																printf("opy = %c, cstr = %s, dstr = %s\n", opy, cstr, dstr);
																memset(fstr, 0, sizeof(fstr));
																lop = 0;

								}else if (((op == '+' || op == '-') && lop!='*' &&  lop!='/') ||(op == 0 && (lop == '+' || lop == '-' )))
								{
												memset(astr, 0, sizeof(astr));
												Derivation(temp, astr);

												memset(bstr, 0, sizeof(bstr));
												sprintf(bstr, "%s", astr);
												strcat(dstr, bstr);
//												printf("normal1 bstr = %s, dstr = %s, op = %d\n", bstr, dstr, op);

												memset(bstr, 0, sizeof(bstr));
												if(op != 0)
												{
																sprintf(bstr, "%c", op);
												}
												strcat(dstr, bstr);
	//											printf("normal2 bstr = %s, dstr = %s\n", bstr, dstr);

												lop = 0;

								}else if(op == '*' || op == '/')
								{

		//										printf("opis mul and div\n");
												memset(mstr, 0, sizeof(mstr));
												sprintf(mstr,"%s%c",temp, op);


												strcat(fstr, mstr);
			//									printf("mstr = %s, fstr = [%s]\n", mstr, fstr);


								}else if(op == 0 && lop == 0 )
								{
												if(in[0] == '-' || (in[0]>='0' && in[0]<='9') ) //constant
												{
				//												printf("in = %s, is constant\n", in);
																memset(mstr, 0, sizeof(mstr));
																sprintf(mstr,"0");
																strcat(dstr, mstr);
												}else if(in[0] == 'x')
												{
					//											printf("in = %s, is x\n", in);
																memset(mstr, 0, sizeof(mstr));
																sprintf(mstr,"1");
																strcat(dstr, mstr);
												}else if(in[0] == 'l')
												{

																len1 =  strlen(temp);
																memset(astr, 0, sizeof(astr));
																memcpy(astr, temp+3, len1 - 4 );
														//		printf("in = %s, is ln, astr = %s\n", in, astr);

																memset(bstr, 0, sizeof(bstr));
																Derivation(astr, bstr);
																memset(cstr, 0, sizeof(cstr));

																sprintf(cstr, "(%s)/(%s)", bstr, astr);
																strcat(dstr, cstr);
															//	printf("in = %s, is ln, astr = %s, bstr = %s, cstr = %s, dstr\n", in, astr, bstr, cstr, dstr);

												}else if(in[0] == '(')
												{
																len1 =  strlen(temp);
																memset(astr, 0, sizeof(astr));
																memcpy(astr, temp+1, len1 - 2 );
																memset(bstr, 0, sizeof(bstr));

															//	printf("in = %s, is kuohao astr = %s\n", in, astr);

																Derivation(astr, bstr);

																memset(cstr, 0, sizeof(cstr));
																sprintf(cstr, "(%s)", bstr);

																strcat(dstr, cstr);

															//	printf("in = %s, is kuohao astr = %s, bstr = %s, dstr = %s\n", in, astr, bstr, dstr);
												}

								}


								flag == 1; //

				}

				sprintf(out, "%s", dstr);
				return ;
								
}





int main()
{
				int i, j;

				init();
				while(scanf("%s", istr)!=EOF)
				{

				//				printf("istr = %s\n", istr );

								Derivation(istr, ostr);

					//			printf("ostr = %s\n", ostr );
								printf("%s\n", ostr );



								init();
				}

}


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