generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Calculator code.

Previous Thread :: Next Thread 
bikerking300 is not online. bikerking300
Joined: 31 Mar 2010
Total Posts: 1034
27 Oct 2011 05:06 PM
Run this in Code::Blocks then hit Compile and run then you sir/Ma`am have a Calculator.

    #include <​iostream>
#include <​string>
using namespace std;
int main()
{
    cout << "These are the symbols you can use: -, +, /, *, Thats all. \n";

    int a;
    int b;
    int sum;
    string s;
    cout << "\nEnter a # \n";
    cin ​>> a;
    cout << "Enter another # \n";
    cin ​>> b;
    cout << "Enter a sign you want \n";
    cin ​>> s;
    if(s == "+")
    {
        sum = a + b;
        cout << "You`re sum is \n" << sum << endl;
    }
    else if(s=="*")
    {
        sum = a * b;
        cout << "You`re sum is \n" << sum << endl;
    }
    else if(s=="/")
    {
        sum = a/b;
        cout << "You`re sum is \n" << sum << endl;
    }
        else if(s=="-")
    {
        sum = a - b;
        cout << "You`re sum is \n" << sum << endl;
    }
    getchar();
    getchar();
    return 0;
}
Report Abuse
bikerking300 is not online. bikerking300
Joined: 31 Mar 2010
Total Posts: 1034
27 Oct 2011 05:07 PM
Change include to include <​string>
Report Abuse
crazyman32 is online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
27 Oct 2011 05:07 PM
What language is this? C++? Java? I really have no idea and it makes me feel stupid :(
Report Abuse
bikerking300 is not online. bikerking300
Joined: 31 Mar 2010
Total Posts: 1034
27 Oct 2011 05:07 PM
Wow that failed.. include> include < ​s t r i n g ​> Take out the spaces.
Report Abuse
bikerking300 is not online. bikerking300
Joined: 31 Mar 2010
Total Posts: 1034
27 Oct 2011 05:08 PM
@Crazy its C++. And Crazy you`re not stupid.. You`re awesome.
Report Abuse
crazyman32 is online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
27 Oct 2011 05:09 PM
I only know Lua and have JavaScript stored in my head somewhere :P

But I'm taking Java programming classes at my school starting in like 3 months :D
Report Abuse
bikerking300 is not online. bikerking300
Joined: 31 Mar 2010
Total Posts: 1034
27 Oct 2011 05:10 PM
Awesome and lucky you know JavaScript, I only know C, C++, Lua, RBX.Lua.
Report Abuse
crazyman32 is online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
27 Oct 2011 05:12 PM
JavaScript is extremely easy to learn. It's nearly identical to Lua, but with syntax more like standard C language.

if (condition) {
BLAH
}
elseif (condition) {
BLAH
}
else {
BLAH
}
Report Abuse
pwnedu46 is not online. pwnedu46
Joined: 23 May 2009
Total Posts: 7534
27 Oct 2011 05:14 PM
I a Java calculator applet.

Here's a sample execution screenshot of it.
http://www.roblox.com/CalcApplet-item?id=64561493
Report Abuse
bikerking300 is not online. bikerking300
Joined: 31 Mar 2010
Total Posts: 1034
27 Oct 2011 05:15 PM
Wow. I`ll start learning that sometime next week.
Report Abuse
pwnedu46 is not online. pwnedu46
Joined: 23 May 2009
Total Posts: 7534
27 Oct 2011 05:16 PM
"But I'm taking Java programming classes at my school starting in like 3 months :D"
I'm taking one now. You want the source code for the calculator applet?
Report Abuse
bikerking300 is not online. bikerking300
Joined: 31 Mar 2010
Total Posts: 1034
27 Oct 2011 05:16 PM
Ya I would love it but what program to run it on?
Report Abuse
pwnedu46 is not online. pwnedu46
Joined: 23 May 2009
Total Posts: 7534
27 Oct 2011 05:26 PM
You will need to download the Java developer kit (JDK) and the java runtime environment (JRE) to compile it into bytecode, then get JCreator and create a new applet project. You MUST name the project CalculatorApplet (EXACTLY as I just typed it) or it won't work because of the way Java runs. JCreator automatically generates the HTML code, so you just have to build the project and run the HTML file. Most of the code is instantiating buttons.

import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.text.DecimalFormat;
import javax.swing.*;

public class CalculatorApplet extends Applet implements ActionListener {

TextFormat txtFmt;
JTextField text1,text2;
JLabel l1,l2;
JPanel p;
TextArea txtArea;
double cal;
DecimalFormat fmt = new DecimalFormat("0.###");


public void init(){
txtArea = new TextArea("", 10, 30 , TextArea.SCROLLBARS_VERTICAL_ONLY);
txtArea.setEditable(false);
add(txtArea, "left");
txtFmt = new TextFormat(txtArea);
txtFmt.center("Waiting for input . . .");

text1 = new JTextField("", 12);
text2 = new JTextField("", 12);

l1 = new JLabel("Enter Number 1:");
l2 = new JLabel("Enter Number 2:");

p = new JPanel(true); // double buffered
Panel p1 = new Panel(new GridLayout(6,3));
Panel p2 = new Panel(new GridLayout(1,1));


JButton la1 = new JButton("Answer");
JButton la2 = new JButton("Answer ");
JButton add = new JButton("Add");
JButton sub = new JButton("Subtract");
JButton mul = new JButton("Multiply");
JButton div = new JButton("Divide");
JButton mod = new JButton("Modulus");
JButton exp = new JButton("Exponent");
JButton nrt = new JButton("Root");
JButton lgb = new JButton("Log with base b");
JButton clr = new JButton("Clear");

la1.addActionListener(this);
la1.setActionCommand("Answer");

la2.addActionListener(this);
la2.setActionCommand("Answer ");

add.addActionListener(this);
add.setActionCommand("Add");

sub.addActionListener(this);
sub.setActionCommand("Subtract");

mul.addActionListener(this);
mul.setActionCommand("Multiply");

div.addActionListener(this);
div.setActionCommand("Divide");

mod.addActionListener(this);
mod.setActionCommand("Modulus");

exp.addActionListener(this);
exp.setActionCommand("Exponent");

nrt.addActionListener(this);
nrt.setActionCommand("Root");

lgb.addActionListener(this);
lgb.setActionCommand("Log with base b");

clr.addActionListener(this);
clr.setActionCommand("Clear");



p1.add(l1);
p1.add(text1);
p1.add(la1);
p1.add(l2);
p1.add(text2);
p1.add(la2);
p1.add(new Label());
p1.add(new Label());
p1.add(new Label());
p1.add(add);
p1.add(sub);
p1.add(mul);
p1.add(div);
p1.add(mod);
p1.add(exp);
p1.add(nrt);
p1.add(lgb);
p1.add(clr);
p.add(p1);
p.add(p2);
add(p);


}

public void actionPerformed(ActionEvent evt){

String value1 = text1.getText();
double num1 = Double.parseDouble(value1);
String value2=text2.getText();
double num2 = Double.parseDouble(value2);
// JButton source = (JButton) evt.getSource();
String action;
action = evt.getActionCommand();

if (action.equals("Add")){ // Add
cal = num1 + num2;
txtFmt.center(num1 + " + " + num2 + " = " + fmt.format(cal));

} else if (action.equals("Subtract")){ // Subtract
cal = num1 - num2;
txtFmt.center(num1 + " - " + num2 + " = " + fmt.format(cal));

} else if (action.equals("Multiply")){ // Multiply
cal = num1 * num2;
txtFmt.center(num1 + " * " + num2 + " = " + fmt.format(cal));

} else if (action.equals("Divide")){ // Divide
cal = num1 / num2;
txtFmt.center(num1 + " / " + num2 + " = " + fmt.format(cal));

} else if (action.equals("Modulus")){ // Modulus (mod)
cal = num1 % num2;
txtFmt.center(num1 + " % " + num2 + " = " + fmt.format(cal));

} else if (action.equals("Exponent")){ // exponent
cal = Math.pow(num1, num2);
txtFmt.center(num1 + " ^ " + num2 + " = " + fmt.format(cal));

} else if (action.equals("Root")){ // nth root
cal = nRoot(num1, num2);
txtFmt.center(num1 + "^(1 / " + fmt.format(num2) + ") = " + fmt.format(cal));

} else if (action.equals("Log with base b")) { // log with base b
cal = logb(num1, num2);
txtFmt.center("log base " + fmt.format(num2) + " of " + fmt.format(num1) + " = " + fmt.format(cal));

} else if (action.equals("Answer")){ // puts prevous solution in textbox # 1
text1.setText(Double.toString(cal));

} else if (action.equals("Answer ")){ // puts prevous solution in textbox # 2
text2.setText(Double.toString(cal));

} else if (action.equals("Clear")){ // clear both textboxes and the output box
text1.setText("");
text2.setText("");
txtFmt.center("Cleared!");
}
}

public double logb(double arg, double base){
return Math.log(arg)/Math.log(base);
}

public double nRoot(double n1, double n2){
return Math.pow(n1, 1/n2);
}

}

class TextFormat { // somewhat buggy, but it gets the job done.

private static TextArea txt;
private static final int size = 60; // length of text box in characters.
// I'm not entirely sure why it's 60 instead of 30 like I declared it, but 60 seems to work.

public TextFormat(TextArea t) { // Class constructor
txt = t;
}

private static boolean chkOverflow(int len) { return Math.abs(len) != len; }
// returns true if an overflow occured, else returns false

private static void skip(int n, String str)
{
txt.setText("\n\n");
for (int k = 0; k <= n; k++)
txt.append(" ");
txt.append(str);
}

public static void leftJustify(String str)
{
txt.setText(str);
}

public static void center(String str)
{
int len = str.length();
int tab = (size - len)/2;
if (chkOverflow(tab) == false) {
skip(tab, str);
} else {
leftJustify(str);
}
}

public static void rightJustify(String str)
{
int len = str.length();
int tab = size - len;
if (chkOverflow(tab) == false) {
skip(tab, str);
} else {
leftJustify(str);
}
}

}
Report Abuse
bikerking300 is not online. bikerking300
Joined: 31 Mar 2010
Total Posts: 1034
27 Oct 2011 05:28 PM
I remember + " + " + num2 + " = " + for C#...
Report Abuse
pwnedu46 is not online. pwnedu46
Joined: 23 May 2009
Total Posts: 7534
27 Oct 2011 05:29 PM
Syntactically, C languages are very similar to Java.
Report Abuse
bikerking300 is not online. bikerking300
Joined: 31 Mar 2010
Total Posts: 1034
27 Oct 2011 05:30 PM
Oh.
Report Abuse
skipperguy12 is not online. skipperguy12
Joined: 09 Oct 2009
Total Posts: 955
27 Oct 2011 05:51 PM
YOu people are lucky. I have not learned ANY programming languages fluently. Just Lua. I only know markup languages, like HTML and CSS. I tried C++....I decided I should learn it later in the future ;)
Report Abuse
MrgamesNwatch is not online. MrgamesNwatch
Joined: 02 Feb 2009
Total Posts: 7729
27 Oct 2011 05:58 PM
i know RBX.Lua, Batch(umg super easy, started few days ago), and i'm learning C++ (going pretty well)
Report Abuse
scripterzack is not online. scripterzack
Joined: 22 Jan 2011
Total Posts: 562
27 Oct 2011 06:40 PM
I started learning Java, got bored and continued on with my 14 year old life >.< i like lua better or visual studio because they're easier in my opinion :P
Report Abuse
pwnedu46 is not online. pwnedu46
Joined: 23 May 2009
Total Posts: 7534
27 Oct 2011 06:42 PM
Lua is easier than Java, however Java has many more features, has WAY better documentation, and is much more widely used.
Report Abuse
crazyman32 is online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
27 Oct 2011 06:56 PM
@pwnedu46 - Think your code will work on Eclipse as well?
Report Abuse
bikerking200 is not online. bikerking200
Joined: 11 Mar 2010
Total Posts: 5047
27 Oct 2011 07:02 PM
I got my account back from being banned..
Report Abuse
myrkos is not online. myrkos
Joined: 06 Sep 2010
Total Posts: 8072
27 Oct 2011 07:15 PM
I've made a calculator in C++ that uses modules and is easily extendable through classes :D
Report Abuse
TheNewScripter is not online. TheNewScripter
Joined: 02 Mar 2010
Total Posts: 2432
27 Oct 2011 07:17 PM
Well you used cout and you didn't include iostream...

    #include <​iostream>

That was the error I saw just at first... Didn't look over it all.
Report Abuse
bikerking200 is not online. bikerking200
Joined: 11 Mar 2010
Total Posts: 5047
27 Oct 2011 07:19 PM
The New scripter, It works without it.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image