Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ I/O问题,三个输入文件使用此代码,但第一个输出文件当前已完成,另外两个在c++;_C++_Arrays_Function_Sorting_Io - Fatal编程技术网

C++ I/O问题,三个输入文件使用此代码,但第一个输出文件当前已完成,另外两个在c++;

C++ I/O问题,三个输入文件使用此代码,但第一个输出文件当前已完成,另外两个在c++;,c++,arrays,function,sorting,io,C++,Arrays,Function,Sorting,Io,我正在尝试使用冒泡排序对三个文件(I/O)进行排序。为此,我编写了以下代码 #include <bits/stdc++.h> using namespace std; typedef long long ll; void bubble_Sort_ascending_order(ll n, ll v[]){ ll i,j; for(i=1; i<=n-1; i++) { for(j=1; j<=n-i; j++)

我正在尝试使用冒泡排序对三个文件(I/O)进行排序。为此,我编写了以下代码

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

void bubble_Sort_ascending_order(ll n, ll v[]){
    ll i,j;
    for(i=1; i<=n-1; i++)
    {
        for(j=1; j<=n-i; j++)
        {
            if(v[j]>v[j+1])
                swap(v[j],v[j+1]);
        }
    }
}

int bubble_Sort_ascending_input1() {

    freopen("input1.txt","r",stdin);
    freopen("output_File1.txt","w",stdout);

    ll n;
    cin >> n;
    ll t[n];
    for(ll i=1; i<=n; i++)
    {
        cin >> t[i];
    }

    bubble_Sort_ascending_order(n,t);

    for(ll k=1; k<=n; k++)
    {
        cout << t[k] << endl;
    }
}

int bubble_Sort_ascending_input2() {

    freopen("input2.txt","r",stdin);
    freopen("output_File2.txt","w",stdout);

    ll n;
    cin >> n;
    ll r[n];
    for(ll i=1; i<=n; i++)
    {
        cin >> r[i];
    }

    bubble_Sort_ascending_order(n,r);

    for(ll k=1; k<=n; k++)
    {
        cout << r[k] << endl;
    }
}

int bubble_Sort_ascending_input3() {

    freopen("input3.txt","r",stdin);
    freopen("output_File3.txt","w",stdout);

    ll n;
    cin >> n;
    ll v[n];
    for(ll i=1; i<=n; i++)
    {
        cin >> v[i];
    }

    bubble_Sort_ascending_order(n,v);

    for(ll k=1; k<=n; k++)
    {
        cout <<v[k]<<endl;
    }
}


int main(){
    bulble_Sort_ascending_input1();
    bubble_Sort_ascending_input2();    
    bubble_Sort_ascending_input3();
}
#包括
使用名称空间std;
typedef long-long-ll;
无效气泡排序升序(ll n,ll v[]{
ll i,j ;;
对于(i=1;i>n;
ll t[n];
对于(ll i=1;i>t[i];
}
冒泡排序升序(n,t);
对于(llk=1;kr[i];
}
冒泡排序升序(n,r);
对于(llk=1;kv[i];
}
气泡排序升序(n,v);

for(ll k=1;k这可能无法回答您的问题,但我发现您的
冒泡排序算法中存在一些缺陷。首先,索引必须从0开始,否则您将永远无法读取数组中的第一个元素,然后您可以在for循环中根据正确的条件将其加快一点

然后您可以编写
bubble\u Sort\u ascending\u input()
方法,作为一个参数,例如
std::string input\u file\u name
std::string output\u file\u name
。这样您就可以更轻松地调试程序并查看问题所在

出于对上帝的爱,不要使用
#include

#包括
使用名称空间std;
typedef long-long-ll;
无效气泡排序升序(ll n,ll v[]{
ll i,j ;;
对于(i=0;iv[j+1])
互换(v[j],v[j+1]);
}
}
}
无效气泡排序升序(常量标准::字符串和输入文件,常量标准::字符串和输出文件){
fstream输入(“../”+input_文件,fstream::in);
fstream输出(“../”+输出文件,fstream::out);
//“./”仅用于IDE,如果从cmd运行,请删除路径
ll n;
if(input.is_open()){
输入>>n;
ll arr[n];
int i=0;
while(输入>>arr[i]){
++一,;
}
气泡排序升序(n,arr);
if(output.is_open()){
适用于(ll&编号:arr){

输出请格式化代码,以便它可以读取,也可以看到为什么使用C++代码> C++ >代码:而不是<代码> STD::fSturth?读取,然后使用。
#include <fstream>

using namespace std;

typedef long long ll;

void bubble_Sort_ascending_order(ll n, ll v[]) {
    ll i,j;
    for(i = 0; i < n-1; i++) {
        for(j = 0; j < n - i -1; j++) {
            if(v[j] > v[j+1])
                swap(v[j], v[j+1]);
        }
    }
}

void bubble_Sort_ascending(const std::string& input_file, const std::string& output_file) {

    fstream input("../"+input_file, fstream::in);
    fstream output("../"+output_file, fstream::out);
    // "../" is just for IDE, if you run it from cmd, delete the path

    ll n;
    if (input.is_open()) {
        input >> n;
        ll arr[n];
        int i = 0;
        while (input >> arr[i]) {
            ++i;
        }
        bubble_Sort_ascending_order(n, arr);
        if (output.is_open()) {
            for (ll& number : arr) {
                output << number << " ";
            }
        }
    } else {
        cout << "input not open";
    }
}

int main() {
   bubble_Sort_ascending("input1.txt", "output1.txt");
   return 0;
}