该程序从用户处获取输入数字并按升序对其进行排序。
当用户点击“Enter”而不输入整数时,程序将停止取数并对循环进行排序。
我的节目并没有停止接受数字,而是陷入无限循环的数字。那是为什么?
#include <stdio.h>
#include "genlib.h"
SortIntegerArray()
{
int i, k, n, Array[200];
for (i = 0; i < 200; ++i)
{
for (k = i+1; k < 200; ++k)
{
if (Array[i] > Array[k])
{
n = Array[k];
Array[i] = Array[k];
Array[k] = n;
}
}
}
}
main()
{
int i, k, n, Array[200], number;
i = 0;
n = 0;
printf("Enter numbers\n");
number = GetInteger();
for (i = 0; i < 200; ++i)
{
if (number == "")
{
break;
}
scanf("%d", &Array[i]);
}
printf("The input array is: %d", Array[200]);
SortIntegerArray();
printf("The sorted array is: %d", Array[200]);
}
答案 0 :(得分:1)
试试这段代码
public void ConfigureServices(IServiceCollection services)
{
// Add Application settings to the services container.
services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));
// Add EF services to the services container.
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
// Add Identity services to the services container.
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Configure the options for the authentication middleware.
// You can add options for Google, Twitter and other middleware as shown below.
// For more information see http://go.microsoft.com/fwlink/?LinkID=532715
services.Configure<FacebookAuthenticationOptions>(options =>
{
options.AppId = Configuration["Authentication:Facebook:AppId"];
options.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
});
services.Configure<MicrosoftAccountAuthenticationOptions>(options =>
{
options.ClientId = Configuration["Authentication:MicrosoftAccount:ClientId"];
options.ClientSecret = Configuration["Authentication:MicrosoftAccount:ClientSecret"];
});
// Add MVC services to the services container.
services.AddMvc();
services.AddSingleton(a =>
{
//AppSettings settingsModel = ?? //GET CONFIGURATION SETTINGS FILLED
// TECHNICAL ARTIFICE TO RETRIEVE CURRENT SETTINGS
//var settingsModel = new AppSettings();
//var config = Configuration.GetSubKey("AppSettings");
//foreach (var item in typeof(AppSettings).GetProperties().Where(b => b.CanWrite))
{
//item.SetValue(settingsModel, config.Get(item.Name));
}
return new FooService(settingsModel);
});
//Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers.
//You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json.
services.AddWebApiConventions();
}
答案 1 :(得分:0)
GetInteger
返回一个整数,因此if (number == "")
永远不会评估为true,循环永远不会中断。
scanf
返回读取的项目数,如果用户未输入任何内容,则返回零。因此,您可以使用返回值来打破循环。
我不确定GetInteger
实际上做了什么,但它看起来并不像真的需要,因为你正在填充数组以使用scanf
在for循环中进行排序。
for (i = 0; i < 200; ++i)
{
if (scanf("%d", &Array[i]) < 1)
{
break;
}
}
答案 2 :(得分:0)
在您对函数中的任何内容进行排序之前,必须将数组传递给您的函数。打印或排序Array[200]
毫无意义。您必须迭代Array[i]
(或某个索引)才能实际访问各个成员。当你将Array
传递给sort函数时,你还必须传递数组中的元素数量(否则无法确定函数中的元素数量(除非你使用了一个sentinel元素,等等)。 。)
话虽如此,你的排序功能也是不正确的。使用简单的bubblesort进行校正,提供以下示例:
#include <stdio.h>
// #include "genlib.h"
int *SortIntegerArray (int *array, int n)
{
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < (n-1); j++) {
if (array[j] > array[j + 1]) {
int temp;
temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
}
return array;
}
int main (void)
{
int i, n, number;
int Array[200] = {0};
i = n = number = 0;
printf ("\nEnter number of elements for array below:\n\n");
printf (" number of elements: ");
scanf (" %d", &n);
printf ("\nEnter the array values:\n\n");
for (i = 0; i < n; i++)
{
printf (" input: ");
scanf (" %d", &Array[i]);
}
printf ("\nThe array values are:\n\n");
for (i = 0; i < n; i++)
printf (" Array[%3d] : %d\n", i, Array[i]);
SortIntegerArray (Array, n);
printf ("\nThe sorted array values are:\n\n");
for (i = 0; i < n; i++)
printf (" Array[%3d] : %d\n", i, Array[i]);
printf ("\n");
return 0;
}
<强>输入/输出强>
$ ./bin/sort_loop
Enter number of elements for array below:
number of elements: 6
Enter the array values:
input: 9
input: 2
input: 5
input: 3
input: 8
input: 4
The array values are:
Array[ 0] : 9
Array[ 1] : 2
Array[ 2] : 5
Array[ 3] : 3
Array[ 4] : 8
Array[ 5] : 4
The sorted array values are:
Array[ 0] : 2
Array[ 1] : 3
Array[ 2] : 4
Array[ 3] : 5
Array[ 4] : 8
Array[ 5] : 9
如果您有任何疑问,请与我们联系。