2013-05-09 112 views
1

該程序應該詢問用戶設置的數值,然後再次詢問用戶的值,對它們進行排序,然後用戶將輸入一個值。程序應該搜索這個值,並返回值的位置或告訴用戶它不是他給出的列表的一部分。爲什麼我的程序在使用不同的編輯器和編譯器時會有所不同?

我用記事本++和pocketC++來運行和編譯整個程序,然後我調用了這個UNIX編輯器,putty和g ++,這是我遇到問題的地方。當我使用putty和g ++時,程序編譯得很好,但只會返回中間排序的值。例如,我想要3個數字,1 2 3.然後,我詢問2是哪個程序,它返回2在第二個位置,但其他任何數字都會給我一個垃圾值。這隻適用於putty和C++,而pocketC++可以處理所有的值。

#include <iostream> 
#include <iostream> 
#include <string> 
#include <cstdlib> 
using namespace std; 

int main() { 
    char answer; 
    int list,target, first, last; 
    int *array; 
    int search(int , int , int , int); 
    int sort(int a[], int); 
    int index_of_smallest(const int array[], int first, int target); 
    void swap(int& v1, int& v2); 
    int search(int data[], int target, int first, int last); 

    cout << "How many integers does your list have?\n"; 
    cin >> list; 
    array = new int[list]; 
    cout << "Please enter your integers: "; 
    for (int i = 0; i < list; i++) 
     cin >> array[i]; 

    do { 
     cout << "\nWhat is the target value?\n"; 
     cin >> target; 
     sort(array,list); 
     first = 0; 
     last = list-1; 
     int k= search(array, target, first, last); 
     if (k == -1) cout << "That number is not a part of your list of integers.\n"; 
     else cout << "The location of " << target << " is spot " << k+1 << endl; 
     cout << "Would you like to search a different number?" << endl; 
     cin >> answer; 
    } while (answer !='n' && answer !='N'); 
    return 0; 
} 

int index_of_smallest(const int array[], int first, int target){ 
    int min= array[first]; 
    int index_min=first; 
    for (int i = first + 1; i < target; i++) 
     if (array[i] < min) {min = array[i]; 
    index_min = i;} 
    return index_min; 
} 

void swap(int& v1, int& v2){ 
    int temp; 
    temp = v1; 
    v1=v2; 
    v2=temp; 
} 

void sort(int a[], int num){ 
    int next_smallest; 
    for (int i= 0; i < num - 1; i++) 
    { 
     next_smallest =index_of_smallest(a, i, num); 
     swap (a[i],a[next_smallest]); 
    } 
} 

int search(int data[], int target, int first, int last) { 
    int middle; 
    if (first > last) 
     return -1; 
    else { 
     middle=(first + last)/2; 
     if (target == data[middle]) 
      return middle;  
     else if (target < data[middle]) 
      search(data, target, first, middle-1); 
     else if (target > data[middle]) 
      search(data, target, middle +1, last);} 
} 
+5

如果不同的編譯器提供不同的結果,它可能是你調用未定義行爲(例如,在你沒有分配的內存中取消引用指針,在緩衝區溢出或溢出等),但是編譯器可以根據需要編碼,但實際上結果將取決於他們如何決定將堆棧中的內存打包在一起和堆 - 這將從編譯到編譯有所不同。 – Patashu 2013-05-09 03:49:26

+0

是否應該在主函數中放置函數聲明? – olevegard 2013-05-09 03:55:56

+2

您需要使用更傳統且一致的縮進樣式來改進代碼的格式,供其他人閱讀。同樣,在C++中,沒有必要或沒有理由在塊的開頭聲明每個變量。避免使用'namespace std',特別是當你使用名爲'array'的變量時''std''中的一個類型的名字。當你沒有編寫低級內存管理類時,調用'new'和'delete'是內存損壞的常見原因。函數前向聲明通常放在現代C++的函數之外。請使用http://sscce.org/。 – Yakk 2013-05-09 03:56:21

回答

2

在簡單地查看代碼時,沒有什麼比這更奇怪的了。話雖這麼說,如果我通過編譯器運行代碼,以下警告出來:

[11:04pm][[email protected] /tmp] g++ -Wall foo.cc 
foo.cc: In function ‘int search(int*, int, int, int)’: 
foo.cc:76: warning: control reaches end of non-void function 

所以我們看一下搜索功能。它看起來像你預期的功能是遞歸的,所以我相信你應該有更多的回報語句寫它:

int search(int data[], int target, int first, int last) { 
    int middle; 
    if (first > last) 
     return -1; 
    else { 
     middle=(first + last)/2; 
     if (target == data[middle]) 
      return middle;  
     else if (target < data[middle]) 
      return search(data, target, first, middle-1); 
     else if (target > data[middle]) 
      return search(data, target, middle +1, last); 
    } 
} 
+0

這是我的問題!我忘了我的回報!謝謝!!感謝所有幫助過的人! – 2013-05-09 04:12:43

相關問題