概述
列表内容
1、随机不重复点名
2、成员出勤考察
3、成员的增删查改、保存、显示、排序
4、采用线性表的方式实现
#define _CRT_SECURE_NO_DEPRECATE//vs scanf 不用转为 scanf_s
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <memory.h>
#include <time.h>
#include<stdarg.h>
#define LIST_INIT_SIZE 100 //初始化节点空间大小
#define LIST_INCREMENT 10//每增加一次默认空间大小为10
typedef struct
{
int Stuxh;
int StuNum;
char StuName[20];//姓名
}Stu;//定义成员基本信息
int xx[30] = { 0 };
typedef struct
{
Stu *elem;
int length;//线性表的长度
int listsize;//线性表大小
}StuList;//定义线性表
typedef int status;
static int sss = 0;//定义全局变量(值可以改变)
StuList student; //全局变量成员
int number = 0;
int nuM[30] = { 0 };
void ClearList(StuList &L) //清空线性表
{
L.length = 0;
}
status ListDelete(StuList &L, int i, Stu &e)//删除指定的节数
{
Stu *p, *q;
if (i<1 || i>L.length)
return 0;
p = L.elem + i - 1;
e = *p;
q = L.elem + L.length - 1;
for (p++; p <= q; p++)
*(p - 1) = *p;
L.length--;
return 1;
}
status DestroyList(StuList &L) //销毁线性表
{
if (L.elem)
free(L.elem);
L.elem = NULL;
L.length = 0;
L.listsize = 0;
return 1;
}
void Show()//菜单界面
{
printf("******************************************************************n");
printf("* 欢迎进入点名系统 *n");
printf("* 按Z键:添加多个成员 *n");
printf("* 按K键:出勤状况 *n");
printf("* 按N键:添加单个成员 *n");
printf("* 按M键:修改成员 *n");
printf("* 按D键:删除成员 *n");
printf("* 按Q键:查找成员 *n");
printf("* 按P键:排序成员 *n");
printf("* 按H键:显示成员 *n");
printf("* 按F键:进行点名 *n");
printf("* 按E键:退出点名系统 *n");
printf("* 其他:回到首页 *n");
printf("******************************************************************n");
}
int ListLength(StuList L)//返回线性表长度
{
return L.length;
}
status ListInsert(StuList &L, int i, Stu e)//插入值
{
Stu *newbase, *q, *p;
if (i<1 || i>L.length + 1)
return NULL;
if (L.length == L.listsize)
{
newbase = (Stu*)realloc(L.elem, (L.listsize + LIST_INCREMENT) * sizeof(Stu));
if (!newbase)
exit(-1);
L.elem = newbase;
L.listsize += LIST_INCREMENT;
}
q = L.elem + i - 1;
for (p = L.elem + L.length - 1; p >= q; --p)
*(p + 1) = *p;
*q = e;
L.length++;
}
static status InitList(StuList &L)
{
L.elem = (Stu*)malloc(LIST_INIT_SIZE * sizeof(Stu));
if (!L.elem)
return 0;
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return 1;
}
void printElement(Stu& e)
{
printf("-------------------------------------------------------------n");
printf(" 序号 %d | 学号 %d| 姓名 %s n", e.Stuxh, e.StuNum, e.StuName);
printf("-------------------------------------------------------------n");
}
//显示成员
void ListTraverse(StuList L, void(*visit)(Stu& e))
{
Stu *p = L.elem;
for (int i = 1; i <= L.length; i++)
visit(*p++);
printf("n");
}
void zAdd()//增加成员
{
int i, n;
printf("请输入成员个数:");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
Stu information;
printf("请输入序号 学号 姓名n");
scanf("%d%d%s", &information.Stuxh, &information.StuNum, &information.StuName);
ListInsert(student, i + 1, information);
}
ListTraverse(student, printElement);
system("pause");
}
void Add()
{
Stu information;
int len = student.length;
printf("请输入序号 学号 姓名n");
scanf("%d%d%s", &information.Stuxh, &information.StuNum, &information.StuName);
ListInsert(student, len + 1, information);
ListTraverse(student, printElement);
system("pause");
}
void kaocha()//考勤处理
{
int ssd;
int c = 9;
int i;
int j = 0;
int t = 0;
int count = 0;
int ssa[26] = { 0 };
int ssb[26] = { 0 };
int ssc[26] = { 0 };
printf("出勤考查n");
printf("|出勤输入 1 |请假输入 2| 迟到输入 3|n");
//Stu *q=student.elem;
for (i = 0; i <= student.length - 1; i++)
{
printf("%s ", student.elem[i].StuName);
scanf("%d", &c);
if (c == 1)
{
ssa[j] = i; j++;
}
if (c == 2)
{
ssb[t] = i; t++;
}
if (c == 3)
{
ssc[count] = i; count++;
}
}
printf("n----出勤人数------:%dn", j);
for (i = 0; i < j; i++)
{
ssd = ssa[i];
Stu a;
a = student.elem[ssd];
printElement(a);
}
printf("n-----请假人数------:%dn", t);
for (i = 0; i < t; i++)
{
ssd = ssb[i];
Stu b;
b = student.elem[ssd];
printElement(b);
}
printf("n-----迟到人数------:%dn", count);
for (i = 0; i < count; i++)
{
ssd = ssc[i];
Stu d;
d = student.elem[ssd];
printElement(d);
}
system("pause");
}
void Modify()//修改
{
char xm[10];
printf("请输入要修改的姓名n");
scanf("%s", &xm);
int i = 1;
Stu *p = student.elem;
while (i <= student.length)
{
if (strcmp(p->StuName, xm) == 0)
{
printf("找到姓名为%s当前信息为:n", xm);
printElement(*p);
printf("请输入要修改的序号:n");
scanf("%d", &(p->Stuxh));
printf("请输入要修改的学号:n");
scanf("%d", &(p->StuNum));
printf("请输入要修改的姓名:n");
scanf("%s", &(p->StuName));
printf("修改后的信息为:n");
printElement(*p);
}
i++;
p++;
}
if (i >= student.length)
{
printf("没有找到姓名为%s的学生n", xm);
}
system("pause");
}
void Delete()//删除
{
char del[10];
printf("请输入要删除的姓名n");
scanf("%s", &del);
int i = 1;
Stu *p = student.elem;
while (i <= student.length)
{
if (strcmp(p->StuName, del) == 0)
break;
i++;
p++;
}
if (i <= student.length)
{
Stu delxm;
ListDelete(student, i, delxm);
printf("所删除的信息为:n");
printElement(delxm);
}
else
{
printf("没有找到姓名为%s的学生n", del);
}
system("pause");
}
void query()//查找
{
char que[10];
printf("请输入要查找的姓名n");
scanf("%s", &que);
int i = 1;
Stu *p = student.elem;
while (i <= student.length)
{
if (strcmp(p->StuName, que) == 0)
{
printElement(*p);
}
i++;
p++;
}
system("pause");
}
void Open()//读取
{
FILE* fp = fopen("data.txt", "r");
if (!fp)
{
printf("打开文件%s失败");
return;
}
ClearList(student);
int nNum;
fscanf(fp, "%dn", &nNum);
Stu e;
int i = 1;
while (i <= nNum)
{
fscanf(fp, "%d %d %sn", &e.Stuxh, &e.StuNum, &e.StuName);
ListInsert(student, i, e);
i++;
}
fclose(fp);
}
void Save()//保存
{
system("cls");
FILE* fp = fopen("data.txt", "w+");
if (!fp)
{
printf("打开文件%s失败");
return;
}
fprintf(fp, "%dn", student.length);
int i = 1;
int len = ListLength(student);
while (i <= len)
{
Stu e;
e = student.elem[i - 1];
fprintf(fp, "%d %d %s n", e.Stuxh, e.StuNum, e.StuName);
i++;
}
fclose(fp);
}
void dianname()//点名
{
system("cls");
FILE* fp = fopen("data.txt", "r");
if (!fp)
{
printf("打开文件%s失败");
return;
}
ClearList(student);
int nNum;
fscanf(fp, "%dn", &nNum);
Stu e;
int i = 1;
while (i <= nNum)
{
fscanf(fp, "%d %d %sn", &e.Stuxh, &e.StuNum, &e.StuName);
ListInsert(student, i, e);
i++;
}
if (student.length == 0)
{
printf("文件中不存在数据!");
return;
}
fclose(fp);
int j = 1;
int t = 1;
int n;
int ss[26] = { 0 };
int ssx[26] = { 0 };
printf("输入你想点名的人数:");
scanf("%d", &n);
if (n > nNum)
{
printf("超出范围!n");
return;
}
srand((unsigned)time(NULL));
for (j = 0; j < n; j++)//while(j<n)
{
ss[j] = rand() % (student.length);
for (i = 0; i < j; i++)
if (ss[i] == ss[j])
j--;
}
int cx = 0;
while (cx < j)
{
Stu a;
a = student.elem[ss[cx]];
printElement(a);
cx++;
}
system("pause");
}
void paixu()//排序
{
system("cls");
FILE* fp = fopen("data.txt", "r");
if (!fp)
{
printf("打开文件%s失败");
return;
}
ClearList(student);
int nNum;
fscanf(fp, "%dn", &nNum);
Stu e;
int i = 1;
while (i <= nNum)
{
fscanf(fp, "%d %d %sn", &e.Stuxh, &e.StuNum, &e.StuName);
ListInsert(student, i, e);
i++;
}
if (student.length == 0)
{
printf("文件中不存在数据!");
return;
}
int t;
fclose(fp);
for (i = 1; i < student.length; i++)
for (int j = 1; j < student.length; j++)
if (student.elem[j - 1].Stuxh > student.elem[j].Stuxh)
{
t = student.elem[j - 1].Stuxh;
student.elem[j - 1].Stuxh = student.elem[j].Stuxh;
student.elem[j].Stuxh = t;
}
ListTraverse(student, printElement);
system("pause");
}
void DianName()//操作
{
InitList(student);
Open();//读取txt文件中的内容
while (1)
{
Show();
char c;
fflush(stdin);
c = getchar();
switch (c)
{
case 'N':
case 'n':
Add();
Save();
system("cls");
break;
case 'D':
case 'd':
Delete();
Save();
system("cls");
break;
case 'M':
case 'm':
Modify();
Save();
system("cls");
break;
case 'F':
case 'f':
dianname();
system("cls");
break;
case 'H':
case 'h':
ListTraverse(student, printElement);
system("pause");
system("cls");
break;
case 'Z':
case 'z':
zAdd();
Save();
system("cls");
break;
case 'k':
case 'K':
kaocha();
system("cls");
break;
case 'q':
case 'Q':
query();
system("cls");
break;
case 'P':
case 'p':
paixu();
system("cls");
break;
case 'E':
case 'e':
return;
system("cls");
break;
default:
system("cls");
break;
}
}
DestroyList(student);
system("pause");
}
int main()
{
DianName();
return 0;
}
最后
以上就是花痴树叶为你收集整理的c语言随机点名系统的全部内容,希望文章能够帮你解决c语言随机点名系统所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复