200分求程序

lianglianqing 2003-11-26 11:16:06
在下临近考试实在无力完成一个老师的实验题目,所以敕重金购一程序,望各位不吝帮忙:

做一个学生链表,要求有学号,姓名,科目,任课老师,分数等信息。可以实现新建,插入,删除,排序,查找等功能。

另100分必付。

...全文
43 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
北极猩猩 2003-11-26
  • 打赏
  • 举报
回复
这种东西有一晚上足够写完的了,我就不信考试考得一晚上时间都没有!
如果真是这样,你平时都干什么去了!
mousemice 2003-11-26
  • 打赏
  • 举报
回复

int main()
{
char filename[10];
FILE *fp;
char cmd, tag2;
struct stud *head;
int tag;
/* Command Receiver */
do{
// clrscr();

printf( "\n 1----Creat a new record and save" );
printf( "\n 2----Add new records to a existed file." );
printf( "\n 3----Search someone's record" );
printf( "\n 4----Sort students' marks according to the record file." );
printf( "\n 5----Print all students' record." );
printf( "\n 6----Quit." );
printf( "\nWaiting order:" );
do{
scanf( "%c", &cmd );
}while( cmd == '\n' );
/* Command Transmitter */
switch ( cmd )
{
case '1' : puts( "\nEnter the records" );
head = creat();
save( head );
puts( "\nNow you have a record file." );
list_traverse( head );
clear( head );
getch();
break;

case '2' : printf( "\nEnter the name of record file you want add:" );
scanf( "%s", filename );
fp = fopen( filename,"r" );
if( !fp ) { printf( "Error!!!" ); break; }

printf( "\nThe current records are:" );
print_file( fp );
printf( "\nNow you can add new record: ");
add( filename );
printf( "The new records are:" );
rewind( fp );
print_file( fp ); getch();
break;

case '3' : printf( "\nWhich key value do you want to accord? ");
printf( "\n 1---name\t0---number:" );
scanf ( "%d", &tag );
if ( tag ) search_name();
else search_num();
getch();
break;

case '4' : printf( "\nSort according to what ?" );
printf( "1---name\t2---number\t3---score" );
do {
scanf ( "%c", &tag2 );
}while ( tag2 == '\n' );
switch ( tag2 )
{
case '1' : head = creat_order2( compare_name );
break;
case '2' : head = creat_order2( compare_num );
break;
case '3' : head = creat_order2( compare_score );
break;
}
list_traverse( head );
clear ( head );
getch();
break;

case '5' : printf( "\nEnter the name of record file you want see:" );
scanf( "%s", filename );
fp = fopen( filename, "r" );
if( !fp ) { printf( "Error!!" ); break; }

printf( "\nThe records in this file are:" );
print_file( fp );getch();
break;
}
}while( cmd != '6' );
retrurn true;
}
mousemice 2003-11-26
  • 打赏
  • 举报
回复

10(附加题)编写一个简单的学籍管理程序,程序中有下列功能:
1)建立学生数据结构,学生数据结构有学生姓名,学号,20门成绩
2)输入数据功能模块,完成数据输入,添加。数据用文件保存。
3)查询功能模块,可以按姓名,学号查询学生纪录
4)排序输出模块,可以按姓名,学号,平均成绩输出学生纪录

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <malloc.h>

struct stud{
char name[10];
int num;
float score[5];
struct stud *next;
};


struct stud *creat() /*---Creat a record list---*/
{
struct stud *p, *q, *head;
int n=0,i;
float t,sum=0.0;
char cmd='y';

head = p = ( struct stud * )malloc( sizeof( struct stud ) );

while( cmd!='N' && cmd!='n' )
{
q=( struct stud * )malloc( sizeof( struct stud ) );
q->next = NULL;
++n; sum = 0.0; /*---Average Marks---*/

printf( "\nPlease enter No. %d student's name: ",n);
scanf ( "%s", &q->name );
printf( " \nNow enter his or her number:" );
scanf ( "%d",&q->num );
printf( "\nNow enter the student's marks of the 4 subjects(Enlish,Maths,Programming and Data Structrue:");
for( i = 0; i < 4; ++i )
{
scanf( "%f", &t );
q->score[i] = t;
sum += t;
}
q->score[4] = sum/4; /* score[4] is used to record the average */
p->next = q;
p = q;

printf( "\nContine(Y/N)?" );
do{
cmd = getchar();
}while( cmd=='\n' );
}
return head;
}

void search_name (void) /* Search according to name */
{
FILE *fp;
struct stud p;
int i;
char search_name[10],filename[10];

printf( "\nEnter the record file's name:" );
scanf ( "%s", filename );
fp = fopen( filename, "r" );
if( !fp ) { printf( "Error!!!" ); return; }
printf( "\nEnter the name of the student you want to search:" );
scanf ( "%s", search_name );

while( !feof( fp ) )
{
fread( &p, sizeof( struct stud ), 1, fp );
/* If found , stop! */
if( strcmp( p.name, search_name ) == 0 ) break;
}

if( !feof( fp ) ) /* found */
{
printf( "\n%s", p.name );
printf( "\t%d\n", p.num );
for(i=0;i<5;++i)
printf( "\t%5.2f", p.score[i] );
}
else printf( "NO FOUND!" );
fclose( fp );
}

void search_num (void) /* Search according to number */
{
FILE *fp;
struct stud p;
int i,search_num;
char filename[10];

printf( "\nEnter the record file's name:" );
scanf( "%s", filename );
fp = fopen( filename, "r" );
if( !fp ) { printf( "Error!!!" ); return; }
printf( "\nEnter the number of the student you want to search:" );
scanf ( "%d", &search_num );

while( !feof( fp ) )
{
fread( &p, sizeof( struct stud ), 1, fp );
if( p.num == search_num ) break;
}

if(!feof(fp)) /* found */
{printf( "\n%s", p.name );
printf( "\t%d\n", p.num );
for( i = 0; i < 5; ++i )
printf( "\t%5.2f", p.score[i] );
}
else printf( "NO FOUND!" );
fclose( fp );
}


void list_traverse ( struct stud *head ) /* Print according to LinkList */
{
struct stud *p;
int i;

p = head->next;
if( p )
{
printf( "\nThe Records are:" );
while( p )
{
printf( "\n%s\t%d\n", p->name, p->num );
for( i = 0; i < 5; ++i ) printf( "\t%5.2f", p->score[i] );
p = p->next;
}
}
else printf("There is no records at all!");
}

void print_file ( FILE *fp ) /* Print according to File */
{
struct stud p;
int i,t;
printf( "\nThe Records are:" );
while ( !feof( fp ) ){
fread( &p, sizeof( struct stud ), 1, fp );
if ( t == p.num ) continue;
printf( "\n%s\t%d\n" , p.name ,p.num );
for( i = 0; i < 5; ++i ) printf( "\t%5.2f", p.score[i] );
t = p.num;
}
}


int compare_name ( struct stud *left, struct stud *right )
{
if ( strcmp ( left->next->name, right->name ) > 0 ) return 1;
else return 0;
}

int compare_num ( struct stud *left, struct stud *right )
{
return left->next->num > right->num;
}

int compare_score ( struct stud *left, struct stud *right )
{
return left->next->score[4] > right->score[4];
}

struct stud *creat_order2( int ( *compare )( struct stud*, struct stud* ) )
{
struct stud *p, *q, *head, temp;
int i, j;
FILE *fp;
char filename[10];

printf( "\nEnter the file's name:" );
scanf ( "%s", filename );
fp = fopen( filename, "r" );
if( !fp ) { printf("Error"); return NULL; }

head = ( struct stud * )malloc( sizeof( struct stud ) );
head->next = NULL;
while( !feof( fp ) )
{
q = ( struct stud * )malloc( sizeof( struct stud ) );
q->next = NULL;

fread( &temp, sizeof( struct stud ), 1, fp );
if ( temp.num == j) break;
strcpy( q->name, temp.name );
j = q->num = temp.num;
for( i = 0; i < 5; ++i ) q->score[i] = temp.score[i];

p = head;
while( p->next && ( ( *compare )( p, q ) ) ) p = p->next;
q->next = p->next;
p->next = q;
}
return head;
}

void add ( char *name ) /* "name" means filename */
{
FILE *fp;
struct stud t;
int i;
char cmd='y';
float temp, sum = 0.0;

fp = fopen( name,"a" );
if ( !fp ) { printf( "Error!!!" ); return; }

while( cmd != 'N' && cmd != 'n' ){
printf( "\nPlease enter the student's name :" );
scanf ( "%s", &t.name );
printf( "\nNow enter his or her number:" );
scanf ( "%d", &t.num );
printf( "\nNow enter the student's marks of the 4 subjects(Enlish,Maths,Programming and Data Structrue:" );
for( i = 0; i < 4; ++i )
{
scanf( "%f", &temp );
t.score[i] = temp;
sum += temp;
}
t.score[4] = sum/4;

fwrite( &t, sizeof( struct stud ), 1, fp );

printf( "\nContine(Y/N)?" );
do{
cmd = getchar();
}while( cmd == '\n' );
}
fclose( fp );
}

void save (struct stud *head)
{
FILE *fp;
struct stud *p;
char filename[10];

printf( "\nPlease enter the record file's name :" );
scanf ( "%s" ,filename );
if( ( fp=fopen( filename,"w" ) )==NULL ) { printf("\nError!!!"); return; }

p = head->next;
while( p )
{
fwrite( p, sizeof( struct stud ), 1, fp );
p = p->next;
}
printf( "\nFile Saved." );
fclose( fp );
}
struct stud *clear ( struct stud *head ) {
struct stud *p, *q;

p = head->next;
while ( p ){
q = p->next;
free ( p );
p = q;
}
free ( head );
return NULL;
}

struct stud *creat(); /* Functions Declaration */
void save ( struct stud *head );
void search_name ( void );
void search_num ( void );
void list_traverse ( struct stud *head );
void print_file ( FILE *fp );
void add ( char *name );
struct stud *clear ( struct stud *head );
bohut 2003-11-26
  • 打赏
  • 举报
回复
/***********以下是实现的步骤***********/

1.定义表的结构
typedef struct
{
int nID; //学号
char szName[10]; //姓名
char szSubject[40]; //科目
char szTeacher[10]; //任课老师
int nScore; //分数
}MYSTR;

DEFLIST(int,MYSTR);

2.声明变量用于操作;(相当于新建功能)
MYSTR m_strList;
MYSTRLIST m_objList;

3.插入
m_strList.nID = 1; //学号
strcpy(m_strList.szName,"张三");//姓名
strcpy(m_strList.szSubject,"数学");//科目
strcpy(m_strList.szTeacher,"bohut");//任课老师
m_strList.nScore = 100 ;//分数
m_objList.AddToTail(m_strList.nSyh,m_strList);
4.删除(删除学号为2的记录)
m_objList.Remove(2);
5.查找(查找学号为1的记录)
m_objList.Find(1);

写了这么多,可以给分了吧?呵呵,祝你成功.
bohut 2003-11-26
  • 打赏
  • 举报
回复
/********链表程序(接上)*********/

template <class KeyType,class ValType>
BOOL ListT<KeyType,ValType>::InsertDescend(KeyType NewKey,
ValType NewObject)
{
//Allocate memory for the new node
ListNodeT<KeyType,ValType>* pNewNode =
new ListNodeT<KeyType,ValType>(NewKey,NewObject);
if (!pNewNode)
return FALSE;
//Insert the node into the list
ListNodeT<KeyType,ValType>* pCursor1 = head;
ListNodeT<KeyType,ValType>* pCursor2 = head;
//search the key grand then the insert
if (this->GetListSize() == 0) //the list have no member, must set the head
head = pNewNode;
else if (this->GetListSize() == 1)
{
if (pCursor1->GetKey() < NewKey)
{
head = pNewNode;
pNewNode->SetNext(pCursor1);
}
else
pCursor1->SetNext(pNewNode);
}
else //have some member (haven sorted ascend)
{
if (pCursor1->GetKey() < NewKey) //must inster in head
{
head = pNewNode;
pNewNode->SetNext(pCursor1);
}
else
{
//search the position of the newobject
while (pCursor1->GetNext()) {
pCursor2 = pCursor1;
pCursor1 = pCursor1->GetNext();
if (pCursor2->GetKey() > NewKey &&
pCursor1->GetKey() < NewKey)
{
pCursor2->SetNext(pNewNode);
pNewNode->SetNext(pCursor1);
NumItems++;
return TRUE;
}
}
//tail
pCursor1->SetNext(pNewNode);
}
}
NumItems++;
return TRUE;
}

template <class KeyType, class ValType>
BOOL ListT<KeyType,ValType>::UpdateMember(KeyType SearchKey, ValType NewObject)
{
ValType* pVal;
KeyType* pKey = NULL;
ListNodeT<KeyType,ValType>* pCursor = head;
while (pCursor) {
if (pCursor->GetKey() == SearchKey)
{ //do update
pVal = pCursor->GetContents();
memcpy(pVal,&NewObject,sizeof(ValType));
pKey = pCursor->GetKeyRef();
*pKey = SearchKey;
return TRUE;
}
else
pCursor = pCursor->GetNext();
}
return FALSE;
}//end of UpdateMember

template <class KeyType, class ValType>
BOOL ListT<KeyType,ValType>::Remove(KeyType SearchKey)
{
ListNodeT<KeyType, ValType>* pCursor = head;
//Is there a list?
//if not,can not find SearchKey in this, so False
if (!pCursor)
return FALSE;
//Check the head first
if (pCursor->GetKey() == SearchKey) {
head = pCursor->GetNext();
delete pCursor;
NumItems--;
return TRUE;
}

//Scan the list
while (pCursor->GetNext()) {
if (pCursor->GetNext()->GetKey() == SearchKey) {
ListNodeT<KeyType,ValType>* pTemp =
pCursor->GetNext();
pCursor->SetNext(pTemp->GetNext());
delete pTemp;
NumItems--;
return TRUE;
}
pCursor = pCursor->GetNext();
}
return FALSE;
}

template <class KeyType, class ValType>
BOOL ListT<KeyType,ValType>::RemoveAll()
{
ListNodeT<KeyType, ValType>* pCursor = head;
// KeyType SearchKey;
//Is there a list?
if (!pCursor)
return TRUE;
//Check the head first
while (pCursor) {
head = pCursor->GetNext();
delete pCursor;
pCursor = head;
NumItems--;
}
return TRUE;

}

template <class KeyType,class ValType>
ValType* ListT<KeyType,ValType>::Find(KeyType SearchKey) const
{
ListNodeT<KeyType,ValType>* pCursor = head;
while (pCursor) {
if (pCursor->GetKey() == SearchKey)
return pCursor->GetContents();
else
pCursor = pCursor->GetNext();
}
return NULL;
}


//start from zero
template <class KeyType,class ValType>
int ListT<KeyType,ValType>::SearchPosition(KeyType SearchKey) const
{
int nIndex = 0;
ListNodeT<KeyType,ValType>* pCursor = head;
while (pCursor) {
if (pCursor->GetKey() == SearchKey)
return nIndex;
else
{
pCursor = pCursor->GetNext();
nIndex++;
}
}
return -1; //not found

}


template <class KeyType,class ValType>
int ListT<KeyType,ValType>::GetListSize() const
{
return NumItems;
}

//Operators
template <class KeyType, class ValType>
ValType* ListT<KeyType,ValType>::operator[](int Position)
{
ListNodeT<KeyType,ValType>* pCursor = head;
int nCount = 0;
while (pCursor) {
if (nCount == Position)
return pCursor->GetContents();
nCount++;
pCursor = pCursor->GetNext();
}
return NULL;
}

#define DEFLIST(X,Y) typedef ListT<X,Y> Y##LIST
#endif
bohut 2003-11-26
  • 打赏
  • 举报
回复
/*******链表程序*******/

#if !defined __LISTT__
#define __LISTT__

#define FALSE 0
#define TRUE 1
//typedef unsigned char BOOL;

//ListT NODE
template <class KeyType,class ValType>
class ListNodeT {

public:
ListNodeT(KeyType NewKey, ValType NewObject);
~ListNodeT();
void SetNext(ListNodeT* pNewNext);
ValType* GetContents() const;
KeyType GetKey() const;
KeyType* GetKeyRef();
ListNodeT* GetNext() const;

private:
ListNodeT* next;
ValType* value;
KeyType key;
};

//ListT
template <class KeyType, class ValType>
class ListT {

public:
ListT();
~ListT();
BOOL AddToHead(KeyType NewKey, ValType NewObject);
BOOL AddToTail(KeyType NewKey, ValType NewObject);
BOOL Remove(KeyType SearchKey);
BOOL RemoveAll();
BOOL InsertAscend(KeyType NewKey, ValType NewObject);
BOOL InsertDescend(KeyType NewKey, ValType NewObject);
BOOL UpdateMember(KeyType SearchKey, ValType NewObject);
ValType* Find(KeyType SearchKey) const;
int SearchPosition(KeyType SearchKey) const;
ValType* operator[](int Position);
int GetListSize() const;
private:
ListNodeT<KeyType,ValType>* head;
unsigned int NumItems;
};



//ListNodeT
//Constructor
template <class KeyType, class ValType>
ListNodeT<KeyType,ValType>::ListNodeT(KeyType NewKey,
ValType NewObject)
{

//Initialize member data
next = NULL;
value = new ValType;
memcpy (value,&NewObject,sizeof(ValType));
key = NewKey;

} //end of constructor

template <class KeyType, class ValType>
ListNodeT<KeyType,ValType>::~ListNodeT()
{
if (value)
delete value;
}

//Public member functions
template<class KeyType, class ValType>
void ListNodeT<KeyType,ValType>::SetNext(ListNodeT* pNewNext)
{
next = pNewNext;
}

template<class KeyType, class ValType>
ValType* ListNodeT<KeyType,ValType>::GetContents() const
{
return value;
}

template<class KeyType, class ValType>
KeyType ListNodeT<KeyType,ValType>::GetKey() const
{
return key;
}

template<class KeyType, class ValType>
KeyType* ListNodeT<KeyType,ValType>::GetKeyRef()
{
return &key;
}

template<class KeyType, class ValType>
ListNodeT<KeyType,ValType>* ListNodeT<KeyType,ValType>::GetNext() const
{
return next;
}


//Now ListT
//constructor
template <class KeyType, class ValType>
ListT<KeyType,ValType>::ListT():

//Initialize member data
head(NULL),
NumItems(0)
{}

//Destructor
template <class KeyType, class ValType>
ListT<KeyType,ValType>::~ListT()
{
//Delete all the nodes in the list
while (head) {
ListNodeT<KeyType,ValType>* pTemp = head->GetNext();
delete head;
head = pTemp;
}
}

//Public member function
template <class KeyType, class ValType>
BOOL ListT<KeyType,ValType>::AddToHead(KeyType NewKey,
ValType NewObject)
{
//Allocate memory for the new node
ListNodeT<KeyType,ValType>* pNewNode =
new ListNodeT<KeyType,ValType>(NewKey,NewObject);
if (!pNewNode)
return FALSE;
//Insert the node into the list
pNewNode->SetNext(head);
head = pNewNode;
NumItems++;
return TRUE;
}


template <class KeyType, class ValType>
BOOL ListT<KeyType,ValType>::AddToTail(KeyType NewKey,
ValType NewObject)
{
//Allocate memory for the new node
ListNodeT<KeyType,ValType>* pNewNode =
new ListNodeT<KeyType,ValType>(NewKey,NewObject);
// ListNodeT<KeyType,ValType>* pNewNode =
// &(ListNodeT<KeyType,ValType>(NewKey,NewObject));
if (!pNewNode)
return FALSE;
//Insert the node into the list
ListNodeT<KeyType,ValType>* pCursor = head;

//search the tail of the list
if (!pCursor) //have no member,must set the head
head = pNewNode;
else
{
while (pCursor->GetNext())
pCursor = pCursor->GetNext();

pCursor->SetNext(pNewNode);
}
NumItems++;
return TRUE;
} //end of AddToTail

template <class KeyType,class ValType>
BOOL ListT<KeyType,ValType>::InsertAscend(KeyType NewKey,
ValType NewObject)
{
//Allocate memory for the new node
ListNodeT<KeyType,ValType>* pNewNode =
new ListNodeT<KeyType,ValType>(NewKey,NewObject);
if (!pNewNode)
return FALSE;
//Insert the node into the list
ListNodeT<KeyType,ValType>* pCursor1 = head;
ListNodeT<KeyType,ValType>* pCursor2 = head;
//search the key grand then the insert
if (this->GetListSize() == 0) //the list have no member, must set the head
head = pNewNode;
else if (this->GetListSize() == 1)
{
if (pCursor1->GetKey() > NewKey)
{
head = pNewNode;
pNewNode->SetNext(pCursor1);
}
else
pCursor1->SetNext(pNewNode);
}
else //have some member (haven sorted ascend)
{
if (pCursor1->GetKey() > NewKey) //must inster in head
{
head = pNewNode;
pNewNode->SetNext(pCursor1);
}
else
{
//search the position of the newobject
while (pCursor1->GetNext()) {
pCursor2 = pCursor1;
pCursor1 = pCursor1->GetNext();
if (pCursor2->GetKey() < NewKey &&
pCursor1->GetKey() > NewKey)
{
pCursor2->SetNext(pNewNode);
pNewNode->SetNext(pCursor1);
NumItems++;
return TRUE;
}
}
//tail
pCursor1->SetNext(pNewNode);
}
}
NumItems++;
return TRUE;
}

sxbobo2002 2003-11-26
  • 打赏
  • 举报
回复
呵。。。来晚啦。
zhouqingyuan 2003-11-26
  • 打赏
  • 举报
回复
呵呵,前面的写了这么多,真是有心啊
12s 2003-11-26
  • 打赏
  • 举报
回复
我不是画拿分的,只想说一句话:“我们是为学习!不是为了拿分!”
seayou 2003-11-26
  • 打赏
  • 举报
回复
勤劳一定就行了

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧