One dollar sparse polynomial calculator: Title of a data structure experiment

xiaoxiao2021-03-06  60

It is said that I have to pay this metamorphosis today, so I wrote such a small procedure overnight, but I got the school teacher today, but I didn't hit today, and I was hit by my sleep. Oh, how much is it finished, and I have a look at everyone. Enter a one-yuan polynomial, for example: -23x ^ 9 54x ^ 8 22x ^ 9, etc., merge the same item as the index, and arrange the output string according to the index size. It's almost. Teacher asked to use linear forms, I will do it as required. The source code contains four files, DataStruct.h is the definition of the data structure; ListOper.h is a declaration of linear table operation functions; ListOper.cpp is the definition of linear table operation function; main.cpp is the main unit contains master and strings Analysis function. DataStruct.htypedEf struct list {int C; // Multi-class INT E; // Multi-class index structure strunt resT * next; // Next node};

Typedef struct list * linklist; typef struct list node; the following is the operational correlation function declaration of linear tables, corresponding file listoper.h: // file: listoper.h

#ifndef datastruct # define Datastruct # include "datastruct.h" #ENDIF

// Some functions declaretioin

bool CreateList (LinkList & L); Node * CreateNode (int e, int c); void FreeList (LinkList & L); void SortList (LinkList & L); void DeleteNextNode (Node * d); void SweepNextNode (Node * s); void OutPutList (LINKLIST & L); Realization of related functions, corresponding file Listoper.cpp: // file: listoperty.cpp

#include #include #ifndef datastruct # deflude "datastruct.h" # Endif # include "listoper.h"

Using namespace std;

Bool Createlist (Linklist & l) {// Todo: Create a linear linked list node * head; head = (node ​​*) malloc (sizeof (node)); if (head == null) {cout << "memory allocation error" << Endl; Return False;} head-> next = null; head-> c = 0; head-> e = 0; l = head; return true;}

Node * Createnode (int E, INT C) {// Todo: Create Node Node * POS; POS = (Node *) Malloc (SizeOf (Node)); if (POS == NULL) {COUT << "Memory Assignment Error << Endl; exit (1);} POS-> E = E; POS-> C = C; POS-> Next = null; Return Pos;} Void Freelist (Linklist & l) {// Todo: Release the entire Memory space occupied by linear table; Node * Next; POS = L; While (POS! = Null) {Next = POS-> Next; Free (POS); POS = next;}} Void Sortlist (Linklist & l ) {Bool flag = true; // Do you need a sort flag node * head = l-> next; node * pos; node * last; node * temp; if (head-> next == null) {Return;} while Flag) {flag = true; las = head; pOS = last-> next; if (Last == Null || Last-> next == null) {Break;} while (Last! = Null && Last-> Next! = Null) {flag = false; pOS = last-> next; if (last-> e e) // Hahahaha, html code {sweepnextNode (Last); flag = true; IF (Last-> E == POS-> E) {Last-> C = POS-> C; deletenextNode (Last); Flag = true; / * last = last-> next; pOS = last-> next; * /} Las = last-> next;}}}

Void deletenextNode (Node * D) {node * Temp; Temp = D-> Next; D-> Next = TEMP-> NEXT; Free (TEMP);

Void SweepnextNode (Node * S) // Laying method, only swap value, does not modify the pointer {INT C, E; c = S-> C; E = S-> E; S-> c = S-> Next -> c; s-> e = S-> Next-> E; S-> Next-> c = C; S-> Next-> E = E;} Void OutputList (Linklist & l) {node * POS; POS = L-> Next; Cout << "Output expression:"; While (POS! = Null) {if (POS-> C> 0) {cout << ";} if (POS-> C! = 1) {cout << pos-> c;} if (pOS-> e! = 0) {cout << "x ^"; cout << pos-> e;} POS = POS-> next;} Cout < #include #include #include "listoper.h"

Using namespace std;

Linklist Anaystring (char astring [], int aLength

INT Main (int Argc, char * argv []) // ----------------------------- {linkList L; char InStr [1024]; int len; cout << "one yuan sparse polynomial calculator" << endl; cout << "Copyright @ 1999-2004, Gashero Liu." << endl; cout << "author: Liu" << Endl << Endl; Cout << "Please enter a sparse polynomial within 1024 characters:"; cin >> instr; len = strlen (instr); l = anaystring (instr, len); sortlist (L); OutputList L); freeelist (L); System ("pause"); return 0;}

Linklist AnayString (CHAR Astring [], INT aLENGTH) / / --------------- // Todo: String Analysis Function {LinkList L = NULL; Node * POS = NULL; Node * Last; Node * Head; Createlist (L); Head = L; Last = Head; INT C = 0; Int E = 0; char Temp [1]; char TP; BOOL PLUS = true; char status = 'n'; // Status indicator, I omitted the factor-negative situation / * N: Non-operational state C: Positive calculation factor E: calculation index P: Index is 0 f: Complete a project * / for (int I = 0; I

E = 0; CONTINUE;} // The possible IF (TP == ' ') {plus = true; status = 'p'; CONTINUE;} if (tp == ') - ') {Plus = false; status =' p '; Continue;} / * if (Temp [0] ==' ^ ') {status =' e '; E = 0; CONTINUE;} * / // Cairinue;} // Prescent the coefficient case 'e': {if (tp == '^') {Continue;} if (isdigit (TP)) {E = E * 10 ATOI (TEMP) Continue;} if (tp == ' ') {plus = true; status = 'f'; Continue; } IF (tp == '-') {plus = false; status = 'f'; continue;}} // is in parsing coefficient case 'p': {E = 0; status = 'f'; CONTINUE;} 'f': {POS = Createnode (E, C); Last-> Next = POS; LAST = POS; C = 0; E = 0;

转载请注明原文地址:https://www.9cbs.com/read-111837.html

New Post(0)