积极答复者
随机函数的问题

问题
-
#include<iostream>
#include<ctime>
#include<cstdlib>
//#include"random.h"
using namespace std;
enum AppendNewline{noNewline,addNewline}addnl;
template<class T>
class Node
{
private:
//next为指向下一结点的指针
Node<T> *next;
public:
//data 为公有成员
T data;Node(const T &item,Node<T> *prtnext =NULL);//构造函数
void InsertFront(Node<T> *&head,T item);//往表头插入结点
void PrintList(Node<T> *head,AppendNewline addnl);//输入链表
Node<T> *NextNode(void) const;//NextNode方法可以访问指针域next,该方法返回的是next的值,因而可以用来遍历表
Node<T> *GetNode(const T& item,Node<T>*nextPtr=NULL);//创建一个结点,数据值为item,指针为nextPtr
};template<class T>
Node<T>::Node(const T &item, Node<T> *prtnext):data(item),next(prtnext)
{}
template<class T>
Node<T> *Node<T>::NextNode() const
{
return next;
}template<class T>
Node<T> *Node<T>::GetNode(const T &item,Node<T> *nextPtr=NULL)
{
Node<T> *newNode;
newNode = new Node<T>(item,nextPtr);
if(newNode == NULL)
{
cerr<<"Memory allocation failure!"<<endl;
exit(1);
}
return newNode;
}template<class T>
void Node<T>::InsertFront(Node<T> *&head, T item)
{
head = GetNode(item,head);
}
template<class T>
void Node<T>::PrintList(Node<T> *head,AppendNewline addnl)
{
Node<T> *currPtr = head;
while(currPtr != NULL)
{
if(addnl == addNewline)
cout<<currPtr->data<<endl;
else
cout<<currPtr->data<<" ";
currPtr = currPtr->NextNode();
}
}
int main(void)
{int n = 10;
Node<int>st(n);
Node<int> *head =NULL,*currPtr;
int i,key,count = 0;
//RandomNumber rnd;
for(i = 0;i<n;i++)
{
//st.InsertFront(head,int(rnd.random))
srand(time(0));
st.InsertFront(head,int(rand()%n +1));
}
cout<<"List: ";
st.PrintList(head,noNewline);
cout<<endl;
cout<<"Enter a key:";
cin>>key;currPtr =head;
while(currPtr != NULL)
{
if(currPtr->data == key)
count++;
currPtr = currPtr->NextNode();
}
cout<<"The data value"<<key <<"occurs "<<count<<"times in the list"<<endl;
}
我要生成10个不同的随机数 然后我输入一个数 看看在这个数在10个随机数中出现了几次,并记录下出现的次数。 上面的程序结果是产生了10个相同的随机数,请问我该怎么修改下 --! 我随机数不是很懂 谢谢