预处理
# define _CRT_SECURE_NO_WARNINGS
# include <stdio.h>
# include <stdlib.h>
# include <windows.h>
# include <conio.h>
# define OVERFLOW - 2
# define FALSE 0
# define TRUE 1
# define OK 1
# define ERROR 0
typedef int Status;
# define STACK_INIT_SIZE 100
# define STACKINCREAMENT 10
菜单
menu ( )
{
printf ( "\n\n\t\t*******************************************\n" ) ;
printf ( "\t\t* 学生信息管理系统 *\n" ) ;
printf ( "\t\t* 1.查询学生信息 *\n" ) ;
printf ( "\t\t* 2.增加学生信息 *\n" ) ;
printf ( "\t\t* 3.删除学生信息 *\n" ) ;
printf ( "\t\t* 4.显示全部信息 *\n" ) ;
printf ( "\t\t* 5.排序 *\n" ) ;
printf ( "\t\t* 0.退出系统 *\n" ) ;
printf ( "\t\t*******************************************\n" ) ;
}
结构体
typedef struct
{
char name[ 10 ] ;
float score;
} SElemType;
typedef struct
{
SElemType* base;
SElemType* top;
int stacksize;
} SqStack;
SElemType data;
SqStack stack;
主函数
void main ( )
{
char x;
InitStack ( & stack) ;
while ( 1 )
{
system ( "cls" ) ;
menu ( ) ;
x = getchar ( ) ;
switch ( x)
{
case '0' : exit ( 0 ) ;
case '1' : look ( ) ; break ;
case '2' : add ( ) ; break ;
case '3' : dele ( ) ; break ;
case '4' : show ( ) ; break ;
case '5' : SelectionSortStack ( & stack) ;
printf ( "排序完成!按任意键继续..." ) ;
_getch ( ) ;
break ;
default : break ;
}
}
exit ( 0 ) ;
}
函数声明
Status visit ( ) ;
Status InitStack ( SqStack* S) ;
Status DestroyStack ( SqStack* S) ;
Status ClearStack ( SqStack* S) ;
Status StackEmpty ( SqStack S) ;
int StackLength ( SqStack S) ;
Status GetTop ( SqStack S, SElemType* e) ;
Status Push ( SqStack* S, SElemType e) ;
Status Pop ( SqStack* S, SElemType* e) ;
Status StackTraverse ( SqStack S, Status ( * visit) ( ) ) ;
栈操作
Status InitStack ( SqStack* S)
{
S-> base = ( SElemType* ) malloc ( STACK_INIT_SIZE * sizeof ( SElemType) ) ;
if ( ! S-> base) exit ( OVERFLOW) ;
S-> top = S-> base;
S-> stacksize = STACK_INIT_SIZE;
return OK;
}
Status GetTop ( SqStack S, SElemType* e)
{
if ( S. top == S. base) return ERROR;
* e = * ( S. top - 1 ) ;
return OK;
}
Status Push ( SqStack* S, SElemType e)
{
if ( S-> top - S-> base >= S-> stacksize)
{
S-> base = ( SElemType* ) realloc ( S-> base, ( S-> stacksize + STACKINCREAMENT) * sizeof ( SElemType) ) ;
if ( ! S-> base) exit ( OVERFLOW) ;
S-> top = S-> base + S-> stacksize;
S-> stacksize += STACKINCREAMENT;
}
* S-> top++ = e;
return OK;
}
Status Pop ( SqStack* S, SElemType* e)
{
if ( S-> base == S-> top) return ERROR;
* e = * -- S-> top;
return OK;
}
Status StackEmpty ( SqStack S)
{
if ( S. base == S. top)
{
return TRUE;
}
else
{
return FALSE;
}
}
Status ClearStack ( SqStack* S)
{
S-> top = S-> base;
return OK;
}
Status DestroyStack ( SqStack* S)
{
S-> base = NULL ;
return OK;
}
功能实现
void look ( )
{
char name[ 10 ] ;
SElemType* p;
printf ( "请输入学生姓名:" ) ;
scanf ( "%s" , name) ;
p = stack. top;
for ( -- p; p >= stack. base; p-- )
{
if ( strcmp ( p-> name, name) == 0 )
{
printf ( "%s的成绩为%4.2f\n" , p-> name, p-> score) ;
printf ( "请按任意键返回!" ) ;
_getch ( ) ;
return ;
}
}
printf ( "没有查询到该学生! 请按任意键返回!" ) ;
_getch ( ) ;
}
void add ( )
{
printf ( "请输入学生姓名:" ) ;
scanf ( "%s" , data. name) ;
printf ( "请输入学生成绩:" ) ;
scanf ( "%f" , & data. score) ;
Push ( & stack, data) ;
printf ( "添加学生成功!请按任意键返回" ) ;
_getch ( ) ;
}
void dele ( )
{
Pop ( & stack, & data) ;
printf ( "成功删除刚刚添加的学生!:%s\n按任意键返回!" , data. name) ;
_getch ( ) ;
}
void show ( )
{
SElemType* p;
p = stack. top;
if ( ! p) printf ( "栈为空" ) ;
for ( -- p; p >= stack. base; p-- )
{
printf ( "%s的成绩为%4.2f\n" , p-> name, p-> score) ;
}
printf ( "请按任意键返回!" ) ;
_getch ( ) ;
return ;
}
int StackLength ( SqStack S) {
return S. top - S. base;
}
Status visit ( SElemType e) {
printf ( "%s, 分数: %.2f " , e. name, e. score) ;
return OK;
}
Status StackTraverse ( SqStack S, Status ( * visit) ( SElemType) ) {
SElemType* p = S. base;
for ( p = S. base; p != S. top; p++ ) {
visit ( * p) ;
}
printf ( "\n" ) ;
return OK;
}
void PrintStack ( SqStack* S) {
if ( StackEmpty ( * S) ) {
printf ( "Stack is empty.\n" ) ;
return ;
}
SElemType e;
int stackLength = StackLength ( * S) ;
for ( int i = stackLength - 1 ; i >= 0 ; i-- ) {
e = ( * S) . base[ i] ;
printf ( "姓名:%s 分数: %4.2f\n" , e. name, e. score) ;
}
printf ( "\n" ) ;
}
void SelectionSortStack ( SqStack* S) {
int i, j, maxIndex;
SElemType temp, currentTop;
printf ( "原始栈: \n" ) ;
PrintStack ( S) ;
printf ( "按任意键继续:\n" ) ;
_getch ( ) ;
for ( i = 0 ; i <= StackLength ( * S) - 1 ; i++ ) {
maxIndex = i;
for ( j = i + 1 ; j <= StackLength ( * S) - 1 ; j++ ) {
if ( ( S) -> base[ j] . score > ( S) -> base[ maxIndex] . score) {
maxIndex = j;
}
}
if ( i != maxIndex) {
temp = ( S) -> base[ i] ;
( S) -> base[ i] = ( S) -> base[ maxIndex] ;
( S) -> base[ maxIndex] = temp;
Status status = GetTop ( * S, & currentTop) ;
printf ( "交换了索引为 %d 与索引为 %d 的元素,当前栈顶分数为:%.2f\n" , i, maxIndex, currentTop. score) ;
} else {
printf ( "获取栈顶元素失败。\n" ) ;
}
}
printf ( "排序后的栈: \n" ) ;
PrintStack ( S) ;
printf ( "按任意键继续:\n" ) ;
_getch ( ) ;
}