日报表
| 项目名称 | 【苏嵌实训-嵌入式 linux C 第 8 天】 |
|---|
| 今日进度以及任务 | 第八次作业 |
| 本日任务完成情况(详细说明本日任务是否按计划完成,开发的代码量) | 完成四项作业 |
| 本日开发中出现的问题汇总 | 无 |
| 本日未解决问题 | 无 |
| 本日开发收获 | 进一步了解了C的开发规划 |
| 其他 | 无 |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX sizeof(char) * 20
enum RESULT
{
RANK_FAIL,
RANK_SUCCESS,
Reverse_fail,
Reverse_success,
Search_fail,
Search_success
};
struct students
{
char * name;
char sex;
int age;
struct students *next;
};
typedef struct students Student;
typedef Student * STU;
void init_head(STU *head)
{
*head = (STU)malloc(sizeof(Student));
(*head)->next = NULL;
}
void insert_tail(STU newstudent,STU *head)
{
STU temp = *head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = newstudent;
newstudent->next = NULL;
}
int rank_age(STU *head)
{
int i;
int j;
int n;
int count = 0;
STU p = *head;
STU s = p->next;
STU t = s->next;
#if 1
if(((*head)->next == NULL) || ((*head)->next->next == NULL))
{
return RANK_FAIL;
}
else
{
#endif
STU temp = (*head)->next;
while(temp != NULL)
{
count++;
temp = temp->next;
}
n = count - 1;
for(i = 0;i < n;i++)
{
for(j = 0;j < (n - i);j++)
{
if(s->age > t->age)
{
p->next = t;
s->next = t->next;
t->next = s;
s = t;
t = p->next->next;
}
t = t->next;
s = s->next;
p = p->next;
}
p = *head;
s = p->next;
t = s->next;
}
return RANK_SUCCESS;
}
}
#if 1
int reverse(STU *head)
{
if(((*head)->next == NULL) || ((*head)->next->next == NULL))
{
return Reverse_fail;
}
else
{
STU p = (*head)->next;
STU s = p->next;
STU t = s->next;
while(t != NULL)
{
s->next = p;
p = s;
s = t;
t = t->next;
}
s->next = p;
(*head)->next->next = NULL;
(*head)->next = s;
return Reverse_success;
}
}
#endif
int search_age(STU *head,STU *head1,int age)
{
int min;
int MIN = 100;
STU temp = (*head)->next;
STU t = (*head)->next;
if((*head)->next == NULL)
{
return Search_fail;
}
else
{
while(temp != NULL)
{
if(((temp->age) - age) >= 0)
{
min = (temp->age) - age;
}
else
{
min = age - (temp->age);
}
if(min <= MIN)
{
MIN = min;
}
temp = temp->next;
}
printf("与所查年龄相差 %d 岁\n",MIN);
while(t != NULL)
{
if(((t->age) - age) >= 0)
{
min = (t->age) - age;
}
else
{
min = age - (t->age);
}
if(min == MIN)
{
STU find_stu = (STU)malloc(sizeof(Student));
find_stu->name = t->name;
find_stu->sex = t->sex;
find_stu->age = t->age;
insert_tail(find_stu,&(*head1));
}
t = t->next;
}
return Search_success;
}
}
void display(STU head,char sex)
{
STU temp = head->next;
printf(" 姓名 性别 年龄 \n");
if(sex == 'a')
{
while(temp != NULL)
{
printf("%10s",temp->name);
printf("%10c",temp->sex);
printf("%10d\n",temp->age);
temp = temp->next;
}
}
else
{
while(temp != NULL)
{
if(temp->sex == sex)
{
printf("%10s",temp->name);
printf("%10c",temp->sex);
printf("%10d\n",temp->age);
}
temp = temp->next;
}
}
}
int main()
{
int i;
int age;
int num;
int function;
int count;
int rank_result;
int reverse_result;
int search_result;
STU head;
init_head(&head);
STU head1;
init_head(&head1);
#if 1
STU stu1 = (STU)malloc(sizeof(Student));
stu1->name = "ZhangYL";
stu1->sex = 'g';
stu1->age = 20;
insert_tail(stu1,&head);
STU stu2 = (STU)malloc(sizeof(Student));
stu2->name = "xiaoZhang";
stu2->sex = 'b';
stu2->age = 18;
insert_tail(stu2,&head);
STU stu3 = (STU)malloc(sizeof(Student));
stu3->name = "xiaoWang";
stu3->sex = 'g';
stu3->age = 16;
insert_tail(stu3,&head);
STU stu4 = (STU)malloc(sizeof(Student));
stu4->name = "XiaoHong";
stu4->sex = 'g';
stu4->age = 30;
insert_tail(stu4,&head);
STU stu5 = (STU)malloc(sizeof(Student));
stu5->name = "XiaoMing";
stu5->sex = 'b';
stu5->age = 24;
insert_tail(stu5,&head);
STU stu6 = (STU)malloc(sizeof(Student));
stu6->name = "YUN";
stu6->sex = 'b';
stu6->age = 36;
insert_tail(stu6,&head);
#endif
printf("\033[2J");
while(1)
{
printf("*********学生管理系统**********\n");
printf("** 创建男生链表(1) **\n");
printf("** 创建女生链表(2) **\n");
printf("** 按年龄排序(3) **\n");
printf("** 按年龄逆序(4) **\n");
printf("** 查找年龄学生(5) **\n");
printf("** 退出(0) **\n");
printf("*******************************\n");
printf("\n");
printf("请输入你想要的操作\n");
scanf("%d",&function);
switch(function)
{
case 1:
{
printf("\033[2J");
printf("您需要输入几个男学生的信息?\n");
scanf("%d",&num);
for(i = 0;i < num;i++)
{
STU newstudent = (STU)malloc(sizeof(Student));
newstudent->sex = 'b';
printf("请输入学生 %d 姓名\n",i + 1);
newstudent->name = (char *)malloc(MAX);
scanf("%s",(newstudent->name));
printf("请输入学生 %d 年龄\n",i + 1);
scanf("%d",&(newstudent->age));
insert_tail(newstudent,&head);
}
printf("\033[2J");
printf("/**********男生信息**********/\n");
printf("\n");
display(head,'b');
printf("\n");
printf("\n");
printf("\n");
break;
}
case 2:
{
printf("\033[2J");
printf("您需要输入几个女学生的信息?\n");
scanf("%d",&num);
for(i = 0;i < num;i++)
{
STU newstudent = (STU)malloc(sizeof(Student));
newstudent->sex = 'g';
printf("请输入学生 %d 姓名\n",i + 1);
newstudent->name = (char *)malloc(sizeof(char) * 20);
scanf("%s",newstudent->name);
printf("请输入学生 %d 年龄\n",i + 1);
scanf("%d",&(newstudent->age));
insert_tail(newstudent,&head);
}
printf("\033[2J");
printf("/**********女生信息**********/\n");
printf("\n");
display(head,'g');
printf("\n");
printf("\n");
printf("\n");
break;
}
case 3:
{
printf("\033[2J");
printf("/*******排序结果为(由大到小)*******/\n");
printf("\n");
rank_result = rank_age(&head);
if(rank_result == RANK_FAIL)
{
printf("只有一个学生或者没有学生;故无法排序!\n");
}
if(rank_result == RANK_SUCCESS)
{
display(head,'a');
}
printf("\n");
printf("\n");
printf("\n");
break;
}
case 4:
{
printf("\033[2J");
printf("/**********逆序结果**********/\n");
printf("\n");
reverse_result = reverse(&head);
if(reverse_result == Reverse_fail)
{
printf("只有一个学生或者没有学生数据;无法逆序!\n");
}
if(reverse_result == Reverse_success)
{
display(head,'a');
}
printf("\n");
printf("\n");
printf("\n");
break;
}
case 5:
{
printf("\033[2J");
printf("请输入要找的年龄:\n");
scanf("%d",&age);
printf("/**********查找结果**********/\n");
printf("\n");
search_result = search_age(&head,&head1,age);
if(search_result == Search_fail)
{
printf("没有学生数据,无法查找!\n");
}
printf("\n");
if(search_result == Search_success)
{
display(head1,'a');
}
head1->next = NULL;
printf("\n");
printf("\n");
printf("\n");
break;
}
case 0:
{
printf("\033[2J");
printf("已成功退出!使用愉快!\n");
return 0;
}
default:
{
printf("\033[2J");
printf("输入错误,清重新输入!\n");
printf("\n");
printf("\n");
printf("\n");
break;
}
}
}
return 0;
}