C语言程序设计(图书管理系统)//源代码一定要运行的起来
#ifndef _SIMPLE_BOOK_MANAGER_H
#define _SIMPLE_BOOK_MANAGER_H
typedef struct BookRecord
{
int index; //编号
char catalog[20]; //分类
char book_name[40]; //书名
char author[20]; //作者
char publisher[80]; //出版社
int quantity; //数量
float single_price; //单价
float total_price; //总价
}BookRecord, PtrBookRecord;
typedef struct CatalogRecord
{
int index; //编号
char catalog[20]; //分类
int total_number; //总册数
}CatalogRecord, PtrCatalogRecord;
#endif
void ReadBookRecord(BookRecord, FILE);
void FillRecord(char line, BookRecord);
FILE open_file(const char file_name);
void GetNumber(const char file_name, char number);
void print_main_menu();
char get_command();
void SearchAndDisplay(BookRecord);
void Arrange(BookRecord);
void PriceStat(BookRecord, const char output_file_name);
void NumberStat(BookRecord, const char output_file_name);
void print_search_and_disp_menu();
void SearchByName(BookRecord);
void SearchByAuthor(BookRecord);
void ShowAllBook(BookRecord);
void print_head();
void print_book_record(BookRecord);
void print_arrange_menu();
void ArrangeByCatalog(BookRecord);
void ArrangeByPublisher(BookRecord);
int total_book_number;
int main(int argc, char argv[])
{
FILE fp;
char cmd;
char number[10] = {0};
char price_file_name[20] = {0};
char catalog_file_name[20] = {0};
BookRecord book_records[20];
int loop = 1;
memset(book_records, 0, sizeof(book_records));
if (argc >= 2)
{
fp = open_file(argv[1]);
GetNumber(argv[1], number);
}
else
{
char file_name[] = "DX0112txt";
fp = open_file(file_name);
GetNumber(file_name, number);
}
if (fp == NULL)
{
printf("打开文件失败!");
return -1;
}
else
{
strcpy(price_file_name, "ZJ");
strcat(price_file_name, number);
strcat(price_file_name, "txt");
strcpy(catalog_file_name, "ZL");
strcat(catalog_file_name, number);
strcat(catalog_file_name, "txt");
}
ReadBookRecord(book_records, fp);
while (loop)
{
print_main_menu();
cmd = get_command();
switch(cmd)
{
case 'S': //查询显示
case 's':
SearchAndDisplay(book_records);
break;
case 'A': //排序
case 'a':
Arrange(book_records);
break;
case 'P': //统计总价
case 'p':
PriceStat(book_records, price_file_name);
break;
case 'N': //统计总册数
case 'n':
NumberStat(book_records, catalog_file_name);
break;
case 'Q':
case 'q':
loop = 0;
break;
default:
break;
}
}
fclose(fp);
}
#include <iostreamh>
#include <fstreamh>
int mark[]; //右来保存走法的
int num[2][2];//棋盘
int temp;
int k ;
int q ;
string strAim = "123804765" //目标字符串,空格用0表示
string States[100];//字符窜数组
int iQuick; //有来退出递归的标志,1,表示找到走法,可以退出,0,怎么没有找到走法,继续走
Hrd(int i,int j)//华龙道算法,好像能找到结果,但是不能找到最少步数的结果,我不确定会不会还有漏洞。结果保存在mark[]中
{ string str1;
if (i!=0) //如果可以向上走,则向上走一步
{
temp=num[i-1][j];
num[i-1][j]=num[i][j];
num[i][j]=temp ; //交换空格和它上面的数字
// 保存交换后的状态
str1=Change2Str(num[][]); //转化成字符串,用来比较
if (CompState(str1)) //比较变换后的状态是否在以前的状态中出现过
{
//如果以前出现过,则返回
temp=num[i-1][j];
num[i-1][j]=num[i][j];
num[i][j]=temp ; // 把原来交换过的数字再交换过来
return ;
}
else//比较后,发现目前的状态以前没有出现过
{ mark[k++]=1 ;
if(strcmp(&strAim,& str1)=1)//跟目标比,看是否相等
{
iquick=1;
return;
}
else //不相等,没有达到最后我们的要求
{
//1 代表向上走了一步
Add2States(str1); //保存现在的状态到状态数组中
Hrd(i-1,j);
if (iquick=1) //可以退出
{
return;
}
} //递归,继续遍历
}
}
if(j!=2)//如果可以向右走,则向右走一步
{
temp=num[i][j+1];
num[i][j+1]=num[i][j] ;
num[i][j]=temp;
// 保存交换后的状态
str1=Change2Str(num[][]); //转化成字符串,用来比较
if (CompState(str1)) //比较变换后的状态是否在以前的状态中出现过
{
//如果以前出现过,返回
temp=num[i][j+1];
num[i][j+1]=num[i][j] ;
num[i][j]=temp; // 把原来交换过的数字再交换过来
return;
}
else//比较后,发现目前的状态以前没有出现过
{ mark[k++]=2 ; //2 代表向右走了一步
if(strcmp(&strAim,& str1)=1)//跟目标比,看是否相等
{
iquick=1;
return;
}
else //没有达到结果
{
Add2States(str1); //保存现在的状态到状态数组中
Hrd(i,j+1) ; //递归,继续遍历
if (iquick=1)
{
return;
}
}
}
}
if (i!=2)// 如果可以向下走,则向下走一步
{
temp=num[i+1][j];
num[i+1][j]=num[i][j];
num[i][j]=temp;
// 保存交换后的状态
str1=Change2Str(num[][]); //转化成字符串,用来比较
if (CompState(str1)) //比较变换后的状态是否在以前的状态中出现过
{
//如果以前出现过,则返回
temp=num[i+1][j];
num[i+1][j]=num[i][j];
num[i][j]=temp; // 把原来交换过的数字再交换过来
return ;
}
else//比较后,发现目前的状态以前没有出现过
{ mark[k++]=3 ; //3 代表向下走了一步
if(strcmp(&strAim,& str1)=1)//跟目标比,看是否相等
{
iquick=1;
return;
}
else //没有达到最后我们的要求
{
Add2States(str1); //保存现在的状态到状态数组中
Hrd(i+1,j) ; //递归,继续遍历
if (iquick=1)
{
return;
}
}
}
}
if (j!=0)//如果可以向左走,则向左走一步
{
temp=num[i1][j-1];
num[i][j-1]=num[i][j];
num[i][j]=temp;
// 保存交换后的状态
str1=Change2Str(num[][]); //转化成字符串,用来比较
if (CompState(str1)) //比较变换后的状态是否在以前的状态中出现过
{
//如果以前出现过,则回溯
temp=num[i][j-1];
num[i][j-1]=num[i][j];
num[i][j]=temp; // 把原来交换过的数字再交换过来
return ;
}
else//比较后,发现目前的状态以前没有出现过
{ mark[k++]=4 ; //1 代表向右走了一步
if(strcmp(&strAim,& str1)=1)//跟目标比,看是否相等
{
iquick=1;
return;
}
else //没有达到最后我们的要求
{
Add2States(str1); //保存现在的状态到状态数组中
Hrd(i,j-1) ; //递归,继续遍历
if (iquick=1)
{
return;
}
}
}
}
//如果都走过了,则返回
k--;
return;
}
void Change2Str(int arr[][])// 把二维数组转换成一个字符串
{int i,j
string strState
for (i=0;i<3 ;i++ )
{ for (j=0;j<3 ;j++ )
{
strState= strState & arr[i][j];
}
}
return strState;
}
void CompState(string str1 )//比较当前的状态是否再以前的状态中出现过
{ int i;
for (i=0;i<len(states[])-1 ; i++)//len(states[]),取得数组的长度,有点忘了
{
if (strcmp(&States[i],&str1)!=0)//如果两个字符串相等,则函数返回true
{
return true;
}
}
return false;
}
Add2States(string str1)
{
States[q++]=str1 ;
}
main()
{ num[0][0]=1;
num[0][1]=2;
num[0][2]=3;
num[1][0]=8;
num[1][1]=6;
num[1][2]=4;
num[2][0]=1;
num[2][1]=0;
num[2][2]=5;
hrd(2,1);
for(int i=0 ,i<100,i++)
{count<<mark[i]<<endl;
}
}
Dim moveup As Integer '蛇向上转向的标志
Dim movedown As Integer
Dim moveleft As Integer
Dim moveright As Integer
Public u As Integer 'u,l,r,d为蛇转向时的控制数值
Public d As Integer
Public l As Integer
Public r As Integer
Public score As Integer '游戏分数
Public speed As Integer '游戏速度
Public pause As Boolean '游戏暂停标志
Public gamebegin As Boolean '游戏开始标志
Public s As Integer '蛇的当前长度,开始时设为6
Public eatd As Boolean '蛇是否吃到食物的标志
Public lastx As Integer '若蛇吃到食物,lastx,lasty用来存放当前最后一个shape的运动状态,以供设定新的shape运动状态之用
Public lasty As Integer
Option Base 1
Dim X(100) As Long '数组存放蛇的长度
Dim Y(100) As Long
Public Sub eat() '判断蛇是否吃到食物的函数
If X(1) = -200 Then
If Shape1(1)Left <= Shape2Left And Shape1(1)Top = Shape2Top Then
eatd = True
End If
End If
If Y(1) = -200 Then
If Shape1(1)Top <= Shape2Top And Shape1(1)Left = Shape2Left Then
eatd = True
End If
End If
If X(1) = 200 Then
If Shape1(1)Left >= Shape2Left + 200 And Shape1(1)Top = Shape2Top Then
eatd = True
End If
End If
If Y(1) = 200 Then
If Shape1(1)Top >= Shape2Top + 200 And Shape1(1)Left = Shape2Left Then
eatd = True
End If
End If
End Sub
Public Sub ensure_position() '此函数确定新的shape1(s)的位置
If X(s - 1) = -200 Then
Shape1(s)Left = Shape1(s - 1)Left + 200: Shape1(s)Top = Shape1(s - 1)Top
End If
If Y(s - 1) = -200 Then
Shape1(s)Left = Shape1(s - 1)Left: Shape1(s)Top = Shape1(s - 1)Top + 200
End If
If X(s - 1) = 200 Then
Shape1(s)Left = Shape1(s - 1)Left - 200: Shape1(s)Top = Shape1(s - 1)Top
End If
If Y(s - 1) = 200 Then
Shape1(s)Left = Shape1(s - 1)Left: Shape1(s)Top = Shape1(s - 1)Top - 200
End If
End Sub
Public Sub ensure_state() '此过程用来确定新的控件的初始运动状态
If X(1) = -200 Then
If moveleft = 1 Then
X(s) = X(s - 1): Y(s) = Y(s - 1)
Else
X(s) = lastx: Y(s) = lasty
End If
End If
If Y(1) = -200 Then
If moveup = 1 Then
X(s) = X(s - 1): Y(s) = Y(s - 1)
Else
X(s) = lastx: Y(s) = lasty
End If
End If
If X(1) = 200 Then
If moveright = 1 Then
X(s) = X(s - 1): Y(s) = Y(s - 1)
Else
X(s) = lastx: Y(s) = lasty
End If
End If
If Y(1) = 200 Then
If movedown = 1 Then
X(s) = X(s - 1): Y(s) = Y(s - 1)
Else
X(s) = lastx: Y(s) = lasty
End If
End If
End Sub
Public Sub strike_oneself() '测试蛇头是否撞到自己
Dim i
Dim j: j = 0
If Y(1) = 200 Then '蛇头向下撞到自己
For i = 7 To s '蛇头只能和第六块或以后的蛇身相撞
If Shape1(1)Top = Shape1(s)Top And Shape1(1)Left = Shape1(s)Left Then
Form1initialize
Form1outputmessage
Exit For
End If
Next i
End If
If X(1) = 200 Then '蛇头向右撞到自己
For i = 7 To s
If Shape1(1)Left = Shape1(i)Left - 200 And Shape1(1)Top = Shape1(i)Top Then
Form1initialize
Form1outputmessage
Exit For
End If
Next i
End If
If Y(1) = -200 Then '蛇头向上撞到自己
For i = 7 To s
If Shape1(1)Top = Shape1(i)Top + 200 And Shape1(1)Left = Shape1(i)Left Then
Form1initialize
Form1outputmessage
Exit For
End If
Next i
End If
If X(1) = -200 Then '蛇头向左撞到自己
For i = 7 To s
If Shape1(1)Left = Shape1(i)Left + 200 And Shape1(1)Top = Shape1(i)Top Then
Form1initialize
Form1outputmessage
Exit For
End If
Next i
End If
End Sub
Public Sub strike_wall() '测试蛇头是否撞到墙壁
If Shape1(1)Top < 0 Or Shape1(1)Top > Line1Y1 - 200 Or Shape1(1)Left < 0 Or Shape1(1)Left > Form1Width - 200 Then
Form1outputmessage
Form1initialize
End If
End Sub
Public Sub outputmessage() ' 游戏结束后的输出信息
gameovermessage = "GAME OVER!"
dx = (8000 - TextWidth(gameovermessage)) / 2 '在窗体中间输出"GAME OVER!"和得分
dy = (6000 - TextHeight(gameovermessage)) / 2
CurrentX = dx: CurrentY = dy
Form1Print gameovermessage
scoremessage = "你的得分:" + Text1Text
dx = (8000 - TextWidth(scoremessage)) / 2
dy = dy + TextHeight(scoremessage)
CurrentX = dx: CurrentY = dy
Form1Print scoremessage
End Sub
Public Sub initialize() '游戏结束(失败)后,游戏初始化函数
moveup = 0
movedown = 0
moveleft = 0
moveright = 0
u = 0: d = 0: l = 0: r = 0
Label5Caption = "停止"
For i = 1 To s ' 初始化蛇的位置和状态
Shape1(i)Left = 1200 + (6 - i) 200
Shape1(i)Top = 5000
X(i) = 200
Y(i) = 0
Next i
If s > 6 Then
For i = 7 To s
Unload Shape1(i) '卸载多余的shape,使初始的shape个数为六个
Next i
End If
'以下的这段是用来查看游戏初始化时的中的参数
'CurrentX = 0: CurrentY = 0
'Form1FontSize = 10
'Print moveup: Print movedown: Print moveleft: Print moveright
'Print u: Print r: Print l: Print d
'For i = 1 To s
'Print X(i): Print Y(i)
'Next i
s = 6
Timer1Enabled = False
gamebegin = False '游戏未开始
pause = False
HScroll1Enabled = True
End Sub
Private Sub Command1_Click()
Timer1Enabled = False
Dim r
r = MsgBox("你真的要退出吗", 33, "警告")
If r = 1 Then
Unload Form1
Else
Timer1Enabled = True
End If
End Sub
Private Sub Form_KeyPress(keyascii As Integer)
If gamebegin = True And pause = False Then '只有游戏处于开始时方向键才有效
'下面四个if语句是蛇的方向控制函数
If keyascii = 119 Then
If Y(1) <> 200 Then '只有当蛇头不正好向下运动时,按下向上才有效
moveup = 1 '向下上为w
End If
End If
If keyascii = 97 Then '向左为a
If X(1) <> 200 Then
moveleft = 1
End If
End If
If keyascii = 115 Then '向下为s
If Y(1) <> -200 Then
movedown = 1
End If
End If
If keyascii = 100 Then '向右为d
If X(1) <> -200 Then
moveright = 1
End If
End If
End If
If keyascii = 32 Then '判断游戏是否在开始,按下空格键开始,否则按下空格键为暂停
If gamebegin = False Then
Timer1Enabled = True
Label5Caption = "开始"
Form1Cls
score = 0
Text1Text = Str$(score)
HScroll1Enabled = False
gamebegin = True
Else
If pause = True Then '判断暂停与否
Timer1Enabled = True
Label5Caption = "开始"
Else
Timer1Enabled = False
Label5Caption = "暂停"
End If
pause = Not pause
End If
End If
End Sub
Private Sub Form_Load() '对各参数的初始化
moveup = 0
moveleft = 0
moveright = 0
movedown = 0
u = 0: l = 0: d = 0: r = 0
s = 6 '蛇的初始长度
eatd = False
gamebegin = False
pause = False
speed = HScroll1Value '初始速度
Label5Caption = "停止"
Dim i
For i = 1 To s '蛇的初始运动方向
X(i) = 200
Y(i) = 0
Next i
Timer1Enabled = False '没开始游戏
Shape2Left = 200 Int(30 Rnd) '贪吃蛇的食物的位置初始化
Shape2Top = 200 Int(30 Rnd)
End Sub
Private Sub HScroll1_Change()
speed = HScroll1Value
End Sub
Private Sub Timer1_Timer()
Timer1Interval = 150 / speed '游戏速度
If movedown = 1 Then '按下下方向键,蛇向下转向
d = d + 1
X(d) = 0: Y(d) = 200
If d = s Then
movedown = 0: d = 0
End If
End If
If moveup = 1 Then '按下向上方向键,蛇向上转向
u = u + 1
X(u) = 0: Y(u) = -200
If u = s Then
moveup = 0: u = 0
End If
End If
If moveleft = 1 Then '向左
l = l + 1
X(l) = -200: Y(l) = 0
If l = s Then
moveleft = 0: l = 0
End If
End If
If moveright = 1 Then '向右
r = r + 1
X(r) = 200: Y(r) = 0
If r = s Then
moveright = 0: r = 0
End If
End If
Dim i '蛇的运动代码
For i = 1 To s
Shape1(i)Left = Shape1(i)Left + X(i)
Shape1(i)Top = Shape1(i)Top + Y(i)
Next i
Form1eat '调用过程eat(),判断蛇是否吃了食物
If eatd = True Then
score = score + 5 HScroll1Value
Text1Text = Str$(score)
Shape2Left = 200 Int(30 Rnd) '如果食物被吃则随机产生贪吃蛇的食物的位置
Shape2Top = 200 Int(30 Rnd)
Shape2FillColor = RGB(Int(255 Rnd), Int(255 Rnd), Int(255 Rnd)) '随机产生食物的颜色
If Shape2FillColor = &H0& Then
Shape2FillColor = RGB(100, 150, 200)
End If
lastx = X(s) '蛇吃到食物,lastx,lasty用来存放当前最后一个shape的运动状态-
lasty = Y(s) '-以供设定新的shape运动状态之用
s = s + 1
Load Shape1(s) '产生新的控件shape1数组,使蛇的长度加长
Form1ensure_state '调用设定新的shape控件的运动状态过程
Form1ensure_position '设定新的shape1的位置
Shape1(s)Visible = True
eatd = False
End If
Form1strike_oneself '测试游戏是否失败的函数
Form1strike_wall
End Sub
#include<stdioh>
#include<conioh>
#include<stdlibh>
#include<stringh>
typedef struct
{
long class_1; /班级/
long number; /学号/
char name[20]; /姓名/
float tall; /身高/
float math; /数学/
float c_program; /C语言/
float physics; /大学物理/
float english; /大学英语/
float sum; /总分/
float average; /平均分/
}Student;
Student stud[100];
int i=0;
int menu() /菜单函数/
{
int a;
system("graftabl 936");
printf("学生信息管理系统\n\n\n");
printf("\t\t\t(1)输入学生信息\n");
printf("\t\t\t(2)显示所有学生信息\n");
printf("\t\t\t(3)按平均分升降排序\n");
printf("\t\t\t(4)根据学号查找学生信息\n");
printf("\t\t\t(5)插入学生信息\n");
printf("\t\t\t(6)删除学生信息\n");
printf("\t\t\t(7)修改学生信息\n");
printf("\t\t\t(8)从文件中读入数据\n");
printf("\t\t\t(9)将所有记录写入文件\n");
printf("\t\t\t(0)退出本系统\n\n\n");
printf("学生信息管理系统\n");
printf("请选择你要的操作(0~9):");
scanf("%d",&a); /读入一个数/
while(a<0||a>9)
{
printf("输入错误!请重新输入:\n");
scanf("%d",&a); /输入错误就一直循环/
}
system("cls"); /清屏/
return(a); /返回a的值/
}
int input(Student stud[])
{
char flag='Y';
while(flag=='Y'||flag=='y')
{
printf("请输入该学生的班级:");
scanf("%ld",&stud[i]class_1);
printf("请输入该学生的学号:");
scanf("%ld",&stud[i]number);
printf("请输入该学生的姓名:");
scanf("%s",stud[i]name);
printf("请输入学生的身高:");
scanf("%f",&stud[i]tall);
printf("请输入该学生的“数学”分数:");
scanf("%f",&stud[i]math);
printf("请输入该学生的“C语言”分数:");
scanf("%f",&stud[i]c_program);
printf("请输入该学生的“大学物理”分数:");
scanf("%f",&stud[i]physics);
printf("请输入该学生的“大学英语”分数:");
scanf("%f",&stud[i]english);
getchar();
stud[i]sum=stud[i]math+stud[i]c_program+stud[i]physics+stud[i]english;
stud[i]average=stud[i]sum/4;
printf("该学生的总分为:%62f\t平均分为:%62f\n",stud[i]sum,stud[i]average);
i++;
printf("继续输入学生信息请按:Y or y,退出请按:N or n\n");
scanf("%c",&flag); /判断是否继续输入/
}
printf("按任意键回主菜单\n");
getchar();
system("cls");
return(i);
}
void Display(Student stud[],int n) /显示所有记录/
{
int k;
if(n==0)
printf("暂无学生记录!\n");
else
{
printf(" 班别 学号 姓名 身高 数学 C语言 物理 英语 总分 平均分\n");
for(k=0;k<n;k++) /循环输出/
{
printf("%7ld",stud[k]class_1);
printf("%7ld",stud[k]number);
printf("%9s",stud[k]name);
printf("%71f",stud[k]tall);
printf("%72f",stud[k]math);
printf("%72f",stud[k]c_program);
printf("%72f",stud[k]physics);
printf("%72f",stud[k]english);
printf("%72f",stud[k]sum);
printf("%82f\n",stud[k]average);
}
}
system("pause");
system("cls");
}
void Sort(Student stud[],int i) /按平均分排序/
{
int m,n,k; /m,n为循环变量/
Student t;
for(m=0;m<i-1;m++) /排序/
for(n=m+1;n<i;n++)
if(stud[m]average<stud[n]average)
{
t=stud[m];
stud[m]=stud[n];
stud[n]=t;
}
printf("排序后的顺序为:\n");
printf(" 班别 学号 姓名 身高 数学 C语言 物理 英语 总分 平均分\n");
for(k=0;k<n;k++) /循环输出/
{
printf("%7ld",stud[k]class_1);
printf("%7ld",stud[k]number);
printf("%9s",stud[k]name);
printf("%71f",stud[k]tall);
printf("%72f",stud[k]math);
printf("%72f",stud[k]c_program);
printf("%72f",stud[k]physics);
printf("%72f",stud[k]english);
printf("%72f",stud[k]sum);
printf("%82f\n",stud[k]average);
}
system("pause");
system("cls");
}
void Search(Student stud[],int i) /查找并显示一个记录/
{
int p,k; /p为要查找的学生学号/
char flag='y';
while(flag=='y'||flag=='Y')
{
printf("请输入要查找的学生学号:");
scanf("%d",&p);
getchar();
for(k=0;p!=stud[k]number&&k<i;k++)
if(k==i)
printf("该学号不存在!\n");
else
{
printf("该学生的信息为:\n");
printf(" 班别 学号 姓名 身高 数学 C语言 物理 英语 总分 平均分\n");
printf("%7ld",stud[k]class_1);
printf("%7ld",stud[k]number);
printf("%9s",stud[k]name);
printf("%71f",stud[k]tall);
printf("%72f",stud[k]math);
printf("%72f",stud[k]c_program);
printf("%72f",stud[k]physics);
printf("%72f",stud[k]english);
printf("%72f",stud[k]sum);
printf("%82f\n",stud[k]average);
}
printf("继续查找学生信息请输入:Y or y,退出输入:N or n\n");
scanf("%c",&flag);
}
system("cls");
}
int Insert(Student stud[],int i) /插入学生信息/
{
char flag='Y';
while(flag=='y'||flag=='Y')
{
printf("请输入要插入学生的班别:");
scanf("%ld",&stud[i]class_1);
printf("请输入要插入学生的学号:");
scanf("%ld",&stud[i]number);
printf("请输入要插入学生的姓名:");
scanf("%s",&stud[i]name);
printf("请输入要插入学生的身高:");
scanf("%f",&stud[i]tall);
printf("请输入要插入学生的“数学”分数:");
scanf("%f",&stud[i]math);
printf("请输入要插入学生的“C语言”分数:");
scanf("%f",&stud[i]c_program);
printf("请输入要插入学生的“大学物理”分数:");
scanf("%f",&stud[i]physics);
printf("请输入要插入学生的“大学英语”分数:");
scanf("%f",&stud[i]english);
getchar();
stud[i]sum=stud[i]math+stud[i]c_program+stud[i]physics+stud[i]english;
stud[i]average=stud[i]sum/4;
printf("该学生的总分为:%62f\n",stud[i]sum);
printf("该学生的平均分为:%63f\n",stud[i]average);
i++;
printf("继续插入学生信息请输入:Y or y,退出输入:N or n\n");
scanf("%c",&flag); /判断是否继续输入/
}
getchar();
printf("是否需要排序,需要请按'Y' or 'y';不需要请按'N' or 'n'\n");
flag=getchar();
if(flag=='y'||flag=='Y')
Sort(stud,i);
system("cls");
return(i);
}
int Delete(Student stud[],int i) /根据学号删除学生信息/
{
long s;
int t,p;
char a='Y'; /判断是否继续/
while(a=='Y'||a=='y')
{
printf("请输入需要删除学生的学号:\n");
scanf("%ld",&s);
getchar();
for(t = 0; t < i && s!=stud[i]number; t++)
if(s==stud[t]number)
{
for(p=t;p<i;p++)
stud[p]=stud[p+1];
i=i-1;
printf("删除成功!\n");
}
else printf("没有找到要删除的学号!\n");
printf("是否继续删除,继续请按'Y' or 'y',退出请按'N' or 'n'\n");
a=getchar(); /判断是否继续/
getchar(); /消除回车键的影响/
}
system("cls");
return(i);
}
void change(Student stud[],int i) /修改学生信息/
{
long p=0; /p用来表示要修改的学生学号/
int k; /k为循环变量/
char a='y';
while(a=='y'||a=='Y')
{
printf("\t\t学生信息修改:\n");
printf("请输入要修改的学生的学号:\n");
scanf("%ld",&p);
for(k=0;p!=stud[k]number && k<i;k++)
if(k==i)
printf("无法找到该学生的信息:\n");
else
{
printf("该学生的信息为:\n");
printf(" 班别 学号 姓名 身高 数学 C语言 物理 英语 总分 平均分\n");
printf("%7ld",stud[k]class_1);
printf("%7ld",stud[k]number);
printf("%9s",stud[k]name);
printf("%71f",stud[k]tall);
printf("%72f",stud[k]math);
printf("%72f",stud[k]c_program);
printf("%72f",stud[k]physics);
printf("%72f",stud[k]english);
printf("%72f",stud[k]sum);
printf("%82f\n",stud[k]average);
printf("请输入该学生的新信息:\n");
printf("请输入该学生的班级:");
scanf("%d",&stud[k]class_1);
printf("请输入该学生的学号:");
scanf("%d",&stud[k]number);
printf("请输入该学生的姓名:");
scanf("%s",stud[k]name);
printf("请输入该学生的身高:");
scanf("%f",&stud[k]tall);
printf("请输入该学生的“数学”分数:");
scanf("%f",&stud[k]math);
printf("请输入该学生的“C语言”分数:");
scanf("%f",&stud[k]c_program);
printf("请输入该学生的“大学物理”分数:");
scanf("%f",&stud[k]physics);
printf("请输入该学生的“大学英语”分数:");
scanf("%f",&stud[k]english);
getchar();
stud[k]sum=stud[k]math+stud[k]c_program+stud[k]physics+stud[k]english;
stud[k]average=stud[k]sum/4;
printf("修改成功!\n");
}
printf("是否继续修改,继续请按'y'或'Y',退出请按'N'或'n'\n");
a=getchar();
}
system("pause");
system("cls");
}
int Read(Student stud[],int i) /从文件中读入学生信息/
{
int k=i;
FILE fp; /定义一个文件类型的指针/
char filename[20]; /定义文件名/
printf("请输入文件名:\n");
scanf("%s",&filename);
if((fp=fopen(filename,"rb"))==NULL) /打开文件/
{
printf("无法打开文件!\n"); /打开失败信息/
return 0;
}
printf("从文件%s中读入的数据为:\n",filename);
fread(&stud[k],sizeof(Student),1,fp);
printf(" 班别 学号 姓名 身高 数学 C语言 物理 英语 总分 平均分\n");
while(!feof(fp))
{
printf("%7ld",stud[k]class_1);
printf("%7ld",stud[k]number);
printf("%9s",stud[k]name);
printf("%71f",stud[k]tall);
printf("%72f",stud[k]math);
printf("%72f",stud[k]c_program);
printf("%72f",stud[k]physics);
printf("%72f",stud[k]english);
printf("%72f",stud[k]sum);
printf("%82f\n",stud[k]average);
k++;
fread(&stud[k],sizeof(Student),1,fp);
}
fclose(fp);
printf("读入成功!\n");
system("pause");
system("cls");
return(k);
}
void Save(Student stud[],int i) /将所有记录写入文件/
{
int k=0;
FILE fp; /定义文件指针/
char filename[80]; /定义文件名/
printf("请输入您要储存学生信息的文件名"); /输入文件名/
scanf("%s",&filename);
if( (fp=fopen(filename,"wt"))==NULL) /打开文件/
{
printf("无法打开该文件!\n");
return;
}
while(k<i)
{
fwrite(&(stud[k]), sizeof(Student), 1, fp);
k++;
}
printf("信息保存成功!\n");
fclose(fp);
}
int main(void)
{
int n=0;
while(1) /选择判断要执行的语句/
{
switch(menu())
{
case 1:printf("\n输入学生信息\n"); /输入学生信息/
n=input(stud);break;
case 2:printf("\n显示所有学生的信息\n");
Display(stud,n);break;
case 3:printf("\n按平均分升降排序\n");
Sort(stud,n);break;
case 4:printf("\n根据学号查找学生的信息\n");
Search(stud,n);break;
case 5:printf("\n插入学生的信息\n");
n=Insert(stud,n);break;
case 6:printf("\n删除学生信息\n");
n=Delete(stud,n);
case 7:printf("\n修改学生的信息\n");
change(stud,n);break;
case 8:printf("\n从文件中读入数据\n");
n=Read(stud,n);break;
case 9:printf("\n将所有记录写入文件\n");
Save(stud,n);break;
case 0:printf("\n感谢您的使用!\n");
exit(0);
}
}
return 0;
}
学生管理系统,,,这个可以不??
// lilvcpp : 定义控制台应用程序的入口点。
//
#include "stdafxh"
//int _tmain(int argc, _TCHAR argv[])
//{
// return 0;
//}
//
# include <stdioh>
# include <stdlibh>
int main(void)
{
double loan,r_o_y,m_p,r_o_m,first,second,third;//loan为贷款额,r_o_y为年利率,m_p为月还款额,first为第一个月的剩余贷款额(含利息),second及third同上
printf("Enter amount of loan: ");
scanf("%lf",&loan);
printf("Enter interest rate: ");
scanf("%lf",&r_o_y);
r_o_m = (r_o_y/100/12);
printf("Enter monthly payment: ");
scanf("%lf",&m_p);
//first = (loan - m_p)+((loan - m_p)r_o_m);
//second = (first - m_p)+((first - m_p)r_o_m);
//third = (second - m_p)+((second - m_p)r_o_m);
first = (loan - m_p)+((loan)r_o_m);
second = (first - m_p)+(firstr_o_m);
third = (second - m_p)+(secondr_o_m);
printf("Balance remaining after first payment: $%2lf\n",first);
printf("Balance remaining after second payment: $%2lf\n",second);
printf("Balance remaining after third payment: $%2lf\n",third);
system("pause");
return 0;
}
#include <string>
#include <iostream>
#include <timeh>
#include <vector>
using namespace std;
class Employee //定义雇员类
{
public:
Employee(); //构造函数
virtual VEmployee();
virtual void Display()=0;
int GetAge(); //获取年龄
string GetName(); //获取姓名
protected:
int Id; //职工ID号
string Name; //职工姓名
char Sex[5]; //性别
int Wage; //工资
string BirthDay; //出身年月
string WorkTime; //参加工作时间
int Age; //年龄
};
class Worker : public Employee //定义工人类
{
public:
Worker();
virtual VWorker();
void Display();
};
class Teacher : public Employee //定义教师类
{
public:
Teacher();
virtual VTeacher();
void Display();
string GetAcademy();
private:
string Academy;
};
Employee::Employee()
{
}
Employee::VEmployee()
{
}
int Employee::GetAge()
{
return Age;
}
string Employee::GetName()
{
return Name;
}
Teacher::Teacher() //添加教师信息
{
cout<<"请输入该教师的信息:"<<endl;
cout<<"职工编号 : "<<endl;
cin>>Id;
cout<<"姓名 : "<<endl;
cin>>Name;
cout<<"性别 : "<<endl;
cin>>Sex;
cout<<"工资 : "<<endl;
cin>>Wage;
cout<<"出生时间 : (格式xxxx/xx/xx)"<<endl;
cin>>BirthDay;
cout<<"参加工作时间 : (格式xxxx/xx/xx)"<<endl;
cin>>WorkTime;
cout<<"所属院系 : "<<endl;
cin>>Academy;
//从生日里提取出生的年份
int Pos = BirthDayfind_first_of('/');
string BirthYear;
BirthYearassign(BirthDay,0,Pos);
char chBirthYear[6];
strcpy(chBirthYear,BirthDayc_str());
//获取当前系统的年份
char NowYear[5];
time_t tnow = time(0);
strftime(NowYear , 5 , "%Y" , localtime(&tnow));
Age = atoi(NowYear)-atoi(chBirthYear); //计算年龄
}
Teacher::VTeacher()
{
}
void Teacher::Display(){ //display()同名函数,输出教师信息
cout<<"职工编号 : "<<Id<<endl;
cout<<"姓名 : "<<Name<<endl;
cout<<"性别 : "<<Sex<<endl;
cout<<"工资 : "<<Wage<<endl;
cout<<"出生时间 : "<<BirthDay<<endl;
cout<<"参加工作时间 : "<<WorkTime<<endl;
cout<<"年龄 : "<<Age<<endl;
}
string Teacher::GetAcademy(){
return Academy;
}
Worker::Worker() //添加工人信息
{
cout<<"请输入该工人的信息:"<<endl;
cout<<"职工编号 : "<<endl;
cin>>Id;
cout<<"姓名 : "<<endl;
cin>>Name;
cout<<"性别 : "<<endl;
cin>>Sex;
cout<<"工资 : "<<endl;
cin>>Wage;
cout<<"出生时间 : (格式xxxx/xx/xx)"<<endl;
cin>>BirthDay;
cout<<"参加工作时间 : (格式xxxx/xx/xx)"<<endl;
cin>>WorkTime;
//从生日里提取出生的年份
int Pos = BirthDayfind_first_of('/');
string BirthYear;
BirthYearassign(BirthDay,0,Pos);
char chBirthYear[6];
strcpy(chBirthYear,BirthDayc_str());
//获取当前系统的年份
char NowYear[5];
time_t tnow = time(0);
strftime(NowYear , 5 , "%Y" , localtime(&tnow));
Age = atoi(NowYear)-atoi(chBirthYear); //计算年龄
}
Worker::VWorker()
{
}
void Worker::Display(){//display()同名函数,输出工人信息
cout<<"职工编号 : "<<Id<<endl;
cout<<"姓名 : "<<Name<<endl;
cout<<"性别 : "<<Sex<<endl;
cout<<"工资 : "<<Wage<<endl;
cout<<"出生时间 : "<<BirthDay<<endl;
cout<<"参加工作时间 : "<<WorkTime<<endl;
cout<<"年龄 : "<<Age<<endl;
}
void AddTeacher(vector <Teacher> TeaInfo) //新增老师对象
{
system("color 05A"); //设置当前窗口的背景色和前景色
Teacher TeaTemp;
(TeaInfo)push_back(TeaTemp);
}
void AddWoker(vector <Worker> WorkerInfo) //新增工人对象
{
system("color 05A");
Worker Wokertmp;
(WorkerInfo)push_back(Wokertmp);
}
void DisplayAll(vector <Teacher> TeaInfo,vector <Worker> WokerInfo){ //显示全部职工记录
system("color 05A");
cout<<"所有教师的信息:"<<endl;
vector <Teacher>::iterator pTeaInfo;
for (pTeaInfo=(TeaInfo)begin();(TeaInfo)empty()!=1&&pTeaInfo!=(TeaInfo)end();pTeaInfo++) //如果到达vector末尾,则退出
{
(pTeaInfo)Display();
cout<<endl;
}
cout<<"所有工人的信息:"<<endl;
vector <Worker>::iterator pWorkerInfo;
for (pWorkerInfo=(WokerInfo)begin();(WokerInfo)empty()!=1&&pWorkerInfo!=(WokerInfo)end();pWorkerInfo++) //如果到达vector末尾,则退出
{
(pWorkerInfo)Display();
cout<<endl;
}
cout<<"按Enter返回"<<endl;
getchar(); getchar();
}
void DisAvgTeacherAge(vector <Teacher> TeaInfo){ //查询老师平均年龄
system("color 05A");
int SumAge = 0;
vector <Teacher>::iterator pTeaInfo;
for (pTeaInfo=(TeaInfo)begin();(TeaInfo)empty()!=1&&pTeaInfo!=(TeaInfo)end();pTeaInfo++)//遍历老师容器
{ //如果到达vector末尾,则退出
SumAge+= (pTeaInfo)GetAge();
cout<<endl;
}
if((TeaInfo)size()!=0)
cout<<"教师的平均年龄为"<<SumAge/((TeaInfo)size())<<endl;
else cout<<"没有记录"<<endl;
cout<<"按Enter返回"<<endl;
getchar(); getchar();
}
void DisAvgWorkerAge(vector <Worker> WokerInfo){
system("color 05A");
int SumAge = 0;
vector <Worker>::iterator pWorkerInfo;
for (pWorkerInfo=(WokerInfo)begin();(WokerInfo)empty()!=1&&pWorkerInfo!=(WokerInfo)end();pWorkerInfo++) //如果到达vector末尾,则退出
{
SumAge+= (pWorkerInfo)GetAge();
cout<<endl;
}
if((WokerInfo)size()!=0)
cout<<"职工的平均年龄为"<<SumAge/((WokerInfo)size())<<endl;
else cout<<"没有记录"<<endl;
cout<<"按Enter键返回"<<endl;
getchar(); getchar();
}
void DelTeacher(vector <Teacher> TeaInfo){ //用于删除老师信息
system("color 05A");
bool Hflag=false;
string SearchName;
vector <Teacher>::iterator pTeaInfo;
cout<<"请输入您要删除的老师的姓名:"<<endl;
cout<<"姓名: ";
cin>>SearchName;
for(pTeaInfo=(TeaInfo)begin();(TeaInfo)empty() != 1&&pTeaInfo!=(TeaInfo)end();pTeaInfo++) //如果到达vector末尾,则退出
{
if (0==SearchNamecompare((pTeaInfo)GetName())) //如果找到要删除老师的姓名,则执行
{
Hflag=true; //找到老师
(TeaInfo)erase(pTeaInfo); //删除vector中指定的老师
continue;
}
}
if (false==Hflag) //如果没有该老师,则执行
{
cout<<"没有该老师!"<<endl;
}
cout<<"按Enter返回"<<endl;
getchar(); getchar();
}
void DelWorker(vector <Worker> WokerInfo) //用于删除工人信息
{
system("color 05A");
bool Hflag=false;
string SearchName;
vector <Worker>::iterator pWorkerInfo;
cout<<"请输入您要删除的工人的姓名:"<<endl;
cout<<"姓名: ";
cin>>SearchName;
for(pWorkerInfo=(WokerInfo)begin();(WokerInfo)empty() != 1&&pWorkerInfo!=(WokerInfo)end();pWorkerInfo++) //如果到达vector末尾,则退出
{
if (0==SearchNamecompare((pWorkerInfo)GetName())) //如果找到要删除工人的姓名,则执行
{
Hflag=true; //找到工人
(WokerInfo)erase(pWorkerInfo); //删除vector中指定的工人
continue;
}
}
if (false==Hflag) //如果没有该工人,则执行
{
cout<<"没有该老师!"<<endl;
}
cout<<"按Enter返回"<<endl;
getchar();getchar();
}
void SearchByName(vector <Teacher> TeaInfo,vector <Worker> WokerInfo){ //按名字查找职工信息
system("color 05A");
bool Hflag=false;
string SearchName;
vector <Teacher>::iterator pTeaInfo;
cout<<"请输入您要查找的职工的姓名:"<<endl;
cout<<"姓名: ";
cin>>SearchName;
for(pTeaInfo=(TeaInfo)begin();pTeaInfo!=(TeaInfo)end();pTeaInfo++)
{
if (0==SearchNamecompare((pTeaInfo)GetName())) //如果找到输入的职工,则执行
{
Hflag=true; //找到职工
cout<<"这个人是老师,他的信息为:"<<endl;
(pTeaInfo)Display(); //显示职工信息
continue;
}
}
vector <Worker>::iterator pWorkerInfo;
for(pWorkerInfo=(WokerInfo)begin();pWorkerInfo!=(WokerInfo)end();pWorkerInfo++)
{
if (0==SearchNamecompare((pWorkerInfo)GetName())) //如果找到输入的职工,则执行
{
Hflag=true; //找到职工
cout<<"这个人是老师,个人信息为:"<<endl;
(pWorkerInfo)Display(); //显示职工信息
continue;
}
}
if (false==Hflag)
{
cout<<"没有这个职工!"<<endl; //没有找到信息
}
cout<<"按Enter返回"<<endl; //返回
getchar(); getchar();
}
void DisTeacherByAcademy(vector <Teacher> TeaInfo){ //按系输出教师信息
system("color 05A");
bool Hflag=false;
string SearchName;
vector <Teacher>::iterator pTeaInfo;
while((TeaInfo)empty()!=1) { //输出所有指定系的老师
pTeaInfo=(TeaInfo)begin();
string Academy = (pTeaInfo)GetAcademy();
cout<<"系:"<<Academy<<","<<"该系的老师有:"<<endl;
for(pTeaInfo=(TeaInfo)begin();(TeaInfo)empty()!=1&&pTeaInfo!=(TeaInfo)end();){
if (0==Academycompare((pTeaInfo)GetAcademy()))
{
(pTeaInfo)Display();
cout<<endl;
(TeaInfo)erase(pTeaInfo);
pTeaInfo=(TeaInfo)begin();
}
else pTeaInfo++;
}
cout<<endl;
}
cout<<"按Enter返回"<<endl;
getchar(); getchar();
}
void WriteFile(vector <Teacher> TeaInfo,vector <Worker> WokerInfo) //生成新文件
{
FILE fp;
if (NULL==(fp=fopen("Employeetxt","w+")))
{
cout<<"打开文件错误"<<endl;
exit(-1);
}
vector <Worker>::iterator pWorkerInfo;
vector <Teacher>::iterator pTeaInfo;
for(pTeaInfo=(TeaInfo)begin();pTeaInfo!=(TeaInfo)end();pTeaInfo++) //写入vector中所有老师的信息
{
fputs("教师信息:\n",fp);
fprintf(fp,"姓名:%s\n",(pTeaInfo)GetName()c_str());
fprintf(fp,"年龄:%d\n",(pTeaInfo)GetAge());
fputs("\n",fp);
}
fputs("教师信息结束\n",fp);
for(pWorkerInfo=(WokerInfo)begin();pWorkerInfo!=(WokerInfo)end();pWorkerInfo++) //写入vector中所有老师的信息
{
fputs("职工信息:\n",fp);
fprintf(fp,"姓名:%s\n",(pWorkerInfo)GetName()c_str());
fprintf(fp,"年龄:%d\n",(pWorkerInfo)GetAge());
fputs("\n",fp);
}
fputs("职工信息结束\n",fp);
fclose(fp);
}
void Mainmenu(){//菜单界面
system("color 05A"); //设置当前窗口的背景色和前景色
cout<<" ╭――――――――――《教职工管理系统》―――――――――╮"<<endl;
cout<<" ∣ 请选择功能 ∣"<<endl;
cout<<" ├――――――――――――――――――――――――――――┤"<<endl;
cout<<" ∣ 1新建并输入职工数据 ∣"<<endl;
cout<<" ∣ 2按条件输出职工数据 ∣"<<endl;
cout<<" ∣ 3删除职工数据 ∣"<<endl;
cout<<" ∣ 0退出 ∣"<<endl;
cout<<" ╰――――――――――――――――――――――――――――╯"<<endl;
cout<<" ――――――――――――――――――――――――――――――"<<endl;
cout<<" 请选择您要服务的类别: " ;
}
void Insert(vector <Teacher> TeaInfo,vector <Worker> WorkerInfo) //增加职工数据菜单界面
{
system("color 05A"); //设置当前窗口的背景色和前景色
int select = -1;
while(select!=0)
{
cout<<" ╭――――――――――《增加职工数据》――――――――――╮"<<endl;
cout<<" ∣ 请选择操作: ∣"<<endl;
cout<<" ├――――――――――――――――――――――――――――┤"<<endl;
cout<<" ∣ 1增加一位教师记录 ∣"<<endl;
cout<<" ∣ 2增加一位工人记录 ∣"<<endl;
cout<<" ∣ 0返回主菜单 ∣"<<endl;
cout<<" ╰――――――――――――――――――――――――――――╯"<<endl;
cin>>select;
switch(select){
case 1:AddTeacher(TeaInfo);break;
case 2:AddWoker(WorkerInfo);break;
case 3:Mainmenu();break;
default:
cout<<"回到主菜单!"<<endl;
}
}
}
void Search(vector <Teacher> TeaInfo,vector <Worker> WorkerInfo) //查询职工数据菜单界面
{
system("color 05A"); //设置当前窗口的背景色和前景色
int select = -1;
while(select!=0)
{
cout<<" ╭――――――――――《查询职工数据》――――――――――╮"<<endl;
cout<<" ∣ 请选择操作: ∣"<<endl;
cout<<" ├――――――――――――――――――――――――――――┤"<<endl;
cout<<" ∣ 1显示全部职工记录 ∣"<<endl;
cout<<" ∣ 2按系输出教师信息 ∣"<<endl;
cout<<" ∣ 3按姓名检索所有信息 ∣"<<endl;
cout<<" ∣ 4计算教师平均年龄 ∣"<<endl;
cout<<" ∣ 5计算工人平均年龄 ∣"<<endl;
cout<<" ∣ 0返回主菜单 ∣"<<endl;
cout<<" ╰――――――――――――――――――――――――――――╯"<<endl;
cin>>select;
switch(select){
case 1:DisplayAll(TeaInfo,WorkerInfo);break;
case 2:DisTeacherByAcademy(TeaInfo);break;
case 3:SearchByName(TeaInfo,WorkerInfo);break;
case 4:DisAvgTeacherAge(TeaInfo);break;
case 5:DisAvgWorkerAge(WorkerInfo);break;
case 6:Mainmenu();break;
default:
cout<<"回到主菜单!"<<endl;
}
}
}
void Delete(vector <Teacher> TeaInfo,vector <Worker> WorkerInfo) //删除职工数据
{
system("color 05A"); //设置当前窗口的背景色和前景色
int select = -1;
while(select!=0)
{
cout<<" ╭――――――――――《删除职工数据》――――――――――╮"<<endl;
cout<<" ∣ 请选择操作: ∣"<<endl;
cout<<" ├――――――――――――――――――――――――――――┤"<<endl;
cout<<" ∣ 1删除一位教师记录 ∣"<<endl;
cout<<" ∣ 2删除一位工人记录 ∣"<<endl;
cout<<" ∣ 0返回主菜单 ∣"<<endl;
cout<<" ╰――――――――――――――――――――――――――――╯"<<endl;
cin>>select;
switch(select){
case 1:DelTeacher(TeaInfo);break;
case 2:DelWorker(WorkerInfo);break;
case 3:Mainmenu();break;
default:
cout<<"回到主菜单!"<<endl;
}
}
}
void MainCho(vector <Teacher> TeaInfo,vector <Worker> WorkerInfo)
{ //主菜单操作
int choice = -1;
while(choice!=0){
Mainmenu();
cin>>choice;
switch(choice){
case 1:Insert(TeaInfo,WorkerInfo);break;
case 2:Search(TeaInfo,WorkerInfo);break;
case 3:Delete(TeaInfo,WorkerInfo);break;
case 0:break;
default:
cout<<"操作错误!"<<endl;
}
}
}
void main() //主函数
{
vector <Teacher> TeaInfo; //
vector <Worker> WorkerInfo; //
vector <Teacher>::iterator pTeacher; //
vector <Worker>::iterator pWoker; //
pTeacher = TeaInfobegin(); //
pWoker = WorkerInfobegin(); //
MainCho(&TeaInfo,&WorkerInfo); //调用主要处理程序
WriteFile(&TeaInfo,&WorkerInfo); //存入文件操作
}
头文件:::
#ifndef H_STUDENT_HH
#define H_STUDENT_HH
#include "stdioh"
#include "stringh"
#include "malloch"
#define LEN sizeof(struct message_student) /一个结构体数组元素的长度/
#define numsubs 5 /学科数目/
typedef struct message_student /结构体定义/
{
char number[6];
char name[20];
char sex[4];
float subject[numsubs];
float score;
float average;
int index;
}student;
extern int numstus; /学生数目/
extern student pointer; /指向结构体数组/
extern int lens;
int menu_select(); /函数声明/
int openfile(student stu[]);
int findrecord(student stud[]);
int writetotext(student stud[]);
void welcome();
void display1();
void showtable();
void sort(student stu[]);
void deleterecord(student stu[],int i);
void addrecord(student stud[]);
void display(student stud[],int n1,int n2);
void amendrecord(student stud[]);
void count(student stud[]);
void sortnum(student stud[]);
void sortnum2(student stud[]);
void sortname(student stud[]);
void sortname2(student stud[]);
void sortcount(student stud[]);
void sortcount2(student stud[]);
void statistic(student stud[]);
void display1();
#endif
#include "headh"
int menu_select()
{
char c;
printf("\n\n");
printf(" | 1 增加学生记录 5统计信息 |\n");
printf(" | 2 查询学生记录 6打开文件 |\n");
printf(" | 3 修改学生记录 7保存文件 |\n");
printf(" | 4 学生纪录排序 8显示记录 |\n");
printf(" | 0退出系统 |\n");
printf("\n\n");
printf("请选择(0-8):");
c=getchar();
getchar();
return (c-'0');
}
#include "headh"
int findrecord(student stud[]) /查找信息/
{
char str[2];
int i,num;
if(numstus==0)
{
printf("没有可被查找的记录\n");
return -1;
}
else
{
printf("以何种方式查找?\n1学号\t2姓名\t3名次\n");
gets(str);
if(str[0]=='1') /按学号查找/
{
printf("请输入学号:");
gets(str);
for(i=0;i<=numstus;i++)
if(strcmp(str,stud[i]number)==0)
{
display(stud,i,i);
break;
}
else continue;
}
else if(str[0]=='2') /按姓名查找/
{
printf("请输入姓名:");
gets(str);
for(i=0;i<=numstus;i++)
if(strcmp(str,stud[i]name)==0)
{
display(stud,i,i);
break;
}
else continue;
}
else if(str[0]=='3') /按名次查找/
{
printf("请输入名次:");
scanf("%d",&num);
getchar();
for(i=0;i<=numstus;i++)
if(num==stud[i]index)
{
display(stud,i,i);
break;
}
else continue;
}
if(i>numstus)
{
printf("没有查找所要的信息。\n");
return -1;
}
return i;
}
}
#include"headh"
int openfile(student stu[])
{
int i=0,j;
FILE fp;
char filename[20],str[2];
if(numstus!=0)
{
printf("已经有记录存在,是否保存(y/n)");
gets(str);
if(str[0]=='y'||str[0]=='Y')
writetotext(stu);
}
printf("请输入文件名:");
gets(filename);
numstus=0;
if((fp=fopen(filename,"rb+"))==NULL)
{
printf("无法打开该文件\n");
return(-1);
}
fscanf(fp,"%d",&numstus);
fgetc(fp);
while(i<numstus)
{
fscanf(fp,"%s",stu[i]number);
fscanf(fp,"%s",stu[i]name);
fscanf(fp,"%s",stu[i]sex);
for(j=0;j<numsubs;j++)
fscanf(fp,"%f",&stu[i]subject[j]);
fscanf(fp,"%f",&stu[i]score);
fscanf(fp,"%f",&stu[i]average);
fscanf(fp,"%d",&stu[i]index);
i++;
}
fclose(fp);
printf("文件读取成功\n");
printf("是否显示纪录(y/n)");
gets(str);
if(str[0]=='y'||str[0]=='Y')
display(stu,0,numstus-1);
return(0);
}
#include "headh"
void sort(student stud[])
{
int i,j=0;
char str[5];
student p;
p=stud;
if(numstus==0)
{
printf("没有可供查询的记录!");
}
while(1)
{
for(i=0;;i++)
{
printf(" 请输入查询方式:");
printf("(直接输入回车则结束查询操作)\n");
printf("1按照学号\t");
printf("2按照姓名\t");
printf("3按照名次\n");
gets(str);
if(strlen(str)==0) break;
if(str[0]=='1')
{
printf("请输入排序次序:\n");
printf("1升序排列\t");
printf("2降序排列\n");
gets(str);
if(str[0]=='1')
sortnum2(p);
else
sortnum(p);
display(stud,0,numstus-1);
}
else if(str[0]=='2')
{
printf("请输入排序次序:\n");
printf("1升序排列\t");
printf("2降序排列\n");
gets(str);
if(str[0]=='1')
sortname2(p);
else
sortname(p);
display(stud,0,numstus-1);
}
else if(str[0]=='3')
{
printf("请输入排序次序:\n");
printf("1升序排列\t");
printf("2降序排列\n");
gets(str);
if(str[0]=='1')
sortcount2(p);
else
sortcount(p);
display(stud,0,numstus-1);
}
else printf("请输入1~3");
printf("是否退出排序(y/n)");
gets(str);
if(str[0]=='y'||str[0]=='Y') break;
}
return;
}
}
void sortnum(student stud[])
{
int i,j;
student temp;
student p;
p=stud;
for(i=0;i<numstus;i++)
for(j=0;j<numstus-i-1;j++)
{
if(strcmp(stud[j+1]number,stud[j]number)>0)
{
temp=(p+j);
(p+j)=(p+j+1);
(p+j+1)=temp;
}
}
}
void sortnum2(student stud[])
{
int i,j;
student temp;
student p;
p=stud;
for(i=0;i<numstus;i++)
for(j=0;j<numstus-i-1;j++)
{
C语言程序设计(图书管理系统)//源代码一定要运行的起来
本文2023-09-22 07:27:37发表“资讯”栏目。
本文链接:https://www.lezaizhuan.com/article/42191.html