c - C 程序编译没有错误。它显示垃圾。我认为第一个while循环可能是罪魁祸首
问题描述
这个程序将创建一个菜单,可以
- 将 2
char
秒作为char
数组中的点 - 计算点之间的距离
- 显示方程中的线
y = mx + b
- 计算离线最近的点
- exit [程序只会在选择 e 时终止]
某处存在逻辑错误,导致无法显示正确的输出。我一直在用我的头撞我的键盘,我非常希望我犯了一个愚蠢的简单错误。菜单显示太多次,所以我怀疑 while 循环有误。我不是程序员,但我的数学应该很扎实。请帮我修正我的逻辑。
int main()
{
char PtLbl[9] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I' }; /*char and xcoordinates and y coordinates arrays hardcoded*/
double Xcoord[9] = { 50.2, 45.7, 76.7, 12, -25, 23, 34.6 };
double Ycoord[9] = { 75.8, 48, 99.1, 14, 48, -36, -123 };
double temp, m = 0, b = 0, dist;
double small = 99999999999.0;
char choice = 'z';
char pt1 = 'z', pt2 = 'z';
int in1 = -1, in2 = -1, i, finalin;
int k = 0, l = 1;
while (1)
{
printf("a) Enter Points:\nb) Compute the distance\nc) Display the line equation\nd) Find the closest point to the line equation in c\ne) Exit\n");
scanf("%c", &choice); /*Displays the menu options, takes in a menu choice, and converts it to lowercase if upper*/
choice = tolower(choice);
if (choice == 'a') /*choice a selected*/
{
k = 1;
while (in1 == -1 && in2 == -1)
{
printf("Please enter 2 points separated by one space\n");
scanf("%c %c", &pt1, &pt2);
pt1 = toupper(pt1);
pt2 = toupper(pt2);
for (i = 0; i < 9; i++) /*stores the index of the char in PtLbl array as index1 and index2*/
{
if (pt1 == PtLbl[i])
{
in1 = i;
}
if (pt2 == PtLbl[i])
{
in2 = i;
}
}
}
}
else if ((choice == 'b' || choice == 'c' || choice == 'd') && k == 0) /*since points need to be selected first, this checks if a has been selected*/
{
printf("Please enter 2 points before making a further selection\n");
continue;
}
else if (choice == 'b') /*calulates the distance between the points with the corresponding indexes in the x-coordinates and y-coordinates arrays*/
{
temp = sqrt(pow(Xcoord[in2] - Xcoord[in1], 2) + pow(Ycoord[in2] - Ycoord[in1], 2));
printf("The distance between points %c and %c is %f\n", PtLbl[in1], PtLbl[in2], temp);
}
else if (choice == 'c') /*calculates slope (m) and y intercept (b) to put in a line equations and display*/
{
m = (Ycoord[in2] - Ycoord[in1]) / (Xcoord[in2] - Xcoord[in1]);
b = (Ycoord[in1] - m * Xcoord[in1]);
printf("The equation of the line passing through %c and %c is y = %f x+ %f\n", PtLbl[in1], PtLbl[in2], m, b);
}
else if (choice == 'd') /*calculates the from the line to nearest point*/
{
if (m == 0 && b == 0) /*only calculates (m) and (b) if c hasnt been selected yet*/
{
m = (Ycoord[in2] - Ycoord[in1]) / (Xcoord[in2] - Xcoord[in1]);
b = (Ycoord[in1] - m * Xcoord[in1]);
}
for (i = 0; i < 9; i++)
{
if (i == in1 || i == in2)
{
continue;
}
dist = abs(m*Xcoord[i] + (-1)*Ycoord[i] + b) / sqrt((m*m) + 1); /*smallest distance is recorded and index is stored*/
if (dist < small)
{
small = dist;
finalin = i;
}
}
printf("The closest point to the line y = %f x + %f is %c at a distance of %f\n", m, b, PtLbl[finalin], small); /*displays data from d*/
}
else if (choice == 'e') /*program only exits when choice e is selected*/
{
exit(0);
}
else
{
printf("Please enter a valid selection"); /*prompts user to try again if input invalid*/
continue;
}
}
}
解决方案
明显的错误——你没有在你的 scanf 调用中忽略空格(如换行符),所以你的代码会被它们弄糊涂和抛弃。您想%c
在格式字符串中的指令之前放置空格以忽略空格:
scanf(" %c", &choice);
和
scanf(" %c %c", &pt1, &pt2);
推荐阅读
- javascript - 在我做完所有事情后,我的所有照片都找不到课程
- c# - 将 WebView2 合并到 WinForm 自定义控件中
- python - Pulp 整数编程约束被忽略
- javascript - 检索(并传递)某个类的所有元素的 ID
- graphics - VK_DEPENDENCY_BY_REGION_BIT的含义和含义
- docker - 在 Sonatype Nexus 3 中具有私有 Docker 注册表的 Gitlab CI 的 DinD 配置
- javascript - 如何在 server.js 中导入 ES 模块
- javascript - 我的服务器文件出错 - 无效的 URL:index.js
- flutter - Flutter - 以编程方式将 SharedPreferences + AppDocsDirectory 文件备份到 Drive/iCloud
- python - 如何在 matplotlib 中绘制带有索引日期的 Pandas DataSeries?