Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
This is the java code i need to convert to a c++ code:

Java
import static java.lang.Math.*;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.exit;
import static java.lang.System.arraycopy;
import static java.util.Arrays.sort;
import static java.util.Arrays.binarySearch;
import static java.util.Arrays.fill;
import java.util.*;
import java.io.*;

public class Main {

    public static void main(String[] args) throws IOException {
        try {
            if (new File("input.txt").exists())
                System.setIn(new FileInputStream("input.txt"));
        } catch (SecurityException e) {
        }
        new Main().run();

    }

    BufferedReader in;
    PrintWriter out;
    StringTokenizer st = new StringTokenizer("");

    long powTen[] = new long[11];

    class Sol implements Comparable<sol> {
        long head, tail, c;
        int N;

        Sol() {
            head = tail = c = N = 0;
        }

        long toInt(){
            return head * powTen[N + 1] + c * powTen[N] + tail;
        }
        @Override
        public int compareTo(Sol arg0) {
            if(toInt() > arg0.toInt())
                return 1;
            return -1;
        }

    }

    private void run() throws IOException {
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);

        powTen[0] = 1;
        for (int i = 1; i <= 10; i++)
            powTen[i] = powTen[i - 1] * 10;

        long X = nextLong();

        Sol Ans[] = new Sol[1000000];
        int V = 0;

        HashSet<long> used = new HashSet<long>();
        
        Sol temp = new Sol();

        for (int N = 0; N < 10 && powTen[N] <= X; N++)
            for (long c = 0; c < 10; c++) {
                for(long head11 = X / powTen[N] - c - 3; head11 <= X / powTen[N] - c + 1; head11++){
                    if(head11 >= 0 && head11 % 11 == 0){
                        long head = head11 / 11;
                        long tail2 = X - powTen[N] * (head11 + c);
                        long tail = tail2 / 2;
                        
                        if(tail2 >= 0 && tail < powTen[N] && tail2 % 2 == 0){
                            temp.N = N;
                            temp.c  = c;
                            temp.head = head;
                            temp.tail = tail;
                            if(!used.contains(temp.toInt())){
                                if(temp.toInt() > temp.head * powTen[N] + temp.tail){
                                    used.add(temp.toInt());
                                    Ans[V++] = temp;
                                    temp = new Sol();
                                }
                            }
                        }
                    }
                }
            }
        
        sort(Ans, 0, V);
        out.println(V);
        for(int i = 0; i < V; i++){
            out.print(Ans[i].toInt() + " + ");
            long len1 = Long.toString(Ans[i].toInt()).length();
            long len2 = Long.toString((Ans[i].head * powTen[Ans[i].N] + Ans[i].tail)).length();
            for(long j = 0; j < len1 - len2 - 1; j++)
                out.print("0");
            out.print((Ans[i].head * powTen[Ans[i].N] + Ans[i].tail));
            out.println(" = "+X);
        }
        in.close();
        out.close();
    }

    void chk(boolean b) {
        if (b)
            return;
        System.out.println(new Error().getStackTrace()[1]);
        exit(999);
    }

    void deb(String fmt, Object... args) {
        System.out.printf(Locale.US, fmt + "%n", args);
    }

    String nextToken() throws IOException {
        while (!st.hasMoreTokens())
            st = new StringTokenizer(in.readLine());
        return st.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    String nextLine() throws IOException {
        st = new StringTokenizer("");
        return in.readLine();
    }

    boolean EOF() throws IOException {
        while (!st.hasMoreTokens()) {
            String s = in.readLine();
            if (s == null)
                return true;
            st = new StringTokenizer(s);
        }
        return false;
    }
}




[edit]Code block added[/edit]







This is the converted C++ code, but I'm having a lot of error, i used the converter:

C++
#pragma once

using namespace System;
using namespace System::Collections::Generic;

//JAVA TO C++ CONVERTER TODO TASK: The Java 'import static' statement cannot be converted to C++:
import static System.currentTimeMillis;
//JAVA TO C++ CONVERTER TODO TASK: The Java 'import static' statement cannot be converted to C++:
import static System.exit;
//JAVA TO C++ CONVERTER TODO TASK: The Java 'import static' statement cannot be converted to C++:
import static System.arraycopy;
//JAVA TO C++ CONVERTER TODO TASK: The Java 'import static' statement cannot be converted to C++:
import static Arrays.sort;
//JAVA TO C++ CONVERTER TODO TASK: The Java 'import static' statement cannot be converted to C++:
import static Arrays.binarySearch;
//JAVA TO C++ CONVERTER TODO TASK: The Java 'import static' statement cannot be converted to C++:
import static Arrays.fill;

public ref class Main
{
internal:
    ref class Sol : IComparable<Sol^>
    {
    private:
        initonly Main ^outerInstance;

    internal:
        Int64 head, tail, c;
        int N;

        Sol(Main ^outerInstance);

        virtual Int64 toInt();
    public:
        virtual int compareTo(Sol ^arg0) override;

    };


//JAVA TO C++ CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static void main(String[] args) throws IOException
    static void main(array<String^> ^args);

internal:
    BufferedReader ^in_Renamed;
    PrintWriter ^out;
    StringTokenizer ^st;

    array<Int64> ^powTen;

private:
//JAVA TO C++ CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void run() throws IOException
    void run();

internal:
    virtual void chk(bool b);

    virtual void deb(String ^fmt, ... array<Object^> ^args);

//JAVA TO C++ CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: String nextToken() throws IOException
    virtual String ^nextToken();

//JAVA TO C++ CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: int nextInt() throws IOException
    virtual int nextInt();

//JAVA TO C++ CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: long nextLong() throws IOException
    virtual Int64 nextLong();

//JAVA TO C++ CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: double nextDouble() throws IOException
    virtual double nextDouble();

//JAVA TO C++ CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: String nextLine() throws IOException
    virtual String ^nextLine();

//JAVA TO C++ CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: boolean EOF() throws IOException
    virtual bool EOF();

private:
    void InitializeInstanceFields();

public:
    Main()
    {
        InitializeInstanceFields();
    }
};
Posted
Updated 23-Jun-13 7:04am
v2
Comments
[no name] 23-Jun-13 9:25am    
And your specific question is what? The errors and warnings seem pretty clear to me so what is the problem?
Patricia Bianca Duka 23-Jun-13 10:39am    
I need to write a program to find all pairs of integers such that their sum must be equal to a given integer number N and the second number results from the first one by striking out one of its digits. The first integer always has at least two digits and starts with a non-zero digit. The second integer always has one digit less than the first integer and may start with a zero.
Input
The first line of the input is an integer N, from 1 to 20, giving the number of test cases. This is then followed by N lines, containing one integer each, giving the desired sums (maximum of 5 digits each).
then the output shall commence only after the last test case (desired sum) had been inputted. For each test case, state how many pairs were found and list the pairs in equation form, one on each following line, as shown in the sample runs.
the list should be arranged in ascending order of the first addend.
and show leading 0’s to emphasize the number of digits of the second addend.

Patricia Bianca Duka 23-Jun-13 11:12am    
WHEN I USE THE CONVERTED CODE IN DEV-C++ I GET LOTS OF ERROR.
[no name] 23-Jun-13 11:49am    
Screaming at me is no way to get any help. And again. The errors and warnings you are getting are perfectly clear and understandable, so unless you come up with an actual question or describe an actual problem, I do not see what kind of help you would need.
Sergey Alexandrovich Kryukov 23-Jun-13 12:44pm    
This is not a question but an invitation to do your job for you. This is not how it might work.
If you don't know how to use Java, learn it. If you have particular concerns, ask specific questions.
—SA

1 solution

This sounds a lot like help with an assignment, but I am going to give you the benefit of the doubt on that one.

I don't think you should try the Java to C++ converter you're using as that seems to create C++.NET code which I'm guessing you don't want.

I think something like this might solve your problem;

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

string to_string(const int& value)
{
	ostringstream ss;
    ss << value;
    return ss.str();
}

int to_int(const string& value)
{
	int i;
	istringstream ss(value);
	ss >> i;
	return i;
}

int main(int argc, char* argv[])
{
	fstream file("C:\\Temp\\input.txt");

	int number_of_testcases;
	file >> number_of_testcases;

	ostringstream result;

	for(int i = 0; i < number_of_testcases; ++i) 
	{
		result << "Test case: " << (i + 1) << endl;
		int number;
		file >> number;
		
		ostringstream test_case_result;
		int matches = 0;
		for (int first = 10; first < number; ++first)
		{
			string second_string = to_string(first);
			for(int j = 0; j < second_string.size(); ++j) {
				const string removed_digit = second_string.substr(0, j) + second_string.substr(j + 1, second_string.size() - j);
				const int second = to_int(removed_digit);
				if (first + second == number)
				{
					++matches;
					test_case_result << first << " + " << second << " = " << number << endl;
				}
			}
		}
		result << matches << " match" << (matches == 0 ? "" : "es") << ": " << endl;
		result << test_case_result.str();
	}

	cout << result.str() << endl;

	return 0;
}


Hope this helps,
Fredrik
 
Share this answer
 
v2
Comments
zlogdan 2-Jul-13 15:38pm    
5 stars.
Fredrik Bornander 2-Jul-13 16:41pm    
Thanks dude!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900