满足条件后如何终止函数

How to terminate a function once a condition has been met

本文关键字:终止 函数 何终止 条件 满足      更新时间:2023-10-16

描述:

我有一个格式如下的文件:number number number,这里有几行:

0 00 19
0 900000 949999
0 9500000 9999999
1 00 09
2 00 19
2 200 349
2 35000 39999
2 900000 949999
2 9500000 9999999
3 00 02
3 7000 8499
3 9500000 9999999
4 00 19
4 7000 8499
80 00 19
80 900000 999999
81 00 19
81 900000 999999
82 990000 999999
83 7000 8499
958 95000 99999
959 7000 8499
960 85000 99999
961 00 19

第一列表示area,第二列代表num1num2

我已经设法将每一列存储在其各自的变量中,因此:

int isRegistered(FILE* fp, int area)
{
    int finished = 1;
    int scanned_area;
    char num1[7], num2[7];
    int rc = 1;
    rewind(fp);
    while (finished != EOF) {
        finished = fscanf(fp,"%d %s %sn", &scanned_area, &num1, &num2);
    }
    return rc;
}

我知道我"不应该使用fscanf",也不应该使用%s扫描,但这些都是特定于任务的,我不能更改它们。

现在,我将所有这些数字存储在它们各自的变量中,第一列数字存储在scanned_area中,第二列存储在num1中,第三列存储在num2中。

场景

我的面积值为1。如何对其进行编码,以便在scanned_area==1时,函数停止?

我的面积值为1。如何对其进行编码,以便在scanned_area==1时,函数停止?

您可以使用break退出循环:

while (condtion)
{
  // some code
  if (scanned_area == 1)
  {
     break;
  }
}

如果您希望函数停止,那么只需返回一个适当的值,在您的情况下,返回一个整数。一旦函数命中返回语句,就会返回该值,而在执行该值之后不会返回任何其他值。

希望这有帮助:D