| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 473 人关注过本帖
标题:中途自动退出和变量问题...
只看楼主 加入收藏
terisevend
Rank: 1
等 级:新手上路
帖 子:62
专家分:0
注 册:2007-6-2
收藏
 问题点数:0 回复次数:2 
中途自动退出和变量问题...

刚才写了个插入排序和合并排序的结合排序法, 但是很奇怪.

Q1: 我定义了MAXSIZE为50, 但是当输入完24个数据后,会自动退出输入...接着我把50改成99, 当输入完72个数据后,又
自动退出输入...(初步怀疑是编译器问题)

Q2: 在Insert_Sort程序块中, 在while里面的条件判定中,如果使用a[i]<a[j]&&...,会导致排序失败(当需要排序的个数小时,排序成功.但当个数大时,排序失败). 如果改成tmp<a[i]&&...就不会出现排序失败.为什么呢?

贴出源程序(没加入输入数据为数字的判定...忽略吧~)

#include <cstdlib>
#include <iostream>

#define MAXSIZE 50

using namespace std;

void Put_In( int[], int& );
void Sort( int[], int, int );

int main(int argc, char *argv[])
{

int a[MAXSIZE], k, i = 0;

Put_In( a, i ); //Put_In

if(i)
{
cout << "\n请输入分段段数! (即把数据分成多少段进行排序,最后合并!)\n" << endl;

do{

cout << "分段段数为: ";
cin >> k;

if( k <= 0 || k > i )
cout << "输入错误!请重新输入!" << endl; //end if

}while( k <= 0 || k > i ); //end do-while

Sort( a, i, k ); //Sort

for(int j = 0; j < i; j++ )
cout << a[j] << " "; //end for

} //end if

cout << endl;

system("PAUSE");
return EXIT_SUCCESS;

} //main


void Put_In( int a[], int& i )
{

cout << "请输入非负整数据进行排序(数据个数不大于50,输入-1结束!)\n" << endl;

do{
cout << "第" << i+1 << "个数据为: ";
cin >> a[i];

if( a[i] < 0 && a[i] != -1 )
cout << "输入错误!请重新输入!" << endl;
else if( a[i] != -1 )
i++; //end if
//end if-else

} while( i < MAXSIZE && a[i] != -1 ); //end do-while

} //Put_In


void Insert_Sort( int a[], int p, int q )
{

int i, j, tmp;

for( i = p + 1; i <= q; i++ )
{
j = i-1;
tmp = a[i];

while( tmp < a[j] && j >= p )
{
a[j+1] = a[j];
j--;
} //end while

a[j+1] = tmp;
} //end for

} //Insert_Sort


void Merge( int a[], int p, int q, int r )
{

int tmp[r-p+1], i = 0, k = p, t = q+1;

while( p <= q && t <= r )
{
if( a[p] <= a[t] )
tmp[i++] = a[p++];

else
tmp[i++] = a[t++]; //end if-else
} //end while

while( p <= q )
tmp[i++] = a[p++]; //end while

while( t <= r )
tmp[i++] = a[t++]; //end while

for( int j = 0 ; k <= r ; k++ )
a[k] = tmp[j++]; //end for

} //Merge


void Sort( int a[], int i, int k )
{

int p, j, t[k+1];

if(i%2)
p = (i+1)/k;
else
p = i/k;

for( j = 0; j < k; j++ )
t[j] = j*p; //end if-else

t[j] = i;


for( int j = 0; j < k; j++ ){
Insert_Sort( a, t[j], t[j+1]-1 ); //end for

for( int w = t[j]; w <= t[j+1]-1; w++ )
cout << a[w] << " || "; //end for, test Insert_Sort, ignore...

cout << endl;

} //end for

for( int j = 0; j < k; j++ )
Merge( a, 0, t[j]-1,t[j+1]-1 ); //end for

} //Sort





[此贴子已经被作者于2007-7-13 16:56:10编辑过]

搜索更多相关主题的帖子: 变量 自动 
2007-07-13 16:52
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
回复:(terisevend)中途自动退出和变量问题...
does not compile on vs 2005. please note that

int tmp[r-p+1],....

is not standard C++. In starndard C++, a static array has as its size constant expression.



------ Build started: Project: algorithms, Configuration: Debug Win32 ------
Compiling...
main.cpp
.\main.cpp(92) : error C2057: expected constant expression
.\main.cpp(92) : error C2466: cannot allocate an array of constant size 0
.\main.cpp(92) : error C2133: 'tmp' : unknown size
.\main.cpp(118) : error C2057: expected constant expression
.\main.cpp(118) : error C2466: cannot allocate an array of constant size 0
.\main.cpp(118) : error C2133: 't' : unknown size
Build log was saved at "file://D:\unzip\!tmp\BuildLog.htm"
algorithms - 6 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-07-14 02:12
terisevend
Rank: 1
等 级:新手上路
帖 子:62
专家分:0
注 册:2007-6-2
收藏
得分:0 

恩, 这点明白...不过问题出在输入的时候...输入的模块中,并没有调用Merge函数吧...


2007-07-14 13:58
快速回复:中途自动退出和变量问题...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.045918 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved