gpt4 book ai didi

c++ - C++ 新手,编码语法

转载 作者:行者123 更新时间:2023-11-28 02:33:36 24 4
gpt4 key购买 nike

<分区>

可以使用我在空闲时间编写的这段代码的帮助。我是 C++ 的初学者。我正在寻求帮助的事情:

-需要正确循环的帮助,尤其是当用户输入不是整数而是字符时- 当我尝试编译时,case语句显示错误,它指向括号,我不知道为什么。

该代码应该使用从物理课中学到的运动学方程求解某些变量。这是我习惯 C++ 入门的一种方式,但可以使用高级编码人员的一些帮助。

这也是我第一次在 stackoverflow 上发帖,如果有人有一些正确发帖的技巧请告诉我,我会在我的编程课结束后检查是否有任何回复,也会询问我的教授。

//Arthur Byra
//27 January 2015
//Program to calculate kinetic equations


/*
vf = vi + a(t)
x = vi(t) + 1/2(a)(t)^2
vf^2 = vi^2 +2a(s)

where:

vi + initial velocity
vf = final velocity
s = distance
a = acceleration (must be constant)
t = time in seconds
*/

#include<iostream>
#include<cmath>
#include<iomanip>

using namespace std;

int main()
{

int userInput = 0;
double initialVelocity = 0;
double finalVelocity = 0;
double acceleration = 0;
double time = 0;
double deltaDistance = 0;
char userInputSolveFor;

cout << "This is a program to calculate certain variables using kinematic equations." << endl;
cout << "Remember that kinematic equations only work when acceleration is constant!" << endl;
cout << endl;
cout << "Which equation did you want to use?" << endl;
cout << endl;
cout << "1) vf = vi + a(t)" << endl;
cout << "2) x = vi(t) + 1/2(a)(t)^2" << endl;
cout << "3) vf^2 = vi^2 + 2(a)(x)" << endl;
cout << endl;
cout << "Input number of the equation you want to use (1,2,3): " << endl;
cin >> userInput;
switch (userInput)
{
case (userInput == 1):

cout << "You are using vf = vi + a(t)." << endl;
cout << endl;
cout << "What are you trying to solve for?" << endl;
cout << endl;
cout << "Use a for acceleration (in m/s/s)." << endl;
cout << "Use vi for initial velocity (in m/s)." << endl;
cout << "Use vf for final velocity (in m/s)." << endl;
cout << "Use t for time (in seconds)." << endl;
cin >> userInputSolveFor;
cout << endl;
switch (userInputSolveFor)
{
case (userInputSolveFor == a):

cout << "You are solving for acceleration." << endl; //Solving for acceleration
cout << endl;
cout << "What is the initial velocity (in m/s)?" << endl;
cin >> initialVelocity;
cout << "What is the final velocity (in m/s)?" endl;
cin >> finalVelocity;
cout >> "What is the time (in seconds)?" << endl;
cin >> time;
cout << "The acceleration is " << (finalVelocity - initialVelocity) / time << setprecision(10) << " m/s/s." << endl;
break;

case (userInputSolveFor == vi):

cout << "You are solving for initial velocity." << endl; //Solving for initial velocity
cout << endl;
cout << "What is your acceleration (in m/s/s)?" << endl;
cin >> acceleration;
cout << "What is your final velocity (in m/s)?" endl;
cin >> finalVelocity;
cout >> "What is the time (in seconds)?" << endl;
cin >> time;
cout << "The initial velocity is " << finalVelocity / (acceleration * time) << setprecision(10) << " m/s." << endl;
break;

case (userInputSolveFor == vf):

cout << "You are solving for final velocity." << endl; //Solving for final velocity
cout << endl;
cout << "What is your acceleration (in m/s/s)?" << endl;
cin >> acceleration;
cout << "What is your initial velocity (in m/s)?" endl;
cin >> initialVelocity;
cout >> "What is the time (in seconds)?" << endl;
cin >> time;
cout << "The final velocity is " << initialVelocity + (acceleration * time) << setprecision(10) << " m/s." << endl;
break;

case (userInputSolveFor == t):

cout << "You are solving for time." << endl; //Solving for time
cout << endl;
cout << "What is the acceleration (in m/s/s)?" << endl;
cin >> acceleration;
cout << "What is the initial velocity (in m/s)?" endl;
cin >> initialVelocity;
cout >> "What is the final velocity (in m/s)?" << endl;
cin >> finalVelocity;
cout << "The time is " << (finalVelocity - initialVelocity) / acceleration << setprecision(10) << " seconds." << endl;
break;

default:

cout <<"The input you have entered is not valid." << endl;
break;
}

case (userInput == 2):

cout << "You are using x = vi(t) + 1/2(a)(t)^2." << endl;
cout << endl;
cout << "What are you trying to solve for?" <<endl;
cout << endl;
cout << "Use x for distance (in meters)." << endl;
cout << "Use vi for initial velocity (in m/s)." << endl;
cout << "Use t for time (in seconds)." << endl;
cout << "Use a for acceleration (in m/s/s)." <<endl;
cin >> userInputSolveFor;
cout << endl;
switch (userInputSolveFor)
{
case (userInputSolveFor == a):
cout << "You are solving for acceleration." << endl;
cout << endl;
cout << "What is the distance (in meters)?" << endl;
cin >> deltaDistance;
cout << "What is the initial velocity (in m/s)?" << endl;
cin >> initialVelocity;
cout << "What is the time (in seconds)?" << endl;
cin >> time;
cout << "The acceleration is " << deltaDistance - (initialVelocity * time) / (0.5 * pow(time, 2.0)) << setprecision(10) << " m/s/s." << endl;
break;

case (userInputSolveFor == vi):

cout << "You are solving for initial velocity." << endl;
cout << endl;
cout << "What is the distance (in meters)?" << endl;
cin >> deltaDistance;
cout << "What is the acceleration (in m/s/s)?" << endl;
cin >> acceleration;
cout << "What is the time (in seconds)?" << endl;
cin >> time;
cout << "The initial velocity is " << (deltaDistance - ((pow(time, 2.0)) * acceleration * 0.5)) / time << setprecision(10) << " m/s." << endl;
break;

case (userInputSolveFor == x):

cout << "You are solving for distance." << endl;
cout << endl;
cout << "What is the acceleration (in m/s/s)?" << endl;
cin >> acceleration;
cout << "What is the initial velocity?" << endl;
cin >> initialVelocity;
cout << "What is the time (in seconds)?" << endl;
cin >> time;
cout << "The distance is " << (initialVelocity * time) + ((pow(time, 2.0)) * acceleration * 0.5) << setprecision(10) << " meters." << endl;
break;

case (userInputSolveFor == t):

cout << "You are solving for time." << endl;
cout << endl;
cout << "What is the acceleration (in m/s/s)?" << endl;
cin >> acceleration;
cout << "What is the initial velocity?" << endl;
cin >> initialVelocity;
cout << "What is the distance (in meters)?" << endl;
cin >> deltaDistance;

if (initialVelocity == 0):
{
cout << "The time is " << sqrt(deltaDistance - (0.5 * acceleration)) << setprecision(10) << " seconds." << endl;
}
else if (acceleration == 0):
{
cout << "The time is " << (deltaDistance / initialVelocity) << setprecision(10) << " seconds." << endl;
}

break;

default:
cout <<"The input you have entered is not valid." << endl;

break;
}

case (userInput == 3):

cout << "You are using vf^2 = vi^2 + 2(a)(x)." << endl;
cout << endl;
cout << "What are you trying to solve for?" <<endl;
cout << endl;
cout << "Use vf for final velocity (in m/s)." << endl;
cout << "Use vi for initial velocity (in m/s)." << endl;
cout << "Use a for acceleration." << endl;
cout << "Use x for distance (in meters)." <<endl;
cin >> userInputSolveFor;
cout << endl;
switch (userInputSolveFor)
{
case (userInputSolveFor == vf):

cout << "You are solving for final velocity." << endl;
cout << endl;
cout << "What is the initial velocity (in m/s)?" << endl;
cin >> initialVelocity;
cout << "What is the acceleration (in m/s/s)?" << endl;
cin >> acceleration;
cout << "What is the distance (in meters)?" << endl;
cin >> deltaDistance;
cout << "The final velocity is " << (sqrt(pow(initialVelocity, 2.0))) + (2 * acceleration * deltaDistance) << setprecision(10) << " m/s." << endl;
break;

case (userInputSolveFor == vi):

cout << "You are solving for initial velocity." << endl;
cout << endl;
cout << "What is the final velocity (in m/s)?" << endl;
cin >> finalVelocity;
cout << "What is the acceleration (in m/s/s)?" << endl;
cin >> acceleration;
cout << "What is the distance (in meters)?" << endl;
cin >> deltaDistance;
cout << "The initial velocity is " << (sqrt(pow(finalVelocity, 2.0)) / (2 * acceleration * deltaDistance) << setprecision(10) << " m/s." << endl;
break;

case (userInputSolveFor == a):

cout << "You are solving for acceleration." << endl;
cout << endl;
cout << "What is the final velocity (in m/s)?" << endl;
cin >> finalVelocity;
cout << "What is the initial velocity (in m/s)?" << endl;
cin >> initialVelocity;
cout << "What is the distance (in meters)?" << endl;
cin >> deltaDistance;
cout << "The acceleration is " << (sqrt(pow(finalVelocity, 2.0)) - (sqrt(pow(initialVelocity, 2.0)))) / (2 * deltaDistance)) << setprecision(10) << " m/s/s." << endl;
break;

case (userInputSolveFor == x):

cout << "You are solving for distance." << endl;
cout << endl;
cout << "What is the final velocity (in m/s)?" << endl;
cin >> finalVelocity;
cout << "What is the initial velocity (in m/s)?" << endl;
cin >> initialVelocity;
cout << "What is the acceleration (in m/s/s)?" << endl;
cin >> acceleration;
cout << "The distance is " << (sqrt(pow(finalVelocity, 2.0)) - sqrt(pow(initialVelocity, 2.0))) / (2.0 * acceleration) << setprecision(10) << " meters." << endl;
break;

default:
cout <<"The input you have entered is not valid." << endl;
break;
}

default:
while (userInput <= 1 || userInput >= 3)
{
cout << "The number you have entered is not valid." << endl;
cin >> userInput;
}
}
return 0;
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com