学生信息的录入和打印,函数的封装

内容: 
用链表的形式完成一个学生信息录入和打印;
使用结构体,定义相应的数据类型;
实现链表的创建和插入,使用函数封装(不同的函数放入不同的文件中,在主函数中添加头文件,然后调用);
实现学生成绩的录入和打印;
编写主函数进行测试。
使用gcc进行编译
编写Makefile文件,使用预定义变量和自动变量

使用gdb进行调试


今天写的就是这样的程序,这是一个比较简单的程序,有机会写到这样的程序,想发表一下自己的观点,这是一个c语言的程序,但是在嵌入式中,这样的程序就要有封装的效果,以及在学c的时候没有的makefile等。。

程序我不多说,每个人有每个人的习惯,当然代码也不同,那怕是简单的程序,代码在下,仔细看,总会找到与自己不同的想法,我说的就是函数的封装,对与刚接触c的人来说,这也算是一个难点啦,这是在Linux上实现的代码,不同的函数放入不同的文件中,这一点不难,相信很多人都会这一个问题。但是放入后怎么运行就是我们要注意的问题啦,在刚开始的时候我们都将函数什么都放在main函数中,这对以后大的程序来说是不好的,所以有了函数的封装,main函数中应该放什么,这一点,其实通俗点,就是,main函数中是函数的调用,在.h中放入结构体的声明,以及函数的声明,在.c文件中实现不同的函数实现。比如下面的代码:

main.c:

#include <stdio.h>
#include "han.h"


int main()
{
S *head;
head = createhead(head);
input(head);
output(head);
return 0;
}


han.h:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN sizeof(struct student)


typedef struct student
{
char name[20];
char sex[20];
int age;
struct student *next;
}S;


S* createhead(S* head);
void input(S* head);
void output(S* head);


createhead.c:

#include "han.h"


S* createhead(S *head)
{
head = (S *)malloc(LEN);
if(head == NULL)
{
printf("you are error\n");
return 0;
}
head -> next = NULL;
return head;
}


input.c:

#include "han.h"


void input(S *head)
{
S  *p,*q;
printf("(输入数据,名字输入0结束)\n");
while(1)
{
p = (S *)malloc(LEN);
printf("输入学生的名字\n");
scanf("%s",p->name);
if((strcmp(p->name,"0"))==0)
break;
printf("输入学生的性别\n");
scanf("%s",p->sex);
printf("输入学生的年龄\n");
scanf("%d",&p->age);
p->next = head -> next;
head->next = p;
q = p;
}
}

output.c:

#include "han.h"


void output(S *head)
{
S *p;
p = head->next;
printf("输出学生的姓名\t");
printf("输出学生的性别\t");
printf("输出学生的年龄\n");
while(p != NULL)
{
printf("%s\t\t",p->name);
printf("%s\t\t",p->sex);
printf("%d\t\n",p->age);
p=p->next;
}
}
这就是函数的封装,当然,想法不同,我也不能这么说,只能说这是基本的,对于这种程序怎么在linux中运行呢,Linux上是用的gcc编译器,以这个题为例子,gcc main.c createhead.c input.c output.c -o main这样就实现多文件的编译。

对于makefile文件,在这一篇中不多说,毕竟这个东西也多,我将代码给出来;可以参考下:

student:main.o input.o output.o createhead.o
gcc main.o input.o output.o createhead.o -o main
main.o:main.c han.h 
gcc main.c -c
input.o:input.c han.h
gcc input.c -c
output.o:output.c han.h
gcc output.c -c
createhead.o:createhead.c han.h
gcc createhead.c -c
clean:
rm -f *.o student

有机会我会单独说下makefile文件的写法,最后一个就是gdb调试,这个是Linux上自带的调试工具,比如我们的程序运行过之后,我们就能用gdb来进行调试,这一点我也是刚接触,不能给大家好的讲解,大家如果对这一点感兴趣的话可以看下相关的资料,给大家说下基本的知识吧,进入gdb的方法,对这个而言,有一个main的执行文件,所以我们进入的方法就是gdb main,这样就行,设置断点,用b+行号,运行是run等。

希望给大家一些帮助。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值