Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7682043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:28:35+00:00 2026-05-31T18:28:35+00:00

For my program I need to generate valid infix expressions with customizable complexity. The

  • 0

For my program I need to generate valid infix expressions with customizable complexity. The problem is that I cant figure out a way to prevent division by zero, floating point answers and negative answers.

To prevent negative answers, I’m taking a dirty approach. That is, generate an expression, evaluate it, if it turns out to be negative, generate again.Here are some things you should know:

  1. inToPost() is a method that converts the generated infix expression to postfix for evaluation.
  2. complexityLevel <= DIVIDE implies that we are not supposed to put parenthesis in the expression.
  3. complexityLevel == ARITHMETIC_PARENTHESIS implies that parenthesis are included.

How can I make sure that a) there are no divisions by zero b) no divisions result in floating point (figured out a dirty way to do this) c) final result is not negative
Here is the code

public void generateRandom(int operandLimit, int operatorCount, int complexityLevel) {
        Random rand = new Random();
        infix.clear();

        int i = 0;
        infix.add( rand.nextInt(operandLimit) + 1 );

        while(i < operatorCount) {
            int operator;
            if(complexityLevel <= DIVIDE)
                operator = rand.nextInt(complexityLevel - 1)*1000 + 1000;
            else
                operator = rand.nextInt(complexityLevel - 3)*1000 + 1000;

            int operand = rand.nextInt(operandLimit) + 1;

            if( operator == Operator.DIVIDE ) {
                int lastNum = infix.get(infix.size() - 1);

                if( lastNum < operand) {
                    int temp = operand;
                    operand = lastNum;
                    lastNum = temp;
                }

                lastNum -= lastNum % operand;
                infix.set(infix.size() - 1, lastNum);
            }

            infix.add( operator );
            infix.add( operand );

            ++i;
        }

        if(complexityLevel == ARITMETIC_PARENTHESIS) {
            int braceOpen = rand.nextInt( operatorCount ) * 2;
            infix.add(braceOpen, Operator.BR_OPEN );
            infix.add(braceOpen + 4, Operator.BR_CLOSE);
        }

        inToPost();
        if(evaluate() < 0)
            generateRandom(operandLimit, operatorCount, complexityLevel);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:28:37+00:00Added an answer on May 31, 2026 at 6:28 pm

    It looks like you’ve dealt with your conditions (b) and (c) already. Since your operands are never 0, I would guess that the only possible violation of (a) is if the added parentheses happen to wrap a zero value, and the operator before that is a division. You could check for that case if you modified your inToPost() to take subexpressions:

    if(braceOpen > 0 && infix.get(braceOpen) == Operator.DIVISION && 
            evaluate(inToPost(infix.subList(braceOpen, braceOpen + 3))) == 0) {
        // Put parentheses elsewhere, or cancel
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to generate a program that generates the Fibonacci Sequence Here is what
I need to generate multiple databases to SQL, so I need script/program to automatically
In my program I need a way to click on an Image control, the
I have a .NET program and a Borland Win32 program that need to pass
I have a program that I need to run under *nix and windows. because
I'm working on a program for class that involves solving the Chinese Postman problem
I need something that can generate a PDB from a DLL file (C# .NET
I am writing a program to try to solve a math problem. I need
I'm writing a program for Employees Management and i need to automatically generate a
I need to generate an SSH key pair that I'll be working with later

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.