| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1843 人关注过本帖
标题:新手有问题想请教,一段比较简单的代码
只看楼主 加入收藏
qssssss
Rank: 1
等 级:新手上路
帖 子:105
专家分:0
注 册:2017-4-3
结帖率:25%
收藏
已结贴  问题点数:20 回复次数:15 
新手有问题想请教,一段比较简单的代码
我不知道我输出的数组为什么一直错
int main(int argc, char *argv[])
{
    void SIZEINPUT(int nbounds, char* bndnames, double* boundarycoeffs);
    int nbounds = 3;
    char* bndnames[] = { "line_x","line_xy_1","line_xy_1" };
    double coeffs[9];
    int j;
    SIZEINPUT(nbounds, bndnames, coeffs);
    for (j = 0; j < 9; j++) {printf("\n  %f    \n", coeffs[j]);
     }
}
void SIZEINPUT(int nbounds, char* bndnames, double* boundarycoeffs)
{
    int i;
    double radius1 = 0.2;
    double radius2 = 0.45;
    double radius3 = 0.46;
    double radius4 = 0.5;
    double radius5 = 0.8;
    double hwidth = 0.8;


    for (i = 0; i<nbounds; i++) {
        if (bndnames[i] == "circle_1")
        {
            boundarycoeffs[i * 3] = 0.0;
            boundarycoeffs[i * 3 + 1] = 0.0;
            boundarycoeffs[i * 3 + 2] = radius1;
        }
        else if (bndnames[i] == "circle_2")
        {
            boundarycoeffs[i * 3] = 0.0;
            boundarycoeffs[i * 3 + 1] = 0.0;
            boundarycoeffs[i * 3 + 2] = radius2;
        }
        else if (bndnames[i] == "circle_3")
        {
            boundarycoeffs[i * 3] = 0.0;
            boundarycoeffs[i * 3 + 1] = 0.0;
            boundarycoeffs[i * 3 + 2] = radius3;
        }

        else if (bndnames[i] == "circle_4")
        {
            boundarycoeffs[i * 3] = 0.0;
            boundarycoeffs[i * 3 + 1] = 0.0;
            boundarycoeffs[i * 3 + 2] = radius4;
        }

        else if (bndnames[i] == "circle_5")
        {
            boundarycoeffs[i] = 0.0;
            boundarycoeffs[i + 1] = 0.0;
            boundarycoeffs[i + 2] = radius5;
        }
        else if (bndnames[i] == "line_x")
        {
            boundarycoeffs[i * 3] = 1.0;
            boundarycoeffs[i * 3 + 1] = 0.0;
            boundarycoeffs[i * 3 + 2] = 0.0;
        }
        else if (bndnames[i] == "line_y")
        {
            boundarycoeffs[i * 3] = 0.0;
            boundarycoeffs[i * 3 + 1] = 1.0;
            boundarycoeffs[i * 3 + 2] = 0.0;
        }
        else if (bndnames[i] == "line_xy_1")
        {
            boundarycoeffs[i * 3] = 1.0;
            boundarycoeffs[i * 3 + 1] = -1.0;
            boundarycoeffs[i * 3 + 2] = 0.0;
        }
        else if (bndnames[i] == "line_xy_2")
        {
            boundarycoeffs[i * 3] = 1.0;
            boundarycoeffs[i * 3 + 1] = 1.0;
            boundarycoeffs[i * 3 + 2] = 0.0;
        }
        else if (bndnames[i] == "line_x_1")
        {
            boundarycoeffs[i * 3] = 1.0;
            boundarycoeffs[i * 3 + 1] = 0.0;
            boundarycoeffs[i * 3 + 2] = hwidth;
        }
        else if (bndnames[i] == "line_x_2")
        {
            boundarycoeffs[i] = 1.0;
            boundarycoeffs[i * 3 + 1] = 0.0;
            boundarycoeffs[i * 3 + 2] = -hwidth;
        }
        else if (bndnames[i] == "line_y_1")
        {
            boundarycoeffs[i * 3] = 0.0;
            boundarycoeffs[i * 3 + 1] = 1.0;
            boundarycoeffs[i * 3 + 2] = hwidth;
        }
        else if (bndnames[i] == "line_y_2")
        {
            boundarycoeffs[i * 3] = 0.0;
            boundarycoeffs[i * 3 + 1] = 1.0;
            boundarycoeffs[i * 3 + 2] = -hwidth;
        }
        else
        {

            printf("\n error! \n");

        }
    }
}
搜索更多相关主题的帖子: double 
2017-04-03 14:09
qssssss
Rank: 1
等 级:新手上路
帖 子:105
专家分:0
注 册:2017-4-3
收藏
得分:0 
我一直输出的是error值没赋上去
2017-04-03 14:28
qssssss
Rank: 1
等 级:新手上路
帖 子:105
专家分:0
注 册:2017-4-3
收藏
得分:0 
有没有大神帮下忙的,真的感谢,很急
2017-04-03 14:45
初学编程的人
Rank: 2
等 级:论坛游民
威 望:2
帖 子:90
专家分:84
注 册:2017-3-12
收藏
得分:7 
你这个代码能通过编译?

bndnames[i] == "circle_1"你用这样的逻辑判断字符串是否相同?
2017-04-03 14:48
qssssss
Rank: 1
等 级:新手上路
帖 子:105
专家分:0
注 册:2017-4-3
收藏
得分:0 
回复 4楼 初学编程的人
通过编译了但是我也不知道对不对因为我本身因为做一些别的,要利用C语言编程,就只能边做边学,你能指导下我错在哪吗
2017-04-03 14:58
renkejun1942
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:不是这样
等 级:贵宾
威 望:33
帖 子:1645
专家分:5297
注 册:2016-12-1
收藏
得分:7 
字符串判断用strcmp函数,你需要#include <string.h>
相等返回0,第一个参数大于第二个参数,返回正数,第一个参数小于第二个参数返回负数。

如果你有其他语言的经验的话,我觉得你还是买本C语言的书看吧,C程序设计语言,正文只有150页不到,快的话一晚上就够了。

[此贴子已经被作者于2017-4-3 15:05编辑过]


09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
2017-04-03 15:02
qssssss
Rank: 1
等 级:新手上路
帖 子:105
专家分:0
注 册:2017-4-3
收藏
得分:0 
回复 6楼 renkejun1942
字符串的大小是什么意思啊?我刚刚试着用了一下这个函数可是编译虽然过了但是程序运行不出来
2017-04-03 15:08
renkejun1942
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:不是这样
等 级:贵宾
威 望:33
帖 子:1645
专家分:5297
注 册:2016-12-1
收藏
得分:0 
你修改后的代码还是得发上来啊。难道我靠猜?

09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
2017-04-03 15:13
qssssss
Rank: 1
等 级:新手上路
帖 子:105
专家分:0
注 册:2017-4-3
收藏
得分:0 
回复 8楼 renkejun1942
不好意思。。。我现在只改了if语句的第一个,你能帮我看下吗谢谢
int main(int argc, char *argv[])
{
    void SIZEINPUT(int nbounds, char* bndnames, double* boundarycoeffs);
    int nbounds = 3;
    char* bndnames[] = { "circle_1","line_x","line_xy_1" };
    double coeffs[9];
    int j;
    int k;
    SIZEINPUT(nbounds, bndnames, coeffs);
    for (j = 0; j < 9; j++) {
        printf("\n  %f    \n", coeffs[j]);
    }
    for (k = 0; k < 3; k++) {
            printf("\n   %s   \n", bndnames[k]);
        }

   
   

}
void SIZEINPUT(int nbounds, char* bndnames, double* boundarycoeffs)
{
    int i;
    double radius1 = 0.2;
    double radius2 = 0.45;
    double radius3 = 0.46;
    double radius4 = 0.5;
    double radius5 = 0.8;
    double hwidth = 0.8;


    for (i = 0; i<nbounds; i++) {
        if (!strcmp(bndnames[i], "circle_1"))
        {
            boundarycoeffs[i * 3] = 0.0;
            boundarycoeffs[i * 3 + 1] = 0.0;
            boundarycoeffs[i * 3 + 2] = radius1;
        }
        else if (bndnames[i] == "circle_2")
        {
            boundarycoeffs[i * 3] = 0.0;
            boundarycoeffs[i * 3 + 1] = 0.0;
            boundarycoeffs[i * 3 + 2] = radius2;
        }
        else if (bndnames[i] == "circle_3")
        {
            boundarycoeffs[i * 3] = 0.0;
            boundarycoeffs[i * 3 + 1] = 0.0;
            boundarycoeffs[i * 3 + 2] = radius3;
        }

        else if (bndnames[i] == "circle_4")
        {
            boundarycoeffs[i * 3] = 0.0;
            boundarycoeffs[i * 3 + 1] = 0.0;
            boundarycoeffs[i * 3 + 2] = radius4;
        }

        else if (bndnames[i] == "circle_5")
        {
            boundarycoeffs[i] = 0.0;
            boundarycoeffs[i + 1] = 0.0;
            boundarycoeffs[i + 2] = radius5;
        }
        else if (bndnames[i] == "line_x")
        {
            boundarycoeffs[i * 3] = 1.0;
            boundarycoeffs[i * 3 + 1] = 0.0;
            boundarycoeffs[i * 3 + 2] = 0.0;
        }
        else if (bndnames[i] == "line_y")
        {
            boundarycoeffs[i * 3] = 0.0;
            boundarycoeffs[i * 3 + 1] = 1.0;
            boundarycoeffs[i * 3 + 2] = 0.0;
        }
        else if (bndnames[i] == "line_xy_1")
        {
            boundarycoeffs[i * 3] = 1.0;
            boundarycoeffs[i * 3 + 1] = -1.0;
            boundarycoeffs[i * 3 + 2] = 0.0;
        }
        else if (bndnames[i] == "line_xy_2")
        {
            boundarycoeffs[i * 3] = 1.0;
            boundarycoeffs[i * 3 + 1] = 1.0;
            boundarycoeffs[i * 3 + 2] = 0.0;
        }
        else if (bndnames[i] == "line_x_1")
        {
            boundarycoeffs[i * 3] = 1.0;
            boundarycoeffs[i * 3 + 1] = 0.0;
            boundarycoeffs[i * 3 + 2] = hwidth;
        }
        else if (bndnames[i] == "line_x_2")
        {
            boundarycoeffs[i] = 1.0;
            boundarycoeffs[i * 3 + 1] = 0.0;
            boundarycoeffs[i * 3 + 2] = -hwidth;
        }
        else if (bndnames[i] == "line_y_1")
        {
            boundarycoeffs[i * 3] = 0.0;
            boundarycoeffs[i * 3 + 1] = 1.0;
            boundarycoeffs[i * 3 + 2] = hwidth;
        }
        else if (bndnames[i] == "line_y_2")
        {
            boundarycoeffs[i * 3] = 0.0;
            boundarycoeffs[i * 3 + 1] = 1.0;
            boundarycoeffs[i * 3 + 2] = -hwidth;
        }
        else
        {

            printf("\n error! \n");

        }
    }
}
2017-04-03 15:15
renkejun1942
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:不是这样
等 级:贵宾
威 望:33
帖 子:1645
专家分:5297
注 册:2016-12-1
收藏
得分:0 
没有碰你的变量什么的,只是让它可以运作。不知道运行结果是不是你想要的。

程序代码:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
    void SIZEINPUT(int nbounds, char **bndnames, double* boundarycoeffs);
    int nbounds = 3;
    char* bndnames[] = { "circle_1","line_x","line_xy_1" };
    double coeffs[9];
    int j;
    int k;
    SIZEINPUT(nbounds, bndnames, coeffs);
    for (j = 0; j < 9; j++) {
        printf("\n  %f    \n", coeffs[j]);
    }
    for (k = 0; k < 3; k++) {
            printf("\n   %s   \n", bndnames[k]);
        }

    
    

}
void SIZEINPUT(int nbounds, char **bndnames, double* boundarycoeffs)
{
    int i;
    double radius1 = 0.2;
    double radius2 = 0.45;
    double radius3 = 0.46;
    double radius4 = 0.5;
    double radius5 = 0.8;
    double hwidth = 0.8;


    for (i = 0; i<nbounds; i++) {
        if (!strcmp(bndnames[i], "circle_1"))
        {
            boundarycoeffs[i * 3] = 0.0;
            boundarycoeffs[i * 3 + 1] = 0.0;
            boundarycoeffs[i * 3 + 2] = radius1;
        }
        else if (!strcmp( bndnames[i], "circle_2" ) )
        {
            boundarycoeffs[i * 3] = 0.0;
            boundarycoeffs[i * 3 + 1] = 0.0;
            boundarycoeffs[i * 3 + 2] = radius2;
        }
        else if (!strcmp( bndnames[i] , "circle_3" ) )
        {
            boundarycoeffs[i * 3] = 0.0;
            boundarycoeffs[i * 3 + 1] = 0.0;
            boundarycoeffs[i * 3 + 2] = radius3;
        }

        else if (!strcmp( bndnames[i] , "circle_4" ))
        {
            boundarycoeffs[i * 3] = 0.0;
            boundarycoeffs[i * 3 + 1] = 0.0;
            boundarycoeffs[i * 3 + 2] = radius4;
        }

        else if (!strcmp( bndnames[i] , "circle_5" ) )
        {
            boundarycoeffs[i] = 0.0;
            boundarycoeffs[i + 1] = 0.0;
            boundarycoeffs[i + 2] = radius5;
        }
        else if (!strcmp( bndnames[i] , "line_x" ) )
        {
            boundarycoeffs[i * 3] = 1.0;
            boundarycoeffs[i * 3 + 1] = 0.0;
            boundarycoeffs[i * 3 + 2] = 0.0;
        }
        else if (!strcmp( bndnames[i], "line_y" ))
        {
            boundarycoeffs[i * 3] = 0.0;
            boundarycoeffs[i * 3 + 1] = 1.0;
            boundarycoeffs[i * 3 + 2] = 0.0;
        }
        else if (!strcmp( bndnames[i], "line_xy_1") )
        {
            boundarycoeffs[i * 3] = 1.0;
            boundarycoeffs[i * 3 + 1] = -1.0;
            boundarycoeffs[i * 3 + 2] = 0.0;
        }
        else if (!strcmp( bndnames[i] , "line_xy_2" ) )
        {
            boundarycoeffs[i * 3] = 1.0;
            boundarycoeffs[i * 3 + 1] = 1.0;
            boundarycoeffs[i * 3 + 2] = 0.0;
        }
        else if (!strcmp( bndnames[i] , "line_x_1" ) )
        {
            boundarycoeffs[i * 3] = 1.0;
            boundarycoeffs[i * 3 + 1] = 0.0;
            boundarycoeffs[i * 3 + 2] = hwidth;
        }
        else if (!strcmp( bndnames[i], "line_x_2" ))
        {
            boundarycoeffs[i] = 1.0;
            boundarycoeffs[i * 3 + 1] = 0.0;
            boundarycoeffs[i * 3 + 2] = -hwidth;
        }
        else if (!strcmp( bndnames[i], "line_y_1" ) )
        {
            boundarycoeffs[i * 3] = 0.0;
            boundarycoeffs[i * 3 + 1] = 1.0;
            boundarycoeffs[i * 3 + 2] = hwidth;
        }
        else if (!strcmp( bndnames[i] , "line_y_2" ) )
        {
            boundarycoeffs[i * 3] = 0.0;
            boundarycoeffs[i * 3 + 1] = 1.0;
            boundarycoeffs[i * 3 + 2] = -hwidth;
        }
        else 
        {

            printf("\n error! \n");

        }
    }
}


[此贴子已经被作者于2017-4-3 15:26编辑过]


09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
2017-04-03 15:15
快速回复:新手有问题想请教,一段比较简单的代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037798 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved