我用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 onlyn");
printf("2. Net forcen");
printf("3. Force due to resistance onlyn");
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);
}
如果这个问题应该用算术来解决,我的建议是:
force_grav = m * g;
force_res = (-1) * ur * vi;
net_force = force_res + force_grav;
force = force_grav*(2-choice)*(3-choice)/2 +
force_res*(1-choice)*(2-choice)/2 +
net_force*(1-choice)*(3-choice)*(-1);
printf("Desired force: %2.3lf", force);
变量"选择"现在有效地选择,它的总和有助于项的结果。
该术语当然可以简化,但我让它"按原样"来显示这个概念。
这是我的第一次尝试
#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 onlyn");
printf("2. Net forcen");
printf("3. Force due to resistance onlyn");
printf("Please enter your choice: ");
scanf("%d", &choice);
//Calculations
force_grav = m * g;
force_res = (-1) * ur * vi;
printf("Desired force: %2.3lfn", ((force_res*((choice&2)>>1)) + (force_grav*(choice&1))));
}
另一个可以是
printf("Desired force: %2.3lfn", ((force_res*(choice/2)) + (force_grav*(choice%2))));
小解释:
在第一个示例中,您可以使用按选择设置的位来"允许对输出求和值:
-
0x01
仅位 0 集 -
0x02
只有位 1 套 -
0x03
位 0 和 2 集
在第二个示例中,您可以使用取模和整数除法来允许对值求和。
这是一个简单但不安全的方法。请注意,没有检查输入的值/选择,但检查需要 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 onlyn");
printf("2. Net forcen");
printf("3. Force due to resistance onlyn");
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.3lfn", forces[choice-1]);
return(0);
}