免费注册 查看新帖 |

Chinaunix

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

这个语句是什么意思 lab: result=0; 它有什么作用? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-02-24 16:26 |只看该作者 |倒序浏览
     这个是网友写的一个学生成绩管理小程序,我把里面的清屏函数换掉了,换成了标准C中的命令。另外还有别的错误,他写了一个delete函数,改了个名字,暂时改成了Sdelete。

find sub program里面有一个语句,是 lab: result=0;这个语句是怎么回事?接下来的子函数中也有类似的lab:语句。lab:,这个不会是跟颜色显示有关的吧?我在百度和谷歌上找不到什么,请求知道的说说看,谢谢。



#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXN 35
#define MAX_NAME 256

typedef struct student
{
   int  no;
   char name[MAX_NAME];
          int  english;
          int  math;
          int  computer;
          int  mark_ave;
          int  mark_tot;
} STUDENT;

/*-----------input sub program---------*/

void input(STUDENT *data, int *len)
{
   int no;
   char ch[MAX_NAME];
   int mark;

   putchar('\n');
   printf("%s\n", "Enter the new records.And if you want exit, enter -1.");
   printf("%s\n", "format: number");
   printf("%s\n", "        name english math computer average total");
   putchar('\n');
   printf("such as:\n");
   printf("1\n");
   printf("Gates 100 100 100 100 400\n");

   scanf("%d", &no);
   while(no != -1) {
     data[*len].no = no;
     scanf("%s %d %d %d %d %d", data[*len].name, &data[*len].english, &data[*len].math,
            &data[*len].computer, &data[*len].mark_ave, &data[*len].mark_tot);
     (*len)++;
     scanf("%d", &no);
   }
}


/*---------------output sub program---------------*/

void output(STUDENT *data, int len)
{
  int i;

  system("cls");
  printf("%8s", "number");
  printf("%8s", "name");
  printf("%8s", "english");
  printf("%8s", "math");
  printf("%10s", "computer");
  printf("%12s", "average");
  printf("%10s", "total");
  putchar('\n');

  for (i =0; i < 80; i++)
    putchar('=');
  putchar('\n');

  for (i = 0; i< len; i++)
  {
    printf("%8d", data[i].no);
    printf("%8s", data[i].name);
    printf("%8d", data[i].english);
    printf("%8d", data[i].math);
    printf("%10d", data[i].computer);
    printf("%12d", data[i].mark_ave);
    printf("%10d", data[i].mark_tot);
    putchar('\n');
  }

  for (i =0; i < 80; i++)
    putchar('=');
  putchar('\n');
  printf("Press enter to continue.");
  getchar();
  getchar();
}



/*--------------sort sub program------------*/

void sort(STUDENT *data, int len)
{
  int i,j,k;
  STUDENT temp;

  for (i=0; i<len-1; i++) {
    for (k=i, j=i+1; j<len; j++)
      if (data[k].mark_ave > data[j].mark_ave) k=j;

    if (k != i) {
      temp = data[i];
      data[i] = data[k];
      data[k] = temp;
    }
  }
}


/*--------------------find sub program-----------------------*/

void find(STUDENT *data, int len)
{
  int find_no, result ;
  int i;

  lab: result=0;
  printf("%s\n", "Enter the number which you want to find. If you want exit, enter -1.");
  scanf("%d", &find_no);
  if (find_no == -1) return;     /*exit the fine sub program*/
  while( data[result].no != find_no && result < len) result ++;
  if (result >= len ) {
    printf("%s\n", "There is no the record which you want.");
    goto lab;
  }
  else {
    system("cls");
    printf("%s\n", "The flowing record is you want.");

    for (i =0; i < 80; i++)
    putchar('=');
    putchar('\n');

    printf("%8s", "number");
    printf("%8s", "name");
    printf("%8s", "english");
    printf("%8s", "math");
    printf("%10s", "computer");
    printf("%12s", "average");
    printf("%10s", "total");
    putchar('\n');



    printf("%8d", data[result].no);
    printf("%8s", data[result].name);
    printf("%8d", data[result].english);
    printf("%8d", data[result].math);
    printf("%10d", data[result].computer);
    printf("%12d", data[result].mark_ave);
    printf("%10d", data[result].mark_tot);
    putchar('\n');

    for (i =0; i < 80; i++)
    putchar('=');
    putchar('\n');

    goto lab;
  }
}


/*-----------------insert sub program-------------*/

void insert(STUDENT *data, int *len)
{
  int no, pos, english, math, computer, mark_ave, mark_tot, i;
  char name[MAX_NAME];

  lab: printf("%s\n", "Enter the new records. If you want exit, enter -1.");
  printf("%s\n", "format: number");
  printf("%s\n", "        name english math computer average total");

  scanf("%d", &no);
  if (no == -1) return;
  data[*len].no = no;
  scanf("%s %d %d %d %d %d", name, &english, &math, &computer, &mark_ave, &mark_tot);

  pos = 0;
  while ((data[pos].mark_ave < mark_ave) && (pos < *len) )
    pos ++;
  for (i = *len-1; i >= pos; i--)
    data[i+1] = data[i];
  data[pos].no = no;
  strcpy(data[pos].name, name);
  data[pos].english = english;
  data[pos].math = math;
  data[pos].computer = computer;
  data[pos].mark_ave = mark_ave;
  data[pos].mark_tot = mark_tot;
  (*len)++;
  goto lab;
}

/*--------------delete sub program--------------*/
void Sdelete(STUDENT *data,int *len)
{
  int no, i, pos;

  lab: pos=0;
  printf("%s\n", "Enter the number which you want to delete. If you want exit, enter -1.");

  scanf("%d", &no);
  if (no == -1) return;
  while( (data[pos].no != no) && (pos < *len) ) pos = pos +1;
  if (pos >= *len) {
    printf("%s\n", "There is no the record which you want.");
    goto lab;
  }
  else {
    for (i = pos+1;i < *len; i++)
      data[i-1] = data[i];
    *len = *len -1;
    if (*len == 0) {
      printf("%s\n", "The number of records is zero, press enter to return.");
      getchar();
      getchar();
      return;
    }
  goto lab;
  }
}


/*---------------stat sub program------------*/

void stat(STUDENT *data, int len)
{
  int no_59=0, no_69=0, no_79=0, no_89=0, no_100=0;
  int i;

  for (i = 0;i < len; i++) {
    if (data[i].mark_ave <=59) no_59++;
    else if (data[i].mark_ave <=69) no_69++;
         else if (data[i].mark_ave <=79) no_79++;
              else if (data[i].mark_ave <= 89) no_89++;
                   else no_100++;
  }

  system("cls");
  for (i =0; i < 80; i++)
    putchar('=');
    putchar('\n');
  printf("%10s", "score");
  printf("%10s", "0--59");
  printf("%10s", "60--69");
  printf("%10s", "70--79");
  printf("%10s", "80--89");
  printf("%10s", "90--100");
  putchar('\n');
  putchar('\n');

  printf("%10s", "student");
  printf("%10d", no_59);
  printf("%10d", no_69);
  printf("%10d", no_79);
  printf("%10d", no_89);
  printf("%10d", no_100);

  putchar('\n');

  for (i =0; i < 80; i++)
    putchar('=');
  putchar('\n');
  printf("Press enter to return.");
  getchar();
  getchar();
}


/*---------------manu------------*/

void paint()
{
  int i;

  system("cls");
  printf("%43s\n", "Manu");

  for (i = 0; i < 80; i++)
    putchar('=');
  putchar('\n');

  printf("             1 input                            2 output\n");
  printf("             3 sort(average)                    4 find(number)\n");
  printf("             5 insert                           6 Sdelete(number)\n");
  printf("             7 stat(average)                    0 quit\n");
  for (i = 0; i<= 79; i++)
    putchar('=');
  putchar('\n');

  printf("%s\n", "Please enter the command:");

}

/*------------main program---------------*/

int main()
{
  STUDENT data[MAXN];
  int len = 0;
  char ctrl_ch;

  paint();
  scanf("%c", &ctrl_ch);
  while (ctrl_ch != '0') {
    switch(ctrl_ch) {
      case '1':
input(data, &len);
break;
      case '2':
output(data, len);
break;
      case '3':
sort(data, len);
break;
      case '4':
find(data, len);
break;
      case '5':
insert(data, &len);
break;
      case '6':
Sdelete(data, &len);
break;
      case '7':
stat(data, len);
break;
      default:
if (ctrl_ch != '\n')
   printf("%s\n", "Error command.");
break;
    }
    if (ctrl_ch != '\n') paint();
    scanf("%c", &ctrl_ch);
  }
  return 0;
}

论坛徽章:
0
2 [报告]
发表于 2012-02-24 16:35 |只看该作者
lab: 和 result=0没关系
lab 是一个 label,配合goto lab使用
http://www.java-samples.com/showtutorial.php?tutorialid=510

论坛徽章:
0
3 [报告]
发表于 2012-02-24 16:47 |只看该作者
回复 2# jeung


    谢谢提示,还好我没有花太多时间在死胡同里面。

论坛徽章:
0
4 [报告]
发表于 2012-02-24 17:50 |只看该作者
呵呵,我干脆把这个贴出来,很容易看懂的。




C provides the infinitely-abusable goto statement, and labels to branch to. Formally, the goto statement is never necessary, and in practice it is almost always easy to write code without it.
Nevertheless, there are a few situations where gotos may find a place. The most common is to abandon processing in some deeply nested structure, such as breaking out of two or more loops at once. The break statement cannot be used directly since it only exits from the innermost loop. Thus:

       for ( ... )
           for ( ... ) {
               ...
               if (disaster)
                   goto error;
           }
       ...
   error:
       /* clean up the mess */

This organization is handy if the error-handling code is non-trivial, and if errors can occur in several places.
A label has the same form as a variable name, and is followed by a colon. It can be attached to any statement in the same function as the goto. The scope of a label is the entire function.

As another example, consider the problem of determining whether two arrays a and b have an element in common. One possibility is

       for (i = 0; i < n; i++)
           for (j = 0; j < m; j++)
               if (a[i] == b[j])
                   goto found;
       /* didn't find any common element */
       ...
   found:
       /* got one: a[i] == b[j] */
       ...

Code involving a goto can always be written without one, though perhaps at the price of some repeated tests or an extra variable. For example, the array search becomes
   found = 0;
   for (i = 0; i < n && !found; i++)
       for (j = 0; j < m && !found; j++)
           if (a[i] == b[j])
               found = 1;
   if (found)
       /* got one: a[i-1] == b[j-1] */
       ...
   else
       /* didn't find any common element */
       ...

With a few exceptions like those cited here, code that relies on goto statements is generally harder to understand and to maintain than code without gotos. Although we are not dogmatic about the matter, it does seem that goto statements should be used rarely, if at all.

If this tutorial doesn't answer your question, and you have a specific question, just ask an expert here. Post your question to get a direct answer.

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

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP