69,371
社区成员
发帖
与我相关
我的任务
分享
#include <time.h>
#include <stdlib.h>
#include <windows.h>
int main() {
int a,b[11];//本来是b[10],为判断哪句越界,故意声明为b[11]
srand((unsigned int)time(NULL));//按两次F11,等黄色右箭头指向本行时,调试、新建断点、新建数据断点,地址:&b[10],字节计数:4,确定。
while (1) {//按F5,会停在下面某句,此时a的值为10,b[10]已经被修改为对应0..4之一。
b[(a=rand()%11)]=0;
Sleep(100);
b[(a=rand()%11)]=1;
Sleep(100);
b[(a=rand()%11)]=2;
Sleep(100);
b[(a=rand()%11)]=3;
Sleep(100);
b[(a=rand()%11)]=4;
Sleep(100);
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#pragma warning(disable:4996)
//management 管理、人员管理系统;
#if 0
1、初始化数据库,此时的数据库是文件;(initData2File)
2、读数据库,生成内存数据模型;(createListFromFile)
3、增、查、改、删、排序;
4、更新数据库; saveList2File(保存链表文件) destroyListStu(删除链表)
#endif
typedef struct student
{
char name[30]; //姓名
char sex; //性别
int age; //年龄
int score; //成绩
}Stu;
typedef struct stuNode
{
Stu data;
struct stuNode *next;
}StuNode;
void initData2File(const char *);
StuNode *createListFromFile(const char *filePash); //创建链表,把数据传进来
void traverseStulist(StuNode *head);
void addListStu(StuNode *phead); //增
StuNode *searchListStu(StuNode *head); //查
void deleteListStu(StuNode *head); //删除
void sortListStu(StuNode *head); //排序(按成绩排序)
int lenList(StuNode *head); //求链表长度
void saveList2File(StuNode *head, const char *filePath);
void destroyListStu(StuNode *head);
int main()
{
initData2File("student.data");
StuNode *head = createListFromFile("student.data");
traverseStulist(head);
while (1)
{
system("cls");
traverseStulist(head);
printf("1->add\t 2->search 3->delete 4->sort 5->exit\n");
printf("请输入序号:");
int dix;
scanf("%d", &dix);
StuNode *Phead;
switch (dix)
{
case 1:
addListStu(head);
break;
case 2:
if ((Phead = searchListStu(head)))
{
printf("您要查找的人的信息在本数据库中:\n");
printf("name\t\t\tsex\t\tage\t\tscore\n");
printf("%-10s\t\t%c\t\t%d\t\t%d\n",
Phead->data.name, Phead->data.sex, Phead->data.age, Phead->data.score);
printf("------------------------------\n\n");
}
else
printf("查无此人!\n");
break;
case 3:
deleteListStu(head);
break;
case 4:
sortListStu(head);
break;
case 5:
saveList2File(head,"student.data");
destroyListStu(head);
return 0;
default:
printf("您输入错误!\n");
}
}
return 0;
}
void initData2File(const char *file_path) //1、初始化数据库,此时的数据库是文件;
{
Stu s[4] =
{
{"sunwukong",'x',500,90},
{"wuneng",'x',800,59 },
{"wujing",'m',700,80},
{"tangseng",'m',24,100}
};
FILE *pf = fopen(file_path, "w+");
if (NULL == pf)
exit(-1);
fwrite((char *)s, sizeof(s), 1, pf);
fclose(pf); //关闭文件
}
StuNode *createListFromFile(const char *filePash) //2、读数据库,生成内存数据模型;(createListFromFile)把文件传进来
{
FILE *pf = fopen(filePash, "r+");
if (pf == NULL)
exit(-1);
StuNode *head = (StuNode *)malloc(sizeof(StuNode)); // 创建头结点
if (NULL == head)
exit(-1);
head->next = NULL;
StuNode *cur = (StuNode *)malloc(sizeof(StuNode));
if (!cur)
exit(-1);
while (fread((void *)&cur->data, sizeof(Stu), 1, pf))
{
cur->next = head->next;
head->next = cur;
cur = (StuNode *)malloc(sizeof(StuNode));
if (!cur)
exit(-1);
}
free(cur);
//free(pf);
fclose(pf);
return head;
}
void traverseStulist(StuNode * head)
{
StuNode *p = head;
while (p) {
printf("%-10s\t\t%c\t\t%d\t\t%d\n", p->data.name, p->data.sex, p->data.age, p->data.score);
p = p->next;
}
/*
head = head->next;
printf("name\t\t\tsex\t\tage\t\tscore\n");
while (head)
{
printf("%-10s\t\t%c\t\t%d\t\t%d\n", head->data.name, head->data.sex, head->data.age, head->data.score);
head = head->next;
}
*/
}
void addListStu(StuNode * head) //增加数据
{
StuNode *cur = (StuNode*)malloc(sizeof(StuNode));
if (NULL == cur)
exit(-1);
cur->next = NULL;
system("cls");
printf("name :");
scanf("%s", cur->data.name);
getchar();
printf("sex :");
scanf("%c", &cur->data.sex);
printf("age :");
scanf("%d", &cur->data.age);
printf("score :");
scanf("%d", &cur->data.score);
getchar();
cur->next = head->next;
head->next = cur;
cur = (StuNode*)malloc(sizeof(StuNode));
}
StuNode *searchListStu(StuNode * head) //查
{
StuNode *p = head->next;
char Name[20];
scanf("%s", Name);
while (p) {
if (strcmp(Name, p->data.name) == 0)
return p;
p = p->next;
}
return NULL;
/*
printf("请输入您要查找的人名:");
char Name[20];
scanf("%s", Name);
head = head->next;
while (head)
{
if (strcmp(Name, head->data.name) == 0)
break;
else
head = head->next;
}
*/
return head;
}
void deleteListStu(StuNode * head)
{
StuNode *pcur, *prev;
StuNode *Dhead = searchListStu(head);
int Index;
if (!Dhead) {
printf("查无此人!\n");
return;
}
//if (Dhead)
//{
printf("您要查找的人的信息在本数据库中:\n");
printf("name\t\t\tsex\t\tage\t\tscore\n");
printf("%-10s\t\t%c\t\t%d\t\t%d\n",
Dhead->data.name, Dhead->data.sex, Dhead->data.age, Dhead->data.score);
printf("------------------------------\n\n");
printf("继续删除请按 1 ,取消请按 2 :");
scanf("%d", &Index);
while (1)
{
if (Index == 1)
{
prev = head;
pcur = head->next;
while (pcur != Dhead) {
prev = prev->next;
pcur = pcur->next;
}
if (pcur == Dhead) {
prev->next = pcur->next;
free(pcur);
break;
}
/*
//head = head->next;
while (Dhead != head->next)
{
head = head->next;
}
cur = Dhead;
head->next = Dhead->next;
free(Dhead);
return;
*/
}
else if (Index == 2)
break;
else
{
printf("您输入有误!请重新输入!\n");
printf("继续删除请按 1 ,取消请按 2 :");
scanf("%d", &Index);
continue;
}
}
//}
//else
// printf("查无此人!\n");
}
int lenList(StuNode * head)
{
int len = 0;
StuNode *p = head->next;
while (p) {
len++;
p = p->next;
}
/*
head = head->next;
while (head)
{
len++;
head = head->next;
}
*/
return len;
}
void sortListStu(StuNode * head)
{
StuNode *cur;
StuNode *p;
StuNode *q;
//StuNode *t;
int len = lenList(head);
for (int i = 0; i < len - 1; i++)
{
cur = head;
p = cur->next;
q = p->next;
for (int j = 0; j < len - 1- i; j++)
{
if (p->data.score > q->data.score)
{
cur->next = q;
p->next = q->next;
q->next = p;
cur = q;
q = p->next;
continue;
//t = q;
//q = p;
//p = t;
}
cur = cur->next;
p = cur->next;
q = p->next;
}
}
}
void saveList2File(StuNode * head, const char *filePath) //保存到文件中
{
StuNode *p = head->next;
FILE *pfw = fopen(filePath, "w+");
if (NULL == pfw)
exit(-1);
while (p) {
fwrite((void *)&p->data, sizeof(Stu), 1, pfw);
p = p->next;
}
/*
while (head)
{
fwrite((void *)&head->data, sizeof(Stu), 1, pfw);
head = head->next;
}
*/
fclose(pfw);
}
void destroyListStu(StuNode * head) //销毁链表
{
StuNode *cur;
while (head)
{
cur = head;
head = head->next;
free(cur);
}
}