C语言编程,高手请进,谢谢

2024-10-29 03:35:14
推荐回答(5个)
回答1:

#include
#include
#include
#include
#define NULL 0
#define LEN sizeof(struct student)
#define FILE_DATA_PATH "c:\student.txt"

struct student
{
long int num;
char name[20];
int age;
char sex[4];
char birthday[10];
char address[30];
long int tele_num;
char E_mail[20];
struct student *next;
};

int TOTAL_NUM = 0;
struct student *head = NULL;

void welcome();
void mainmenu();
void record();
void insert(struct student *stu);
void display(struct student *stu);
void displayAll();
void query();
void query_by_num();
void query_by_name();
void readData();
void writeData();
void freeAll();
void del();
void change();
void devise(struct student *p);

int main()
{
char userName[9];
char userPWD[7];
int i;
welcome();
for(i = 0; i < 3; i++)
{
printf("\n管理员初始用户名和密码均为admin\n");
printf("请输入您的用户名:");
scanf("%s", userName);

printf("\n请输入您的密码:");
scanf("%s", userPWD);

if ((strcmp(userName,"admin")==0) && (strcmp(userPWD,"admin")==0))
{
//用户名和密码正确,显示主菜单
mainmenu();
break;
}
else
{
if (i < 2)
{
//用户名或密码错误,提示用户重新输入
printf("用户名或密码错误,请重新输入!");
}
else
{
//连续3次输错用户名或密码,退出系统。
printf("您已连续3次将用户名或密码输错,系统将退出!");
}
}
}
return 0;
}
//显示欢迎信息
void welcome()
{
printf("+---------------------------+\n");
printf("| |\n");
printf("| 欢迎使用学生信息管理系统 |\n");
printf("| |\n");
printf("+---------------------------+\n");
}

//系统主菜单
void mainmenu()
{
int choice;
choice = -1;
readData();
printf("\n温馨提示:为保证您的操作得到保存,请按正常顺序退出系统^_^\n");
do
{
printf("\n\n\n");
printf("\t\t\t--------------------------------\n");
printf("\t\t\t+ 学生信息管理系统 |\n");
printf("\t\t\t--------------------------------\n");
printf("\t\t\t+ [1]----录入学生信息 |\n");
printf("\t\t\t+ [2]----浏览学生信息 |\n");
printf("\t\t\t+ [3]----查询学生信息 |\n");
printf("\t\t\t+ [4]----删除学生信息 |\n");
printf("\t\t\t+ [5]----修改学生信息 |\n");
printf("\t\t\t+ [0]----退出系统 |\n");
printf("\t\t\t+*·*·*·*·*·*·*·*·*·*·|\n");
printf("\t\t\t--------------------------------\n");
printf("请输入您的选择:");
scanf("%d", &choice);
switch(choice)
{
case 0:
writeData();
freeAll();
exit(0);
case 1:
record();
break;
case 2:
displayAll();
break;
case 3:
query();
break;
case 4:
del();
break;
case 5:
change();
break;
default:
printf("\n无效选项!");
break;
}
}
while(choice != 0);
}
//录入学生信息
void record()
{
struct student *p0;

p0 = (struct student *)malloc(LEN);
printf("请输入学生的学号:");
scanf("%ld",&p0->num);
printf("请输入学生的姓名:");
scanf("%s",p0->name);
printf("请输入学生的年龄:");
scanf("%d",&p0->age);
printf("请输入学生的性别:");
scanf("%s",p0->sex);
printf("请输入学生的出生年月:");
scanf("%s",p0->birthday);
printf("请输入学生的地址:");
scanf("%s",p0->address);
printf("请输入学生的电话:");
scanf("%ld",&p0->tele_num);
printf("请输入学生的E-mail:");
scanf("%s",p0->E_mail);

insert(p0);
printf("该学生的信息为:\n");
printf("-------------------------------------------------------------------------------");
printf("学号\t姓名\t年龄\t性别\t出生年月\t\t地址\t电话\tE-mail\n");
display(p0);
}

void insert(struct student *stu)
{
struct student *p0, *p1, *p2;

p1 = head;
p0 = stu;

if(head == NULL)
{
head = p0;
p0->next = NULL;
}
else
{
while((p0->num > p1->num)&&(p1->next != NULL))
{
p2 = p1;
p1 = p1->next;
}
if(p0->num <= p1->num)
{
if(head == p1)
head = p0;
else
p2->next = p0;
p0->next = p1;
}
else
{
p1->next = p0;
p0->next = NULL;
}
}
TOTAL_NUM++;
}

void display(struct student *p)
{
printf("%ld\t%s\t%d\t%s\t%s\t%s\t%ld\t%s\n", p->num, p->name, p->age, p->sex, p->birthday, p->address, p->tele_num, p->E_mail);
}
//浏览学生信息
void displayAll()
{
struct student *p;

printf("学生总数:%d\n", TOTAL_NUM);
p = head;
if(head != NULL)
{
printf("\n学号\t姓名\t年龄\t性别\t出生年月\t地址\t电话\tE-mail\n");
printf("-------------------------------------------------------------------------------");
do
{
display(p);
p = p->next;
}
while(p != NULL);
}
printf("\n");
}
//查询学生信息
void query()
{
int choice;
choice = -1;
do
{
printf("\n");
printf("+--------------------+\n");
printf("| 按学号查询 请按 1 |\n");
printf("| 按姓名查询 请按 2 |\n");
printf("| 取消 请按 0 |\n");
printf("+--------------------+\n");

printf("请输入您的选择:");
scanf("%d", &choice);

switch(choice)
{
case 0:
return;
case 1:
query_by_num();
break;
case 2:
query_by_name();
break;
default:
printf("\n无效选项!");
break;
}
}
while(choice != 0);
}
//按学号查询学生信息
void query_by_num()
{
int num;
struct student *p1;

printf("请输入学生的学号:");
scanf("%ld", &num);
if(head==NULL)
{
printf("无学生记录!\n");
return;
}
p1=head;
while(num!=p1->num && p1->next!=NULL)
p1=p1->next;
if(num == p1->num)
{
printf("\n学号\t姓名\t年龄\t性别\t出生年月\t地址\t电话\tE-mail\n");
printf("-------------------------------------------------------------------------------");
display(p1);
}
else
printf("没有该学生记录,请核对!");
}
//按姓名查询学生信息
void query_by_name()
{
char name[20];
struct student *p1;

printf("请输入学生的姓名:");
scanf("%s", name);
if(head==NULL)
{
printf("无学生记录!\n");
return;
}
p1=head;
while(strcmp(name, p1->name) && p1->next!=NULL)
p1=p1->next;
if(!strcmp(name, p1->name))
{

printf("\n学号\t姓名\t年龄\t性别\t出生年月\t地址\t电话\tE-mail\n");
printf("-------------------------------------------------------------------------------");
display(p1);
}
else
printf("没有该学生记录,请核对!");
}
//写入文件
void writeData()
{
FILE* fp;//文件指针
struct student *p;

fp = fopen(FILE_DATA_PATH, "w");
if (!fp)
{
printf("文件打开错误!");
return;
}

fprintf(fp,"%d\n", TOTAL_NUM);
for(p = head; p!= NULL; p= p->next)
{
fprintf(fp,"%ld\t%s\t%d\t%s\t%s\t%s\t%ld\t%s\n",
p->num, p->name, p->age, p->sex, p->birthday, p->address, p->tele_num, p->E_mail);
}
fclose(fp);
}

void freeAll()
{
struct student *p1, *p2;

p1=p2=head;
while(p1)
{
p2=p1->next;
free(p1);
p1=p2;
}
}
//读取文件
void readData()
{
FILE* fp;//文件指针
struct student *p1, *p2;

fp = fopen(FILE_DATA_PATH, "r");
if (!fp)
{
printf("文件打开错误!");
return 0;
}

fscanf(fp,"%d\n", &TOTAL_NUM);
head = p1 = p2 = (struct student *)malloc(LEN);
fscanf(fp,"%ld\t%s\t%d\t%s\t%s\t%s\t%ld\t%s\n",
&p1->num, p1->name, &p1->age, p1->sex, p1->birthday, p1->address, &p1->tele_num, p1->E_mail);
while(!feof(fp))
{
p1 = (struct student *)malloc(LEN);
fscanf(fp,"%ld\t%s\t%d\t%s\t%s\t%s\t%ld\t%s\n",
&p1->num, p1->name, &p1->age, p1->sex, p1->birthday, p1->address, &p1->tele_num, p1->E_mail);
p2->next = p1;
p2 = p1;
}
p2->next = NULL;
fclose(fp);
}
//删除学生信息
void del()
{
struct student *p1, *p2;
long int num;

if(head == NULL)
{
printf("无学生记录!\n");
return;
}

printf("请输入您要删除的学生的学号:");
scanf("%ld", &num);

p1 = head;

while(num != p1->num && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if(num == p1->num)
{
if(p1 == head)
head = p1->next;
else p2->next = p1->next;
free(p1);
TOTAL_NUM--;
}
else
printf("没有该学生记录,请核对!\n");
}
//修改学生信息
void change()
{
struct student *p1, *p2;
long int num;

if(head == NULL)
{
printf("无学生记录!\n");
return;
}

printf("请输入您要修改的学生的学号:");
scanf("%ld", &num);

p1 = head;

while(num != p1->num && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if(num == p1->num)
devise(p1);
else
printf("没有该学生记录,请核对!\n");

}

void devise(struct student *p)
{
int choice;
choice = -1;
do
{
printf("请选择您要修改的学生的信息内容:\n");
printf("+----------------------+\n");
printf("| 姓名 请按 1 |\n");
printf("| 年龄 请按 2 |\n");
printf("| 性别 请按 3 |\n");
printf("| 出生年月 请按 4 |\n");
printf("| 地址 请按 5 |\n");
printf("| 电话 请按 6 |\n");
printf("| E-mail 请按 7 |\n");
printf("| 取消 请按 0 |\n");
printf("+----------------------+\n");

printf("请输入您的选择:");
scanf("%d", &choice);

switch(choice)
{
case 0:
return;
case 1:
printf("请输入新姓名:");
scanf("%s", p->name);
break;
case 2:
printf("请输入新年龄:");
scanf("%d", &p->age);
break;
case 3:
printf("请输入新性别:");
scanf("%s", p->sex);
break;
case 4:
printf("请输入新出生年月:");
scanf("%s", p->birthday);
break;
case 5:
printf("请输入新地址:");
scanf("%s", p->address);
break;
case 6:
printf("请输入新电话:");
scanf("%ld", &p->tele_num);
break;
case 7:
printf("请输入新E-mail:");
scanf("%s", p->E_mail);
break;
default:
printf("\n无效选项!");
break;
}
}
while(choice != 0);
}

回答2:

/*建立一个通讯录,功能要求,以菜单形式显示
录入资料 (姓名,号码,生日,住址)
查找资料
修改资料
注销记录
浏览...
保存文件
读取文件
退出
*/

#include
#include
#include
#include

struct my_friend
{
char name[50];
char address[50];
char tel[13];
int age;
}friends[32];

void page_title(char *menu_item)
{
system("cls");
printf(">>>> 通 讯 录 <<<<<\n\n %s \n\n",menu_item);
}

void return_confirm(void)
{
printf("\n按任意键返回....");
getch();
}

void friends_new(void)
{
int n;
page_title("^_^ 录入基本资料 ^_^ ");
for(n=0;n<32;n++)

if(friends[n].age==0) break;

printf("姓名:");
scanf("%s",&friends[n].name);
printf("电话:");
scanf("%s",&friends[n].tel);
printf("年龄:");
scanf("%d",&friends[n].age);
printf("住址:");
scanf("%s",&friends[n].address);
return_confirm();

}

int search_name(void)
{
int n;
char c[50];
page_title("^_^ 查找 ^_^");
printf("请输入要查找的人名:");
scanf("%s",&c);
for(n=0;n<32;n++)
{

if(strcmp(friends[n].name,c)==0)
{
printf("姓名:%s\n",friends[n].name);
printf("电话:%s\n",friends[n].tel);
printf("年龄:%d\n",friends[n].age);
printf("住址:%s\n",friends[n].address);
getch();
return n;
}

}
printf("\n输入错误或不存在\n\n按任意键退出...\n");
getch();
return -1;

}

void friends_edit(void)
{
int n;
page_title("^_^ 编辑资料 ^_^");
if((n=search_name())!=-1)
{
printf("\n请重新输入信息:\n姓名:");
scanf("%s",&friends[n].name);
printf("电话:");
scanf("%s",&friends[n].tel);
printf("年龄:");
scanf("%d",&friends[n].age);
printf("住址:");
scanf("%s",&friends[n].address);
}
return_confirm();
}

void friends_del(void)
{
int n;

page_title("^_^ 注销基本记录 ^_^ ");
if((n=search_name())!=-1)
friends[n].age=0;
printf("\n该记录已注销.\n");
return_confirm();
}
void browser(void)
{
int n;

page_title("^_^ 浏览 ^_^");

printf("姓名\t 电话\t 年龄\t 住址\n");

for(n=0;n<32;n++)
if(friends[n].age!=0)
printf("%s\t%s\t%d\t%s\n",friends[n].name,friends[n].tel,friends[n].age,friends

[n].address);
printf("\n按任意键退出...\n");
getch();

}

void sava(void)
{

int n;
FILE *fp;
if((fp=fopen("test.txt","w"))==NULL)

{
printf("can not open file\n");/*没打开*/
exit(0); /*退出*/
}

fprintf(fp,"%s\t%s\t\t%s\t%s\n","姓名", "电话","年龄","住址");
for(n=0;n<32;n++)
if(friends[n].age!=0)
fprintf(fp,"%s\t%s\t%d\t%s\n",friends[n].name,friends[n].tel,friends[n].age,friends

[n].address);
printf("保存成功,按任意键退出...");
getch();
fclose(fp);
}

void load()
{

FILE *fp;
char ch;
page_title("^_^ 读取文件 ^_^");
fp=fopen("test.txt","r");
do
{
ch=fgetc(fp);
putchar(ch);

}while(ch!=EOF);

printf("\n读取成功,任意键退出...");
getch();
fclose(fp);

}

void main(void)
{
char choice;
menu: page_title("操作选单");
printf("请用数字选择\n");
printf("1 录入资料\n2 查找资料\n3 修改资料\n4 注销资料\n5 浏览...\n6 保存文件\n7 读取文件

\n \n0 退出\n");
switch(choice=getchar())
{
case '1' : friends_new();break;
case '2' : search_name();break;
case '3' : friends_edit();break;
case '4' : friends_del();break;
case '5' : browser();break;
case '6' : sava();break;
case '7' : load();break;
case '0' : exit(0);
}
goto menu;
}

回答3:

加我。先去看下我的空间<实习项目名称:学生成绩管理系统>
http://hi.baidu.com/ryw12403/blog/item/6864c138dacdee23b9998f16.html

回答4:

//学生信息的结构体的定义
#include
#include
#include

typedef struct student
{
long int num;
char name[20];
int age;
char sex[4];
char birthday[10];
char address[30];
long int tele_num;
char E_mail[20];
struct student *next;
}node;

//createList()函数
node *creatList()
{
node *p,*head,*s;
int cycle=1;
head = (node *)malloc(sizeof(node));
p=head;
while(cycle)
{
printf("请输入学生的学号:");
scanf("%ld",&p->num);
printf("请输入学生的姓名:");
scanf("%s",p->name);
printf("请输入学生的年龄:");
scanf("%d",&p->age);
printf("请输入学生的性别:");
scanf("%s",p->sex);
printf("请输入学生的出生年月:");
scanf("%s",p->birthday);
printf("请输入学生的地址:");
scanf("%s",p->address);
printf("请输入学生的电话:");
scanf("%ld",&p->tele_num);
printf("请输入学生的E-mail:");
scanf("%s",p->E_mail);
if(p->num != 0 || p->name != 0 || p->age != 0 || p->sex != 0 || p->birthday != 0 || p->address != 0 || p->tele_num != 0)
{
s = (node *)malloc(sizeof(node));
p->next = s;
p=s;
}
else cycle=0;
}
head=head->next;
p->next=NULL;
printf("\n %s",head->name);
return (head);
}

//printList函数
void printList(node *head)
{
node *p;
printf("\n输出所有学生的信息如下: \n");
p=head;
if(head!=NULL)
{
printf("\n学号\t姓名\t年龄\t性别\t出生年月\t地址\t电话\tE-mail\n");
printf("-------------------------------------------------------------------------------");

do
{
printf("%ld\t%s\t%d\t%s\t%s\t%s\t%ld\t%s\n", p->num, p->name, p->age, p->sex, p->birthday, p->address, p->tele_num, p->E_mail);
p = p->next;
}
while(p != NULL);
}
printf("\n");


int main()
{
node *head;
head=creatList();
printList(head);

return 0;
}

回答5:

自己写~~很简单~你从学链表和文件