【发布时间】:2019-03-18 05:45:05
【问题描述】:
我正在尝试制作一个牛顿第二定律计算器,当我输入计算值时,没有任何反应。我不确定它们是否没有返回到主函数或什么。我想要做的是让用户输入一个字符,然后由一个包含 switch 语句的函数获取,在 switch 语句的情况下,函数调用执行算术的函数,然后这些函数将返回一个值switch 函数和 switch 函数将返回一个值给 main 函数,然后将值打印到屏幕上。
// Newtons2ndlaw.cpp : This file contains the 'main' function. Program
execution begins and ends there.
//
#include "pch.h"
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
void varselect(char, float *);
void force(float *);
void acceleration(float *);
void mass(float *);
int main()
{
float eqvalue;
char operation;
printf("Welcome to Newtons 2nd law solver!\n");
system("PAUSE");
printf("Would you like to solve for a Force, Acceleration or Mass?\nType 'F'(for force), 'A'(for acceleration), 'M'(for mass) to select a variable\n");
scanf_s("%c", &operation,1);
if (operation == 'f' || operation == 'F' || operation == 'a' || operation == 'A' || operation == 'm' || operation == 'M') //logic for determing what the user entered
{}
else
{
printf("Please enter a valid character.");
}
varselect(operation,&eqvalue); //function call to receive float value from varselect function
if (operation == 'f' || operation == 'F') //{
{
printf("The force = %f",eqvalue);
}
//this block determines what character string to display with calculated float value
else if (operation == 'a' || operation == 'A')
{
printf("The acceleration = %f", eqvalue);
}
else if (operation == 'm' || operation == 'M')
{
printf("the Mass = %f", eqvalue);
}
} //}
void varselect(char x, float *j)
//this function recieves the user inputed char value and returns the calculated float value to function call.
{ //switch allows program to "understand" users unwillingness to press shift before a,f,m like printf statement tells them to do.
switch (x) // also allows each variable to call its own function.
{
case 'f':
float getval;
force(&getval);
*j = getval;
return;
break;
}
switch (x)
{
case 'F':
float getval;
force(&getval);
*j = getval;
return;
break;
}
switch (x)
{
case 'a':
float getval;
acceleration(&getval);
*j = getval;
return;
break;
}
switch (x)
{
case 'A':
float getval;
acceleration(&getval);
*j = getval;
return;
break;
}
switch (x)
{
case 'm':
float getval;
mass(&getval);
*j = getval;
return;
break;
}
switch (x)
{
case 'M':
float getval;
mass(&getval);
*j = getval;
return;
break;
}
return;
}
void force(float *fma)
{
float acceleration, mass;
printf("Enter a value for 'Mass', then 'Acceleration'.\n\n");
scanf_s("%f\n%f\n", &mass, &acceleration, 2);
*fma = mass * acceleration;
return;
}
void acceleration(float *afm)
{ //functions to take input from user and return float to varselect function
float force, mass;
printf("Enter a value for 'Force', then 'Mass'.\n\n");
scanf_s("%f\n%f\n", &force, &mass, 1);
*afm = force / mass;
return;
}
void mass(float *fam)
{
float force, acceleration;
printf("Enter a value for 'Force', then 'Acceleration'.\n\n");
scanf_s("%f\n%f\n", &force, &acceleration, 1);
*fam = force / acceleration;
return;
}
【问题讨论】:
-
你需要学习如何调试你自己的程序。您需要创建一个 minimal reproducible example 。您在哪里转储所有程序并说“它不起作用”的问题将作为题外话关闭。
-
这不是
switch的用法。您的varselect函数中最多需要一个switch语句。 -
首选
float force()而不是void force(float *fma)。该功能的意图更加清晰。首先,很明显您正在计算和返回。在您阅读代码之前,您不知道该指针发生了什么。可能是函数需要一个数组。可能正在更新一个值,但在这种情况下它应该是void force(float & fma)。毕竟这是 C++。这就引出了一个问题,printf和scanf_s的这些东西是怎么回事?这甚至不是一个符合标准的`scanf。 -
抱歉问题的提出方式,这是我第一次来这个论坛。这个学期我要上一门 C 课程,这个课程只是我在探索它。我确实看到了您对 switch 语句的看法!我也会尝试使用您建议的功能!我使用 scanf_s 的原因是因为在视觉上它在使用 scanf 时给了我一个编译器错误。它希望我使用 scanf_s 以便我可以指定缓冲区限制。除了 printf 我应该使用什么?我什么都不知道,因为这是我第一次接触 C 编程。
标签: c++ function if-statement switch-statement return