| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2064 人关注过本帖, 4 人收藏
标题:C程序学习实例,将不断更新
只看楼主 加入收藏
heroinearth
Rank: 10Rank: 10Rank: 10
来 自:云南曲靖
等 级:青峰侠
帖 子:430
专家分:1506
注 册:2011-10-24
收藏
得分:0 
快速排序和伪随机数生成
程序代码:
#include<stdio.h>
#define NUM 30
static unsigned long int next=1;

extern int rand0(void);
void quick(unsigned int *a,unsigned int i,unsigned int j);

int main(void)
{
    int count;
    unsigned int arr[NUM];
    for(count = 0;count < NUM; count++)
    {
        arr[count]=rand0();
        printf("%6u",arr[count]);
        if((count+1)%12==0)
                printf("\n");
    }
    quick(arr,0,NUM -1);
    printf("\nTo sorte is : \n");
    for(count = 0;count < NUM; count++)
    {
        printf("%6u",arr[count]);
        if((count+1)%12==0)
                printf("\n");
    }

    return 0;
}

void quick(unsigned int *a,unsigned int i,unsigned int j) //快速排序函数,a为要排序的数组,i为开始下标,j为结束下标。

{
    unsigned int m,n,temp;
    unsigned int k; 

    m=i;
    n=j;
    k=a[(i+j)/2]; /*选取的参照*/
    do /*以k为参照,将数组分为小于和大于两个部份*/
    {
        while(a[m]<k&&m<j) m++; /* 从左到右找比k大的元素*/
        while(a[n]>k&&n>i) n--; /* 从右到左找比k小的元素*/
        if(m<=n)
        { /*若找到且满足条件,则交换*/
            temp=a[m];
            a[m]=a[n];
            a[n]=temp;
            m++;
            n--;
        }
    }while(m<=n);
    if(m<j) quick(a,m,j); /*还没有从左找到右,运用递归,下面同理*/
    if(n>i) quick(a,i,n);
} 

int rand0(void)//随机数生成函数
{
    next=next * 1103515245 + 12345;
    return (unsigned int)(next/65536)%32768;
}

 
2012-11-20 15:07
heroinearth
Rank: 10Rank: 10Rank: 10
来 自:云南曲靖
等 级:青峰侠
帖 子:430
专家分:1506
注 册:2011-10-24
收藏
得分:0 
函数指针和菜单实现
程序代码:
#include<stdio.h>
#include<string.h>
#include<ctype.h>

char showmenu(void);
void eatline(void);  //清空行
void show(void(* fp)(char * str),char *str);//在函数中调用函数,输出字符串
void ToUpper(char *);//字符串转大写
void ToLower(char *);//字符串转小写
void Transpose(char *);//大小写转置
void Dummy(char *);

int main(void)
{
    char line[81];
    char copy[81];
    char choice;
    void (*pfun)(char *);//函数指针 指向的函数接受一个char * 参数 并且没有返回值

    puts("Enter a string (empty line to quit):");
    while(gets(line)!=NULL && line[0]!='\0')
    {
        while((choice=showmenu())!='n')
        {
            switch(choice)//根据选择设置指针
            {
            case 'u':pfun=ToUpper;break;
            case 'l':pfun=ToLower;break;
            case 't':pfun=Transpose;break;
            case 'o':pfun=Dummy;break;
            }
            strcpy(copy,line);
            show(pfun,copy);
        }
        puts("Enter a string (empty line to quit):");
    }
    puts("Bye!");

    return 0;
}

char showmenu(void)
{
    char ans;
    puts("Enter nemu choice:");
    puts("u) upper case        l) lower case");
    puts("t) transposed case   o) original case");
    puts("n) next string");
    ans=getchar();
    ans=tolower(ans);
    eatline();
    while(strchr("ulton",ans)==NULL)
    {
        puts("Please enter a u,l,t,o,or n:");
        ans=tolower(getchar());
        eatline();
    }
    return ans;
}


void eatline(void)
{
    while(getchar()!='\n')
        continue;
}

void ToUpper(char *str)
{
    while(*str)
    {
        *str=toupper(*str);
        str++;
    }
}

void ToLower(char *str)
{
    while(*str)
    {
        *str=tolower(*str);
        str++;
    }
}


void Transpose(char *str)
{
    while(*str)
    {
        if(islower(*str))
            *str=toupper(*str);
        else if(isupper(*str))
            *str=tolower(*str);
        str++;
    }
}

void Dummy(char *str)
{
    ;
}

void show(void(* fp)(char *),char * str)
{
    (*fp)(str);
    puts(str);
}
2012-11-20 16:06
heroinearth
Rank: 10Rank: 10Rank: 10
来 自:云南曲靖
等 级:青峰侠
帖 子:430
专家分:1506
注 册:2011-10-24
收藏
得分:0 
爬楼梯问题
程序代码:
# include <stdio.h>
int n(int i)
{
    if(i==1)
        return 1;
    if(i==2)
        return 2;
    else
        return n(i-1)+n(i-2);
}
int main()
{
    int i;
    printf("请输入台阶数:");   
    while(scanf("%d",&i)==1 && i>0)
        printf("走法有: %d种。\n请输入台阶数:",n(i));

   
    return 0;
}

 
2012-11-22 14:26
heroinearth
Rank: 10Rank: 10Rank: 10
来 自:云南曲靖
等 级:青峰侠
帖 子:430
专家分:1506
注 册:2011-10-24
收藏
得分:0 
程序代码:
/*这是一个基于链表的学生管理系统,我练习用的,还有两个大的功能(一是排序,二是插入)和许多细小的功能没有完成,*/
/*这是一个学生信息管理系统*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

struct achievement{
    int Chinese;
    int Mathe;
    int English;
};

struct stdinf{
    char stdid[14];
    char name[20];
    struct achievement achiev;
    int score;
    float aver;
    struct stdinf *next;
};

struct stdinf *openstdinf(void);
int savestdinf(struct stdinf *phead);
struct stdinf *addstdinf(void);
void showstdinf(const struct stdinf *phead);
int modifystdinf(struct stdinf *phead);
void insertstdinf(struct stdinf *phead);
struct stdinf *delstdinf(struct stdinf *phead);
int inputstdinf(struct stdinf *pstdinf);
char mainmenu(void);
struct stdinf *findstdinf(struct stdinf *phead);

int main(void)
{
    struct stdinf *phead,*pend,*pcurrent;
    char menuid=1;

    phead=openstdinf();
    pend=phead;
  
    if(pend!=NULL)
    {
        while(pend->next!=NULL)
            pend=pend->next;
    }

    while(menuid)
    {
      
        switch(menuid=mainmenu())
        {
        case 'a':
          
            if((pcurrent=addstdinf()) != NULL)
            {
                if(phead==NULL)
                {
                    phead=pcurrent;
                    pend=pcurrent;
                }
                else
                {
                    pend->next=pcurrent;
                    pend=pcurrent;
              
                }
                while(pend->next!=NULL)
                        pend=pend->next;
            }
            break;
      
        case 's':
            showstdinf(phead);
            break;

      
        case 'f':
            if((pcurrent=findstdinf(phead))!=NULL)
                printf("%s %s %d %d %d %d %f\n",pcurrent->stdid,pcurrent->name,pcurrent->achiev.Chinese,pcurrent->achiev.Mathe,pcurrent->achiev.English,pcurrent->score,pcurrent->aver);
            else
                printf("No this student's infor\n");
            getchar();

            break;
      
        case 'm':
            modifystdinf(phead);
            break;
      
        case 'i':
            printf("you press %c\n",menuid);
            break;
      
        case 'd':
            phead=delstdinf(phead);
            break;
      
        case 'q':
            savestdinf(phead);
            menuid=0;
            break;
        }
    }
    return 0;
}

struct stdinf *openstdinf(void)
{
    int n;
    struct stdinf *phead=NULL,*pend=NULL,*pcurrent=NULL;
    FILE *pfile;


    pfile=fopen("stdinf.txt","r");
    if(pfile!=NULL)
    {
        pcurrent=(struct stdinf *)malloc(sizeof(struct stdinf));
        if(pcurrent==NULL)
        {
            printf("System error ,exit!\n");
            exit(1);
        }
      
        while((n=fscanf(pfile,"%s%s%d%d%d%d%f",pcurrent->stdid,pcurrent->name,&pcurrent->achiev.Chinese,&pcurrent->achiev.Mathe,&pcurrent->achiev.English,&pcurrent->score,&pcurrent->aver))!=EOF && n==7 )
        {
            pcurrent->next=NULL;
            if(phead==NULL)
            {
                phead=pcurrent;
                pend=pcurrent;
            }
            else
            {
                pend->next=pcurrent;
                pend=pcurrent;
            }
            pcurrent=(struct stdinf *)malloc(sizeof(struct stdinf));
            if(pcurrent==NULL)
            {
                printf("System error ,exit!\n");
                exit(1);
            }
        }
        free(pcurrent);
        fclose(pfile);
    }
  
    return phead;
}


char mainmenu(void)
{
    char c;
    int i=1;
    while(i)
    {
        system("cls");
        printf("\n\n       ******* Welcome to student information manage system *******\n\n");
        printf("             a Add student infor       s Show student infor\n");
        printf("             f Find student infor      m Modi student infor\n");
        printf("             i Insert student infor    d Del student infor\n");
        printf("             q Save and exit system\n\n");
        printf("             Plaese choose:");
        c=getch();
        if(c=='a' || c=='s' || c=='f' || c=='m' || c=='i' || c=='d' || c=='q')
        {
            break;
        }
    }
    return c;
}

struct stdinf *delstdinf(struct stdinf *phead)
{
    char szstdid[7],szbuffer[30];
    struct stdinf *pcurrent,*pbefore;
    printf("\nInput student's ID that you want to delete:");
    gets(szbuffer);
    sscanf(szbuffer,"%6s",szstdid);
    if(szstdid[0]!='\0')
    {
        pcurrent=phead;
        while(pcurrent!=NULL)
        {
            if(strcmp(pcurrent->stdid,szstdid)==0)
                break;
            pbefore=pcurrent;
            pcurrent=pcurrent->next;
        }
    }
    if(pcurrent!=NULL)
    {
        if(pcurrent==phead)
        {
            phead=pcurrent->next;
            free(pcurrent);
        }
        else
        {
            pbefore->next=pcurrent->next;
            free(pcurrent);
        }

    }
    else
    {
        printf("No this student infor!\n");
        return 0;
    }
    return phead;
}


int modifystdinf(struct stdinf *phead)
{
    char szstdid[7],szbuffer[30];
    struct stdinf *pcurrent;
    printf("\nInput student's ID that you want to modify:");
    gets(szbuffer);
    sscanf(szbuffer,"%6s",szstdid);
    if(szstdid[0]!='\0')
    {
        pcurrent=phead;
        while(pcurrent!=NULL)
        {
            if(strcmp(pcurrent->stdid,szstdid)==0)
                break;
            pcurrent=pcurrent->next;
        }
    }
    if(pcurrent!=NULL)
    {
        inputstdinf(pcurrent);
        return 1;
    }
    else
    {
        printf("No this student infor!\n");
        return 0;
    }
}

struct stdinf *findstdinf(struct stdinf *phead)
{
    char szstdid[7],szbuffer[30];
    struct stdinf *pcurrent;
    printf("\nInput student's ID:");
    gets(szbuffer);
    sscanf(szbuffer,"%6s",szstdid);
    if(szstdid[0]!='\0')
    {
        pcurrent=phead;
        while(pcurrent!=NULL)
        {
            if(strcmp(pcurrent->stdid,szstdid)==0)
                break;
            pcurrent=pcurrent->next;
        }
    }
    return pcurrent;
}


int savestdinf(struct stdinf *phead)
{
    struct stdinf *pcurrent,*pbefor;
    FILE *pfile;
    if((pfile=fopen("stdinf.txt","w"))==NULL)
    {
        printf("Can't save file.\n");
        return 0;
    }
    pcurrent = phead;
    while(pcurrent != NULL)
    {
        fprintf(pfile,"%s %s %d %d %d %d %f\n",pcurrent->stdid,pcurrent->name,pcurrent->achiev.Chinese,pcurrent->achiev.Mathe,pcurrent->achiev.English,pcurrent->score,pcurrent->aver);
        pbefor=pcurrent;
        pcurrent=pcurrent->next;
        free(pbefor);
    }
    fclose(pfile);
    return 1;
}

struct stdinf *addstdinf(void)
{
    struct stdinf *phead=NULL,*pend=NULL,*pcurrent=NULL;
    int n=5;

    while(n==5)
    {
        pcurrent=(struct stdinf *)malloc(sizeof(struct stdinf));
        if(pcurrent==NULL)
        {
            printf("Error ,can't add stdinf.\n");
            break;
        }
        n=0;
        n=inputstdinf(pcurrent);
      
        if(n==5)
        {
            pcurrent->next=NULL;
            if(phead==NULL)
            {
                phead=pcurrent;
                pend=pcurrent;
              
            }
            else
            {
                pend->next=pcurrent;
                pend=pcurrent;
                pend->next=NULL;
            }
        }
  
    }
    if(pcurrent!=NULL)
        free(pcurrent);

    return phead;
}

void showstdinf(const struct stdinf *phead)
{
    const struct stdinf *pcurrent;
    pcurrent=phead;
    if(pcurrent==NULL)
        printf("\nNo record !\n");
    else
    {
        printf("\n stdid     name    Chinese  Mathe  English  score  aver \n");
        while(pcurrent!=NULL)
        {
            printf("%6s %-8s %7d %7d %7d %8d %6.1f\n",pcurrent->stdid,pcurrent->name,pcurrent->achiev.Chinese,pcurrent->achiev.Mathe,pcurrent->achiev.English,pcurrent->score,pcurrent->aver);
            pcurrent=pcurrent->next;
        }
    }
    getch();
}

int inputstdinf(struct stdinf *pcurrent)
{
    char szarr[51],c;
    int ibool=1,n=0,istrlen;
  
    printf("\nPlease input student information ,press Enter only return.\nstdid  name   Chinese Mathe English\n");
    while(ibool)
    {      
        fgets(szarr,51,stdin);
        istrlen=strlen(szarr);
        fflush(stdin);
  
        if(szarr[0]=='\0' || istrlen<17 )
        {
            printf("Do you want to return(y/n):");
            c=getch();
            fflush(stdin);
            while(c!='y' && c!='Y' && c!='n' && c!='N')  
            {
                c=getch();
            }
          
            if(c=='y' || c=='Y')
            {
                ibool=0;
                n=0;
            }
            else
                printf("Input again:\n");
          
        }
        else //分离出所要的数据
        {
            n=sscanf(szarr,"%6s %18s %d %d %d",pcurrent->stdid,pcurrent->name,&pcurrent->achiev.Chinese,&pcurrent->achiev.Mathe,&pcurrent->achiev.English);
            if(n!=5)
            {
                printf("Data error! Input again.\n");
            }
            else
            {
              
                ibool=0;
                pcurrent->score=pcurrent->achiev.Chinese + pcurrent->achiev.Mathe + pcurrent->achiev.English;
                pcurrent->aver=(float)pcurrent->score/(float)3;
                //pcurrent->next=NULL;
            }
        }
    }
  
    return n;
}

2012-12-05 09:29
快速回复:C程序学习实例,将不断更新
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021987 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved