Untitled

mail@pastecode.io avatar
unknown
c_cpp
a month ago
1.7 kB
1
Indexable
Never
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;

int transformer(string str) //changing number from string to int
{
	int num = 0;
	
	for(int i=0;i<str.size();i++)
		num += (str[i]-'0')*pow(10,str.size()-i-1);
		
	return num;
}

string reverser(int num) //changing number from int to string
{
	string num_char_inversed;
	string num_char;
	
	while((num/10)!=0)
	{
		num_char_inversed+=char(num%10+48);
		num/=10;
	}
	num_char_inversed+=char(num%10+48);
	
	for(int i=0;i<num_char_inversed.size();i++)
		num_char+=num_char_inversed[num_char_inversed.size()-i-1];
		
	return num_char;
	
}

int main()
{
    fstream fs;
    
    char buffer[256] = {};
    string buffer_string = "";
    string number_string = "";
    int number=0,tag=0;
    int number_array[16] = {};
    
    fs.open("exercise.txt",ios::in);
    fs.read(buffer, sizeof(buffer));
    
    buffer_string = buffer;
    
    for(int i=0;i<buffer_string.size();i+=(number_string.size()+1))
    	{
    		number_string="";
    		for(int j=i;buffer_string[j]>='0'&&buffer_string[j]<='9';j++) //get the number without space
    			number_string+=buffer_string[j];
    		number=pow(transformer(number_string)+10,2); //transforming & calculating
    		number_array[tag]=number;
    		tag++;
		}
		
	buffer_string="";
		
	for(int i=0;i<tag;i++)
	{
		buffer_string+=reverser(number_array[i]); //reversing
		buffer_string+=" ";
	}
	
    fs.close();
    
    for(int i=0;i<buffer_string.size();i++)
    	buffer[i] = buffer_string[i];
    
    fs.open("exercise_output.txt",ios::out);
    fs.write(buffer, sizeof(buffer));
    fs.close();
    
    return 0;
}
Leave a Comment