zoukankan      html  css  js  c++  java
  • step2 . day7 C语言阶段小的项目总结

    1.终于再一次实现了上大学时候的课堂大作业,C语言算是进入可以应用的阶段了

    2.写这段代码使用了模块编程的思想,也尝试的用了结构体指针、共用体指针和枚举类型学的传参,解决了全局变量声明及初始化和传参的问题,虽然成功了,但是感觉代码上还是有些资源浪费,

    下一阶段除了总结Linux的基本命令应用,就要开始加强数据结构的知识梳理,贴上代码,做个阶段记录

    1.定义头文件,用来实现调用函数声明及变量声明

    #ifndef __HEAD_H__
    #define __HEAD_H__
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #define N 50
    struct student{
    int id;
    char name[32];
    int age;
    char sex[10];
    float height;
    };
    union ustudent{
    int id;
    char name[32];
    int age;
    char sex[10];
    float height;
    };
    enum select{
    id,
    name,
    age,
    sex,
    height
    };
    void print(void);//打印选择List
    void printhead();
    void print1(struct student *,int);//打印1位同学信息
    int myindex(struct student *);//寻找插入位置
    void add(struct student *);//插入学生信息
    void printAll(struct student *);//打印所有同学信息
    void printName(struct student *);//打印所有同学名字
    void selectbyname(struct student *);
    void printselect(struct student *,union ustudent,enum select);
    void selectbyage(struct student *);
    void selectbysex(struct student *);
    void selectbyheight(struct student *); //4个选择函数
    void del(struct student *);//删除

    #endif

    2.头文件实现的.c文件(没写注释,这不是个好习惯)

    #include"head.h"

    union ustudent st;
    enum select s;
    void print(void){
    printf(" ******************************************************8** ");
    printf("please select a menu: ");
    printf(" 1.Add a student. ");
    printf(" 2.List all student name. ");
    printf(" 3.List all informationes of students. ");
    printf(" 4.Select a student by name. ");
    printf(" 5.Select students by age. ");
    printf(" 6.Select students by sex. ");
    printf(" 7.Select students by height. ");
    printf(" 8.Delete a student. ");
    printf(" 9.Exit. ");
    printf("please select a menu:");
    }

    int myindex(struct student *stu){
    int i=0;
    for(i=0;i<N;i++){
    if(strlen(stu[i].name) ==0)
    break;
    }

    return i;
    }

    void add(struct student *stu){

    int n = myindex(stu);

    if(n == (N-1))
    {
    printf("内存已满,添加失败! ");
    return;
    }

    else{

    printf("please input ID:");
    scanf("%d",&(stu[n].id));
    printf("please input Name:");
    scanf("%s",stu[n].name);
    printf("please input Age:");
    scanf("%d",&(stu[n].age));
    printf("please input sex(1.male,2.Female):");
    scanf("%s",stu[n].sex);
    printf("please input height:");
    scanf("%f",&(stu[n].height));

    printf("add successful! ");
    }

    printhead();
    print1(stu,n);
    }
    void printhead(){

    printf(" ID Name Age Sex Height ");
    }
    void print1(struct student *stu,int n){

    printf(" %d %s %d %s %.2f ",stu[n].id,stu[n].name,stu[n].age,stu[n].sex,stu[n].height);
    }
    void printAll(struct student *stu){

    int i;
    int n = myindex(stu);
    printhead();
    for(i=0;i<n;i++){
    printf(" %d %s %d %s %.2f ",stu[i].id,stu[i].name,stu[i].age,stu[i].sex,stu[i].height);
    }
    }
    void printName(struct student *stu){

    int i;
    int n = myindex(stu);
    printf(" Name: ");
    for(i=0;i<n;i++){
    printf(" %s ",stu[i].name);
    }
    }

    void selectbyname(struct student *stu){
    printf("please input name:");
    scanf("%s",st.name);
    s = name;
    printselect(stu,st,s);
    }

    void selectbyage(struct student *stu){
    printf("please input age:");
    scanf("%d",&st.age);
    s = age;
    printselect(stu,st,s);
    }
    void selectbysex(struct student *stu){
    printf("please input sex:");
    scanf("%s",st.sex);
    s = sex;
    printselect(stu,st,s);
    }
    void selectbyheight(struct student *stu){
    printf("please input height:");
    scanf("%f",&st.height);
    s = height;
    printselect(stu,st,s);
    }
    void printselect(struct student *stu,union ustudent st,enum select s){

    int i;
    int n = myindex(stu);
    switch(s){
    case name:
    printhead();
    for(i=0;i<n;i++){
    if(strcmp(stu[i].name,st.name));
    else
    print1(stu,i);
    }
    break;

    case sex:
    printhead();
    for(i=0;i<n;i++){
    if(strcmp(stu[i].sex,st.sex));
    else
    print1(stu,i);
    }
    break;
    case age:
    printhead();
    for(i=0;i<n;i++){
    if(stu[i].age==st.age)
    print1(stu,i);

    else;
    }
    break;
    case height:
    printhead();
    for(i=0;i<n;i++){
    if((int)stu[i].height == (int)st.height)
    print1(stu,i);
    else ;
    }
    break;
    case id:
    printhead();
    for(i=0;i<n;i++){
    if(stu[i].id==st.id)
    print1(stu,i);
    else ;
    }
    break;
    }
    }
    void del(struct student *stu){

    printf("please input student ID:");
    scanf("%d",&st.id);
    s = id;
    printselect(stu,st,s);
    char chose;

    printf("insure del(y/n):");
    getchar();
    chose = getchar();
    /* if(chose == 'y'){
    stu[st.id-1].id = 0;
    memset(stu[st.id-1].name,0,32);
    stu[st.id-1].age = 0;
    memset(stu[st.id-1].sex,0,10);
    stu[st.id-1].height = 0;
    }
    */
    int i;
    if(chose == 'y'){
    for(i =(st.id-1);i<50;i++)
    stu[i] = stu[i+1];
    }
    else
    return;
    printAll(stu);

    }

    3.main函数

    #include"head.h"

    struct student stu[N];

    int main(int argc, const char *argv[])
    {
    int choice;
    do
    {
    print();
    scanf("%d",&choice);
    switch(choice){
    case 1:add(stu);continue;
    case 2:printName(stu);continue;
    case 3:printAll(stu);continue;
    case 4:selectbyname(stu);continue;
    case 5:selectbyage(stu);continue;
    case 6:selectbysex(stu);continue;
    case 7:selectbyheight(stu);continue;
    case 8:del(stu);continue;
    case 9:printf(" studetManage APP closeed ");return 0;
    default:
    printf("choice error,please input again");
    continue;
    }
    }while(1);

    return 0;
    }

    4Makefile工程文件,这个最简单。。。

    studentManage:main.o head.o
    gcc main.o head.o -o studentManage
    main.o:main.c
    gcc -c main.c -o main.o
    head.o:head.c
    gcc -c head.c -o head.o

    .PHONY:clean

    clean:
    rm *.o studentManage

    写这个功能过程中,遇到了很多bug,在逐步调试的过程中也看到自己的知识薄弱点,所幸调试出来了,也算大有长进,享受这种多屏操作,满屏跑代码的快感

  • 相关阅读:
    Scrum冲刺第一篇
    项目冲刺
    需求改进&系统设计
    需求规格说明书
    团队作业第五周-测试和发布
    冲刺集合贴
    冲刺第6天
    冲刺第7天
    冲刺第5天
    冲刺第四天
  • 原文地址:https://www.cnblogs.com/huiji12321/p/11209815.html
Copyright © 2011-2022 走看看