吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2645|回复: 4
收起左侧

[C&C++ 转载] c语言写的学生信息库系统的作业(小白)

[复制链接]
宇longer 发表于 2019-5-18 23:54
[C] 纯文本查看 复制代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stud_node{
	int num;
	char name[20];
	int score;
	struct stud_node *next;
}; 
struct stud_node * Create_Stu_Doc();//新建链表
struct stud_node * InsertDoc(struct stud_node * head,struct stud_node * stud);
//插入
struct stud_node * DeleteDoc(struct stud_node * head,int num);//删除
void Print_Stu_Doc(struct stud_node * head);//遍历

int main(void)
{
	struct stud_node *head,*p;
	int choice,num,score;//choice:功能选项 
	char name[20];
	int size=sizeof(struct stud_node);
	
	do{
		printf("1:Create 2:Inseret 3:Delete 4:Printf 0:Exit\n");
		scanf("%d",&choice);
		switch (choice){
			case 1:
				head=Create_Stu_Doc();
				break;
			case 2:
			    printf("Input num,name and score: \n");
			    scanf("%d%s%d",&num,name,&score);
				p=(struct stud_node * )malloc (size);
				p->num=num;
				strcpy(p->name,name);
				p->score=score;
				head=InsertDoc(head,p);
				break;
			case 3:
				printf("Input num: \n");
				scanf("%d",&num);
				head=DeleteDoc(head,num);
				break;
			case 4:
				Print_Stu_Doc (head);
				break;
			case 0://3个0 
			    break; 
		}
	} while(choice!=0);
	
	return 0;
}
//创建链表
struct stud_node * Create_Stu_Doc()
{
	struct stud_node * head,*p;
	int num,score;
	char name[20];
	int size=sizeof(struct stud_node);
	
	head=NULL;
	printf("Input num,name and score: \n");
	scanf("%d%s%d",&num,name,&score);
	while(num!=0){
		p=(struct stud_node * )malloc (size);
		p->num=num;
		strcpy(p->name,name);
		p->score=score;
		head=InsertDoc(head,p);//调用插入函数
		scanf("%d%s%d",&num,name,&score); 
	}
	return head;
}

//插入操作
struct stud_node * InsertDoc(struct stud_node * head,struct stud_node * stud)
{
	struct stud_node * ptr,*ptr1,*ptr2;
	ptr2=head;
	ptr=stud;//ptr指向待插入的新的学生记录结点
	//原链表为空时的插入
	if(head==NULL){
		head=ptr;//新插入结点成为头结点 
		head->next=NULL; 
	} 
    //原链表不为空时的插入
	else{ 
		while((ptr->num>ptr2->num)&&(ptr2->next!=NULL)){
			ptr1=ptr2;//1,2各后移一个结点 
			ptr2=ptr2->next;
		}
		if(ptr->num<=ptr2->num){     //1,2之间插入新结点 
			if(head==ptr2)
			head=ptr;
			else ptr1->next=ptr;
			ptr->next=ptr2; 
		} 
		else{   //新插入结点成为尾结点 
			ptr2->next=ptr;
			ptr->next=NULL;
		} 
	}
	return head; 
 } 
 //删除操作 
struct stud_node * DeleteDoc(struct stud_node * head,int num)
{
	struct stud_node * ptr1,*ptr2;
	while(head!=NULL&&head->num==num){
		ptr2=head;
		head=head->next;
		free(ptr2);
	}
	if(head=NULL)
		return NULL;
	ptr1=head;
	ptr2=head->next;
	while(ptr2!=NULL){
		if(ptr2->num=num){
			ptr1->next=ptr2->next;
			free(ptr2);
		}
		else
			ptr1=ptr2;
		ptr2=ptr1->next;
	}
	return head;
 } 
//遍历操作
void Print_Stu_Doc(struct stud_node * head)
{
	struct stud_node * ptr;
	if(head==NULL){
		printf("\nNo Records\n");
		return;
	}
	printf("\nThe students’records are:\n");
	printf("Num\tName\tScore\n");
	for(ptr=head;ptr!=NULL;ptr=ptr->next)
		printf("%d\t%s\t%d\n",ptr->num,ptr->name,ptr->score);
 } 

免费评分

参与人数 1吾爱币 +3 热心值 +1 收起 理由
苏紫方璇 + 3 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

zjg1727 发表于 2019-5-20 08:19
1. 分配内存空间的时候没有错误处理语句啊?
不过一般情况下不会出错。

p=(struct stud_node * )malloc (size);
if (p == NULL) {
   // 错误提示
}

2.
        printf("1:Create 2:Inseret 3:Delete 4:Printf 0:Exit\n");
Inseret->Insert  ?

3. num学号? 没有重复检查啊。

涛声依旧1996 发表于 2019-5-19 09:12
l木之林 发表于 2019-5-19 09:12
 楼主| 宇longer 发表于 2019-5-19 22:21
l木之林 发表于 2019-5-19 09:12
请问这用的哪个编程工具呢?比VC好看多了

Dev C++...........
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-24 13:33

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表