排序数据日期明智...... [英] Sort Data Date wise...

查看:60
本文介绍了排序数据日期明智......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义了一个名为employee的类。我有empname和三个变量来存储日期(日,月和年)。我创建了雇员的e对象。问题是根据日期对员工进行排序并打印出来。 ..?

************************************* ****************

我已经发布了以下代码....我试图解决但我无法得到它..请帮助。 。$

**************************************** ***************



代码:

=======



I have defined a class named employee.I have empname and three variables to store date(day,month and year).I have created e objects of employee.the problem is to sort employee according to date and print it....?
*******************************************************
I have posted code below.... i tried to solve but i am not able to get it..please help..!
*******************************************************

code:
=======

#include<iostream.h>
#include<conio.h>
class employee
{
	char name[20];
	int day,month,year;
       public:
		employee()
		{
			cout<<"Enter Name:";
			cin>>name;
			cout<<"Enter Day:";
			cin>>day;
			cout<<"Enter Month:";
			cin>>month;
			cout<<"Enter Year:";
			cin>>year;
		}
		void display()
		{
			cout<<"Employee Name:"<<name<<endl;
			cout<<"Date:"<<day<<"-"<<month<<"-"<<year<<endl;
		}
};
void main()
{
	int i,j,k;
	clrscr();
	employee tmp;
	employee *e=new employee[5];
	for(i=0;i<5;i++)
	{
		for(j=0;j<=i-1;j++)
		{
			if(e[j]>e[j+1])
			{
				tmp=e[j];
				e[j]=e[j+1];
				e[j+1]=tmp;
			}
		}
	}
	for(k=0;k<5;k++)
	{
		e->display();
	}
    //	e->display();
	getch();
}





代码块添加 - OriginalGriff [/ Edit]



code block added - OriginalGriff [/Edit]

推荐答案

我不太清楚从哪里开始...



它没有按日期排序,因为你没有问它to。

您的排序算法不是比较日期,而是比较员工:哪些不是真正的比较。



你需要改变您目前使用的简单比较来实际比较日期!这意味着要么将它们存储为日期项目 - 这是一个非常好的主意 - 或者比较年份,然后是月份,然后是找出实际排序顺序的那一天。



我要做的是将您的排序代码提取到一个单独的函数中:将其交给一个Employee对象数组,然后返回一个已排序的数组。在这个函数中,我会在Employee类本身中调用一个函数来将它的日期与另一个雇员进行比较。它可以返回-1表示之前,0表示相等,1表示晚于。
I'm not quite sure where to start here...

It doesn't sort by date because you aren't asking it to.
Your sort algorithm isn't comparing dates, it's comparing employees: which doesn't really have a comparison.

You need to change the simple comparison you are using at the moment to actually compare the dates! Which means either storing them as date items - a very good idea - or comparing the year, then the month, then the day to find out the actual sort order.

What I would do is extract your sort code into a separate function: hand it an array of Employee objects, and it returns a sorted array. Within this function, I'd call a function within the Employee class itself to compare it's date with another employee. It could return -1 for before, zero for equal, and 1 for later than.


由于这是您的作业,我不会提供任何代码,但可能会提供一些提示你正确的方向。



主要是你试图做一个所谓的暴力排序,将每个元素与其他元素进行比较。这不是一个好的分拣策略,但它是一个起点。



你的代码出了什么问题,你构造了两个带索引i和j的嵌套循环,但你比较了元素e [j]和e [j + 1]相反,如果e [i]和e [j]。所以这是行不通的。



第二个出错的是j循环覆盖了错误的元素。它应该从i + 1运行到数组的末尾。想法应该是找到索引i之上的所有剩余元素中的最小元素并将其分配给单元格i。这保证了从数组开始到索引i-1的所有元素都将按正确的顺序排列。



一旦你完成所有运行,你应该google for a更好的排序算法(其中有很多,因为排序是一个完整的研究领域)。所以选择一个更好的并实现它应该不难。



除此之外,你的代码中有许多小东西需要修复,太多了列出快速回答。但我相信你会在调试器中多次运行程序时检测到所有这些。
As this is your homework, I will not provide any code, but a couple of hints that might lead you into right direction.

In main you are trying to do what is called a "brute force" sort, comparing each element with all others. That is no good sorting strategy, but it is a starting point.

What goes wrong in your code is that you construct two nested loops with indices i and j, but you compare elements e[j] and e[j+1] instead if e[i] and e[j]. So that won't work.

The second thing that goes wrong is that you j-loop covers the wrong elements. It should run from i+1 to the end of your array. The idea should be to find the smallest of all remaining elements above index i and assign it to cell i. That guarantees that all elements from the start of the array to index i-1 will be in the correct order.

Once you have that all running you should google for a better sorting algorithm (there are many of them, as sorting is an entire research field in its own). So it should not be hard to pick a better one and implement it.

Besides that there are many small things in your code that need to be fixed, too many to be listed in a quick answer. But I am sure you will detect all those when you run your program several times in a debugger.


这篇关于排序数据日期明智......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆