C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 307,153 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,651 people online right now. Registration is fast and FREE... Join Now!




Expanded form of a number?

2 Pages V  1 2 >  

Expanded form of a number?

avirag

12 Aug, 2009 - 11:02 PM
Post #1

D.I.C Head
**

Joined: 29 Jun, 2009
Posts: 87



Thanked: 5 times
My Contributions
hi i m making a window application in which i want that
when i enter a expanded form of a number in a textbox like (5*10000+6*1000+7*100+8*10+9*1) , then after button click it will show the answer of it like (56789).


and i also want reverse of this that when i enter a number like (56789) in textbox, then after button click it will show its expanded form like( 5*10000+6*1000+7*100+8*10+9*1 ).


Can anyone tell me how can i do this?

kindly provide me code, if possible.....

User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >  
Reply to this topicStart new topic
Replies(1 - 19)

janne_panne

RE: Expanded Form Of A Number?

12 Aug, 2009 - 11:46 PM
Post #2

D.I.C Addict
****

Joined: 9 Jun, 2009
Posts: 531



Thanked: 107 times
My Contributions
First, you need to get a random number by using Random class, example:
CODE

Random random = new Random();
int number = random.Next(1, 100000);


Next, I would create a string which has the number in it:
CODE

string strNumber = number.ToString();


Parsing string is easier than number in my opinion.

Next, you get the numbers of strNumber and multiply them by "1" + requiredAmountOfZeros, something like this:
CODE

string line = "";
for (int i = 0; i < strNumber.Length; i++) {
  line += strNumber[i].ToString() + "*1";
  for (int j = 0; j < strNumber.Length - (i + 1); j++)
  {
    line += "0";
  }
}


Might work but I didn't bother to test it. Testing the code and correcting the errors is your job.
User is offlineProfile CardPM
+Quote Post

avirag

RE: Expanded Form Of A Number?

13 Aug, 2009 - 02:05 AM
Post #3

D.I.C Head
**

Joined: 29 Jun, 2009
Posts: 87



Thanked: 5 times
My Contributions
thanks 4 ur rply...
but i m not getting this...
actully i want that first i enterd value in textbox n then on button click it will show the answer on another textbox.
can u tell me the code on button click
User is offlineProfile CardPM
+Quote Post

werta

RE: Expanded Form Of A Number?

13 Aug, 2009 - 02:16 AM
Post #4

New D.I.C Head
*

Joined: 26 Oct, 2007
Posts: 4


My Contributions
tell me if i get it...

you enter something into a textbox like 5*5*5 then it will show 125, then you type 125 and no mater what else you type every time you type 125 you want it to show you 5*5*5 cause is the stored operation for that 125?

QUOTE(avirag @ 13 Aug, 2009 - 02:05 AM) *

thanks 4 ur rply...
but i m not getting this...
actully i want that first i enterd value in textbox n then on button click it will show the answer on another textbox.
can u tell me the code on button click


User is offlineProfile CardPM
+Quote Post

avirag

RE: Expanded Form Of A Number?

13 Aug, 2009 - 03:14 AM
Post #5

D.I.C Head
**

Joined: 29 Jun, 2009
Posts: 87



Thanked: 5 times
My Contributions
well i m made a code for expanded form, but it will give one error
error is:
like i enterd a number 1234
then on button click it will give 1*1000+2*100+3*10+4*1+
here it will print one extra +
how can i remove this problem

here is my code:

CODE
private void button1_Click(object sender, EventArgs e)
        {
            long x = Convert.ToInt64(textBox1.Text);
            string strNumber = x.ToString();
            string line = "";
            for (int i = 0; i < strNumber.Length; i++)
            {
                line +=strNumber[i].ToString() + "*1";
                for (int j = 0; j < strNumber.Length - (i + 1); j++)
                {
                    line += "0";
                }
                line += "+";
            }
            
            this.textBox2.Text = line.ToString();

        }

can anyone resolve my this problem
and also tell me how can i do the reverse of this.
i mean when i enter 1*1000+2*100+3*10+4*1 in one textbox, then on button click it will give its collapse form like 1234......
kindly give me code, if possible

This post has been edited by avirag: 13 Aug, 2009 - 03:18 AM
User is offlineProfile CardPM
+Quote Post

janne_panne

RE: Expanded Form Of A Number?

13 Aug, 2009 - 03:40 AM
Post #6

D.I.C Addict
****

Joined: 9 Jun, 2009
Posts: 531



Thanked: 107 times
My Contributions
You can remove the plus sign either with string.SubString or string.Remove methods, here is example with substring:

CODE

line = line.Substring(0, line.Length - 1);


And the easiest solution for your second problem is probably to store your variable "long x" to class scope instead of method scope, then you could show the same number easily.

CODE

private long x;
private void button1_Click(object sender, EventArgs e) {
// random code
}

private void button_show_answer_click(object sender, EventArgs e) {
  // user clicked show button: show variable x
  TextBoxAnswer.Text = x.ToString();
}

User is offlineProfile CardPM
+Quote Post

avirag

RE: Expanded Form Of A Number?

13 Aug, 2009 - 03:55 AM
Post #7

D.I.C Head
**

Joined: 29 Jun, 2009
Posts: 87



Thanked: 5 times
My Contributions
[quote name='janne_panne' date='13 Aug, 2009 - 03:40 AM' post='733691']
You can remove the plus sign either with string.SubString or string.Remove methods, here is example with substring:

CODE

line = line.Substring(0, line.Length - 1);


this code has not solved my problem,kindly modify my code with another logic........
User is offlineProfile CardPM
+Quote Post

vinnijain

RE: Expanded Form Of A Number?

13 Aug, 2009 - 11:44 PM
Post #8

New D.I.C Head
*

Joined: 26 Jun, 2009
Posts: 12

.a

This post has been edited by vinnijain: 13 Aug, 2009 - 11:46 PM
User is offlineProfile CardPM
+Quote Post

avirag

RE: Expanded Form Of A Number?

14 Aug, 2009 - 01:10 AM
Post #9

D.I.C Head
**

Joined: 29 Jun, 2009
Posts: 87



Thanked: 5 times
My Contributions
Can anyone give me the replay of my problem.!

well i m made a code for expanded form, but it will give one error
error is:
like i enterd a number 1234
then on button click it will give 1*1000+2*100+3*10+4*1+
here it will print one extra +
how can i remove this problem

here is my code:

CODE
private void button1_Click(object sender, EventArgs e)
        {
            long x = Convert.ToInt64(textBox1.Text);
            string strNumber = x.ToString();
            string line = "";
            for (int i = 0; i < strNumber.Length; i++)
            {
                line +=strNumber[i].ToString() + "*1";
                for (int j = 0; j < strNumber.Length - (i + 1); j++)
                {
                    line += "0";
                }
                line += "+";
            }
            
            this.textBox2.Text = line.ToString();

        }

can anyone resolve my this problem
and also tell me how can i do the reverse of this.
i mean when i enter 1*1000+2*100+3*10+4*1 in one textbox, then on button click it will give its collapse form like 1234......
kindly give me code, if possible

User is offlineProfile CardPM
+Quote Post

janne_panne

RE: Expanded Form Of A Number?

14 Aug, 2009 - 01:29 AM
Post #10

D.I.C Addict
****

Joined: 9 Jun, 2009
Posts: 531



Thanked: 107 times
My Contributions
Like I wrote earlier, just a simple substring will do the trick. Here is the substring added to previous code:

CODE
private void button1_Click(object sender, EventArgs e)
{
    long x = Convert.ToInt64(textBox1.Text);
    string strNumber = x.ToString();
    string line = "";
    for (int i = 0; i < strNumber.Length; i++)
    {
        line +=strNumber[i].ToString() + "*1";
        for (int j = 0; j < strNumber.Length - (i + 1); j++)
        {
            line += "0";
        }
        line += "+";
    }

    // this line here:
    line = line.Substring(0, line.Length - 1);
            
    this.textBox2.Text = line.ToString();

}


Hope this helps.
User is offlineProfile CardPM
+Quote Post

avirag

RE: Expanded Form Of A Number?

14 Aug, 2009 - 01:44 AM
Post #11

D.I.C Head
**

Joined: 29 Jun, 2009
Posts: 87



Thanked: 5 times
My Contributions
Thanks 4 ur reply.....
it works
User is offlineProfile CardPM
+Quote Post

avirag

RE: Expanded Form Of A Number?

14 Aug, 2009 - 02:47 AM
Post #12

D.I.C Head
**

Joined: 29 Jun, 2009
Posts: 87



Thanked: 5 times
My Contributions
hi, jaane paane
The solution which u gave me for my second problem does not work when I am making separate applications for both....
Kindly tell me how can I find numeral for expanded form ........
How can i parse the string to find the equivalent numeral
User is offlineProfile CardPM
+Quote Post

avirag

RE: Expanded Form Of A Number?

17 Aug, 2009 - 03:05 AM
Post #13

D.I.C Head
**

Joined: 29 Jun, 2009
Posts: 87



Thanked: 5 times
My Contributions
Can anyone give me the reply to solve my problem.?
User is offlineProfile CardPM
+Quote Post

avirag

RE: Expanded Form Of A Number?

17 Aug, 2009 - 03:48 AM
Post #14

D.I.C Head
**

Joined: 29 Jun, 2009
Posts: 87



Thanked: 5 times
My Contributions
aa

This post has been edited by avirag: 17 Aug, 2009 - 03:49 AM
User is offlineProfile CardPM
+Quote Post

SwiftStriker00

RE: Expanded Form Of A Number?

17 Aug, 2009 - 03:49 AM
Post #15

D.I.C Regular
Group Icon

Joined: 25 Dec, 2008
Posts: 297



Thanked: 18 times
Dream Kudos: 125
My Contributions
QUOTE(avirag @ 17 Aug, 2009 - 05:05 AM) *

Can anyone give me the reply to solve my problem.?


Ok heres what I think you want; I made a console app to test it, but you can easily pull out the methods, and logic from it. If I didn't follow what you wanted as an end result, let me know. (Nothing based off previous, I just did both methods myself)

CODE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DIC_Help {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine("Input String");
            string input = Console.ReadLine();

            if( input.Contains("+") ) {
                //Its in expanded form
                input = expandToNum(input);
            } else {
                input = numToExpand(input);
            }
            Console.WriteLine("\nAnswer: " + input);
            Console.ReadLine();
        }

        private static string numToExpand(string input) {
            string result = "";
            int digit_place = 1;
            string[] places = new string[input.Length];
            for( int i = 0; i < input.Length; i++ ) {
                string temp = input[input.Length - 1 - i] + "*" + digit_place;
                digit_place*=10;
                places[places.Length - 1 - i] = temp;
            }
            for( int i = 0; i < places.Length; i++ ){
                result += places[i] + " + ";
            }
            result = result.Substring(0, result.Length - 2);
            return result;
        }

        private static string expandToNum(string input) {
            string result = "";
            input = input.Replace(" ", ""); //Remove whitespace
            char[] delim = new char[1];
            delim[0] = '+';
            string[] terms = input.Split(delim);
            delim[0] = '*';
            for( int i = 0; i < terms.Length; i++ ) {
                string curTerm = (terms[i].Split(delim))[0];
                result += curTerm;
            }
            return result;
        }
    }
}

User is offlineProfile CardPM
+Quote Post

avirag

RE: Expanded Form Of A Number?

17 Aug, 2009 - 04:15 AM
Post #16

D.I.C Head
**

Joined: 29 Jun, 2009
Posts: 87



Thanked: 5 times
My Contributions
Thanks a lot......
I was working on this code since 7 days...
but thanks to you that u solved a big prob of mine...........
User is offlineProfile CardPM
+Quote Post

avirag

RE: Expanded Form Of A Number?

17 Aug, 2009 - 04:22 AM
Post #17

D.I.C Head
**

Joined: 29 Jun, 2009
Posts: 87



Thanked: 5 times
My Contributions
hi SwiftStriker00........
Can u tell me one more thing..

How can i solve mathmatical expresion(example: A+B*C-D/E, assuming A,B,C,D,E are any numbers) with the help of BODMAS rule.
The main problem which i m facing is that i do not know how to parse the input string and solve it by BODMAS rule.
Kindly help me regarding this......



User is offlineProfile CardPM
+Quote Post

SwiftStriker00

RE: Expanded Form Of A Number?

17 Aug, 2009 - 04:29 AM
Post #18

D.I.C Regular
Group Icon

Joined: 25 Dec, 2008
Posts: 297



Thanked: 18 times
Dream Kudos: 125
My Contributions
Well, now your getting into a little bit more complicated of a subject. Your going to have to break your input into tokens (an array of individual chars). Then your going to need a little recursion and an understanding of Polish Notation (wiki has good explination). The computer works in Polish notation, and that way you do your order of operations (BODMAS/PEDMAS).
User is offlineProfile CardPM
+Quote Post

avirag

RE: Expanded Form Of A Number?

17 Aug, 2009 - 04:52 AM
Post #19

D.I.C Head
**

Joined: 29 Jun, 2009
Posts: 87



Thanked: 5 times
My Contributions
hi SwiftStriker00.
i tried, but could not achieved anything............
Can u provide me the code, if possible........
User is offlineProfile CardPM
+Quote Post

SwiftStriker00

RE: Expanded Form Of A Number?

17 Aug, 2009 - 05:05 AM
Post #20

D.I.C Regular
Group Icon

Joined: 25 Dec, 2008
Posts: 297



Thanked: 18 times
Dream Kudos: 125
My Contributions
QUOTE(avirag @ 17 Aug, 2009 - 06:52 AM) *

hi SwiftStriker00.
i tried, but could not achieved anything............
Can u provide me the code, if possible........


That code is a bit more than a snippet. And would take considerable thought, something I shouldn't be doing at work wink2.gif .

But think about this. In my previous code, I broke the string up based on an operation sign, correct? Use that knowledge to start attacking the issue. You know that * and / go first so look for those so find those, and do the math on those and replace the "B*C" with your new result "F". So your next state would look like " A+F-D/E". Now keep doing this until there are no more math symbols. Btw Parentheses will complicate this process greatly, so avoid them at first, and figure out what your donig.
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 05:07PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month