Setting String equal to char array?

This is a discussion on Setting String equal to char array? within the C++ Programming forums, part of the General Programming Boards category; May I recommend structuring the linked list code into functions. Anyway, there's no reason to make the list doubly linked. ...

  1. #16
    The larch
    Join Date
    May 2006
    Posts
    3,573
    May I recommend structuring the linked list code into functions.

    Anyway, there's no reason to make the list doubly linked. Your assignment says that you need a stack and a queue implemented as linked lists. All you need is a linked list where you can push a new value onto either end, and the ability to read and pop one end.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #17
    Registered User
    Join Date
    Jul 2007
    Posts
    29
    I got the equations to get inserted into the linked list. Now I will work on making the code easier to read. I was initially thinking of making the doubly linked list because input members in the stack would be inserted in reverse order. But I ended up making another stack(making stacks were a little more simple than making a queue although a bit more inefficient) which copied in the data from the first stack. Now when I print what I enter, it is in the correct order. Here is my current working code, I will modify it in due time.

    Here is the code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct node
    {
    float num;
    char opr;
    char type;
    node* next;
    node* prev;
    };
    
    void printList(const node*);
    //entry* insert(int n, node* h);
    //entry* deleteNode(int n, node* h);
    //entry* PQInit(entry* h, entry* PQh);
    float arrayToFloat(char part[]);
    
    int main()
    {
    node *temp = 0, *head = 0;//starts out empty
    node *PQt = 0, *PQh = 0, *PQp = 0;//Polish queue
    float fltval;
    int j, n = 0, flag, count = 0;
    char string[80];
    
    	cout << "Enter an algebraic equation in reverse polish notation: \n";
    	cin.getline(string, 80);
    
    	cout << "Output from func arrayToInt:\n";
    	while(string[n] != '\0')
    	{
    	char part[15] = {'O'};
    
    		for(j = 0, flag = 0; (string[n] != ' ') && (string[n] != '\0'); j++, n++) 
    		{
    			part[j] = string[n];
    			if(!isdigit(part[j]))//if character
    			{
    				flag = 1;
    			}//if
    		}//for
    		n++;
    
    		if(flag == 0)//if part consists of int
    		{
    			if(string[n] != '\0')
    			{
    				fltval = arrayToFloat(part);//converting array based number to float
    				cout << fltval << endl;
    
    				head = new node;
    				head->num = fltval;
    				head->type = 'N';
    				head->next = temp;
    				temp = head;
    			}//if
    		}//if
    		else
    		{
    			if(string[n] != '\0')
    			{
    				head = new node;
    				head->opr = part[0];
    				head->type = 'O';
    				head->next = temp;
    				temp = head;
    			}//if
    		}//else
    
    	}//while
    
    	//PQh = PQInit(head, PQh);
    	cout << "Output from linked list:\n";
    	printList(head);
    
    	//creating PQ stack from initial array
    	while(head != 0)
    	{
    		PQh = new node;
    		if(head->type == 'N')
    		{
    			PQh->num = head->num;
    			PQh->type = 'N';
    			PQh->next = PQt;
    			PQt = PQh;
    		}//if
    		else
    		{
    			PQh->opr = head->opr;
    			PQh->type = 'O';
    			PQh->next = PQt;
    			PQt = PQh;
    		}//else
    
    		head = head->next;
    	}//while
    
    	cout << "Output from PQ: \n";
    
    	printList(PQh);
    
    	return 0;
    }//main
    
    /***************************************/
    //Prints list of Nodes
    
    void printList(const node* pointer)
    {
    	while(pointer != 0)
    	{
    		if(pointer->type == 'N')
    			cout << pointer->num << " ";
    		else
    			cout << pointer->opr << " ";
    
    		pointer = pointer->next;
    	}//while
    	cout << endl;
    
    }//printList
    
    /****************************************/
    //Converts an array of ints to a float value
    
    float arrayToFloat(char part[])
    {
    int L = 0, i, j;
    float val = 0, value[15] = {0};
    float valtab[5] = {1, 10, 100, 1000, 10000};
    //                 1   2   3     4     5
    
    	for(i = 0; part[i]; i++)
    	{
    		value[i] = part[i] - '0';//converting to int from char	
    	}//for
    	
    	L = strlen(part);
    	for(i = 0, j = L - 1; i < L; i++, j--)
    	{
    		val = val + (value[i] * valtab[j]);
    	}//for
    
    	return val;
    }//arrayToFloat
    When I originally started working on making the second stack, I was working on it in a separate function, but I got a ton of errors and just decided to get it working first in main. Just be ware this is very dated function, I am better off copying what I have in main into the function rather than this. I am posting this so you guys can see if I did anything wrong and so you can correct me before I update this function. Any tips to help me with this will be appreciated. Here is the (commented out) function code:

    Code:
    /******************************************/
    //Initializes PQ
    /*
    entry* PQInit(entry* h, entry* PQh)
    {
    node* head = h;
    
    	while(h->next != 0)
    	{
    		if(h->type == 'N')
    			PQh->num = head->num;
    		else
    			PQh->opr = head->opr;
    
    		head = head->next;
    		PQh = PQh->next;
    		
    	}//while
    	return h;
    	
    }//PQInit
    */
    Thanks for all your help!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 12:57 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21