在执行结果循环期间不破坏语句 [英] During execution result loop not breaking the statement
问题描述
#include< stdio.h>
int a [ 100 ],c,n,search;
void L_search( int );
void B_search( int );
int main()
{
int ch;
printf( 输入元素数:);
scanf( %d,& n);
printf( 输入%d个元素:\t,n);
for (c = 0 ; c< n; c ++)
{
scanf( %d,& a [c]);
}
printf( 输入要搜索的元素>>) ;
scanf( %d,& search);
printf( 搜索元素的方式......);
printf( \ n1.Linear search \\\
2.Binary search \ n);
printf( 选择任何...>>);
scanf( %d,& ch);
while (ch)
{
switch (ch)
{
case 1 :
printf( 以线性方式搜索... \ n);
L_search(搜索);
break ;
case 2 :
printf( 以二进制方式搜索... \ n);
B_search(搜索);
break ;
默认:
printf( 无效的选项。\ n请再试一次);
}
}
返回 0 ;
}
void L_search( int )
{
for (c = 0 ; c< = n; c ++)
{
if (a [c] == search)
{
printf( %d出现在位置%d。\ n,search,c + 1);
break ;
}
else
printf( 找不到数字);
}
}
void B_search( int )
{
int first = 0 ;
int last = n-1;
int middle =(first + last)/ 2;
while (第一个< = last)
{
< span class =code-keyword> if (a [middle] < search)
{
first = middle + 1 ;
}
其他 如果(a [middle] ==搜索)
{
printf( %d找到位置%d \ n,search,middle + 1);
break ;
}
else
{
last = middle - 1 跨度>;
}
middle =(first + last)/ 2;
}
如果(首先> last)
{
printf( 未找到!%d不在列表中。,搜索) ;
}
}
我的尝试:
i试图运行...程序运行成功但是在执行结果执行输入值之后...结果循环没有破坏它的语句...
你的问题描述很模糊,并没有解释很多。
但是......一旦你进入main
,您无法退出或更改选项。将选择输入代码移动到循环内,并在列表中添加exit选项。
引用:我试图运行...程序运行成功但是在执行结果期间输入值后...结果循环没有破坏它的语句...
根本不清楚。
当你不明白你的代码是什么时,解决方案是调试器。
有一个允许您查看代码正在执行的工具,其名称为调试程序。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。
当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器。
使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。
调试器 - 维基百科,免费的百科全书 [ ^ ]
掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]
使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]
调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。
调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期效果时,你就接近了一个错误。
你的主要功能有一个重大缺陷,请尝试使用以下代码。
int main()
{
int ch;
printf( 输入元素数:);
scanf( %d,& n);
printf( 输入%d个元素:\t,n);
for (c = 0 ; c< n; c ++)
{
scanf( %d,& a [c]);
}
do
{
printf( 输入要搜索的元素>>);
scanf( %d,& search);
printf( 搜索元素的方式......);
printf( \ n1.Linear search \\\
2.Binary search \ n);
printf( 选择任何...>>);
scanf( %d,& ch);
switch (ch)
{
case 1 :
printf( 以线性方式搜索... \\\
跨度>);
L_search(搜索);
break ;
case 2 :
printf( 以二进制方式搜索... \ n);
B_search(搜索);
break ;
case - 1 :
printf( goodbye.\\\
);
break ;
默认:
printf( 无效的选项。\ n请再试一次\\ n);
break ;
}
} while (ch!= - 1 );
return 0 ;
}
你的L_search
函数中还有一个小缺陷,改为(你必须也改变它的原型):
void L_search()
{
for (c = 0 ; c< = n; c ++)
{
if (a [c] == search)
{
printf( %d出现在位置%d。\ n,search,c + 1);
return ;
}
}
printf( Number Not Found\\\
跨度>);
}
我没有分析B_search
函数,这是留给你的练习。
请注意:使用全局变量是一种不好的做法,我强烈建议你使用局部变量并将它们作为函数参数传递。
#include<stdio.h>
int a[100],c,n,search;
void L_search(int);
void B_search(int);
int main()
{
int ch;
printf("Enter the number of element:");
scanf("%d",&n);
printf("Enter %d elements:\t",n);
for(c=0;c<n;c++)
{
scanf("%d",&a[c]);
}
printf("Enter that element to search>>");
scanf("%d",&search);
printf("way to search an element...");
printf("\n1.Linear search\n2.Binary search\n");
printf("choose any...>>");
scanf("%d",&ch);
while(ch)
{
switch(ch)
{
case 1:
printf("Searching in Linear way...\n");
L_search(search);
break;
case 2:
printf("Searching in binary way...\n");
B_search(search);
break;
default :
printf("Invalid option.\nPlease try again");
}
}
return 0;
}
void L_search(int)
{
for(c=0;c<=n;c++)
{
if (a[c]==search)
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
else
printf("Number Not Found");
}
}
void B_search(int)
{
int first = 0;
int last = n-1;
int middle = (first+last)/2;
while (first <= last)
{
if(a[middle] < search)
{
first = middle + 1;
}
else if(a[middle] == search)
{
printf("%d found at location %d\n", search, middle+1);
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if(first > last)
{
printf("Not found! %d is not present in the list.",search);
}
}
What I have tried:
i have tried to run... program is running successfully but after the input value during execution of result ... the result loop is not breaking its statement...
Your problem description is very vague,and doesn't explain a lot.
But...once you enter the loop inmain
, you have no way to exit, or change the options. Move the selection input code inside the loop, and add an "exit" option to the list.
Quote:i have tried to run... program is running successfully but after the input value during execution of result ... the result loop is not breaking its statement...
Not clear at all.
When you don't understand what is doing your code, the solution is the debugger.
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
You have a major flaw in your main function, try the following code instead.
int main() { int ch; printf("Enter the number of element:"); scanf("%d",&n); printf("Enter %d elements:\t",n); for(c=0;c<n;c++) { scanf("%d",&a[c]); } do { printf("Enter that element to search>>"); scanf("%d",&search); printf("way to search an element..."); printf("\n1.Linear search\n2.Binary search\n"); printf("choose any...>>"); scanf("%d",&ch); switch(ch) { case 1: printf("Searching in Linear way...\n"); L_search(search); break; case 2: printf("Searching in binary way...\n"); B_search(search); break; case -1: printf("goodbye.\n"); break; default : printf("Invalid option.\nPlease try again\n"); break; } } while ( ch != -1); return 0; }
Also you have a minor flaw in yourL_search
function, change to (you have to change its prototype as well):
void L_search() { for(c=0;c<=n;c++) { if (a[c]==search) { printf("%d is present at location %d.\n", search, c+1); return; } } printf("Number Not Found\n"); }
I didn't analyze theB_search
function, that's left to you as an exercise.
Please note: using global variables is a bad practice, I strongly encourage you to use local variables and pass them as function parameters.
这篇关于在执行结果循环期间不破坏语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!