- 论坛徽章:
- 0
|
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
|
|