2014-02-06 46 views
0

我似乎無法有任何輸出,不幸的是我的老師不能幫助我。我使用Xcode,之前我們不得不改變一些東西來讓程序工作,這次我什麼也得不到。起初,我只用了六個數字的泡泡排序並讓它運行,但我們添加了一個選擇排序,而不是有數字,它們是從txt文件讀入的。主從未運行?泡泡排序和選擇排序

#include <iostream> 
#include <string> 
#include <fstream> 
#include <cstdlib> 

using namespace std; 

void bubble(string filename); 
void selectionSort(string filename); 


const int SIZE = 100000; 
int values[SIZE]; 

// Main 
int main() 
{ 
    cout << "Beginning set of our bubble sorts. Please stand by... " << endl; 
    bubble("inOrder.txt"); 
    bubble("revOrder.txt"); 
    bubble("ranOrder1.txt"); 
    bubble("ranOrder2.txt"); 
    cout << "Finished with four bubble sorts. " << endl; 

system("PAUSE"); 
return 0; 
} 

// Functions 

void bubble(string filename) 
{ 
    unsigned long int passCount = 0, compCount = 0, swapCount = 0; 

    int temp; 
    bool swap; 

    ifstream fin(filename.c_str()); 

    for (int j = 0; j < SIZE; j++) 
     fin >> values[j]; 


    do 
    { passCount++; 
     swap = false; 
     for (int count = 0; count < (SIZE -1); count++) 
     { 
      compCount++; 
      if(values[count] > values[count + 1]) 
      { 
       swapCount++; 
       temp = values[count]; 
       values[count] = values[count + 1]; 
       values[count + 1] = temp; 
       swap = true; 
      } 
     } 


    } while (swap); 
    string outputName = "Bubble" + filename; 

    ofstream fout(outputName.c_str()); 

    for (int j = 0; j < SIZE; j++) 
     fout << values[j] << endl; 
    cout << "The number of passes is: " << passCount << endl; 
    cout << "The number of comparious is:" << compCount << endl; 
    cout << "The number of swaps is: " << swapCount << endl; 

} 


// Selection 



void selectionSort(int array[], int size) 
{ 
    int startScan, minIndex, minValue; 

    for (startScan = 0; startScan < (SIZE - 1); startScan++) 
    { 
     minIndex = startScan; 
     minValue = values[startScan]; 
     for(int index = startScan + 1; index < size; index++) 
     { 
      if (values[index] < minValue) 
      { 
       minValue = values[index]; 
       minIndex = index; 
      } 
     } 
     values[minIndex = array[startScan]]; 
     values[startScan] = minValue; 
    } 
} 

我試着改變單詞'數組'的值,但它沒有奏效。在改變函數的參數(並改變上面提到的其他部分)之前,我們只有'字符串文件名',它工作正常。很多東西都在爲老師工作,但她正在使用Visual Express。我現在正在下載,但學校互聯網速度非常慢,所以我正在努力處理我的工作。

任何建議將不勝感激。 此外,大部分代碼都是作爲一個類來完成的,但並不是所有的東西在xcode中都是一樣的。作爲作業,我不得不添加選擇排序,我正在努力一點。

編輯 對不起,我的意思是我需要輸出文件。我們有一個單獨的.cpp文件來生成程序進行排序所需的數量。

#include <fstream> 
#include <iostream> 
#include <stdlib.h> 
using namespace std; 

const int maxSize = 100000; 

int main() 
{ 

    ofstream fout1("inOrder.txt"); 
    ofstream fout2("revOrder.txt"); 
    ofstream fout3("ranOrder.txt"); 
    ofstream fout4("ranOrder.txt"); 

    cout << "Starting to create 4 output files. . ." << endl; 

    for (int i = 1; i <= maxSize; i++) 
    { 
     fout1 << i << endl; 
     fout2 << maxSize - i + 1 << endl; 
     fout3 << rand() << endl; 
     fout4 << rand() << endl; 
    } 
    cout << "Finished creating 4 output files . . ." << endl; 

    system("PAUSE"); 
    return 0; 

} 

所有這些代碼都是由我的導師編寫的。對於Xcode,我只是將文件添加到主程序所在的文件夾中?

+0

我會開始確保您的文件正確打開。 Xcode的run-from目錄並不像看起來那麼簡單。它通常在某個臨時構建文件夾中關閉。如果您確定這是問題,則可以更改它(除非您知道這一點,並且已經在schema-config中解決了這個問題)。 – WhozCraig

+0

你確定沒有任何反應?我希望能得到一些編譯器錯誤,告訴我關於僞造的名稱'array'(你說你修好了),缺少''''',或者'system',因爲你不包含''。請注意,'system(「PAUSE」)'可能不會在非Microsoft平臺上執行任何操作,但僅當您使用一個會在退出時關閉程序輸出窗口的惡意IDE時才需要。 –

+0

爲什麼不簡單地通過調試問題1.添加一些適當的錯誤處理和2.單步執行代碼? – Till

回答

0

我以前也有使用Xcode打開文件的問題。我發現如果你沒有爲你想要打開的txt文件指定一個完整的文件路徑,Xcode會按照預期檢查項目文件夾。問題是你必須將txt文件添加到Xcode項目中,而不是將其放在Finder的項目文件夾中。

在Xcode中,轉到FILE>添加文件,然後將您嘗試打開的txt文件添加到項目中。