c算法150例!
-
- 帖子: 33
- 注册时间: 2011-03-12 17:13
Re: c算法150例!
谢谢楼主啊,下来慢慢看~~
-
- 帖子: 21
- 注册时间: 2011-02-26 6:14
Re: c算法150例!
乱码阿,好可惜阿
- 月下叹逍遥
- 论坛版主
- 帖子: 33994
- 注册时间: 2010-10-07 14:23
- 系统: Archdows10
- 来自: 某系某星某洲某国某省某市
- 联系:
- tangboyun
- 帖子: 701
- 注册时间: 2009-07-25 1:57
- 联系:
Re: c算法150例!
东西是不错。。。。lz是好人,应该被发卡。。。
不过这代码:
1、不规范。。。。年代貌似很久远。。。
2、没怎么注释和空行。。。。光溜溜的。。。
其实这种花点小钱弄本小开面的c语言算法速查手册之类的挺值的。。。

不过这代码:
1、不规范。。。。年代貌似很久远。。。
2、没怎么注释和空行。。。。光溜溜的。。。
其实这种花点小钱弄本小开面的c语言算法速查手册之类的挺值的。。。
https://github.com/tangboyun
http://tangboyun.is-programmer.com/
提问的智慧————Eric Steven Raymond
回答的智慧————Andrew Clarke
吾尝终日而思矣,不如须臾之所学也;吾尝跂而望矣,不如登高之博见也。
急急急标题什么的,最讨厌了!
急急复急急,急急何其多,我生待急急,万事急急急。
http://tangboyun.is-programmer.com/
提问的智慧————Eric Steven Raymond
回答的智慧————Andrew Clarke
吾尝终日而思矣,不如须臾之所学也;吾尝跂而望矣,不如登高之博见也。
急急急标题什么的,最讨厌了!
急急复急急,急急何其多,我生待急急,万事急急急。
- cjxgm
- 帖子: 1952
- 注册时间: 2010-04-23 20:40
- 系统: Arch Linux
- 来自: 浙江·杭州
- 联系:
Re: c算法150例!
随便打开了一个看看,这代码风格,那是叫一个乱(而且还乱码)
[c] #include "stdio.h"
#include "stdlib.h"
#include "ctype.h"
#include "string.h"
#define LENGTH sizeof(struct student)
static int n=0;
struct student
{
long number;
char name[20];
struct student *next;
};
struct student *new(struct student *head)
{
char numstr[20];
struct student *p,*p1=head,*p2=head;
p=(struct student *)malloc(LENGTH);
printf("\n Input the new record`s number:");
gets(numstr);
p->number=atol(numstr);
printf("\n Input the new record`s name:");
gets(p->name);
if(p->number>0)
{
if(head==NULL)
{
head=p;
p->next=NULL;
n++;
}
else
{
while((p->number>p1->number)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p->number<p1->number)
{
if(head==p1)
{
head=p;
p->next=p1;
n++;
}
else
{
p2->next=p;
p->next=p1;
n++;
}
}
else if(p->number==p1->number)
strcpy(p1->name,p->name);
else if(p1->next==NULL)
{
p1->next=p;
p->next=NULL;
n++;
}
}
}
else
printf("\n Input wrong number! \n");
return(head);
}
void list(struct student *head)
{
struct student *p=head;
int i=1;
if(head!=NULL)
{
printf("\n Now %d records are:\n\n record \t number \t name \n",n);
do{
printf("\n %6d \t %-16ld",i++,p->number);
puts(p->name);
p=p->next;
}while(p!=NULL);
}
else
printf("\n\n It is empty! \n\n");
}
struct student *delete(struct student *head)
{
struct student *p1,*p2;
long k;
char numstr[20];
printf("\n Input the number that you want to delete:");
k=atol(gets(numstr));
if(head==NULL)
printf("\n this is an empty list!\n");
else if(head->number==k)
{
p1=head;
head=head->next;
free(p1);
n--;
}
else
{
p1=head;
while(p1->number!=k&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->number==k)
{
p2->next=p1->next;
free(p1);
n--;
}
else
printf("\n The record is not in this list.\n");
}
return(head);
}
main()
{
struct student *head;
char ch;
int flag=1;
if(n==0)
head=NULL;
while(flag)
{
printf("\n Input 'N' or 'n' to input new record,'L' or 'l' to list all record,\n 'D' or 'd' to delete a record,other to exit:");
ch=toupper(getchar());
getchar();
if(ch=='N')
head=new(head);
else if(ch=='L')
list(head);
else if(ch=='D')
head=delete(head);
else
flag=0;
}
return;
}[/c]
汗,居然还有 Desktop_.ini?
再汗,这注释居然能让论坛崩溃?有图有真相
[c] #include "stdio.h"
#include "stdlib.h"
#include "ctype.h"
#include "string.h"
#define LENGTH sizeof(struct student)
static int n=0;
struct student
{
long number;
char name[20];
struct student *next;
};
struct student *new(struct student *head)
{
char numstr[20];
struct student *p,*p1=head,*p2=head;
p=(struct student *)malloc(LENGTH);
printf("\n Input the new record`s number:");
gets(numstr);
p->number=atol(numstr);
printf("\n Input the new record`s name:");
gets(p->name);
if(p->number>0)
{
if(head==NULL)
{
head=p;
p->next=NULL;
n++;
}
else
{
while((p->number>p1->number)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p->number<p1->number)
{
if(head==p1)
{
head=p;
p->next=p1;
n++;
}
else
{
p2->next=p;
p->next=p1;
n++;
}
}
else if(p->number==p1->number)
strcpy(p1->name,p->name);
else if(p1->next==NULL)
{
p1->next=p;
p->next=NULL;
n++;
}
}
}
else
printf("\n Input wrong number! \n");
return(head);
}
void list(struct student *head)
{
struct student *p=head;
int i=1;
if(head!=NULL)
{
printf("\n Now %d records are:\n\n record \t number \t name \n",n);
do{
printf("\n %6d \t %-16ld",i++,p->number);
puts(p->name);
p=p->next;
}while(p!=NULL);
}
else
printf("\n\n It is empty! \n\n");
}
struct student *delete(struct student *head)
{
struct student *p1,*p2;
long k;
char numstr[20];
printf("\n Input the number that you want to delete:");
k=atol(gets(numstr));
if(head==NULL)
printf("\n this is an empty list!\n");
else if(head->number==k)
{
p1=head;
head=head->next;
free(p1);
n--;
}
else
{
p1=head;
while(p1->number!=k&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->number==k)
{
p2->next=p1->next;
free(p1);
n--;
}
else
printf("\n The record is not in this list.\n");
}
return(head);
}
main()
{
struct student *head;
char ch;
int flag=1;
if(n==0)
head=NULL;
while(flag)
{
printf("\n Input 'N' or 'n' to input new record,'L' or 'l' to list all record,\n 'D' or 'd' to delete a record,other to exit:");
ch=toupper(getchar());
getchar();
if(ch=='N')
head=new(head);
else if(ch=='L')
list(head);
else if(ch=='D')
head=delete(head);
else
flag=0;
}
return;
}[/c]
汗,居然还有 Desktop_.ini?
再汗,这注释居然能让论坛崩溃?有图有真相