当涉及文件和字符串时,如何做循环?

时间:2018-01-21 17:19:26

标签: c string file if-statement while-loop

我最近试图编写一个代码,程序将使用产品规则显示函数的派生。该函数可以是以下之一: 18/01/21 16:34:19 INFO yarn.Client: Uploading resource file:/usr/local/spark/examples/jars/spark-examples_2.11-2.0.1.jar -> hdfs://192.168.198.10:8020/user/cloud-user/.sparkStaging/application_1516548465362_0014/spark-examples_2.11-2.0.1.jar 18/01/21 16:34:19 INFO yarn.Client: Uploading resource file:/tmp/spark-f37b5cec-a81f-46c3-9b5e-6ce7854c6dd4/__spark_conf__2008488553335511154.zip -> hdfs://192.168.198.10:8020/user/cloud-user/.sparkStaging/application_1516548465362_0014/__spark_conf__.zip 18/01/21 16:34:19 INFO spark.SecurityManager: Changing view acls to: cloud-user 18/01/21 16:34:19 INFO spark.SecurityManager: Changing modify acls to: cloud-user 18/01/21 16:34:19 INFO spark.SecurityManager: Changing view acls groups to: 18/01/21 16:34:19 INFO spark.SecurityManager: Changing modify acls groups to: 18/01/21 16:34:19 INFO spark.SecurityManager: SecurityManager: authentication disabled; ui acls disabled; users with view permissions: Set(cloud-user); groups with view permissions: Set(); users with modify permissions: Set(cloud-user); groups with modify permissions: Set() 18/01/21 16:34:19 INFO yarn.Client: Submitting application application_1516548465362_0014 to ResourceManager 18/01/21 16:34:19 INFO impl.YarnClientImpl: Submitted application application_1516548465362_0014 18/01/21 16:34:20 INFO yarn.Client: Application report for application_1516548465362_0014 (state: ACCEPTED) 18/01/21 16:34:20 INFO yarn.Client: client token: N/A diagnostics: N/A ApplicationMaster host: N/A ApplicationMaster RPC port: -1 queue: default start time: 1516552459599 tracking URL: http://master.abc.com:8088/proxy/application_1516548465362_0014/ user: cloud-user 18/01/21 16:34:21 INFO yarn.Client: Application report for application_1516548465362_0014 (state: ACCEPTED) 18/01/21 16:34:22 INFO yarn.Client: Application report for application_1516548465362_0014 (state: ACCEPTED) 18/01/21 16:34:23 INFO yarn.Client: Application report for application_1516548465362_0014 (state: ACCEPTED) 18/01/21 16:34:24 INFO yarn.Client: Application report for application_1516548465362_0014 (state: ACCEPTED) 18/01/21 16:34:25 INFO yarn.Client: Application report for application_1516548465362_0014 (state: ACCEPTED) 18/01/21 16:34:26 INFO yarn.Client: Application report for application_1516548465362_0014 (state: ACCEPTED) 18/01/21 16:34:27 yarn.Client: Application report for application_1516548465362_0014 (state: ACCEPTED) x^nsin(a*x)。作为示例,它可以是一组三个函数,因此文件可能如下所示:

cos(a*x)

我写了一些本地函数,比如

x^7*sin(3.14*x) 
cos(4*x)*sin(5.2*x)
cos(2*x)*cos(8*x)

但是,我遇到的问题是主要功能。我想知道如何处理while循环,因此本地函数可以工作。这是主要的:

void derivcc ()
{
    double a, b;
    fscanf(f,"cos(%lf*x)*cos(%lf*x)",&a, &b);
    if (a<0 && b<0) 
        printf("%lfsin(%lf*x)*cos(%lf*x)+%lfsin(%lf*x)*cos(%lf*x)\n",
                -a,a,b,-b,b,a);

    else if (a<0 && b>0) 
        printf("%lfsin(%lf*x)*cos(%lf*x)-%lfsin(%lf*x)*cos(%lf*x)\n",
                -a,a,b,b,b,a);

    else if (a>0 && b<0) 
        printf("-%lfsin(%lf*x)*cos(%lf*x)+%lfsin(%lf*x)*cos(%lf*x)\n",
                a,a,b,-b,b,a);

    else
        printf("-%lfsin(%lf*x)*cos(%lf*x)-%lfsin(%lf*x)*cos(%lf*x)\n", 
            a,a,b,b,b,a);
}

1 个答案:

答案 0 :(得分:1)

你已经获得了大部分代码。你只需要稍微重新排列它就可以使它工作。这是你的代码:

int c;
fgets(lines, sizeof(lines), f); 
char sin[]="sin";
char cos[]="cos";
char x[]="x^";
char *checkc, *checks, *checkx;
checkc = strstr(lines,cos);
checks = strstr(lines,sin);
checkx = strstr(lines, x);
double a,b;

while((c = getc(f)) != EOF)
{ 

}

代码读取第一行,然后使用strstr函数获取有关该行的一些信息。然后while循环开始一次读取一个字符。

需要发生的是while循环需要一次读取一行。并且strstr调用需要在while循环内。所以代码应该是这样的:

char sin[]="sin";
char cos[]="cos";
char x[]="x^";
char *checkc, *checks, *checkx;
double a,b;

while(fgets(lines, sizeof(lines), f) != NULL)
{ 
   checkc = strstr(lines,cos);
   checks = strstr(lines,sin);
   checkx = strstr(lines, x);


}