dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19

Thread: help needed with the c++ program

  1. #16
    Join Date
    May 2015
    Posts
    134

    Re: help needed with the c++ program

    @kaud: Thanks a lot.

    Regarding the data structure, i wanted to use map , as it is sorted. I know it is bit complicated data structure. But it worked for just displaying the input in sorted order. But it wont work for rearranging the timeperiod.

    I want to improve this data structure, so any inputs regarding this will be helpful.

    Next step for me to rearrange the time periond and sort internally
    Last edited by pdk5; March 17th, 2019 at 11:17 AM.

  2. #17
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,608

    Re: help needed with the c++ program

    For the two outputs, I used two sets with a specified comparator predicate which defines the required sort order for the set. That way you get the two outputs in the required order - by iterating the sets - without ever having to do any actual sorting!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.9)

  3. #18
    Join Date
    May 2015
    Posts
    134

    Re: help needed with the c++ program

    Quote Originally Posted by 2kaud View Post
    For the two outputs, I used two sets with a specified comparator predicate which defines the required sort order for the set. That way you get the two outputs in the required order - by iterating the sets - without ever having to do any actual sorting!
    I changed somethings, to add the predicate sorting, but the list now is not getting sorted. I will check , but today will be very busy at work due to some release. i guess, i can check this only tomorrow afternoon now.

    @ kaud, thankyou very much for your patience and willingness to help. I am sorry, I have been asking lot of questions, which i could have found the answers myself. Actually my previous company has been using c++98, so i still need to come up speed with the latest.

    Code:
    //============================================================================
    // Name        : test.cpp
    // Author      : Pdk
    // Version     :
    // Copyright   : Your copyright notice
    // Description : Hello World in C++, Ansi-style
    //============================================================================
    
    #include <iostream>
    
    #include <string>
    
    #include <vector>
    #include <map>
    #include <list>
    #include <fstream>
    #include <sstream>
    #include <iomanip>
    #include <conio.h>
    
    using namespace std;
    
    const int year = 365;
    const int month= 30;
    const int week= 7;
    const int day =  1;
    
    std::map<string,int> myMap = {
    	    {"y", year},
    	    {"m", month},
    	    {"w", week},
    	    {"d", day}
    };
    
    class Line;
    
    std::list<Line> theList;
    
    class Line
    {
    public:
        //1.1: Private Members
        string m_LineId;
        int m_LineValue;
        int m_Value1;
        int m_Value2;
    
    public:
        //1.2: Department Class Constructor
        Line(const string & input)
        {
        	m_LineId = input;
    
        	std::vector<std::string> inputTokens;
    
        	cout << "\n Input is " << input;
        	stringstream is(input);
            std::string r;
            while(std::getline(is,r,','))
            {
            	inputTokens.push_back(r);
            	cout <<"\n"<< r;
            }
    
        	std::string timespan = inputTokens[0];
    
        	std::string temp;
        	int num=0;
        	int total =0;
        	string tempstring = timespan;
    
        	while(!tempstring.empty())
        	{
        		std::string::size_type sz;   // alias of size_t
        		num = std::stoi(tempstring, &sz);
        		string type = tempstring.substr(sz,1);
        		int num_days = myMap.at(type);
        		total = total+num*num_days;
        		tempstring = tempstring.substr(sz+1);
        	}
    
        	m_LineValue = total;
    
        	m_Value1 = std::stoi(inputTokens[1]);
        	m_Value2 = std::stoi(inputTokens[2]);
    
        	cout << "\n Value1 " << m_Value1;
        	cout << "\n Value2 " << m_Value2;
        }
    
        //1.3: Getter Functions
        string GetLineId()
        {
            return m_LineId;
        }
    
        int GetLineValue()
        {
            return m_LineValue;
        }
    
        int GetLineValue1()
        {
            return m_Value1;
        }
    
        int GetLineValue2()
        {
            return m_Value2;
        }
    
        // Print Content of the Line Class
        void Print_Line()
        {
            cout << "Line - ID" << m_LineId << "  Value:"
                << m_LineValue << ", Value1 and Value2 - "
                <<  m_Value1 << "  " << m_Value2 << endl;
        }
    };
    
    
    bool Compare_time_Predicate_Asc(
        Line First, Line Next)
    {
        //First Argument Stays First (Return true)
        if (First.GetLineValue() < Next.GetLineValue())
            return true;
    
        //First Argument goes Next (Swap) (Return false)
        if (First.GetLineValue() > Next.GetLineValue())
            return true;
    
        //a==b. First Argument Stays first
        //(No need to Swap)
        return true;
    }
    
    void processline(const std::string& input)
    {
       Line theline(input);
    
       theList.push_back(theline);
    
    }
    
    int main()
    {
    	ifstream fin;
    
    	fin.open("C:\\Users\\Pdk\\eclipse-workspace\\test\\src\\data.txt");
    
    	if(!fin.fail())
    	{
    		std::cout << "fin success";
    		std::string line;
    
    		// skip the header
    		std::getline(fin, line);
    
    		// store the contents of each line
    		while (std::getline(fin, line))
    		{
    		    std::cout << line << std::endl;
    
    	        // call the function to store each line
                if(!line.empty())
    		    processline(line);
    		}
    
    
            theList.sort(Compare_time_Predicate_Asc);
    
    		std::cout << " \n Sorted Input is : \n";
    		for (std::list<Line>::iterator it=theList.begin(); it != theList.end(); ++it)
    		{
    			   cout << "\n Line Id is  " << it->m_LineId;
    			   cout << "    Line value is" << it->m_LineValue;
    		}
    	}
    	else
    	{
    		std::cout << "fin failed";
    	}
    }

  4. #19
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,608

    Re: help needed with the c++ program

    If you're just used to C++98 then you have a big learning curve ahead of you to get up to speed with c++17. Going from C++98 to C++11 was massive and then C++11 to C++17 was big (C++14 was really just a tidy up of C++11). C++20 is coming out next year and this again is another massive change from C++17!

    Re the comp predicate. The idea is to use a set/map (I used a set) with a specified predicate for the comparison. This means that the elements are inserted in required sort order and no further sorting is required!

    For example:

    Code:
    	multiset<Rec, PerCmp1> s1;
    Where PerComp1 is the comparator functor.

    re Input. You know the format of the input (which is fixed), so from a string stream you can do this:

    Code:
    istringstream is(input);
    string timespan;
    string coma;
    unsigned value1;
    unsigned value2;
    
    if (getline(is, timespan, ',') && (is >> value1 >> coma >> value2) {
        // Process a good line
    } else {
        // Got a bad line
    }
    You don't then need to explicitly convert a string to a number as the extraction does this. Nor do you need a vector to hold the tokens etc. timespan, value1 and value2 hold the required values if the input line is valid. All that's now needed is to convert timespan to a number.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.9)

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)