将列式数据写入.txt文件。 C ++

时间:2016-10-05 14:29:49

标签: c++

我是C ++的新手。我被要求编写一个包含数据列的文本文件。特别是四列将指数(x)的实际值与我的系列展开函数进行比较,达到多度。 x的范围为0到1,间隔为0.04。

例如

    public class news extends Fragment {

        private RecyclerView recyclerView;
        private ArrayList<Deatails> data;
        private DataAdapter adapter;
        private View myFragmentView;


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            myFragmentView = inflater.inflate(R.layout.news, container, false);
            initViews();
            return myFragmentView;
        }

        private void initViews() {
            recyclerView = (RecyclerView)myFragmentView.findViewById(R.id.card_recycler_view);
            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(layoutManager);

data = new ArrayList<Deatails>();
adapter = new DataAdapter(getActivity(), data);
            recyclerView.setAdapter(adapter);

            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    loadJSON();
                }
            });
            t.start();
        }
        private void loadJSON() {
            if (isNetworkConnected()){
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://memaraneha.ir")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                RequestInterface request = retrofit.create(RequestInterface.class);
                Call<JSONResponse> call = request.getJSON();
                final ProgressDialog progressDialog = new ProgressDialog(getActivity());
                progressDialog.show();
                call.enqueue(new Callback<JSONResponse>() {
                    @Override
                    public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
                        progressDialog.dismiss();
                        JSONResponse jsonResponse = response.body();
                        data.addAll((Arrays.asList(jsonResponse.getAndroid()));
                        adapter.notifyDataSetChanged();
                    }
                    @Override
                    public void onFailure(Call<JSONResponse> call, Throwable t) {
                        progressDialog.dismiss();
                        Log.d("Error", t.getMessage());
                    }
                });
            }
            else {
                Toast.makeText(getActivity().getApplicationContext(), "دستگاه شما به اینترنت متصل نیست!", Toast.LENGTH_LONG).show();}
        }
        private boolean isNetworkConnected() {
            ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo ni = cm.getActiveNetworkInfo();
            if (ni == null) {
                // There are no active networks.
                return false;
            } else
                return true;
        }
    }

但是我很难将我的数据放入文本(.txt)文件中的整齐列中,而我根本不知道如何格式化它。

ExpSeries()是我的系列函数。

  x | exp(x) | M1 | M2 | M3 |
  0 |   1    | 1  |  1 |  1 |
0.04|   1.15 |1.01|1.09|1.12|  (values in example are not exact)
 .  |
 .
 .

4 个答案:

答案 0 :(得分:2)

您需要使用setw参数

设置输出的宽度

为每列使用相同的固定值。

答案 1 :(得分:2)

如果您使用std::setw中的std::setprecision<iomanip>,则可以将其格式化为非常好的&amp;容易。

demoFile << "  x  | exp(x) |  M1  |  M2  |  M3  |" << std::endl;
demoFile << "................................." << std::endl;

double i = 0;
for (; i < 1.0; i += 0.04){
    demoFile << std::setprecision(2) << std::fixed; //set some precision for nice format
    demoFile << std::setw(4) << i << " |";
    demoFile << std::setw(7) << exp(i) << " | ";
    demoFile << std::setw(4) << ExpSeries(i, 2) << " | ";
    demoFile << std::setw(4) << ExpSeries(i, 3) << " | ";
    demoFile << std::setw(4) << ExpSeries(i, 4) << " | ";

    demoFile << '\n';
}

在这里你可以看到输出(只是一些示例双打,但相同的设置):

   x  | exp(x) |  M1  |  M2  |  M3  |
 .................................
 0.00 |   0.00 | 0.00 | 0.00 | 0.00 |
 0.04 |   0.08 | 0.12 | 0.16 | 0.20 |
 0.08 |   0.16 | 0.24 | 0.32 | 0.40 |
 0.12 |   0.24 | 0.36 | 0.48 | 0.60 |
 0.16 |   0.32 | 0.48 | 0.64 | 0.80 |
 0.20 |   0.40 | 0.60 | 0.80 | 1.00 |
 0.24 |   0.48 | 0.72 | 0.96 | 1.20 |
 0.28 |   0.56 | 0.84 | 1.12 | 1.40 |
 0.32 |   0.64 | 0.96 | 1.28 | 1.60 |
 0.36 |   0.72 | 1.08 | 1.44 | 1.80 |
 0.40 |   0.80 | 1.20 | 1.60 | 2.00 |
 0.44 |   0.88 | 1.32 | 1.76 | 2.20 |
 0.48 |   0.96 | 1.44 | 1.92 | 2.40 |
 0.52 |   1.04 | 1.56 | 2.08 | 2.60 |
 0.56 |   1.12 | 1.68 | 2.24 | 2.80 |
 0.60 |   1.20 | 1.80 | 2.40 | 3.00 |
 0.64 |   1.28 | 1.92 | 2.56 | 3.20 |
 0.68 |   1.36 | 2.04 | 2.72 | 3.40 |
 0.72 |   1.44 | 2.16 | 2.88 | 3.60 |
 0.76 |   1.52 | 2.28 | 3.04 | 3.80 |
 0.80 |   1.60 | 2.40 | 3.20 | 4.00 |
 0.84 |   1.68 | 2.52 | 3.36 | 4.20 |
 0.88 |   1.76 | 2.64 | 3.52 | 4.40 |
 0.92 |   1.84 | 2.76 | 3.68 | 4.60 |
 0.96 |   1.92 | 2.88 | 3.84 | 4.80 |

答案 2 :(得分:1)

您可以使用setw设置列宽:

#include<iomanip>
using namespace std;
...
demoFile << setw(4) << exp(i);
demoFile << setw(5) << ExpSeries(i, 2);

您可能还需要使用std :: fixed和std :: precision来确保正确的小数位数。

答案 3 :(得分:0)

在字符串中,您可以使用

    /t

在变量之间。例如

  "exp(x) /t M1 /t M2"

/ t是一个标签。 @stack_danny正在展示正确的方式。

相关问题