473,791 Members | 3,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

looking for advice

To practice C language,I began to develop a simple project which
implements the management of students' information.I have worked it for
one week. I plan to finish it for two weeks.
Are there some experienced friends who would like to read my source
code and give some advice to me.tks :)

---------------------------------------------------------
main.c
---------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include "logic.h"

const char *filename = "student.da t";

main(void)
{
int menu_id = 99;
int student_num = 0;
struct node *phead = NULL;

void load_menu();
void func_menu_new(s truct node *phead);// new a student
void func_menu_searc h(struct node *phead);
void func_menu_edit( struct node *phead);
void func_menu_print (struct node *phead);
void func_menu_exit( struct node *phead);

load_menu();
phead = init();

if(read_datafil e(phead,filenam e) == 1)
{
printf(" The file %s doesn't exist! \n",filename) ;
}

student_num = get_student_num (phead);
printf(" there are %d students' information.\n" ,student_num);

tag_start: scanf("%d",&men u_id);

switch (menu_id)
{
case 0:
func_menu_exit( phead);
break;
case 1:
func_menu_new(p head);
break;
case 2:
func_menu_searc h(phead);
break;
case 3:
func_menu_edit( phead);
break;
case 4:
func_menu_print (phead);
break;
default:
printf("Sorry,O nly 0--4 can be chosen,please input again :)\n");
}
if(menu_id !=0)
goto tag_start;

}

void load_menu()
{
clrscr();

printf("\n");
printf("\n");
printf(" Welcome to Use Student System \n");
printf(" =============== ======menu===== =============== =\n");
printf(" Input a student's informations... ............... 1\n");
printf(" Search student's informations... ............... .2\n");
printf(" Modify student's informations... ............... .3\n");
printf(" Print student's informations... ............... ..4\n");
printf(" exit system......... ............... .............0\ n");
printf("\n");
}

void func_menu_new(s truct node *phead)
{
struct student *pnewstudent = (struct student *)(sizeof(struc t
student));

printf("please input a new student's information\n") ;
scanf("%d",&(pn ewstudent->id));
scanf("%s",pnew student->name);

student_add(phe ad,pnewstudent) ;

printf("you have input a student's informations successfully!\n ");

free(pnewstuden t);
pnewstudent = NULL;
}

void func_menu_print (struct node *phead)
{
struct node *ptemp;
int student_num;

ptemp = phead;
student_num = get_student_num (phead);
printf(" All Student Information\n") ;
printf(" =============== =============== ===\n");
printf(" there are %d students' information.\n" ,student_num);
printf("| Student ID ");
printf("|");
printf(" Student name |\n");

while(ptemp->next != NULL)
{
printf("| %10d ",ptemp->next->stu_info.id) ;
printf("| %14s |\n",ptemp->next->stu_info.name) ;
ptemp = ptemp->next;
}
ptemp = NULL;
}

void func_menu_exit( struct node *phead)
{
if(save_to_data file(phead,file name) == 0)
{
printf(" All students' informations are saved!");
}

free_memo(phead );
}

void func_menu_searc h(struct node *phead)
{
int search_type;
int search_id;
char search_name[20];
struct node *psearch;

printf("Search by what?\n");
printf(" 1: student ID\n");
printf(" 2: student name\n");
scanf("%d",&sea rch_type);

while((search_t ype != 1) && (search_type != 2))
{
printf("sorry, please choose 1 or 2\n");
scanf("%d",&sea rch_type);
}

switch (search_type)
{
case 1:
{
printf("please input the student's ID\n");
scanf("%d",&sea rch_id);
psearch = search_by_id(se arch_id,phead);
if(psearch == NULL)
{
printf("sorry there are no student infomation with ID
=%d\n",search_i d);
}
else
{
printf(" %d :",psearch->stu_info.id) ;
printf(" %s\n",psearch->stu_info.name) ;
}
}
break;
case 2:
{
printf("please input the student's name\n");
scanf("%s",sear ch_name);
psearch = search_by_name( search_name,phe ad);

if(psearch == NULL)
{
printf("sorry there are no student infomation with name =
%d\n",search_na me);
}
else
{
printf(" %d :",psearch->stu_info.id) ;
printf(" %s\n",psearch->stu_info.name) ;
}
}
break;
default:
printf("error occurs");
}
psearch = NULL;
}

void func_menu_edit( struct node *phead)
{
int stu_id,stu_id_n ew;
char stu_name_new[20];
struct node *psearch;

printf(" Please input the ID of the student you want to edit.\n");
scanf("%d",&stu _id);
psearch = search_by_id(st u_id,phead);

if(psearch == NULL)
{
printf("sorry there are no student infomation with ID
=%d\n",stu_id);
}
else
{
printf(" %d :",psearch->stu_info.id) ;
printf(" %s\n",psearch->stu_info.name) ;
}

printf(" Please input the student's new information!\n" );
scanf("%d",&stu _id_new);
scanf("%s",stu_ name_new);
psearch->stu_info.id = stu_id_new;
strcpy(psearch->stu_info.name, stu_name_new);

printf("You update a student's information successfully!\n ");
psearch = NULL;
}


--------------------------------------------------------
logic.c
-------------------------------------------------------
#include "logic.h"
#include <stdio.h>
struct node * init()
{
struct node * ptemp;

ptemp = (struct node*)malloc(si zeof(struct node));
ptemp->next = NULL;

return ptemp;
}

void student_add(str uct node *phead,struct student *pnewstudent)
{
struct node *pnew,*ptemp;

ptemp = phead;
pnew = (struct node*)malloc(si zeof(struct node));
pnew->stu_info.id = pnewstudent->id;
strcpy(pnew->stu_info.name, pnewstudent->name);

while(ptemp->next != NULL)
{
ptemp = ptemp->next;
}
ptemp->next = pnew;
pnew->next = NULL;
pnew = NULL;
ptemp = NULL;
}

int get_student_num (struct node *phead)
{
struct node *ptemp;
int student_num = 0;

ptemp = phead->next;

while(ptemp != NULL)
{
ptemp = ptemp->next;
student_num ++;
}
ptemp = NULL;
return student_num;
}

int save_to_datafil e(struct node *phead,char *filename)
{
struct node *ptemp;
int student_num;
FILE *fp;

ptemp = phead;
student_num = get_student_num (phead);
fp=fopen(filena me, "w");
fprintf(fp,"%d\ n",student_num) ;

while(ptemp->next != NULL)
{
fprintf(fp, "%d", ptemp->next->stu_info.id) ;
fprintf(fp, "%s\n", ptemp->next->stu_info.name) ;
ptemp = ptemp->next;
}

ptemp = NULL;
fclose(fp);
return 0;
}

int read_datafile(s truct node *phead,char *filename)
{
struct node *pnew,*ptemp;
FILE *fp;
int student_num;

ptemp = phead;
fp = fopen(filename, "r");

if(fp == NULL)
{
return 1;
}

fscanf(fp,"%d", &student_num );

while(student_n um > 0)
{
pnew = (struct node*)malloc(si zeof(struct node));
fscanf(fp,"%d", &(pnew->stu_info.id) );
fscanf(fp,"%s", pnew->stu_info.name) ;
ptemp->next = pnew;
ptemp = ptemp->next;
student_num --;
}
ptemp = NULL;
pnew = NULL;
fclose(fp);
return 0;
}

void free_memo(struc t node *phead)
{
struct node *ptemp,*pnext;

ptemp = phead->next;

while(ptemp != NULL)
{
pnext = ptemp->next;
free(ptemp);
ptemp = pnext;
}

free(phead);
phead = NULL;
ptemp = NULL;
pnext = NULL;
}

struct node * search_by_id(in t search_id,struc t node *phead)
{
int id = search_id;
struct node *ptemp,*psearch ;

psearch = NULL;
ptemp = phead->next;

while(ptemp != NULL)
{
if(ptemp->stu_info.id == id)
{
psearch = ptemp;
}
ptemp = ptemp->next;
}

return psearch;
}

struct node * search_by_name( char search_name[20],struct node *phead)
{
struct node *ptemp,*psearch ;

psearch = NULL;
ptemp = phead->next;

while(ptemp != NULL)
{

if(strcmp(ptemp->stu_info.name, search_name) == 0)
{
psearch = ptemp;
}
ptemp = ptemp->next;
}

return psearch;
}

-----------------------------------------------------------
logic.h
-----------------------------------------------------
#ifndef LOGIC_H
#define LOGIC_H

#define LEN sizeof(struct student)

struct student
{
int id;
char name[20];
};

struct node
{
struct student stu_info;
struct node *next;
};

extern struct node * init();
extern int read_datafile(s truct node *phead,char *filename);
extern int get_student_num (struct node *phead);
extern int save_to_datafil e(struct node *phead,char *filename);
extern void student_add(str uct node *phead,struct student
*pnewstudent);
extern void free_memo(struc t node *phead);
extern struct node * search_by_id(in t search_id,struc t node *phead);
extern struct node * search_by_name( char search_name[20],struct node
*phead);
#endif

Nov 14 '05 #1
3 1855

On Wed, 16 Mar 2005, bbcat wrote:

To practice C language,I began to develop a simple project which
implements the management of students' information.I have worked it for
one week. I plan to finish it for two weeks.
Are there some experienced friends who would like to read my source
code and give some advice to me.tks :)


Yes. His name is 'gcc', and you can find him online or on your hard
disk. Try

gcc -W -Wall -ansi -pedantic -O3 -c logic.c
gcc -W -Wall -ansi -pedantic -O3 -c main.c

You will find many trivial yet fatal errors (such as forgetting to
prototype 'malloc' before using it), as well as several errors that
are just plain dumb (such as passing a string to 'printf("%d")')
and several stylistic booboos (such as forgetting to write
'int main(void)' instead of just 'main(void)').
Fix the mistakes the compiler tells you, and then post again.
Oh, and try not to post so *MUCH*. Put the code in three separate
files on your Web site, and post the URL. Then people won't have to
cut and paste so much in order to help you. If you make it hard for
people to help you, they usually won't.

Finally, don't use hard tabs (ASCII character 0x09) in Usenet posts,
or in code you expect a lot of people to read. They can get horribly
mangled by Usenet, and also by text editors (some of which think one
tab is eight spaces, some four, some three, some two, and some even
weirder numbers). Run 'detab' on your source code before posting it.

-Arthur
Nov 14 '05 #2
bbcat wrote:
To practice C language,I began to develop a simple project which
implements the management of students' information.I have worked it for
one week. I plan to finish it for two weeks.
Are there some experienced friends who would like to read my source
code and give some advice to me.tks :)

---------------------------------------------------------
main.c
---------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include "logic.h"

const char *filename = "student.da t"; Do you want a const pointer to constant data?
const char * const filename = "student.da t";

main(void) The main() function returns an int. Declare it properly:
int main(void)
{
int menu_id = 99;
int student_num = 0;
struct node *phead = NULL;

void load_menu();
void func_menu_new(s truct node *phead);// new a student
void func_menu_searc h(struct node *phead);
void func_menu_edit( struct node *phead);
void func_menu_print (struct node *phead);
void func_menu_exit( struct node *phead); Declarations don't need to be encapsulated inside a function.
A popular style is to place all the declarations before the
main() function. Some people even alphabetize the list.

I suggest you place the declarations into a header file
with explanations of what they do, and their return values,
if any.

load_menu();
phead = init();

if(read_datafil e(phead,filenam e) == 1)
{
printf(" The file %s doesn't exist! \n",filename) ;
} Interesting, two points here:
1. A read failure or open failure does not indicate that the file
does not exist, only that the open failed. For example, the
file may exist, but your program doesn't have access rights to
open it.
2. The program announces the file doesn't exist but continues anyway.
Very bad karma.

student_num = get_student_num (phead);
printf(" there are %d students' information.\n" ,student_num);

tag_start: scanf("%d",&men u_id); Input from a user without a prompt.
More bad karma.
Label usage, could be eliminated. Depends on you allergy level
to gotos.

switch (menu_id)
{
case 0:
func_menu_exit( phead);
break;
case 1:
func_menu_new(p head);
break;
case 2:
func_menu_searc h(phead);
break;
case 3:
func_menu_edit( phead);
break;
case 4:
func_menu_print (phead);
break;
default:
printf("Sorry,O nly 0--4 can be chosen,please input again :)\n");
}
if(menu_id !=0)
goto tag_start;

} Try this:
typedef void (*Menu_Function _Pointer)(struc t node *);
struct Menu_Item
{
unsigned int value;
const char * prompt; /* or description */
Menu_Function_P ointer action;
};

const struct Menu_Item Main_Menu[] =
{
{0,
"exit system......... ............... .............0\ n",
func_menu_exit} ,
{1,
"Input a student's informations... ............... 1\n",
func_menu_new},
{2,
"Search student's informations... ............... .2\n",
func_menu_searc h}
};
const unsigned int Num_Main_Menu_I tems =
sizeof(Main_Men u) / sizeof(Main_Men u[0]);

void load_menu(void)
{
static const char * Menu_Header =
"\n"
"\n"
" Welcome to Use Student System\n"
" =============== ======menu===== =============== =\n";
unsigned int i;
fwrite(Menu_Hea der, sizeof(Menu_Hea der), 1, stdout);
for (i = 1; i < Num_Main_Menu_I tems; ++i)
{
printf(" %s", Main_Menu[i].prompt);
}
printf("\n");
return;
}

unsigned int process_menu(st ruct * phead)
{
unsigned int selection;
unsigned int index;

do
{
scanf("%d", &selection);
for (index = 0; index < Num_Main_Menu_I tems; ++index)
{
if (selection == Main_Menu[index].value)
{
(Main_Menu[index].action)(phead) ;
break; /* exit the "for" loop */
}
}
if (index >= Num_Main_Items)
{
printf("Sorry,O nly 0--%d can be chosen,please input again :)\n",
Num_Main_Menu_I tems - 1);
}
} while (index >= Num_Main_Items) ;
return selection;
}

void load_menu()
{
clrscr();

printf("\n");
printf("\n");
printf(" Welcome to Use Student System \n");
printf(" =============== ======menu===== =============== =\n");
printf(" Input a student's informations... ............... 1\n");
printf(" Search student's informations... ............... .2\n");
printf(" Modify student's informations... ............... .3\n");
printf(" Print student's informations... ............... ..4\n");
printf(" exit system......... ............... .............0\ n");
printf("\n");
}

[snip]

I suggest making your linked list into separate
functions so you can use it for other projects. ;-)

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
Nov 14 '05 #3
thank Thomas Matthews and Arthur J. O'Dwyer for your advice very much
:-)

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2582
by: Jason Daly | last post by:
I'm a freshman at college as a computer science major. I'm not sure it has what I want. Does anyone know if a major commonly exists in web design (focusing in server side languages)? I want to program for the internet, but don't know where to get all of my information from to be the most knowledgeable I can be. Do i find what i'm looking for in some class somewhere? if so where do i look? or do i just buy all the asp, php, xml, etc...
2
2358
by: Alex | last post by:
Subject: Looking for an XML (database-based) Query Reporting Tool/advice First off, let me apologize if this thread is somewhat off topic... PLEASE REPLY TO: xml@solex-bi.com I am looking for a way to query XML documents stored in an Oracle 9i database, contained in an unstructured CLOB column, much like one would with a traditional BI query reporting tool, but using XPath or XQuery
4
1936
by: pcunix | last post by:
I'm looking for general advice, pointers to web pages, books, whatever. I have a moderately successful web site. The major complaint that I get, time after time, is "It's UUUGLY" As I have explained at http://aplawrence.com/Blog/B1228.html , I understand that, and to some extent it's deliberate: I am NOT looking for flash over content and I want my pages to remain fast, Lynx friendly, and easy to read.
4
1845
by: Wayne Wengert | last post by:
I am looking for pointers to good beginner books, tutorials or other resources to help me understand how to really use XML data. I program mostly in VB (I have several applications in VB6 but am just starting to convert to VB.NET). Most of my database backends are SQL Server 2000 with a few still Access. I want to educate myself so that I can code (and understand) processes like reading an XML file and adding the contained information to...
29
3757
by: Lauren Wilson | last post by:
Does anyone know how the following info is extracted from the user's computer by a Front Page form? HTTP User Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0 I only ask because I believe I could use the same info as part of a scheme to generate a unique (or at least less common) serialized id code for the user's computer as part of a software locking and activation system. If I had a DLL...
3
1364
by: bradsalmon | last post by:
Hi all, Sorry for the long post, but I don't know how else to explain this one. Just after some advice really as I'm finding it difficult to update a database. Currently this is running in Access 97 on NT but I do have the option to upgrade to Access 2002 on XP. In essence I'm dealing with over 140,000 items of data, these are spread across about 400 identifiers which have about 44 fields specific to 8 years.
1
1733
by: jm | last post by:
Sorry for the off-topic post, but I would love to hear the advice of the members of this forum. We are looking to implement an internal blog server. It needs to run on MS platforms. So far, I'm thinking about DasBlog, and .Text. Are there any others that I should look at? Does anyone have experience with either of these two servers?
13
3116
by: Alan Silver | last post by:
Hello, MSDN (amongst other places) is full of helpful advice on ways to do data access, but they all seem geared to wards enterprise applications. Maybe I'm in a minority, but I don't have those sorts of clients. Mine are all small businesses whose sites will never reach those sorts of scales. I deal with businesses whose sites get maybe a few hundred visitors per day (some not even that much) and get no more than ten orders per day....
3
1597
by: provowallis | last post by:
I'm new to this board so I hope this reqest isn't out of line, but I'm looking for some general advice about creating links in online books. If the link target didn't involve PIs I don't think I'd be here, but since it does, I'd be interested in getting some different perspectives on this. I have an XML book and I plan to create XHTML so I'm looking for the best way to create links from the various components of the book to the page...
1
3170
by: Shawn Northrop | last post by:
I am trying to create an interface for clients to send newsletters through mailing lists. for the past few weeks I have been exploring options though i still feel a bit lost. First I tried to setup a drupal site with a newsletter module installed. I was successful at creating the basic site though learning drupal seemed like a rather large task and the end result seemed not so user friendly. Next I explored PHPList in hopes of...
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9517
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10428
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9997
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.