一些函数的集合

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 10

struct node
{
    int num;
    struct node *next;
};
struct stud
{
    char name[10];
    int num;
}sw[5],sr[5],*pw,*pr;

void selectsort(int *);
void bubblesort(int *);
void quicksort(int *,int,int);
void InsertSort(int *,int );
int BinSearch(int *,int,int,int);
struct node * creat(int);
void reverse(struct node *);
struct node *merge(struct node *,struct node*);
struct node * circle(int);
int select(struct node *,int ,int );
void WordCount(char str[]);

int main()
{
    int i,j;
    int a[10]={3,2,5,4,1,8,9,7,6,0};

    for(i=0;i<SIZE;i++)
    {
        printf("%d ",a[i]);
    }
    printf("\n");
    //bubblesort(a);
    //selectsort(a);
    //quicksort(a,0,SIZE-1);
    InsertSort(a,SIZE);

    for(j=0;j<SIZE;j++)
    {
       printf("%d ",a[j]);
    }
    int x;
    printf("\nPlease input the number you want to find:");
    scanf("%d",&x);
    int pos=BinSearch(a,0,SIZE-1,x);
    printf("The position of %d is: %d\n",x,pos+1);

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

    struct node *cir;
    int c,king;
    printf("The total of circle:");
    scanf("%d",&c);
    cir=circle(c);
    king=select(cir,c,3);
    printf("The king is:%d\n",king);

    //**************************
    struct node *p,*q,*m;
    int n,mm;
    printf("Please input the total node you want:");
    scanf("%d",&n);
    p=creat(n);
    printf("Please input the total node you want:");
    scanf("%d",&mm);
    q=creat(mm);

    /*
    p=p->next;
    while(p)
    {
        printf("%d ",p->num);
        p=p->next;
    }*/
    //printf("\nafter reverse:\n");
    //q=creat(n);
    //reverse(p);
    m=merge(p,q);
    printf("\nafter reverse:\n");
    m=m->next;
    while(m)
    {
        printf("%d ",m->num);
        m=m->next;
    }
    /*
    p=p->next;
    while(p)
    {
        printf("%d ",p->num);
        p=p->next;
    }
    */

    //

    FILE *fp;
    if((fp=fopen("d:\\st.dat","wb+"))==NULL)
    {
        printf("Cannot open!\n");
        return ;
    }
    pw=sw;
    pr=sr;
    printf("Please input data:name num\n");
    for(i=0;i<5;i++)
    {
        scanf("%s%d",sw[i].name,&sw[i].num);
    }
    fwrite(pw,sizeof(struct stud),5,fp);
    rewind(fp);
    fread(pr,sizeof(struct stud),5,fp);
    for(i=0;i<5;i++)
        printf("%s %d",sw[i].name,sw[i].num);

    return 0;
}
void selectsort(int a[])
{
    int i,j,key,temp;
    for(i=0;i<SIZE-1;i++)
    {
        key=i;
        for(j=i+1;j<SIZE;j++)
        {
            if(a[key]>a[j])
                key=j;
        }
        if(key!=i)
        {
            temp=a[key];
            a[key]=a[i];
            a[i]=temp;
        }
    }
}

void bubblesort(int *a)
{
    int i,j,temp;
    for(i=0;i<SIZE-1;i++)
    {
        for(j=0;j<SIZE-i-1;j++)
        {
            if(a[j]>a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
    }
}

void quicksort(int *a,int l,int r)
{
    int i=l,j=r,key=a[l];
    if(l>=r)
        return ;
    while(i<j)
    {
        while(i<j&&a[j]>=key)
            j--;
        a[i]=a[j];
        while(i<j&&a[i]<=key)
            i++;
        a[j]=a[i];
    }
    a[i]=key;
    quicksort(a,l,i-1);
    quicksort(a,i+1,r);
}

int BinSearch(int *a,int s,int t,int key)
{
    int low=s,high=t,mid;
    if(s<=t)
    {
        mid=(low+high)/2;
        if(a[mid]==key)
            return mid;
        else if(a[mid]>key)
            return BinSearch(a,low,mid-1,key);
        else
            return BinSearch(a,mid+1,high,key);
    }
    return -1;
}

struct node * creat(int n)
{
    int i;
    struct node *head,*p,*tail;
    head=(struct node *)malloc(sizeof(struct node));
    head->next=NULL;
    tail=head;
    for(i=0;i<n;i++)
    {
        p=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&p->num);
        p->next=NULL;
        tail->next=p;
        tail=p;
    }
    return head;

}
void reverse(struct node *head)
{
    struct node *p,*q;
    p=head->next;
    head->next=NULL;
    while(p)
    {
        q=(struct node *)malloc(sizeof(struct node));
        q->num=p->num;
        q->next=head->next;
        head->next=q;
        p=p->next;
    }
}

struct node *merge(struct node *head1,struct node *head2)
{
    struct node *h1,*h2,*tail;
    h1=head1->next;
    h2=head2->next;
    tail=head1;
    head2->next=NULL;
    while(h1&&h2)
    {
        if(h1->num<h2->num)
        {
            tail->next=h1;
            tail=h1;
            h1=h1->next;
        }
        else
        {
            tail->next=h2;
            tail=h2;
            h2=h2->next;
        }
    }
    if(h1)
        tail->next=h1;
    else
        tail->next=h2;
    return head1;
}

struct node * circle(int n)
{
    struct node *p,*head,*tail;
    int i;
    head=(struct node *)malloc(sizeof(struct node));
    tail=head;
    head->next=NULL;
    scanf("%d",&head->num);
    for(i=2;i<=n;i++)
    {
        p=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&p->num);
        p->next=NULL;
        tail->next=p;
        tail=p;
    }
    p->next=head;
    return head;
}

int select(struct node *head,int n,int m)
{
    struct node *p,*q;
    int i=0,count=0;
    //p=head->next;
    q=head;
    while(q->next!=head)
        q=q->next;
    while(count<n-1)
    {
        i++;
        p=q->next;
        if(i%m==0)
        {
            printf("%3d ",p->num);
            q->next=p->next;
            count++;
        }
        else
            q=p;
    }
    printf("\n");
    return q->num;
}

void InsertSort(int a[],int length)
{
    int i,j,key;
    //length=strlen(a);
    for(i=1;i<length;i++)
    {
        key=a[i];
        for(j=i-1;j>=0;j--)
        {
            if(a[j]>key)
                a[j+1]=a[j];
            else
                break;
        }

        a[j+1]=key;
    }
}
void WordCount(char str[])
{
    int i,word=0,num=0;
    for(i=0;str[i]!=‘\0‘;i++)
    {
        if(str[i]==‘ ‘)
            word=0;
        else if(word==0)
        {
            num++;
            word=1;
        }
    }
    printf("Total Number:%d\n",num);

}

  

时间: 2025-01-21 21:11:20

一些函数的集合的相关文章

laravel的filter()方法的使用 (方法使用给定的回调函数过滤集合的内容,只留下那些通过给定真实测试的内容)

filter 方法使用给定的回调函数过滤集合的内容,只留下那些通过给定真实测试的内容: $collection = collect([1, 2, 3, 4]); $filtered = $collection->filter(function ($value, $key) { return $value > 2; }); $filtered->all(); // [3, 4] 實例代碼: //商家 $business= Business::Status(1)->get(); //帥

python -- 函数、集合

1.集合 集合也属于一种数据类型,类似于list,具有无序.去重的特点,即集合里没有重复的数据. 通过{}来定义集合,用set()来强制转换成集合. 1 list = [1,2,2,4,5,3,2] #定义列表 2 s_list = set(list) #强制转换为集合 3 print(s_list) 4 5 s_list_2 = {1,2,3,3,4,5} #通过{}直接定义集合 6 print(s_list_2) 集合操作 set1.intersection(set2) 交集:两个集合都有

01Python内置函数_集合操作类

basestring basestring() 说明:basestring是str和unicode的超类(父类),也是抽象类, 因此不能被调用和实例化,但可以被用来判断一个对象是否为str或者unicode的实例, isinstance(obj, basestring) 等价于isinstance(obj, (str, unicode)): 版本:python2.3版本以后引入该函数,兼容python2.3以后python2各版本. 注意:python3中舍弃了该函数,所以该函数不能在pytho

python学习笔记 - 函数,集合,包,模块

一.函数 a=1, b=2, 交换值 定义中间量c,C=None, a,b=b,a a,b,c=1,2,3 sys.argv 实现指定的某些功能,使用的时候可以直接调用,简化代码,提高代码复用性 def fun():#定义一个函数,后面是函数名                print("Hello World")#函数体 例如: 1.def sayHello(): print("Hello World") sayHello()  --调用 2.def sayNam

hihocoder #1152 Lucky Substrings 【字符串处理问题】strsub()函数+set集合去重

#1152 : Lucky Substrings时间限制:10000ms单点时限:1000ms内存限制:256MB描述A string s is LUCKY if and only if the number of different characters in s is a fibonacci number. Given a string consisting of only lower case letters, output all its lucky non-empty substrin

std::map中函数用法集合

1 STL的map表里有一个erase方法用来从一个map中删除掉指令的节点  2 eg:  3 map<string,string> mapTest;  4 typedef map<string,string>::iterator ITER;  5 ITER iter=mapTest.find(key);  6 mapTest.erase(iter);  7 像上面这样只是删除单个节点,map的形为不会出现任务问题,  8 但是当在一个循环里用的时候,往往会被误用,那是因为使用者

php xml常用函数的集合(比较详细)

1.DOM 函数 a.DOMDocument-<load()作用:加载xml文件用法:DOMDocument-<load( string filename )参数:filename,xml文件:返回:如果成功则返回 TRUE,失败则返回 FALSE. b.DOMDocument-<loadXML()作用:加载xml代码用法:DOMDocument-<loadXML( string source )参数:source xml代码:返回:如果成功则返回 TRUE,失败则返回 FALSE

python(四)切片,内置函数,集合,json操作

1. 切片 切片是只取列表或字符串中一部分或全部. 定义一个list:l=['Monica','Josie','Shirley','Leo','Tina'] s=[:]   取的是整个列表元素 s=[0:2]  取得是下标为零和1的元素(不管是从前数还是从后数,切片的特点包头不包尾) s=[:2]  取得是小标为0和1的元素,当冒号前面不写下标时,默认从第一个元素开始 s=[2:]  取得是下标为2一直到这个列表的最后一个 s=[-3:-1] 写负数是从列表的最后开始数,要把小的写在前面 s=[

javascript基础 方法 函数 闭包 集合

定义类 ,实例化对象类 ,调用 为类对象增加数据成员 --