step2. day7 C language small stage project summary

1. finally realized once again the college classroom a big operation, C language can be applied to enter the stage

2. Write code uses the idea of ​​programming module, also try to use the structure pointer, union and enumeration pointer typology of mass participation, to solve the problem of global variable declaration and initialization and parameter passing, though a success , but the code still feel some waste of resources,

The next stage in addition to summarize the basic commands of Linux applications, will begin to strengthen the sort of knowledge of data structures, paste the code, to be a stage record

1. Define the header files used to implement the function declaration and call the variable declarations

__HEAD_H__ #ifndef
#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 ];
a float height;
};
Union ustudent {
int ID;
char name [32];
int Age;
char Sex [10];
a float height;
};
enum SELECT {
ID,
name,
Age,
Sex,
height
};
void Print ( void); // Print select List
void Printhead ();
void print1 (struct Student *, int); // print a classmate information
int myindex (struct student *); // find the insertion position
void add (struct student *); // insert student information
void printAll (struct student *); // print all the students information
void printName (struct student *); // prints the names of all the students
void selectbyname (struct Student *);
void printselect (Student * struct, Union ustudent, enum sELECT);
void selectbyage (Student * struct);
void selectbysex (Student * struct);
void selectbyheight (Student * struct); // selection function. 4
void del (struct student *) ;//delete

#endif

2. .c file header file implementation (I did not write notes, this is not a good habit)

#include"head.h"

union ustudent st;
enum select s;
void print(void){
printf("\n******************************************************8**\n");
printf("please select a menu:\n");
printf("\t1.Add a student.\n");
printf("\t2.List all student name.\n");
printf("\t3.List all informationes of students.\n");
printf("\t4.Select a student by name.\n");
printf("\t5.Select students by age.\n");
printf("\t6.Select students by sex.\n");
printf("\t7.Select students by height.\n");
printf("\t8.Delete a student.\n");
printf("\t9.Exit.\n");
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-== (. 1-N))
{
the printf ( "memory is full, addition failed \ n-!");
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!\n");
}

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

printf("\tID\tName\tAge\tSex\tHeight\n");
}
void print1(struct student *stu,int n){

printf("\t%d\t%s\t%d\t%s\t%.2f\n",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("\t%d\t%s\t%d\t%s\t%.2f\n",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("\tName:\n");
for(i=0;i<n;i++){
printf("\t%s\n",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 function

#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("\nstudetManage APP closeed\n");return 0;
default:
printf("choice error,please input again");
continue;
}
}while(1);

return 0;
}

4Makefile project file, the easiest. . .

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

 

This feature writing process, met a lot of bug, step through the process of knowledge also see their weak points, but fortunately debugging out, it is also considered improved a lot, enjoy this multi-screen operation, the thrill of the code to run full screen

Guess you like

Origin www.cnblogs.com/huiji12321/p/11209815.html