免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1581 | 回复: 0
打印 上一主题 下一主题

第四周:C语言 函数指针,结构体 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-21 08:41 |只看该作者 |倒序浏览
1.面试题:
有一组数,从1~1000, 求最大3个数的重复次数。
思路:遍历一遍这些数字,作为1001个数组的下标,然后从后往前遍历头三个数


2.填空题:
4 ? 3 ? 3 = 15
求?所代表的运算符号。

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

int add(int a,int b)
{
    return a+b;
}
int mul(int a,int b)
{
    return a*b;
}

int sub(int a,int b)
{
    return a-b;
}

int divide(int a,int b)
{
    return a/b;
}

main(void)
{
    int a=4,b=3,c=3;
    char *op[]={"+","*","-","/"};
    int (*my_op[])(int ,int)={add,mul,sub,divide};
    int i,j;
    int result=0;
    for(i=0;i<4;i++)
    {
        result =my_op[i](a,b);
        for(j=0;j<4;j++)
            {
                if(my_op[j](result,c) == 15)
                    printf("%d %s %d %s %d = 15\n",a,op[i],b,op[j],c);
            }
    }   
}


3.结构体:
/***********************
 *  结构
 **********************/

1)结构的基本知识

考虑一下,如果我们要存储一个复数,或者是一个矢量,或者是一个坐标值。

struct point {
    int x;
    int y;
};

关键字 struct 引入结构声明。结构声明由包含在花括号内的一系列声明组成。
关键字 struct 后面的名字是可选的,称为结构标记。结构标记为结构命名。
在定义之后,结构标记就代表花括号内的声明,可以用它作为该声明的简写形式。

结构中定义的变量称为成员。结构成员、结构标记和普通变量可以重名。

struct point a, b, c;
struct {...} x, y, z;
int x, y, z;

初始化,必须是常量表达式
struct point foo = {3000, 8000};

引用
结构名.成员

结构可以嵌套。

sizeof 对象
sizeof(类型名)

基本代码:

::::::::::::::
list.c
::::::::::::::
#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE    30

struct student
{
    char name[MAXSIZE];
    int id;
    int score;
};

struct node_st{
    struct student mydata;
    struct node_st *next;
};

int insert(struct node_st **list, struct student *data)
{
    struct node_st *new;
    new = malloc(sizeof(*new));
    if(new == NULL)
        return -1;
    new->mydata = *data;
    new->next = *list;
    *list = new;

    return 0;   
}

void travel(struct node_st *list)
{
    for(list;list != NULL;list = list->next)
        printf("name:%s   id:%d   score:%d\n",list->mydata.name, list->mydata.id, list->myda
ta.score);
}

struct node_st *find(struct node_st *list, int id)
{
    for(list;list != NULL;list = list->next)
    {
        if(list->mydata.id == id)
            return list;
    }
    return NULL;
}

void delete(struct node_st *list ,int id)
{
    struct node_st * current = list;
    struct node_st * pre = list;
    for(current;current != NULL;current = current->next)
    {
        if(current->mydata.id == id)
            pre->next = current->next;
        pre = current;   
    }
}

void destroy(struct node_st *list)
{
    struct node_st *pre = list;
    for(list;list != NULL;list = list->next)
    {
        if(pre != list)
            free(pre);
        pre = list;
    }
    free(pre);

}

::::::::::::::
main.c
::::::::::::::
#include "list.c"

main(void)
{
    struct node_st *list = NULL;
    struct student person;
    struct node_st *current = NULL;
    int i;
    for(i=0;i<10;i++)
    {
    snprintf(person.name,8,"naitao%d",i);
    person.id=9402207+i;
    person.score=100-i;

    insert(&list, &person);
    }
    current = find(list,9402209);
    if(current != NULL)
        printf("%s: %d : %d\n",current->mydata.name,current->mydata.id,current->mydata.score
);
    delete(list,9402209);
    travel(list);
    destroy(list);
       
}

4.作业:学生成绩管理系统

::::::::::::::
main.c
::::::::::::::
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "scorelist.c"

void input_info(struct student *stu)
{
    char name[MAXSIZE];
    int i=0;
    char ch;
    printf("NAME:");
    while((ch=getchar())==':');
//    fgets(stu->name,8,stdin);
    while((name[i++]=getchar())!='\n');
    name[--i]='\0';
    strcpy(stu->name,name);
    printf("ID:");
    scanf("%d",&stu->id);
    printf("SCORE:");
    scanf("%d", &stu->score);
}

main(void)
{
    char input  = '1';
    int id;
    char ch;
    int i;
    struct student person;
    struct node_st *list = NULL;
    struct node_st *newlist = NULL;
    struct node_st *current;
   

    enum {QUIT,INSERT,DELETE,FINDID,FINDNAME,APPEND,READ};
    while(isdigit(input))
    {
    printf("Please enter your choice:\n");
    printf("1-----Insert a student's information\n");
    printf("2-----Delete a student's information\n");
    printf("3-----Find a student's information with ID\n");
    printf("4-----Find a student's information with name\n");
    printf("5-----Append the student's informations into file\n");
    printf("6-----Read the informations from file\n");
    printf("0-----Quit\n");
    scanf(" %c",&input);
    switch(input-'0'){
        case INSERT:
            input_info(&person);
            insert(&list,&person);
            printf("\n");
            printf("\n");
            printf("\n");
            printf("========================================\n");
            travel(list);
            printf("========================================\n");
            break;
        case DELETE:
            printf("please input id:");
            scanf("%d", &id);
            delete(list,id);
            break;
        case FINDID:
            printf("please input id:");
            scanf("%d", &id);
            newlist = NULL;
            idfind(list,id,&newlist);
        //    current = find(list,id);
//            if(current != NULL)
  //                      printf("NAME:%s ID:%d   SCORE:%d\n",current->mydata.name, current->m
ydata.id, current->mydata.score);

            if(newlist != NULL)
            {
                travel(newlist);
                destroy(newlist);
            }
            else
                printf("No found!\n");
            break;
        case FINDNAME:
            while((ch=getchar()) != '\n');
            //printf("name:\n");
            //while((ch=getchar())!='\n');
//            fgets(person.name,CHECKSIZE,stdin);
//            person.name[8]='\0';
            i = 0;
            while((person.name[i++]=getchar())!='\n');
            person.name[--i]='\0';
           
    //        printf("%s\n",person.name);
            newlist = NULL;
            namefind(list, person.name, &newlist);
            if(newlist != NULL)
            {
                travel(newlist);
                destroy(newlist);
            }
            else
                printf("No found!\n");
            break;
        case READ:
            list = NULL;
            readlist(&list);
            break;
        case APPEND:
            append(list);
            break;
        case QUIT:
            return 0;
            break;
        }
    }
}
::::::::::::::
scorelist.c
::::::::::::::
#include <sys/types.h>
#include <unistd.h>

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

#define MAXSIZE 30
#define CHECKSIZE 8
#define MAX    100

static    char    *name = "test.txt";
static    char    *name2 = "test2.txt";
static    FILE    *fp;
//static    FILE    *fp2;
static    char     buffer1[MAX];
static    char     buffer2[MAX];

struct student
{
        char name[MAXSIZE];
        int id;
        int score;
};

struct node_st{
        struct student mydata;
        struct node_st *next;
};

void append(struct node_st *list)
{
    struct node_st *current = NULL;
    fp=fopen(name,"a+");
    int check=0;

    for(current = list; current != NULL; current = current->next)
    {   
        check = 0;
        sprintf(buffer1,"NAME:%8s ID:%4d SCORE:%4d\n",current->mydata.name, current->mydata.
id, current->mydata.score);
        fseek(fp,0,0);
        while(fgets(buffer2,MAX,fp)!=NULL)
        {
        if(strcmp(buffer2,buffer1)==0)
            {
            check = 1;
            break;
            }
        }
        if(check == 0)
        {
        //fseek(fp,0,2);   
         fputs(buffer1,fp);
         printf("%s",buffer1);
        }
    }
    fclose(fp);
}

int insert(struct node_st **list, struct student *data)
{
        struct node_st *new;
        new = malloc(sizeof(*new));
        if(new == NULL)
                return -1;
        new->mydata = *data;
        new->next = *list;
        *list = new;
                                    return 0;
}

void travel(struct node_st *list)
{
        for(list;list != NULL;list = list->next)
                printf("NAME:%8s ID:%4d   SCORE:%4d\n",list->mydata.name, list->mydata.id, list->myd
ata.score);
}

struct node_st *find(struct node_st *list, int id)
{
        for(list;list != NULL;list = list->next)
            {
                if(list->mydata.id == id)
                        return list;
            }
        return NULL;
}
void readlist(struct node_st **list)
    {
        struct student tempdata;
        fp=fopen(name,"r+");
        while(fgets(buffer1,MAX,fp)!=NULL)
        {
            sscanf(buffer1,"NAME:%8s ID:%4d SCORE:%4d\n",tempdata.name, &tempdata.id, &t
empdata.score);
//            printf("%s %d %d\n",tempdata.name, tempdata.id, tempdata.score);
            insert(list, &tempdata);
                    printf("%s",buffer1);
        }
        fclose(fp);
    }


void idfind(struct node_st *list, int id, struct node_st **newlist)
{
        for(list;list != NULL;list = list->next)
            {
                if(list->mydata.id == id)
                        insert(newlist,&list->mydata);
            }
}

void namefind(struct node_st *list, char *name, struct node_st **newlist)
{
        for(list;list != NULL;list = list->next)
            {
    //    printf("%s : %s \n",list->mydata.name, name);
                if(strncmp(list->mydata.name, name, CHECKSIZE)==0)
                        insert(newlist,&list->mydata);
            }
   
}

void delete(struct node_st *list ,int id)
{
        struct node_st * current = list;
        struct node_st * pre = list;
        for(current;current != NULL;current = current->next)
            {
                if(current->mydata.id == id)
                        pre->next = current->next;
                pre = current;
            }
}

void destroy(struct node_st *list)
{
        struct node_st *pre = list;
        for(list;list != NULL;list = list->next)
            {
                if(pre != list)
                       free(pre);
                pre = list;
            }
        free(pre);
    pre=NULL;

}

::::::::::::::
test.txt
::::::::::::::
NAME:     yep ID:9302214 SCORE: 100
NAME: jeffery ID: 100 SCORE: 100
NAME:  naitao ID:9312217 SCORE: 100
NAME:     yep ID:9402213 SCORE: 100




您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP