Рекомендация: прочитайте задачу! Выполните ее и сравните полученный результат с картинкой CMD
Если вы полностью уверены что не можете осилить эту задачу, советую вам просмотреть код и полностью в нем разобраться! Протестировать все в отладчике!
Разработать программу для чтения и записи в файл двумерного массива чисел типа double. Отдельные операции обработки представить функциями. Наличие меню приветствуется. Обработкой исключений и ошибок ввода можно пренебречь.
//#include "H.h"
#pragma once
#include <iostream>
#include <fstream>
#include <time.h>
#include <stdlib.h>
#include <string>
using namespace std;
#define Stop_Console_Windows system("pause>>void");
double ** Create_Arr(double ** &arr, int row, int col);
void Delete_New(double ** &arr, int row);
void Filling_Matrix_User(double ** &arr, int row, int col);
void Rand_Matrix(double ** &arr, int row, int col);
void display(double ** &arr, int row, int col);
//Functions.cpp
#include "H.h"
void display(double ** &arr, int row, int col) { //Print Console MATRIX
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
void Rand_Matrix(double ** &arr, int row, int col) { // RAND
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
arr[i][j] = 1 + rand() % (1000 * (10 - 1)) / 1000.0f; // min + rand() % (max - min)
}
}
}
void Filling_Matrix_User(double ** &arr, int row, int col) { // User _ INPUT
string s;
double tmp = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cin >> arr[i][j];
}
}
}
double ** Create_Arr(double ** &arr, int row, int col) { // ARRAY CREATE
arr = new double *[row]();
for (int i = 0; i < row; i++)
arr[i] = new double[col]();
return arr;
}
void Delete_New(double** &arr, int row) { // DELETE MATRIX
for (int i = 0; i < row; i++)
delete[] arr[i];
delete[] arr;
}
#include "H.h"
struct User_Data {
double ** Ptr_Write;
double ** Ptr_Read;
};
int main() {
srand(time(NULL));
int row = 0, col = 0, menu_insert = 0, count = 0;
string User_Input, name_file;
ofstream file_write;
ifstream file_read;
User_Data a;
//-------------------------------------------------------------------------------------------------------------------
//Create USER MATRIX
cout << "Enter row col: " << endl;
cin >> row >> col;
Create_Arr(a.Ptr_Write, row, col);
//-------------------------------------------------------------------------------------------------------------------
do { // User_Input
cout << "Fill the matrix with fractional numbers" << endl;
cout << "1. Hands" << endl;
cout << "2. RAND" << endl;
cin >> menu_insert;
switch (menu_insert) { // Menu
case 1: {
Filling_Matrix_User(a.Ptr_Write, row, col);
break;
}
case 2: {
Rand_Matrix(a.Ptr_Write, row, col);
break;
}
default: cout << "error" << endl; break;
}
} while (menu_insert > 2 || menu_insert < 1);
display(a.Ptr_Write, row, col);
cout << "(Create a file, specify the name with the extension .txt) :" << endl;
cin >> name_file; //Name_FILE
file_write.open(name_file); //FILE OPEN
if (!file_write.is_open()) { // write
cout << "error open file" << endl;
}
else
{
const char ch = ' ';
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
file_write << a.Ptr_Write[i][j]; // write " " (SET)
file_write << ch;
}
file_write << endl;
}
}
file_write.close(); // FILE CLOSE
file_read.open(name_file); // FILE OPEN
if (!file_read.is_open()) { // read
cout << "error open file" << endl;
}
else
{
row = 0;
col = -1; // -1 \n
string tmp;
while (!file_read.eof()) {
getline(file_read, tmp);
if (tmp.find(' ') != string::npos) // row
row++;
}
file_read.close();
file_read.open(name_file); // Carriage return
while (!file_read.eof()) { // col
file_read >> tmp;
col++;
}
file_read.close();
file_read.open(name_file); // Carriage return
col = col / row;
Create_Arr(a.Ptr_Read, row, col);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
file_read >> tmp; // Read " " (GET)
a.Ptr_Read[i][j] = stod(tmp); // Convert DOUBLE
}
}
}
file_read.close(); // FILE CLOSE
display(a.Ptr_Read, row, col); //DISPLAY READ FILE
Delete_New(a.Ptr_Write, row);
Delete_New(a.Ptr_Read, row);
Stop_Console_Windows
return 0;
}