原来学C的时候,只是把它当编程基础来学的,现在再回头看一下,发现其实要是想编程,而不是做应用,还是C爽。(不代表偶认为c不可以做应用,相反,有的应用只有c可以做,不过做B/S的东东还是用java\c#\php方便点。)
一个简单的学生信息管理,由于是用vim编辑的,中文注释就没加。主要函数有三个,main()
new_stud() 和 listall() 一看就知道结构和功能啦,呵呵。
C
#include<stdio.h>
#include<stdlib.h>
struct stud
{
long num;
char name[20];
struct stud * next;
};
void new_stud();
void listall();
struct stud *head,*cur,*newp;
int main(char argc,char** argv )
{
char ch;
int isClose=0;
head=NULL;
while(!isClose)
{
printf("\n type 'E' or 'e' to enter new student,");
printf("\n type 'L' or 'l' to list all students:");
ch=getchar();
getchar();
switch(ch)
{
case 'e':
case 'E':new_stud();break;
case 'l':
case 'L':listall();break;
default:isClose=1;
};
}
return 0;
}
void new_stud()
{
char numstr[20];
newp=(struct stud *)malloc(sizeof(struct stud));
if(head==NULL)
{
head=newp;
}
else
{
cur=head;
while(cur->next!=NULL)
{
cur=cur->next;
}
cur->next=newp;
}
cur=newp;
printf("\n enter NO.:");
gets(numstr);
cur->num=atol(numstr);
printf("\n enter name:");
gets(cur->name);
cur->next=NULL;
}
void listall()
{
int i=0;
if(head==NULL)
{
printf("\n No Student!");
return;
}
cur=head;
do
{
printf("\n=============================");
printf("\n Num: %ld",cur->num);
printf("\n Name: %s",cur->name);
printf("\n------------------------");
cur=cur->next;
}
while(cur!=NULL);
}
#include<stdio.h>
#include<stdlib.h>
struct stud
{
long num;
char name[20];
struct stud * next;
};
void new_stud();
void listall();
struct stud *head,*cur,*newp;
int main(char argc,char** argv )
{
char ch;
int isClose=0;
head=NULL;
while(!isClose)
{
printf("\n type 'E' or 'e' to enter new student,");
printf("\n type 'L' or 'l' to list all students:");
ch=getchar();
getchar();
switch(ch)
{
case 'e':
case 'E':new_stud();break;
case 'l':
case 'L':listall();break;
default:isClose=1;
};
}
return 0;
}
void new_stud()
{
char numstr[20];
newp=(struct stud *)malloc(sizeof(struct stud));
if(head==NULL)
{
head=newp;
}
else
{
cur=head;
while(cur->next!=NULL)
{
cur=cur->next;
}
cur->next=newp;
}
cur=newp;
printf("\n enter NO.:");
gets(numstr);
cur->num=atol(numstr);
printf("\n enter name:");
gets(cur->name);
cur->next=NULL;
}
void listall()
{
int i=0;
if(head==NULL)
{
printf("\n No Student!");
return;
}
cur=head;
do
{
printf("\n=============================");
printf("\n Num: %ld",cur->num);
printf("\n Name: %s",cur->name);
printf("\n------------------------");
cur=cur->next;
}
while(cur!=NULL);
}
注: long atol(const char *nptr) 会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('')才结束转换,并将结果返回。 返回转换后的长整型数。