您的位置:首页 > 编程语言 > C语言/C++

c语言课程设计常用功能

2009-04-19 22:46 393 查看
C语言课程设计常用功能,我做了个示例:

 

主要是学生的一个系统,毕竟学生对这个比较熟悉,而且基本功能都有了。这个有部分排错没写完整,比如数据长度校验。

使用vc6.0编译的,如果换作其他的估计换的也不是太多,根据提示修改即可。

 

/*******************************
* 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 <malloc.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

#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;
}


 

基本功能我测试了一下,都可以使用的。

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c 语言 null list file search