Forum Replies Created
- AuthorPosts
- GWILouisaxwzklaParticipant
heres a start ( I’m not certain why you are dividing every purchase in the program by 1000 ) . By the way don’t use “gotos” in high level programming languages , this is considered bad programming practice ( google Dijkstra ). Anyway , this is how I might code what you have ( the total comes out 0 since everything is divided by 1000 ). Anyway……..
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485<br /><br />#include<iostream.h><br />#include<conio.h><br />#include <ctype.h><br /><br />float b=1000;<br />void main()<br />{<br />//clrscr();<br /><br />int total = 0;<br />double amount;<br />double temp;<br />char choice;<br />cout << "nwelcome 2 my grocery store!!!!";<br />cout << "nwould u like 2 see the items available for purchase(y/n)? ";<br />cin >> choice;<br />cin.get();<br />if ( choice == 'Y' || choice == 'y' )<br />{<br /><br />do<br />{<br /><br />cout << "the items available are:";<br />cout << "nno."<<'t'<<"item "<<'t'<<"quantity(b)(in grams)"<<'t'<<"m.r.p(c)";<br />cout << "n---"<<'t'<<"---- "<<'t'<<"<hr class="bbcode_rule" />"<<'t'<<"<hr class="bbcode_rule" />";<br />cout << "n1. "<<'t'<<"urad dal "<<'t'<<"1000g "<<'t'<<"Rs.50.25";<br />cout << "n2. "<<'t'<<"tur dal "<<'t'<<"1000g "<<'t'<<"Rs.60.75";<br />cout << "n3. "<<'t'<<"tea powder "<<'t'<<"1000g "<<'t'<<"Rs.200.0";<br />cout << "n enter choice => ";<br />cin >> choice;<br />cin.get();<br /><br /><br />if( choice == '1' )<br />{<br />cout << "nenter the amount of the item u want to purchase ";<br />cin >> amount;<br />temp = ( amount * 50.25 ) / 1000.00;<br />total = temp + total;<br />cout << "n the amount to be paid is:" << temp << "n";<br />}<br />else if( choice == '2' )<br />{<br />cout << "nenter the amount of item ";<br />cin >> amount;<br />temp = ( amount * 60.75 ) / 1000.00;<br />total = temp + total;<br />cout << "nthe amount 2 b paid is:" << total << "n";<br />}<br />else if( choice == '3' )<br />{<br />cout << "nenter the amount of item ";<br />cin >> amount;<br />temp = ( amount * 200.00 ) / 1000.00;<br />total = temp + total;<br />cout << "nthe amount 2 be paid is:" << total << "n";<br />}<br />cout << "ndo u want to buy another item? ";<br />cin >> choice;<br />cin.get();<br />} while ( choice == 'y' || choice == 'Y' );<br /><br />cout<<"nplease wait.... ur bill is being calculated...." << endl;<br />cout << "the total is " << total << endl;<br /><br /><br />cout << "nplease pay the requested amount" << endl;<br />cout << "nthank you.please visit again" << endl;<br /><br />cout<<"nthanks for visiting my store !!!" << endl;<br /><br />}<br />cout << "nthank you.kindly try purchasing the next time u visit the store!!!!!" << endl;<br /><br />getch();<br />}<br /><br />GWILouisaxwzklaParticipantcould you post your source code?
GWILouisaxwzklaParticipantMight try here :
GWILouisaxwzklaParticipantHeres how I might do this ( this uses pointer arthimatic instead of indexes ) :
1234567891011121314151617181920212223242526272829303132333435363738394041<br /><br />#include <stdio.h><br />#include <iostream.h><br /><br />void draw(char *w, int len);<br /><br />int main()<br />{<br /><br />char words [ 100 ] = ("This is my hundredth attept st this extremely annoying task :)");<br />char * index = NULL;<br />int len = 21;<br /><br />draw( words + 11, len );<br /><br /><br /><br />return 0;<br />}<br /><br />void draw ( char * words, int lengthOutput ) //words points to words [ 11 ]<br /><br />{<br /><br />char * upperLimit; //pointer for storing upper limit of output<br /><br /><br />upperLimit = words + lengthOutput; //upperLimit points to the 32nd char of words [ 100 ]<br />putchar ( 'n' ); //output newline<br />while ( words != upperLimit ) //while not at the 32nd char of array<br />{<br />putchar ( * words); //output the current char<br />words ++; //goto next char in the array<br />}<br />putchar ( 'n' ); //output a newline<br /><br />}<br /><br /><br />GWILouisaxwzklaParticipantTo understand the problem completely, remember that arrays are configured as contiguous blocks of memory ( the first index in memory precedes the next and so on ) and realize the fact that pointers are simply variables that hold the address of other variables ( the size depends on your hardware and operating system ). Also keep in mind that in C the semantic value of an array’s name is the address of the first item in the array ( array [ 0 ] ). So that in your program you have a situation like :
1234<br /><br />draw(words + 11, len); //pass in the address of the 11th 8-bit item in memory, words [11]<br />since the array looks like this in memory ( let 0x0065fd94 be the first address of the array ):
memory address item
0x0065fd94 ‘T’
0x0065fd95 ‘h’
0x0065fd96 ‘i’
0x0065fd97 ‘s’
…………..and so on your loop:
123456<br /><br />for(index; *index != *(index + len); index++)<br />putchar(*index);<br />}<br />starts at index == words [ 11 ] which is ‘h’ and continues until ( *index == *(index + len); ) . But since * ( index + len ) is words [ 32 ] == ‘h’ your loop never executes. I think what you wanted to do is this:
1234567891011121314151617181920212223242526272829303132333435363738394041<br />#include <stdio.h><br />#include <iostream.h><br /><br />void draw(char *w, int len);<br /><br />int main()<br />{<br /><br />char words [ 100 ] = ("This is my hundredth attept st this extremely annoying task :)");<br />char * index = NULL;<br />int len = 21;<br /><br /><br /><br />draw( words + 11, len );<br /><br /><br /><br />return 0;<br />}<br /><br />void draw ( char * w, int len )<br /><br />{<br />char * index = NULL;<br />int i = 0;<br /><br />index = w;<br /><br />putchar ( 'n' );<br />for( ; i < len ; index ++ , i ++ )<br />{<br />putchar ( * index );<br />}<br />putchar ( 'n' );<br />}<br /><br /><br /><br />GWILouisaxwzklaParticipantWould help to post your code and errors. Make sure that the file your including has the correct “include” syntax , like:
1234<br /><br />#include "c:myfile.cpp"<br />and make sure that your function prototypes and declarations match perfectly or the linker will not associate the two correctly….
March 16, 2009 at 8:40 pm in reply to: Need help URGENTLY on my assignment (instructions inside) #3523GWILouisaxwzklaParticipantNot totally sure what you need ( not sure what the ‘p’ condition should be ) but heres a start:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253<br /><br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : March,16,2009<br />* Comments : new project<br />* Compiler/Assembler :<br />* Modifications :<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 3:34:03 p.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include <br />//#include <br />//#include <br />//#include <br />//#include <br />//#include <br /><br />using namespace std;<br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />char hello [ 12 ] = "Hello world";<br />char ch;<br />char newline = 10;<br /><br />cout < ";<br />ch = cin.get();<br />while ( ch != newline )<br />{<br /><br /><br />if ( ch > 0 && ch < 0 )<br />cout << "x" << endl;<br />else if ( ch - '0' >= 0 && ch - '0' <= 11 )<br />cout << hello [ ch - '0' ] << endl;<br />else if ( ch - '0' 11 )<br />cout << "z" << endl;<br />ch = cin.get();<br />}<br />return 0 ;<br />}<br /><br />GWILouisaxwzklaParticipantcould do:
C++1234567891011121314151617181920212223242526#include <iostream>#include <string>using namespace std;int main(void){string name;string math;int a;int d;char mathGrade;cout << "Enter name: n";cin >> name;cout << "Enter math grade " << name << " got: n";cin >> mathGrade;if ( tolower ( mathGrade ) == 'a' || tolower ( mathGrade ) == 'b' || tolower ( mathGrade ) == 'c' )cout << name << " passed n";else if ( tolower ( mathGrade ) == 'd' || tolower ( mathGrade ) == 'f' )cout << name << " failed n";system("PAUSE");return 0;}GWILouisaxwzklaParticipantI think read() is for arrays. Check out this link on the topic: http://www.cplusplus.com/reference/iostream/istream/read.html
GWILouisaxwzklaParticipantcin.get() is a member function that gets the next character from the input stream:
1234567<br />char ch;<br /><br />ch = cin.get(); //get a single character from the keyboard and store in 'ch'<br />ch = cin.get(); //get a single character from the keyboard , but don't store it ( throw it out )<br /><br />GWILouisaxwzklaParticipantThis seems to work:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980<br /><br />#include <iostream><br />#include <fstream><br />#include <string.h><br />#include <stdlib.h><br />#include <ctype.h><br />#define MAX_NAME 1000<br /><br /><br />using namespace std;<br />char sendtext;<br />char username [ MAX_NAME ];<br /><br />void read(){<br />system("cls");<br />cout << "You autorised as:" << username << "n";<br />ifstream in;<br />in.open("c:\programs\in1.txt");<br />if (!in) {<br />cout << "Input file cannot be opened.n";<br />//return 1;<br />}<br />char str[20000];<br />while (!in.eof()) {<br />in >> str;<br />cout << str << "n";<br /><br />}<br />cout << "n";<br />in.close();<br /><br />}<br />void write ()<br />{<br />char badInput;<br />char choice;<br />cout << "do you want to input data to file ( y or n )" << endl;<br />cin >> choice;<br />cin.get(); //get newline<br />while ( tolower ( choice ) != 'y' && tolower ( choice ) != 'n' )<br />{<br />badInput = cin.get();<br />while ( badInput != 'n' ) //clear garbage out of stream<br />{<br />badInput = cin.get();<br />}<br />cout << "bad choice!" << endl;<br />cout << "do you want to input data to file ( y or n )" << endl;<br />cin >> choice;<br />cin.get(); //get newline<br />}<br /><br />if ( tolower ( choice ) == 'y' )<br />{<br />char inputText [ MAX_NAME ];<br />cout << "enter input text: " << endl;<br />cin.getline ( inputText , MAX_NAME , 'n' );<br /><br />ofstream outputFile;<br />//PUT YOUR DATA FILE NAME HERE!<br />outputFile.open ( "msngr.txt" , ios::app );<br />outputFile << inputText << endl;<br />outputFile.close();<br />read();<br />write();<br />}<br /><br />}<br />main(){<br />cout << "Enter your username: ";<br />cin >> username;<br />cin.get();<br />read();<br />write();<br /><br /><br />return (0);<br />}<br /></fstream></iostream>GWILouisaxwzklaParticipantfor the write function I would do something like ( I don’t think I would have the console prompt inside the function , but if this is what you want ):
123456789101112131415161718192021222324252627282930313233<br /><br />void write ()<br />{<br /><br />char choice;<br />cout << "do you want to input data to file ( y or n )" << endl;<br />cin >> choice;<br />cin.get(); //get newline<br />while ( choice != 'y' && choice != 'n' )<br />{<br />cout << "bad choice!" << endl;<br />cout << "do you want to input data to file ( y or n )" << endl;<br />cin >> choice;<br />cin.get(); //get newline<br />}<br /><br />if ( choice == 'y' )<br />{<br />char inputText [ MAX_NAME ];<br />cout << "enter input text: " << endl;<br />cin.getline ( inputText , MAX_NAME , 'n' );<br /><br />ofstream outputFile;<br />//PUT YOUR DATA FILE NAME HERE!<br />outputFile.open ( "c:\programs\help\data3.txt" , ios::app );<br />outputFile << inputText << endl;<br />outputFile.close();<br /><br />}<br /><br />}<br />GWILouisaxwzklaParticipantI think the calculation you are making here:
123<br />overall average = (22.421875 + 19.417475 + 24.000000)/3<br />is the average of the average fuel milages. I belive the calculation for average fuel milage is in fact ( miles ) / ( gallons ) and that this quantity is named “average” since calculating “actual fuel milage” would require introducing a set of standard conditions per vehicle ( standard temperature , air pressure , fuel mixture , road surface friction , weather conditions , driving style , ect. ) to arrive at the value of actual miles per gallon. Since simply dividing the number of miles driven by the number of gallons consumed ignores all the possible parameters in the problem the calculation is refered to as “average miles per gallon”. I’ll look into this but thats my guess…
GWILouisaxwzklaParticipantThe first function “read” looks ok to output the file data one word per line ,except I would probably pass the user’s name as a parameter instead of a global variable :
123<br />void read( char * userName );<br />the second function for adding data to a file ( not sure why you used both fstream.h and stdio.h for file input / output in the same program ( ? ) ) seems to have some problems. First I would allocate an array of the proper size and use strcpy ( string.h ) to copy the data.
1234<br />char yazi [ MAX_SIZE ];<br />strcpy ( yazi , sendText );<br />Next , you should take read() and write() out of the conditional or the program will never end :
12345678910111213<br />if ( sendtext )<br />{<br />FILE * pFile;<br />char yazi[] = {sendtext , 'n'};<br />pFile = fopen ( "msngr.txt" , "a+" );<br />fwrite (yazi , 1 , sizeof(yazi) , pFile );<br />fclose (pFile);<br />read(); //take these out!<br />write();<br />}<br /><br />thats what I see so far…..
GWILouisaxwzklaParticipantHeres a way to do this with a doubly linked list with quicksort. This version of quicksort uses the last item in the
list as a pivot element when partitioning.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446<br /><br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : January,6,2008<br />* Comments : new project<br />* Compiler/Assembler :<br />* Modifications :<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 3:51:46 p.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include < iostream ><br />#include < fstream ><br />//#include < string.h ><br />//#include < conio.h ><br />//#include < math.h ><br />#include < iomanip ><br />#include < ctype.h ><br /><br />using namespace std;<br /><br />//>>>>>>>>>>>>>>>>>>>>>>>> GLOBAL DATA <<<<<<<<<<<<<<<<<<<<<<<<<<br /><br />const int MAX_NAME = 50;<br />const int MAX_FILE_NAME = 50;<br />const char STOP_SYMBOL = '*';<br />enum keys { firstName , lastName , idNumber };<br />keys sortKey ;<br />#define OUTPUT_SPACES 15<br />//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<br /><br /><br /><br />struct person<br />{<br /><br />char firstName [ MAX_NAME ] ;<br />char lastName [ MAX_NAME ] ;<br />char idNumber [ MAX_NAME ] ;<br />person * prior;<br />person * next;<br />};<br /><br /><br />//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@<br /><br />bool readInputFile ( char * fileName , person *& frontList , person *& backList , int & itemsRead );<br />void quickSort ( person ** front , person ** last , int numberItems );<br />void swap ( person ** first , person ** last , person ** front , person ** back );<br />void print ( person * list );<br />void insertionSort ( person * list , int numberOfItems );<br />void destroyList ( person * list );<br />char * key ( person * node );<br /><br />//##################################################################################<br /><br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />person * front , * back;<br />int items;<br /><br />sortKey = firstName;<br />readInputFile ( "c:\programs\data.txt" , front , back , items );<br />print ( front );<br />quickSort ( & front , & back , items );<br />cout << endl << endl;<br />cout << "final ************************" << endl;<br />print ( front );<br />destroyList ( front );<br />return 0 ;<br />}<br /><br /><br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : readInputFile<br />Parameters :<br /><br />fileName a(n) char * ,<br />frontList a(n) person *& ( person *& ) ,<br />backList a(n) person *& ( person *& ) ,<br />itemsRead a(n) int &<br /><br /><br />Returns: bool type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />bool readInputFile ( char * fileName , person *& frontList , person *& backList , int & itemsRead )<br />{<br /><br /><br />fstream inputFile;<br />person * temp;<br />char ch;<br />inputFile.open ( fileName ); //open input file<br />if ( inputFile.fail () )<br />{<br />cout << "input file did not open! " << endl;<br />return false;<br />}<br />temp = new person; //make new node in list<br />if ( temp == 0 ) //if allocation error<br />{<br />cout << "allocation error! " << endl;<br />return false;<br />}<br />itemsRead = 1; //start count at 1<br />frontList = backList = temp; //set front and back pointers to new node<br />temp -> prior = 0; //set new nodes prior to empty<br />inputFile >> temp -> firstName; //get nodes data from file<br />inputFile >> temp -> lastName;<br />inputFile >> temp -> idNumber;<br />ch = inputFile.peek(); //check for end of file character<br />while ( isspace ( ch ) ) //skip white space<br />{<br />inputFile.get();<br />ch = inputFile.peek();<br />}<br />while ( ch != STOP_SYMBOL ) //while not at end of file<br />{<br />temp -> next = new person; //make new item in the list<br />if ( temp -> next == 0 ) //check allocation<br />{<br />cout << "allocation error! " << endl;<br />destroyList ( frontList );<br />return false;<br />}<br />temp -> next -> prior = temp; //set temp to point to new node<br />temp -> next -> next = 0; //set next to empty<br />temp = temp -> next;//move list pointer to next node<br />backList = temp; //make new node the back of list<br />inputFile >> temp -> firstName; //get data<br />inputFile >> temp -> lastName;<br />inputFile >> temp -> idNumber;<br />ch = inputFile.peek(); //get white space<br />while ( isspace ( ch ) )<br />{<br />inputFile.get();<br />ch = inputFile.peek();<br />}<br />itemsRead ++;//count items read<br />}<br /><br />return true;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : quickSort<br />Parameters :<br /><br />front a(n) person *& ( person *& ) ,<br />last a(n) person *& ( person *& ) ,<br />numberItems a(n) int ( int )<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void quickSort ( person ** low , person ** high , int numberItems )<br />{<br /><br /><br />person * savedLow = * low; //lowt<br />person * savedHigh = * high;//hight<br />person * tempLow , * tempHigh;<br />person * pivot; //pivot for sorting<br /><br />if ( numberItems <= 1 ) //nothing to sort<br />return;<br />if ( numberItems == 2 ) //if 2 items then swap if needed<br />{<br />tempLow = * low;<br />tempHigh = * high;<br />if ( strcmp ( key ( tempLow ) , key ( tempHigh ) ) > 0 ) //swap items if out of order<br />swap ( & tempLow , & tempHigh , & savedLow , & savedHigh );<br />* low = tempLow; //reset low and high pointers<br />* high = tempHigh;<br />return ; //end function<br />}<br /><br /><br /><br />pivot = savedHigh; //make the highest element in the array the pivot<br />tempLow = savedLow; //start sorting at the lowest item in the list<br />tempHigh = ( * savedHigh ).prior; //start sorting at the second to last item<br />int lowCount = 1; //start counting at the first item for the low pointer<br />int highCount = numberItems - 1; //start counting at the second to last item for high count<br /><br />do<br />{<br /><br />//while items are less than the pivot move right<br />while ( lowCount <= highCount && ( strcmp ( key ( tempLow ) , key ( pivot ) ) < 0 || strcmp ( key ( tempLow ) , key ( pivot ) ) == 0 ) )<br />{<br /><br /><br />tempLow = tempLow -> next;<br />lowCount ++; //increase count of items behind pointer<br />if ( ! tempLow )<br />break;<br />}<br />//while items are greater than the pivot move right<br />while ( lowCount <= highCount && ( strcmp ( key ( tempHigh ) , key ( pivot ) ) > 0 || strcmp ( key ( tempLow ) , key ( pivot ) ) == 0 ) )<br />{<br /><br /><br />tempHigh = tempHigh -> prior;<br />highCount --; //decrease count of items before pointer<br />if ( ! tempHigh )<br />break;<br />}<br /><br />if ( tempHigh ) //if top pointer has not crossed end of list , swap items<br />swap ( & tempLow , & tempHigh , & savedLow , & savedHigh );<br /><br />} while ( lowCount <= highCount && tempHigh -> prior && tempLow -> next );<br /><br />if ( tempHigh ) //if high pointer has not passed the end to the list , exchange high and low pointers<br />swap ( & tempLow , & tempHigh , & savedLow , & savedHigh );<br /><br />//put pivot in middle of large item and small item lists<br />tempHigh = savedHigh;<br />swap ( & tempLow , & tempHigh , & savedLow , & savedHigh );<br />//sort list of small items<br />quickSort ( & savedLow , & tempLow -> prior , lowCount - 1 );<br />//sort list of large items<br />quickSort ( & tempLow -> next , & savedHigh , numberItems - lowCount );<br />//reset low and high pointers<br />* low = savedLow;<br />* high = savedHigh;<br /><br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : swap<br />Parameters :<br /><br />first a(n) person *& ( person *& ) ,<br />last a(n) person *& ( person *& )<br /><br /><br />Returns: Void type<br />Comments: first<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void swap ( person ** left , person ** right , person ** front , person ** back )<br />{<br /><br /><br />if ( ! * left || !* right ) //if left or right pointer is null , stop<br />return;<br />if ( * left == * right )<br />return;<br />//reset the front and back pointers<br />if ( ( * front ) == ( * left ) ) //if left == front<br />{<br /><br />* front = * right; //right is new front after swap<br />}<br />else if ( ( * front ) == ( * right ) ) //if right == front<br />{<br /><br />* front = * left; //left is new front after swap<br />}<br />if ( ( * back ) == ( * left ) ) //if left == back<br />{<br /><br />* back = * right; //right is back after swap<br />}<br />else if ( ( * back ) == ( * right ) ) //if right is back<br />{<br /><br />* back = * left; //left is back after swap<br />}<br /><br /><br /><br />person * tempN , * tempP;<br /><br />tempN = ( * left ) -> next; //save left's next and prior node setting<br />tempP = ( * left ) -> prior;<br /><br />//case 1: right node follows left in list<br />if ( ( * right ) -> prior == * left )<br />{<br />( * left ) -> prior = ( * right ); //right is left's new prior node<br />( * left ) -> next = ( * right ) -> next; //left's next node is right's old prior node<br />if ( ( * left ) -> next ) //if left had a next node<br />( * left ) -> next -> prior = ( * right ); //set that nodes prior to right<br />( * right ) -> next = ( * left ); //right's next now is left<br />( * right ) -> prior = tempP; //right's new prior is left's old prior<br />if ( tempP ) //if left's old prior is not null<br />tempP -> next = ( * right ); //left's old prior's next is right<br />}<br />//case 2: left follows right in the list<br />else if ( ( * left) -> prior == ( * right ) )<br />{<br />( * left ) -> prior = ( * right ) -> prior; //left's prior is now right's prior<br />( * left ) -> next = ( * right ); //left's next is now right<br />if ( ( * right ) -> prior ) //if right's prior is not null<br />( * right ) -> prior -> next = ( * left ); //set its next to left<br />( * right ) -> prior = ( * left ); //right comes after left node now<br />( * right ) -> next = tempN; //right's next is left's old next<br />if ( tempN ) //if left's old next is non-null<br />tempN -> prior = ( * right ); //set its prior to right<br />}<br />//case 3: the two nodes are seperated by other nodes<br />else<br />{<br />( * left ) -> prior = ( * right ) -> prior; //left's prior equal's right's<br />( * left ) -> next = ( * right ) -> next; //left's next equal's right's<br /><br />if ( ( * right ) -> next ) //if right's next is non-null<br />( * right ) -> next -> prior = ( * left ); //set it's prior to left<br />if ( ( * right ) -> prior )//if right's prior is non-null<br />( * right ) -> prior -> next = ( * left ); //it's next now equals left<br />( * right ) -> prior = tempP; //right's prior now == left's<br />( * right ) -> next = tempN; //right's next now == left's<br />if ( tempP ) //if left's prior was non-null<br />tempP -> next = ( * right ); //set its next to right<br />if ( tempN ) //if left's next was non-null<br />tempN -> prior = ( * right );//set its prior to right<br />}<br /><br /><br />person * temp = ( * left ); //swap left and right pointers to save<br />person * temp2 = ( * right );//position of pointers in the list<br />( * left ) = temp2;<br />( * right ) = temp;<br /><br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : print<br />Parameters :<br /><br />list a(n) person * ( person * )<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void print ( person * list )<br />{<br /><br />cout << endl;<br /><br />cout << setiosflags ( ios::left );<br />cout << setw ( OUTPUT_SPACES ) << "first name"<br /><< setw ( OUTPUT_SPACES ) << "last name"<br /><< setw ( OUTPUT_SPACES ) << "id number" << endl << endl;<br />while ( list ) //while not at the end of the list<br />{<br />//output name ect....<br />cout << setw ( OUTPUT_SPACES ) << list -> firstName << " " << setw ( OUTPUT_SPACES ) << list -> lastName << " " << setw ( OUTPUT_SPACES ) << list -> idNumber << endl;<br />list = list -> next;<br /><br />}<br /><br />return;<br />}<br /><br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : destroyList<br />Parameters :<br /><br />list a(n) person * ( person * )<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void destroyList ( person * list )<br />{<br /><br />person * temp;<br /><br />while ( list ) //for each item in the list , destroy the list<br />{<br />temp = list;<br />list = list -> next;<br />delete temp;<br />}<br /><br /><br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : key<br />Parameters :<br /><br />first a(n) person * ( person * ) ,<br />second a(n) person * ( person * )<br /><br /><br />Returns: char type<br />Comments: use a macro for this......<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />char * key ( person * node )<br />{<br /><br />switch ( sortKey )<br />{<br />case firstName:<br />return node -> firstName;<br />case lastName:<br />return node -> lastName;<br />case idNumber:<br />return node -> idNumber;<br /><br />};<br /><br /><br />}<br />input:
123456789101112131415161718192021<br />bob johns 222<br />suzie barker 333<br />adam west 555<br />zack wilde 111<br />janet woods 444<br />qubert johns 666<br />barak obama 777<br />carly simon 888<br />curt cobain 999<br />tom gadja 1010<br />larry bear 1111<br />pilar nuffer 1212<br />mikey scalcee 1313<br />old puff 1414<br />charolette phifer 1515<br />don dokken 46464<br />adam ant 555<br />willodean mcqueen 222<br />*<br />output ( sorting with first name as key ):
1234567891011121314151617181920212223<br />first name last name id number<br /><br />adam ant 555<br />adam west 555<br />barak obama 777<br />bob johns 222<br />carly simon 888<br />charolette phifer 1515<br />curt cobain 999<br />don dokken 46464<br />janet woods 444<br />larry bear 1111<br />mikey scalcee 1313<br />old puff 1414<br />pilar nuffer 1212<br />qubert johns 666<br />suzie barker 333<br />tom gadja 1010<br />willodean mcqueen 222<br />zack wilde 111<br />Press any key to continue<br /> - AuthorPosts