#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
//首先保证自己选择的数据结构(链表)的实现没问题
struct student
{
char name[20];
char num[20];
int math;
int english;
int py;
int sum; //总分
int average; //平均分
};
//所有关于数据的地方都要修改
struct Node
{
struct student data;
struct Node* next;
};
struct Node* list;
//创建表头-->创建结构体变量
struct Node* createHead()
{
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
assert(headNode); //断言
headNode->next = NULL;
return headNode;
}
struct Node* createNode(struct student data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
assert(newNode);
//->
newNode->data = data;
(*newNode).next = NULL;
return newNode;
}
void insertData(struct Node* headNode, struct student data)
{
struct Node* newNode = createNode(data);
newNode->next = headNode->next;
headNode->next = newNode;
}
void printList(struct Node* headNode)
{
struct Node* pmove = headNode->next;
printf("姓名\t编号\t数学\t英语\tpy\t总分\t平均分\n");
while (pmove != NULL)
{
printf("%s\t%s\t%d\t%d\t%d\t%d\t%d\n", pmove->data.name,
pmove->data.num, pmove->data.math, pmove->data.english, pmove->data.py,
pmove->data.sum, pmove->data.average);
pmove = pmove->next;
}
}
void deleteDataByName(struct Node* headNode, const char* name)
{
struct Node* preNode = headNode;
struct Node* curNode = headNode->next;
while (curNode != NULL && strcmp(curNode->data.name,name))
{
preNode = curNode;
curNode = preNode->next;
}
if (curNode==NULL)
{
printf("删除失败!,没有找到指定数据!\n");
}
else
{
preNode->next = curNode->next;
free(curNode);
}
}
struct Node* searchDataByName(struct Node* headNode, const char* name)
{
struct Node* pmove = headNode->next;
while (pmove != NULL && strcmp(pmove->data.name,name))
{
pmove = pmove->next;
}
return pmove;
}
//管理系统文件操作
//写文件
void saveToFile(struct Node* headNode, const char* fileName)
{
struct Node* pmove = headNode->next;
FILE* fp = fopen(fileName, "w");
while (pmove != NULL)
{
fprintf(fp, "%s\t%s\t%d\t%d\t%d\t%d\t%d\n", pmove->data.name,
pmove->data.num, pmove->data.math, pmove->data.english, pmove->data.py,
pmove->data.sum, pmove->data.average);
pmove = pmove->next;
}
fclose(fp);
}
//读文件
void readFromFile(struct Node* headNode, const char* fileName)
{
FILE* fp = fopen(fileName, "r");
if (fp == NULL)
{
fp = fopen(fileName, "w+");
fclose(fp);
}
struct student temp;
while (fscanf(fp, "%s\t%s\t%d\t%d\t%d\t%d\t%d\n",
temp.name,
temp.num,
&temp.math,
&temp.english,
&temp.py,
&temp.sum,
&temp.average) != EOF)
{
insertData(list, temp);
}
fclose(fp);
}
void makeMenu() //制作菜单
{
//排序+文件
printf("---------【链式学生管理系统】---------\n");
printf("\t\t0.退出功能\n");
printf("\t\t1.录入功能\n");
printf("\t\t2.浏览功能\n");
printf("\t\t3.查找功能\n");
printf("\t\t4.修改功能\n");
printf("\t\t5.删除功能\n");
printf("-------------------------------------\n");
printf("请输入(0~5):");
}
void keyDown()
{
int userkey = 0;
struct student temp; //接受用户的输入
struct Node* result;
scanf_s("%d", &userkey);
switch (userkey)
{
case 0:
printf("----【退出模块】----\n");
system("pause");
exit(0);
break;
case 1:
printf("----【录入模块】----\n");
printf("请输入学生信息(name,num,math,english,py):");
scanf_s("%s%s%d%d%d", temp.name, 20, temp.num, 20, &temp.math, &temp.english, &temp.py);
//手动处理:
temp.sum = temp.math + temp.english + temp.py;
temp.average = temp.sum / 3;
insertData(list, temp);
saveToFile(list, "student.txt");
break;
case 2:
printf("----【浏览功能】----\n");
printList(list);
break;
case 3:
printf("----【查找功能】----\n");
printf("请输入要查找学生姓名:");
scanf_s("%s", temp.name,20); //vs 增强版的函数,20 代表长度
result = searchDataByName(list, temp.name);
if (result == NULL)
{
printf("未找到结果!\n");
}
else
{
printf("姓名\t编号\t数学\t英语\tpy\t总分\t平均分\n");
printf("%s\t%s\t%d\t%d\t%d\t%d\t%d\n",
result->data.name,
result->data.num,
result->data.math,
result->data.english,
result->data.py,
result->data.sum,
result->data.average);
}
break;
case 4:
printf("----【修改功能】----\n");
printf("请输入要修改学生姓名:");
scanf_s("%s", temp.name, 20); //vs 增强版的函数,20 代表长度
result = searchDataByName(list, temp.name);
if (result == NULL)
{
printf("未找到结果!\n");
}
else
{
printf("请输入学生新的信息(name,num,math,english,py):");
scanf_s("%s%s%d%d%d", result->data.name, 20, result->data.num, 20, &result->data.math, &result->data.english, &result->data.py);
//手动处理:
result->data.sum = result->data.math + result->data.english + result->data.py;
result->data.average = result->data.sum / 3;
saveToFile(list, "student.txt");
}
break;
case 5:
printf("----【删除功能】----\n");
printf("请输入要删除学生姓名:");
scanf_s("%s", temp.name, 20); //vs 增强版的函数,20 代表长度
deleteDataByName(list, temp.name);
saveToFile(list, "student.txt");
break;
default:
printf("输入错误!,请重新输入...!\n");
break;
}
}
int main()
{
list = createHead();
readFromFile(list, "student.txt");
while (1)
{
makeMenu();
keyDown();
system("pause"); //按任意键继续
system("cls"); //清屏
}
return 0;
}
/*
C/C++ 一定编程基础
做业务的技术
框架:Qt
通用性技术
1.数据库编程
2.网络编程
3.多线程编程
*/
/*
做外挂,做辅助工具
逆向: 汇编-->反汇编工具,内存,网络
C/C++--->内存
抓包-解包
*/
|