Files
sth/keshe.cpp
2021-12-05 04:38:45 +00:00

305 lines
7.4 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
typedef struct student {
char name[20]; //姓名
char age[10]; //年龄
char sex[10]; //性别
char num[20]; //学号
char add[40]; //住址
int score; //成绩
struct student* next;
} stu;
void welcome()
{
printf("****************【Student Manager】*************\n");
printf("* \n");
printf("* \n");
printf("* 1.New \n");
printf("* 2.Del \n");
printf("* 3.Edit \n");
printf("* 4.Show All \n");
printf("* 5.Search \n");
printf("* 6.Save \n");
printf("* 7.Sort \n");
printf("* 8.Read \n");
printf("* 9.Exit \n");
printf("* \n");
printf("* \n");
printf("***********************************************\n");
}
void tuichu()
{
printf("\nSee you next time!\n");
exit(0);
}
void creat(stu* head)
{
stu* p = head;
stu* p1 = (student*)malloc(sizeof(student));
printf("\nAdding info\n");
printf("Name:\n");
scanf("%s", p1->name);
printf("Sex:\n");
scanf("%s", p1->sex);
printf("Age:\n");
scanf("%s", p1->age);
printf("Id:\n");
scanf("%s", p1->num);
printf("Score\n");
scanf("%d", &p1->score);
printf("Address\n");
scanf("%s", p1->add);
while (p->next != NULL) {
p = p->next;
if (strcmp(p1->num, p->num) == 0) {
printf("Student already exist\n");
system("pause");
free(p1);
return;
}
}
p1->next = NULL;
p->next = p1;
printf("\nDone\n");
system("pause");
}
void del(student* head)
{
stu *p, *p1;
p = head;
p1 = p->next;
char arr[20];
printf("input name:\n");
scanf("%s", arr);
while (p1 != NULL) {
while (strcmp(arr, p1->name) == 0) {
p->next = p1->next;
free(p1);
printf("Done\n");
return;
}
}
if (p1 == 0 || strcmp(arr, p1->name) != 0) {
printf("Can not find\n");
}
}
void look(stu* head)
{
stu* p;
p = head;
p = head->next;
if (p != NULL) {
while (p != NULL) {
printf("");
printf("Name%s\t\t", p->name);
printf("Age%s\t\t", p->age);
printf("Sex%s\t\t", p->sex);
printf("Id%s\t\t", p->num);
printf("Score%d\t\t", p->score);
printf("Address%s\t\t\n", p->add);
p = p->next;
//printf("That's All\n");
}
} else {
printf("No info\n");
}
}
void search(student* head)
{
stu* p = head;
char i[20];
printf("Input Name:\n");
scanf("%s", i);
while (p->next != NULL) {
while (strcmp(p->next->name, i) == 0) {
printf("Info:\n");
printf("Name:%s\n", p->next->name);
printf("Age:%s\n", p->next->age);
printf("Sex:%s\n", p->next->sex);
printf("Id:%s\n", p->next->num);
printf("Score:%d\n", p->next->score);
printf("Address:%s\n\n", p->next->add);
printf("\n");
return;
}
p = p->next;
if (p->next == NULL) {
printf("Can not find\n");
}
}
}
void save(student* head)
{
FILE* fp;
fp = fopen("D:\\student.txt", "a+");
stu* p = head;
p = p->next;
if (fp != NULL) {
while (p != NULL) {
fprintf(fp, "Name: %s ", p->name);
fprintf(fp, "Age: %s ", p->age);
fprintf(fp, "Sex: %s ", p->sex);
fprintf(fp, "Id: %s ", p->num);
fprintf(fp, "Address: %s ", p->add);
fprintf(fp, "Score: %d\n", p->score);
p = p->next;
}
fclose(fp);
printf("Saved to D\n");
}
system("pause");
}
void change(stu* head)
{
stu* p = head->next;
char i[10];
printf("input name:\n");
scanf("%s", &i);
while (p != NULL) {
while (strcmp(p->name, i) == 0) {
printf("New Name:\n");
scanf("%s", &p->name);
printf("New Age:\n");
scanf("%s", &p->age);
printf("New Sex:\n");
scanf("%s", &p->sex);
printf("New Id:\n");
scanf("%s", &p->num);
printf("New Address:\n");
scanf("%s", &p->add);
printf("New Score\n");
scanf("%d", &p->score);
return;
}
p = NULL;
}
printf("Can not find\n");
}
void paixu(student* head)
{
struct student *p, *p1, *t;
p = head;
p1 = head->next;
system("cls");
while (p->next != NULL) {
while (p1->next != NULL) {
if (p->next->score < p1->next->score) {
t = p->next;
p->next = p1->next;
p1->next = p1->next->next;
p->next->next = t;
} else {
p1 = p1->next;
}
}
p = p->next;
p1 = p->next;
}
p = head->next;
printf("Sorted:\n");
while (p) {
printf("Name: %s ", p->name);
printf("Age: %s ", p->age);
printf("Sex: %s ", p->sex);
printf("Id: %s ", p->num);
printf("Address: %s ", p->add);
printf("Score: %d\n", p->score);
p = p->next;
}
}
void duqu(stu* head)
{
FILE* fp;
char ch;
fp = fopen("D:\\student.txt", "r");
if (fp == NULL) {
printf("blank!\n");
} else {
ch = fgetc(fp);
while (!feof(fp)) {
printf("%c", ch);
ch = fgetc(fp);
}
printf("\n");
}
system("pause");
fclose(fp);
}
void ps(char* pStr, int Len)
{
int lSpaceNum = (Len - strlen(pStr)) / 2;
printf("%*s\n", lSpaceNum + strlen(pStr), pStr);
}
int main()
{
struct student stu[100];
student* head = (student*)malloc(sizeof(student));
head->next = NULL;
while (1) {
system("cls");
welcome();
printf("Your choice:\n");
int a;
scanf("%d", &a);
switch (a) {
case 1:
creat(head);
break;
case 2:
del(head);
system("pause");
break;
case 3:
change(head);
system("pause");
break;
case 4:
look(head);
system("pause");
break;
case 5:
search(head);
system("pause");
break;
case 6:
save(head);
break;
case 7:
paixu(head);
system("pause");
break;
case 8:
duqu(head);
break;
case 9:
tuichu();
break;
}
}
return 0;
}