Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C程序:将if语句代码转换为不使用逻辑、关系运算符或选择构造的代码(不允许切换)_C_Arrays_If Statement_Switch Statement_Logical Operators - Fatal编程技术网

C程序:将if语句代码转换为不使用逻辑、关系运算符或选择构造的代码(不允许切换)

C程序:将if语句代码转换为不使用逻辑、关系运算符或选择构造的代码(不允许切换),c,arrays,if-statement,switch-statement,logical-operators,C,Arrays,If Statement,Switch Statement,Logical Operators,我用C语言写了这段代码。它相当简单易懂。如何将程序末尾的if语句转换为不使用任何逻辑或关系运算符、任何选择构造或任何数组的语句?这意味着if语句必须转换为一段简单的算术代码 #include<stdio.h> #include<math.h> int main (void) { //Declarations int m; //mass of the object int vi; //initial velocity of the object

我用C语言写了这段代码。它相当简单易懂。如何将程序末尾的if语句转换为不使用任何逻辑或关系运算符、任何选择构造或任何数组的语句?这意味着if语句必须转换为一段简单的算术代码

#include<stdio.h>
#include<math.h>

int main (void)
{
    //Declarations

    int m;  //mass of the object
    int vi; //initial velocity of the object
    int ur; //coefficient of resistance
    int choice; //choice
    double net_force; //net force acting on the object
    double force_grav; //force due to gravity only
    double force_res; //force due to the resistance only
    double force_des; //Desired force in output section

    #define g 9.8

    //Input statements
    printf("Please input the mass in kilograms:");
    scanf("%d",&m);
    printf("Please input the launch speed (m/s):");
    scanf("%d",&vi);
    printf("Please input the coefficient of resistance (kg/s):");
    scanf("%d",&ur);

    //Executable Statements
    printf("Choices for calculation:\n");
    printf("1. Force due to gravity only\n");
    printf("2. Net force\n");
    printf("3. Force due to resistance only\n");
    printf("Please enter your choice: ");
    scanf("%d", &choice);

    //Calculations
    force_grav = m * g;
    force_res = (-1) * ur * vi;
    net_force = force_res + force_grav;


    if(choice==1)
        printf("Desired force: %2.3lf", force_grav);
    else
        if(choice==2)
            printf("Desired force: %2.3lf", net_force);
        else
            printf("Desired force: %2.3lf", force_res);

   return(0);
}
#包括
#包括
内部主(空)
{
//声明
int m;//对象的质量
int vi;//物体的初始速度
int-ur;//阻力系数
int choice;//choice
双净力;//作用在物体上的净力
双力_grav;//仅由重力产生的力
双力_res;//仅因阻力产生的力
double force_des;//输出部分中的所需力
#定义G9.8
//输入语句
printf(“请以千克为单位输入质量:”);
scanf(“%d”、&m);
printf(“请输入发射速度(m/s):”;
scanf(“%d”和“&vi”);
printf(“请输入阻力系数(kg/s):”;
scanf(“%d”和“ur”);
//可执行语句
printf(“计算选项:\n”);
printf(“1.仅由重力引起的力\n”);
printf(“2.Net力”);
printf(“3.仅因阻力产生的力\n”);
printf(“请输入您的选择:”);
scanf(“%d”,选择(&C);
//计算
重力=m*g;
力的大小=(-1)*ur*vi;
净力=力+重力;
如果(选项==1)
printf(“所需力:%2.3lf”,重力);
其他的
如果(选项==2)
printf(“所需力:%2.3lf”,净力);
其他的
printf(“所需力:%2.3lf”,力大小);
返回(0);
}

这里有一个简单但不安全的方法。注意:没有对输入的值/选项进行检查,但检查需要一个if

#include<stdio.h>
#include<math.h>

int main (void)
{
    //Declarations

    int m;  //mass of the object
    int vi; //initial velocity of the object
    int ur; //coefficient of resistance
    int choice; //choice
    double net_force; //net force acting on the object
    //double force_grav; //force due to gravity only
    //double force_res; //force due to the resistance only
    //double force_des; //Desired force in output section
    double forces[3];

    #define g 9.8

    //Input statements
    printf("Please input the mass in kilograms:");
    scanf("%d",&m);
    printf("Please input the launch speed (m/s):");
    scanf("%d",&vi);
    printf("Please input the coefficient of resistance (kg/s):");
    scanf("%d",&ur);

    //Executable Statements
    printf("Choices for calculation:\n");
    printf("1. Force due to gravity only\n");
    printf("2. Net force\n");
    printf("3. Force due to resistance only\n");
    printf("Please enter your choice: ");
    scanf("%d", &choice);

    //Calculations
    forces[0] = m * g;                   // force_grav
    forces[2] = (-1) * ur * vi;          // force_res
    forces[1] = forces[0] + forces[2];  // net_force

#if 0
    if(choice==1)
        printf("Desired force: %2.3lf", force_grav);
    else
        if(choice==2)
            printf("Desired force: %2.3lf", net_force);
        else
            printf("Desired force: %2.3lf", force_res);
#endif

   // CAUTION: SHOULD HAVE A CHECK ON THE VALUE OF CHOICE
   // E.G. if (choice > 0 && choice < 4)
   // BUT THAT'D REQUIRE AN IF......

   printf("Desired force: %2.3lf\n", forces[choice-1]);
   return(0);
}
#包括
#包括
内部主(空)
{
//声明
int m;//对象的质量
int vi;//物体的初始速度
int-ur;//阻力系数
int choice;//choice
双净力;//作用在物体上的净力
//双力_grav;//仅由重力产生的力
//双力_res;//仅因阻力产生的力
//double force_des;//输出部分中的所需力
双重力量[3];
#定义G9.8
//输入语句
printf(“请以千克为单位输入质量:”);
scanf(“%d”、&m);
printf(“请输入发射速度(m/s):”;
scanf(“%d”和“&vi”);
printf(“请输入阻力系数(kg/s):”;
scanf(“%d”和“ur”);
//可执行语句
printf(“计算选项:\n”);
printf(“1.仅由重力引起的力\n”);
printf(“2.Net力”);
printf(“3.仅因阻力产生的力\n”);
printf(“请输入您的选择:”);
scanf(“%d”,选择(&C);
//计算
力[0]=m*g;//重力
力[2]=(-1)*ur*vi;//力
力[1]=力[0]+力[2];//净力
#如果0
如果(选项==1)
printf(“所需力:%2.3lf”,重力);
其他的
如果(选项==2)
printf(“所需力:%2.3lf”,净力);
其他的
printf(“所需力:%2.3lf”,力大小);
#恩迪夫
//注意:应检查选项的值
//例如,如果(选项>0和选项<4)
//但这需要一个IF。。。。。。
printf(“所需力:%2.3lf\n”,力[choice-1]);
返回(0);
}

我的建议是,如果要用算术方法解决这个问题:

变量“choice”现在有效地选择,它的总和对术语的结果有贡献

这个词当然可以简化,但我让它“照原样”来表达这个概念。

这是我的第一次尝试

#include<stdio.h>
#include<math.h>

int main (void)
{
    //Declarations

    int m;  //mass of the object
    int vi; //initial velocity of the object
    int ur; //coefficient of resistance
    int choice; //choice
    double force_grav; //force due to gravity only
    double force_res; //force due to the resistance only

    const double g = 9.8;

    //Input statements
    printf("Please input the mass in kilograms:");
    scanf("%d",&m);
    printf("Please input the launch speed (m/s):");
    scanf("%d",&vi);
    printf("Please input the coefficient of resistance (kg/s):");
    scanf("%d",&ur);

    //Executable Statements
    printf("Choices for calculation:\n");
    printf("1. Force due to gravity only\n");
    printf("2. Net force\n");
    printf("3. Force due to resistance only\n");
    printf("Please enter your choice: ");
    scanf("%d", &choice);

    //Calculations
    force_grav = m * g;
    force_res = (-1) * ur * vi;

    printf("Desired force: %2.3lf\n", ((force_res*((choice&2)>>1)) + (force_grav*(choice&1))));
}
很少解释:

在第一个示例中您可以使用按选择设置的位“允许对输出值求和:

  • 0x01
    仅设置位0
  • 0x02
    仅设置位1
  • 0x03
    位0和2都已设置

在第二个示例中您可以使用模和整数除法来允许值求和。

@ChunkwinJoe:类似于
constdouble g=9.8;
的方法会更好。使用
if(){…}else if{…}else if{…}else{…}
以确保对代码的更改不会影响控制流。K&R C讨论了使用大括号或不使用大括号的C语法如何影响代码中的控制流。这个问题与“关系代数”无关,后者涉及用于数据库的关系/表上的运算符。(Google/wiki it。)你的作业/练习到底说了什么?正如OP所评论的,数组是不允许的。除非你解释这个概念,否则这个答案没有多大帮助(除了盲目地剪切和粘贴)。它不会“显示这个概念”“它显示了应用这个概念的结果。@ ChunkwinJoe感谢被劝阻,但是请考虑赞成投票的答案(箭头图标)(当你有足够的代表)和/或接受一个最好的(复选标记图标)。
#include<stdio.h>
#include<math.h>

int main (void)
{
    //Declarations

    int m;  //mass of the object
    int vi; //initial velocity of the object
    int ur; //coefficient of resistance
    int choice; //choice
    double force_grav; //force due to gravity only
    double force_res; //force due to the resistance only

    const double g = 9.8;

    //Input statements
    printf("Please input the mass in kilograms:");
    scanf("%d",&m);
    printf("Please input the launch speed (m/s):");
    scanf("%d",&vi);
    printf("Please input the coefficient of resistance (kg/s):");
    scanf("%d",&ur);

    //Executable Statements
    printf("Choices for calculation:\n");
    printf("1. Force due to gravity only\n");
    printf("2. Net force\n");
    printf("3. Force due to resistance only\n");
    printf("Please enter your choice: ");
    scanf("%d", &choice);

    //Calculations
    force_grav = m * g;
    force_res = (-1) * ur * vi;

    printf("Desired force: %2.3lf\n", ((force_res*((choice&2)>>1)) + (force_grav*(choice&1))));
}
printf("Desired force: %2.3lf\n", ((force_res*(choice/2)) + (force_grav*(choice%2))));