翻转数字,c ++代码

时间:2017-05-04 17:57:34

标签: c++

我还不擅长编码,我想问一个关于我的代码的问题。我想要的是翻转文件中写的数字。

与123一样,它会推出321.

我已经编写了一个代码而且我无法弄清楚它有什么问题,我希望你能花一些时间仔细研究它,我错过了一些东西......

#include <iostream>
#include <fstream>


using namespace std;


int main()
{

    char duomenys[] = "2017kuzU1.txt";

    int n;
    int pradinis[] = {};
    int check;
    int skaitmenys[] = {};


    ifstream fd(duomenys);
    fd >> n;


    for(int P = 0; P < n;P++)
    {
        check = 0;
        fd >> pradinis[P];
        while(pradinis[P] > 0){
            skaitmenys[check] = (pradinis[P]%10);
            pradinis[P] /= 10;
            check++;
        }

        for(int j = 0; j < check;j++){
            cout << skaitmenys[j] << endl;
        }
    }

return 0;
}

1 个答案:

答案 0 :(得分:0)

你的代码有很多错误,除了OVER之外没有其他地方可以启动。

// by Dt t 5/4/2017
// this code will repeatedly write to a file then reverse what it wrote.  Go 
//look in the file before and after runs to see the effect...
//create

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


int main()
{
const int SIZE = 5;
ifstream fin;
ofstream fout;
char character;
char myChars[SIZE];

fout.open("2017kuzU1.txt");//creates and then fills the file
fout<<'X'<<'b'<<'c'<<'1'<<'2';
fout.close();//close the file

fin.open("2017kuzU1.txt");//opens and reads the file

for(int i = 0; i<SIZE;i++)
{

    fin.get(character);//now look at the file to be sure the stuff is there
    cout<<character;
    myChars[i] = character;//write it into an array for later
}
cout<<" \n";

fin.close();//close the file
for(int i = 0; i<SIZE;i++)
{       
    cout<<(myChars[i]);
}
cout<<"GTF";
system("pause");
fout.open("2017kuzU1.txt");//open the file for output
for(int i = 0; i<SIZE;i++)
{
    fout<<(myChars[4-i]);//reverse the contents fo the file
    cout<<(myChars[4-i]);//show the reverse on the screen
}
fout.close();
return 0;
}

此代码将在项目文件夹中创建一个文件,将一些内容写入文件然后关闭它。然后它将打开文件并以相反的顺序将文件写回文件。在运行之前查看文件然后在运行之后查看效果。请注意,值会在每次运行开始时重置......