论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > C语言程序设计教程
Tag:新手,函数,指针,数据类型,对象,Turbo,入门,运算符,数组,结构,二级,,tc,游戏,试题,问答,编译,视频教程

C的数据库编程(2)

文章类别:C语言程序设计 | 发表日期:2008-9-24 14:36:18

  if (found_num != 0)
      printf("find %d\n",found_num);
  else
      printf("no record\n");
     }
     else
  printf("out record\n");
     break;
 case 7:
     printf("\n");
     printf("force:");
     scanf("%d",&find_force);
     if (head != nil) {
  c = head;
  l = c->next;
  found_num = 0;
  output_title();
  for (i = 1; i <= count; i++) {
      if (c->force == find_force) {
   output_it(c,i);
   found_num = found_num + 1;
   c = l;
   l = c->next;
      }
      else  {
   c = l;
   l = c->next;
      }    /*IF*/
  }    /*FOR*/
  if (found_num != 0)
      printf("find %d\n",found_num);
  else
      printf("no record\n");
     }
     else
  printf("out record\n");
     break;
 case 8:
     printf("\n");
     printf("smartness:");
     scanf("%d",&find_smartness);
     if (head != nil) {
  c = head;
  l = c->next;
                found_num = 0;
                output_title();
                for (i = 1; i <= count; i++) {
      if (c->smartness == find_smartness) {
   output_it(c,i);
                        found_num = found_num + 1;
   c = l;
   l = c->next;
      }
      else  {
   c = l;
   l = c->next;
      }    /*IF*/
  }    /*FOR*/
  if (found_num != 0)
      printf("find %d\n",found_num);
  else
      printf("no record\n");
     }
            else
  printf("out record\n");
     break;
 default:
     printf("find error\n");
     break;
    }    /*CASE*/

}

void sort_record (void)
{

int condition_code;
int i,
    j;

int long_c,long_f;
struct node *c_bak,
     *l_bak;

    condition_code = condition_select("sort condition:");
    switch (condition_code) {
 case 1:
     printf("record over\n");
     break;
 case 2:
     if ((head != nil) && (head->next != nil)) {
  c_bak = current;
  l_bak = last;    /*沉淀法排序*/
  for (i = count - 1; i >= 1; i--) {
      current = c_bak;
      last = l_bak;
      for (j = 1; j <= i; j++) {
   if (current->number < current->front->number)
       swap_it(¤t,&last,&c_bak,&l_bak);
                        current = current->front;
   last = current->next;
                    }
                }
                current = c_bak;
                last = l_bak;
     }
     printf("no over\n");
     break;
        case 3:
            if ((head != nil) && (head->next != nil)) {
                c_bak = current;
                l_bak = last;    /*沉淀法排序*/
                for (i = count - 1; i >= 1; i--) {
                    current = c_bak;
                    last = l_bak;
                    for (j = 1; j <= i; j++) {
                        if (strcmp(current->name,
    current->front->name) < 0)
                            swap_it(¤t,&last,&c_bak,&l_bak);
                        current = current->front;
   last = current->next;
                    }
                }
                current = c_bak;
                last = l_bak;
            }
     printf("name over\n");
            break;
        case 4:
     if ((head != nil) && (head->next != nil)) {
                c_bak = current;
                l_bak = last;    /*沉淀法排序*/
                for (i = count - 1; i >= 1; i--) {
                    current = c_bak;
                    last = l_bak;
                    for (j = 1; j <= i; j++) {
                        if (current->sex < current->front->sex)
                            swap_it(¤t,&last,&c_bak,&l_bak);
                        current = current->front;
                        last = current->next;
      }
  }
                current = c_bak;
                last = l_bak;
            }
     printf("sex over\n");
     break;
        case 5:
            if ((head != nil) && (head->next != nil)) {
                c_bak = current;
                l_bak = last;    /*沉淀法排序*/
                for (i = count - 1; i >= 1; i--) {
                    current = c_bak;
      last = l_bak;
                    for (j = 1; j <= i; j++) {    /*CURRENT*/
   long_c = current->birthdate.da_year;
                        long_c = long_c * 100;
   long_c = long_c + current->birthdate.da_mon;
                        long_c = long_c * 100;
   long_c = long_c + current->birthdate.da_day;
   /*FRONT*/                        long_f = current->front->birthdate.da_year;
                        long_f = long_f * 100;
                        long_f = long_f +
    current->front->birthdate.da_mon;
                        long_f = long_f * 100;
   long_f = long_f + current->front->birthdate.da_day;

   if (long_c < long_f)
       swap_it(¤t,&last,&c_bak,&l_bak);
   current = current->front;
   last = current->next;
      }
  }
  current = c_bak;
  last = l_bak;
     }
     printf("birthday over\n");
     break;
        case 6:
            if ((head != nil) && (head->next != nil)) {
  c_bak = current;
                l_bak = last;    /*沉淀法排序*/
                for (i = count - 1; i >= 1; i--) {
                    current = c_bak;
                    last = l_bak;
                    for (j = 1; j <= i; j++) {
                        if (current->experience < current->front->experience)
                            swap_it(¤t,&last,&c_bak,&l_bak);
                        current = current->front;
   last = current->next;
                    }
  }
                current = c_bak;
                last = l_bak;
            }
     printf("experience over\n");
     break;
        case 7:
            if ((head != nil) && (head->next != nil)) {
                c_bak = current;
  l_bak = last;    /*沉淀法排序*/
  for (i = count - 1; i >= 1; i--) {
                    current = c_bak;
                    last = l_bak;
                    for (j = 1; j <= i; j++) {
                        if (current->force < current->front->force)
                            swap_it(¤t,&last,&c_bak,&l_bak);
                        current = current->front;
                        last = current->next;
                    }
                }
  current = c_bak;
                last = l_bak;
     }
     printf("force over\n");
     break;
        case 8:
            if ((head != nil) && (head->next != nil)) {
                c_bak = current;
                l_bak = last;    /*沉淀法排序*/
  for (i = count - 1; i >= 1; i--) {
                    current = c_bak;
                    last = l_bak;
                    for (j = 1; j <= i; j++) {
                        if (current->smartness < current->front->smartness)
       swap_it(¤t,&last,&c_bak,&l_bak);
                        current = current->front;
                        last = current->next;
                    }
                }
                current = c_bak;
  last = l_bak;
            }
     printf("smartness over\n");
     break;
    }    /*CASE*/

}

/*BEGIN*/
void modify_record (void)
{

int condition_code;
int i,
    modified_num;


int long_c,long_m;
struct node *c,
     *l;
int mdf_num;
num_range1 mdf_number;
char mdf_name[11];
char mdf_sex;
struct date mdf_birthdate;
num_range2 mdf_experience;
num_range2 mdf_force;
num_range2 mdf_smartness;

    condition_code = condition_select("modify:");
    switch (condition_code) {
 case 1:
     printf("\n");
     printf("record:");
     scanf("%d",&mdf_num);
            if ((mdf_num >= 1) && (mdf_num <= count)) {
  if (head != nil) {
                    c = nil;
                    l = nil;    /*FIND NODE*/
                    for (i = 1; i <= mdf_num; i++) {
   if (i == 1) {
                            c = head;
                            l = c->next;
   }
                        else  {
                            c = l;
       l = c->next;
   }    /*IF*/
                    }    /*FOR*/
                    modify_it(c,l,i);
      printf("%d modify\n",mdf_num);
  }
  else
      printf("no record\n") ;
     }
            else
      printf("out record\n");
     break;
 case 2:
     printf("\n");
     printf("no:");
     scanf("%d",&mdf_number);
            if (head != nil) {
  c = head;
  l = c->next;
  modified_num = 0;
  for (i = 1; i <= count; i++) {
      if (c->number == mdf_number) {
   modify_it(c,l,i);
   modified_num = modified_num + 1;
   c = l;
   c = c->next;
      }
      else  {
   c = l;
   l = c->next;
      }    /*IF*/
  }    /*FOR*/
  if (modified_num != 0)
      printf(" %d modify\n",modified_num);
  else
      printf("no record\n");
     }
     else
      printf("no record\n");
     break;
 case 3:
     printf("\n");
     printf("name:");
     gets(mdf_name);
     if (head != nil) {
  c = head;
  l = c->next;
  modified_num = 0;
  for (i = 1; i <= count; i++) {
                    if (strcmp(c->name,mdf_name) == 0) {
                        modify_it(c,l,i);
   modified_num = modified_num + 1;
                        c = l;
   c = c->next;
      }
      else  {
   c = l;
   l = c->next;
      }    /*IF*/
  }    /*FOR*/
  if (modified_num != 0)
      printf(" %d modify\n",modified_num);
  else
      printf("no record\n");
     }
     else
      printf("no record\n");
     break;
 case 4:
     printf("\n");
     printf("sex:");
     scanf("%c",&mdf_sex);
     if (head != nil) {
  c = head;
  l = c->next;
  modified_num = 0;
  for (i = 1; i <= count; i++) {
      if (c->sex == mdf_sex) {
   modify_it(c,l,i);
   modified_num = modified_num + 1;
                        c = l;
   c = c->next;
                    }
                    else  {
                        c = l;
                        l = c->next;
      }    /*IF*/
  }    /*FOR*/
  if (modified_num != 0)
      printf(" %d modify\n",modified_num);
  else
      printf("no record\n");
     }
     else
      printf("no record\n");
     break;
 case 5:
     printf("\n");
     printf("birthday:");
     scanf("%d%d%d",&mdf_birthdate.da_year,&mdf_birthdate.da_mon,
      &mdf_birthdate.da_day);
     if (head != nil) {
  c = head;
                l = c->next;
                modified_num = 0;
  long_m = mdf_birthdate.da_year;
                long_m = long_m * 100;
  long_m = long_m + mdf_birthdate.da_mon;
                long_m = long_m * 100;
  long_m = long_m + mdf_birthdate.da_day;
  for (i = 1; i <= count; i++) {
      long_c = c->birthdate.da_year;
                    long_c = long_c * 100;
      long_c = long_c + c->birthdate.da_mon;
      long_c = long_c * 100;
      long_c = long_c + c->birthdate.da_day;
      if ((long_c == long_m)) {
                        modify_it(c,l,i);
   modified_num = modified_num + 1;
                        c = l;
                        c = c->next;
                    }
                    else  {
                        c = l;
                        l = c->next;
                    }
  }    /*FOR*/
                if (modified_num != 0)
      printf(" %d modify\n",modified_num);
  else
      printf("no record\n");
     }
     else
      printf("no record\n");
     break;
 case 6:
     printf("\n");
     printf("experience:");
     scanf("%d",&mdf_experience);
            if (head != nil) {
                c = head;
                l = c->next;
                modified_num = 0;
                for (i = 1; i <= count; i++) {
                    if (c->experience == mdf_experience) {
   modify_it(c,l,i);
   modified_num = modified_num + 1;
   c = l;
   c = c->next;
      }
      else  {
   c = l;
   l = c->next;
      }    /*IF*/
  }    /*FOR*/
  if (modified_num != 0)
      printf(" %d modify\n",modified_num);
  else
      printf("no record\n");
     }
     else
      printf("no record\n");
     break;
 case 7:
     printf("\n");
     printf("force:");
     scanf("%d",&mdf_force);
     if (head != nil) {
  c = head;
  l = c->next;
  modified_num = 0;
  for (i = 1; i <= count; i++) {
      if (c->force == mdf_force) {
   modify_it(c,l,i);
   modified_num = modified_num + 1;
   c = l;
   c = c->next;
      }
      else  {
   c = l;
   l = c->next;
      }    /*IF*/
  }    /*FOR*/
  if (modified_num != 0)
      printf(" %d modify\n",modified_num);
  else
      printf("no record\n");
     }
     else
      printf("no record\n");
     break;
 case 8:
     printf("\n");
     printf("smartness:");
     scanf("%d",&mdf_smartness);
            if (head != nil) {
  c = head;
                l = c->next;
  modified_num = 0;
                for (i = 1; i <= count; i++) {
                    if (c->smartness == mdf_smartness) {
                        modify_it(c,l,i);
                        modified_num = modified_num + 1;
                        c = l;
   c = c->next;
                    }
                    else  {
                        c = l;
                        l = c->next;
      }    /*IF*/
  }    /*FOR*/
  if (modified_num != 0)
      printf(" %d modify\n",modified_num);
  else
      printf("no record\n");
     }
     else
      printf("no record\n");
     break;
 default:
     printf("modify error\n");
     break;
    }    /*CASE*/

}

void read_record (void)
{

int n,i,len;
char *path;
char panduan;

    count = 0;    /*文件初始化*/
    printf("\n");
    printf("give the name of dbf like pb.txt:\n");
    gets(path);
    if((db_file=fopen(path,"rt"))==NULL)
 printf("this file is not exist");
    else
    {
    fseek(db_file,0,SEEK_SET);
    fscanf(db_file,"%d",&n);    /*读取最大记录数*/
    for (i = 1; i <=n; i++) {    /*数据载入缓冲*/
 fscanf(db_file,"%d",&number_c);
 fscanf(db_file,"%10s",&name_c);
 fseek(db_file,1,SEEK_CUR);
 fscanf(db_file,"%c",&sex_c);
 fscanf(db_file,"%5d",&birthdate_c.da_year);
 fscanf(db_file,"%3d",&birthdate_c.da_day);
 fscanf(db_file,"%3d",&birthdate_c.da_mon);
 fscanf(db_file,"%4d",&experience_c);
 fscanf(db_file,"%4d",&force_c);
 fscanf(db_file,"%4d",&smartness_c);
 add_record();
    }
    printf("file read end\n");
    printf("record number is %d \n",i-1);
    }

}

void input_record (void)
{

int n,err;

    n = count + 1;
    printf("\n");
    printf("after enter\n");
    number_c=0;
    while ( number_c!=-1 ) {
 printf("record %d\n",n);
 printf("No:");
 scanf("%d",&number_c);
 printf("name:");
 gets(name_c);
 gets(name_c);
 printf("sex(M,F):");
 scanf("%c",&sex_c);
 printf("birthda_day(1978 16 1):");
 scanf("%d %d %d",&birthdate_c.da_year,&birthdate_c.da_day,&birthdate_c.da_mon);
 printf("experience:");
 scanf("%d",&experience_c);
 printf("force:");
 scanf("%d",&force_c);
 printf("smartness:");
 scanf("%d",&smartness_c);
 printf("\n");
 err = check_record(&number_c,&sex_c,&birthdate_c,&experience_c,
  &force_c,&smartness_c);
 switch (err) {
     case 0:
  add_record();
  n = n + 1;
  break;
     case 1:
  printf("no wrong:1-1000\n");

  printf("try again\n");
  break;
     case 2:
  printf("sex wrong:M male F female\n");

  printf("try again\n");
  break;
     case 3:
  printf("birthday wrong:197 8 1 16\n");

  printf("try again\n");
  break;
     case 4:
  printf("experience wrong:0-100\n");

  printf("try again\n");
  break;
     case 5:
  printf("force wrong:0-100\n");

  printf("try again\n");
  break;
     case 6:
  printf("smartness wrong:0-100\n");

  printf("try again\n");
  break;
 }
 printf("enter to continue,-1 to end");
 printf("\n");
    }

}

void insert_record (void)
{

int i,
    n,
    p;
struct node *c,
     *l;

    if (head != nil) {
        printf("\n");
 printf("insert record:");
 scanf("%d",&n);
 if ((n >= 1) && (n <= count)) {
     c = nil;
            l = nil;
            for (i = 1; i <= n; i++) {
                if (i == 1) {
                    c = head;
                    l = c->next;
                }
                else  {
                    c = l;
                    l = c->next;
                }
            }
     insert_it(&c,n,&p);
     if (p == 1)
  printf("insert over\n");
     else
  printf("insert wrong\n");
 }
 else
     printf("out record\n");
    }
    else  {
 insert_it(&head,1,&p);
     if (p == 1)
  printf("insert over\n");
     else
  printf("insert wrong\n");
    }

}

void command_select (char command_line[])
{

    if ((strcmp(command_line,"APPEND") == 0) || (strcmp(command_line,
            "ADD") == 0)) {
 strcpy (command_line,"");//clear command_line
 input_record();//input
    }
    else if ((strcmp(command_line,"DEL") == 0) || (strcmp(command_line,
     "DELETE") == 0)) {
 strcpy (command_line,"");
 del_record();//delete
    }
    else if (strcmp(command_line,"SAVE") == 0) {
 strcpy (command_line,"");
 save_record();//save
    }
    else if (strcmp(command_line,"LOAD") == 0) {
 strcpy (command_line,"");
 read_record();//load
    }
    else if (strcmp(command_line,"INSERT") == 0) {
 strcpy (command_line,"");
 insert_record();//insert
    }
    else if ((strcmp(command_line,"SHOW") == 0) || (strcmp(command_line,
     "DIR") == 0)) {
 strcpy (command_line,"");
 output_record();//show
    }
    else if (strcmp(command_line,"FIND") == 0) {
 strcpy (command_line,"");
 find_record();//find
    }
    else if (strcmp(command_line,"SORT") == 0) {
 strcpy (command_line,"");
 sort_record();//sort
    }
    else if (strcmp(command_line,"MODIFY") == 0) {
 strcpy (command_line,"");
 modify_record();//modify
    }
    else if (strcmp(command_line,"HELP") == 0) {
 strcpy (command_line,"");


 printf("command help:");
 printf("APPEND,");
 printf("DELETE,");
 printf("SAVE,");
 printf("LOAD,");
 printf("INSERT,");
 printf("SHOW,");
 printf("FIND,");
 printf("SORT,");
 printf("CLOSE,");
 printf("MODIFY,");
 printf("QUIT,");
 printf("HELP");
    }
    else if (strcmp(command_line,"CLOSE") == 0) {
 strcpy (command_line,"");
        close_database(1);
    }
    else if ((strcmp(command_line,"QUIT") == 0) || (strcmp(command_line,
     "EXIT") == 0))
 strcpy (command_line,"QUIT");//exit
    else  {
 strcpy (command_line,"");
 printf("wrong command\n");//wrong
    }
}

void optimize(char command[])
{

char ch;
int i;

    ch = command[0];
    i = 1;
    while (ch != 0) {
 if ((ch > 96) && (ch < 123))
     command[i - 1] = ch - 32;
 i = i + 1;
 ch = command[i - 1];
    }

}


void main()
{
    head = nil;    /*变量初始化*/
    current = nil;
    last = nil;
    count = 0;
    tmp = 0;
    strcpy (command_line,"");
    while (strcmp(command_line,"QUIT") != 0) {//press quit to end
 printf("\n");
 printf("PB.COMMAND>");
 gets(command_line);//get command
 optimize(command_line);//up key
 command_select(command_line);//select command
 /*if current<>nil then writeln('test:  ',current^.name);*/    }

}



SHIT秋风大哥来了,因为没有上传大家自己来编,

上一篇:{技巧}C的数据库编程(1) 人气:5762
下一篇:{技巧}TC中的串口编程 人气:7454
视频教程列表
文章教程搜索
 
C语言程序设计推荐教程
C语言程序设计热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058