# Huawei interview questions: one-dollar polynomial simplification C language Realization source code

Source: Internet
Author: User
Tags strtok

The

Program implements the following functions: For the input of the unary polynomial, the same kind of items are merged and sorted in descending order of exponential, the output of the processed unary polynomial.
Description:
1. The polynomial consists of a number of individual elements, and the single-form is a plus and minus (+,-) relationship.
2. A single type refers to the algebraic formula of the product of a number and a power of letters. For unary polynomial, there is only one type of letter.
3. The merger of homogeneous items refers to a single formula in which the exponent of the polynomial is the same, and the coefficients are combined into one single item by adding and decreasing the sum. In descending exponential order, the single-item is concatenated by exponent from large to small sequence
.
Format Description
A unary polynomial input and output is represented as a string, in the form of a
L. A single add-minus operator is used to connect an operator: +,-
2. The individual forms are directly connected by coefficients, letters, index identifiers and indices, and the parts cannot be omitted.
factor: consists of only a few 0 to 9 numeric characters (coefficients not equal to 0 and does not start with 0)
letters: X
index identifiers: ^
index: Consists of only a few 0 to 9 numeric characters (the exponent can be equal to 0, which does not begin with 0)
3. Other conventions
input is not a blank string, and output if 0 is represented by a null string
String In addition to the above characters, not including spaces and other characters, the end of the string with ' \ ' ends
the first single-item in the polynomial before the addition of the operation to omit the + sign, minus the operation has-symbol
Note: The input polynomial conforms to the above format, no need to check; the output polynomial format is guaranteed by the Examinee program
Specification
Input polynomial satisfies the following specifications, the Examinee program does not need to check:
–0< single-factor <=1000<>
–0<= Single-type index <=3000<>
– The number of individual types is not limited, but the coefficients of single-type items are less than 65535 after the same class.

Example
Input:
" -7x^4+5x^6-3x^3+3x^3+1x^0"
Output:
"5x^6-7x^4+1x^0"

<= single-type index <=3000<>
< single-factor <=1000<>

Input: -7x^5+7x^3+1x^2-7x^3+2x^5
Output: -5x^5+1x^2 (requires ordering from high to low)

`#include "string.h" #include "stdio.h" #include "stdlib.h" #define MAX_PATH 256int store = {0};int num = 0;void Getstri  Ngbeforestring (char *out,char *in,char *subflag) {char *t = strstr (In,subflag); if (t!=null) {memcpy (out,in,t-in);} else {memset (out,0,1);}} Char *getunit (char *out,char *in,int *pn) {char *position = NULL; char tmp1 = {0}; char tmp2 = {0}; getstringb Eforestring (Tmp1,in, "+"); Getstringbeforestring (Tmp2,in, "-");  if (strlen (TMP1) ==0 && strlen (TMP2) ==0) {memset (out,0,1); return in;   } if (strlen (TMP1) <strlen (TMP2)) {if (strlen (TMP1)!=0) {memcpy (Out,tmp1,strlen (TMP1) +1);  *PN = 1;//Positive position = in+ (Strstr (in, "+")-in) +1;   } else {memcpy (Out,tmp2,strlen (TMP2) +1);  *PN = 0;//Negative position = in+ (Strstr (In, "-")-in) +1;   }} else {if (strlen (TMP2)!=0) {memcpy (Out,tmp2,strlen (TMP2) +1);  *PN = 0;//Negative position = in+ (Strstr (In, "-")-in) +1;   } else {memcpy (Out,tmp1,strlen (TMP1) +1); *PN = 1;//Positive position = in+ (Strstr (in, "+")-in) +1; }} return position;} Preparation: Storing data, processing data (matrix operations: Elements of rows and columns corresponding to product's and), output int main () {//Input://" -7x^4+5x^6-3x^3+3x^3+1x^0"//output://"5x^6-7x^4+1x^0" Char buffer = {0}; Char out = {0}; Gets (buffer); int pn = 1,pn_next = 1;//record positive or negative, 1 is positive char *position = buffer;  Handle the first if (*position== '-') {pn_next = 0 separately;  PN = Pn_next; Position = position+1;  } while (true) {memset (out,0,1024);  Position = Getunit (Out,position,&pn_next);   if (strlen (out) ==0) {//handles the last set of char *a = strtok (position, "x^");   Char *b = strtok (NULL, "x^");   if (pn==1) {Store[atoi (b)] + = Atoi (a);   } else {Store[atoi (b)]-= Atoi (a);  } break;  } Char *a = Strtok (out, "x^");  Char *b = strtok (NULL, "x^");  if (pn==1) {Store[atoi (b)] + = Atoi (a);  } else {Store[atoi (b)]-= Atoi (a); } pn = Pn_next; } int headflag = 0;  for (int i=1023;i>=0;i--) {int t = Store[i];    if (t!=0) {if (t>0) {if (headflag==1) {printf ("+");   }} printf ("%dx^%d", t,i); Headflag = 1; }}//store[num++] = Atoi (out); return 0;}`

Huawei interview questions: one-dollar polynomial simplification C language Realization source code

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.