c语言课设常用问题,c语言课程设计常用功能

/*******************************

* title: student managerment system

* date: 2009-04-19

*

* version: 1.0

* @author: mrl

*******************************/

// search from user current dir

#include "stdafx.h"

// search from standard lib

#include

#include

#include

#include

#define STU_NAME_LEN 100

// student basic attribute

struct student {

char* name;

int age;

};

// student link table

struct student_list {

struct student stu;

struct student_list *next;

};

// alias variable

typedef struct student_list studentList;

// global variable for student link table

struct student_list *g_stu;

/**

* initalize the system

*/

int init() {

g_stu = NULL;

return 0;

}

/**

* insert one student record

*/

int insert() {

char* c = (char *)malloc(STU_NAME_LEN);

int age;

struct student stu;

printf("please input one student infor student name: ");

scanf("%s", c);

printf("please input one student infor student age: ");

scanf("%d", &age);

stu.name = c;

stu.age = age;

if(g_stu == NULL) {

// malloc return NULL if failed to alloc memory

g_stu = (studentList *)malloc(sizeof(studentList));

if(!g_stu)

{

printf("/n out of memory");

exit(0);

}

(*g_stu).stu = stu;

// g_stu->stu = stu;

(*g_stu).next = NULL;

// g_stu->next = NULL;

} else {

printf("insert one /n");

// insert at last

studentList *stuList = g_stu;

while(stuList->next != NULL) {

stuList = stuList->next;

}

studentList *stuTemp;

stuTemp = (studentList *)malloc(sizeof(studentList));

stuTemp->stu = stu;

stuTemp->next = NULL;

stuList->next = stuTemp;

}

return 0;

}

/**

* show all students records

*/

int showStudents(studentList *list) {

if(list != NULL) {

printf("All Student List as Followed: /n");

studentList* stuPoint = list;

while(stuPoint != NULL) {

printf("student name: %s /t",stuPoint->stu.name);

printf("student age: %d /t", stuPoint->stu.age);

printf("/n");

stuPoint = stuPoint->next;

}

} else {

printf("There are no students. /n");

}

return 0;

}

/**

* update one student

*/

void update(studentList *p) {

// assert p

if(p == NULL) {

printf("student is NULL /n");

}

int age;

char* c = (char *)malloc(STU_NAME_LEN);

studentList* stuPoint = g_stu;

while(stuPoint != NULL) {

if(stuPoint == p) {

printf("please input student new name: ");

scanf(" %s", c);

printf("please input student new age: ");

scanf(" %d", &age);

stuPoint->stu.name = c;

stuPoint->stu.age = age;

break;

}

stuPoint = stuPoint->next;

}

}

/*

* menu choice

*/

void menu() {

printf("Please choose one operation /n");

printf("1: new student /n");

printf("2: add one student /n");

printf("3: update one student /n");

printf("4: delete one student /n");

printf("5: search one student /n");

printf("6: sort student list /n");

printf("7: display student list /n");

printf("8: store student list to file /n");

printf("9: read student list from file /n");

printf("10: write to file test /n");

printf("-1: eixt /n");

}

/**

* search student by name

*/

studentList* searchByName() {

studentList *p = NULL;

char* c = (char *)malloc(STU_NAME_LEN);

printf("please input one student name: ");

scanf("%s", c);

if(g_stu != NULL) {

studentList* stuPoint = g_stu;

while(stuPoint != NULL) {

if(strcmp(stuPoint->stu.name, c) == 0) {

printf("Get it %s /n" , stuPoint->stu.name);

p = stuPoint;

return p;

}

stuPoint = stuPoint->next;

}

} else {

printf("There are no students. /n");

}

if(p == NULL) {

printf("There are no the student %s /n" , c);

}

return p;

}

/**

* delete

*/

int deleteStu(studentList *p) {

if(p == NULL) {

printf("student is NULL /n");

return 0;

}

if(g_stu != NULL) {

studentList* stuPoint = g_stu;

// if the head point

if(g_stu == p) {

// only one element

if(g_stu->next == NULL) {

g_stu = NULL;

return 0;

} else {

g_stu = g_stu->next;

// release free memory

free(p);

return 0;

}

}

// not the head point, begin traverse

while(stuPoint->next != NULL) {

if(stuPoint->next == p) {

// if the tail element

if(stuPoint->next->next == NULL) {

stuPoint->next = NULL;

return 0;

} else {

// not the tail element

stuPoint->next = stuPoint->next->next;

// release memory

free(p);

return 0;

}

}

// read next

stuPoint = stuPoint->next;

}

} else {

printf("There are no students. /n");

}

return 0;

}

/**

* order by age

*/

studentList* sortByAge() {

studentList* stuList = g_stu;

studentList* result;

result = (studentList *)malloc(sizeof(studentList));

// head

if(g_stu != NULL) {

result->stu = g_stu->stu;

result->next = NULL;

stuList = g_stu->next;

}

while(stuList != NULL) {

// from the head begin

studentList* temp = result;

while(temp != NULL) {

// compare to head

if(result->stu.age > stuList->stu.age) {

studentList* p = (studentList *)malloc(sizeof(studentList));

p->stu.name = stuList->stu.name;

p->stu.age = stuList->stu.age;

p->next = result;

result = p;

break;

}

// compare current node

if(stuList->stu.age < temp->stu.age) {

studentList* p = (studentList *)malloc(sizeof(studentList));

p->stu.name = temp->stu.name;

p->stu.age = temp->stu.age;

p->next = temp->next;

temp->stu.name = stuList->stu.name;

temp->stu.age = stuList->stu.age;

temp->next = p;

break;

}

// tail element

if(temp->next == NULL) {

// malloc memory

studentList* p = (studentList *)malloc(sizeof(studentList));

p->stu.name = stuList->stu.name;

p->stu.age = stuList->stu.age;

p->next = NULL;

temp->next = p;

break;

}

temp = temp->next;

}

stuList = stuList->next;

}

return result;

}

/**

* save to file

*/

FILE* save2File() {

FILE *fp;

char* fileName = (char *)malloc(80);

printf("please input fileName: ");

scanf("%s", fileName);

// read and write

fp = fopen(fileName,"rt+");

if(fp == NULL) {

printf("open file failure /n");

// create the file

} else {

printf("input data ...");

studentList* stuPoint = g_stu;

while(stuPoint != NULL){

fputs(stuPoint->stu.name,fp);

fputs("/t",fp);

// digital convert to string

char* a = (char *)malloc(80);

// 10 express decimal system

a = itoa(stuPoint->stu.age,a,10);

fputs(a,fp);

fputs("/n",fp);

stuPoint = stuPoint->next;

}

}

if(fp != NULL) {

fclose(fp);

}

return fp;

}

/**

* read from file

*/

void readFromFile() {

FILE *fp;

char* fileName = (char *)malloc(80);

printf("please input fileName: ");

scanf("%s", fileName);

// read and write

fp = fopen(fileName,"r+");

if(fp == NULL) {

printf("open file failure /n");

} else {

while(!feof(fp)) {

char ch = fgetc(fp);

printf("%c",ch);

}

}

if(fp != NULL) {

fclose(fp);

}

}

/**

* main

*1: new student

*2: add one student

*3: update one student

*4: delete one student

*5: search one student

*6: sort student list

*7: display student list

*8: store student list to file

* 9: read student list from file

*-1: eixt

*/

int main(int argc, char* argv[])

{

printf("Welcome to My Students Management System!/n");

init();

int operate = 0;

while(operate != -1) {

menu();

scanf("%d", &operate);

switch(operate) {

case 1:

case 2: {

insert();

break;

}

case 3: {

// search by name and return the point

studentList* temp = searchByName();

update(temp);

break;

}

case 4: {

// search by name and return the point

studentList* temp = searchByName();

// delete by point

deleteStu(temp);

break;

}

case 6: {

studentList* rs = sortByAge();

showStudents(rs);

break;

}

case 5: {

searchByName();

break;

}

case 7: {

showStudents(g_stu);

break;

}

case 8: {

save2File();

break;

}

case 9: {

readFromFile();

break;

}

default: break;

}

}

printf("exit system /n");

return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值