Tuesday, February 26, 2013

Expression Matching&String

Programme 1#

<?php
function has_space($string){
if (preg_match('/ /', $string)){
return true;
}else{
return false;
}
}
$string='This doesnt have a space';
if (has_space($string)){
echo 'has at least one space';
}else{
echo 'has no space.';
}
?>

Programme 2#

<?php
if(isset($_GET['user_name'])&&!empty($_GET['user_name'])){
$user_name=$_GET['user_name'];
$user_name_lc=strtolower($user_name);
if($user_name_lc=='alex'){
echo 'You are the best';
}else{
echo 'You are not alex';
}
}
?>
<form action="index.php" method="GET">
Name:<input type="text" name="user_name"><br><br>
<input type="submit" value="Submit">
</form>

Tuesday, February 19, 2013

Origin C-Code for Plot the graph

#include <Origin.h>
void plotgraph(){
     int i;

 // Delcare worksheet object and datasets from 1st two columns of the worksheet
 Worksheet wks("Data1");
 Dataset dsX(wks, 0);
 Dataset dsY(wks, 1);
 dsY.SetSize(251);
  for(i=0;i<251;i++)
 {
 
  dsY[i]=dsY[i]*10000;
 }

 // Set up name of custom template to be used for creating a graph
 string strGraphTemplateName = LabTalk.System.Path.Program$
   + "Samples\\Programming\\Automation\\I-V curve original.OTP";
 // Create a graph using custom template
 GraphPage grphData;
 int nOptionG = CREATE_VISIBLE_SAME;
 bool bRetG = grphData.Create(strGraphTemplateName, nOptionG);

 // Declare active layer in current graph page
 GraphLayer grphLayer = grphData.Layers();

  // Declare a curve object using x,y columns of worksheet
 Curve crvData(wks, 0, 1);

  // Plot data curve to active layer
 int nPlot = grphLayer.AddPlot(crvData, IDM_PLOT_LINE);
 if(grphLayer)                                  // If valid graph layer...
  {
   Scale sc1(grphLayer.X);                     // Get X scale object from graph layer
   sc1.From = -1.0;                     // Assign scale from value
   sc1.To = 1.0;                       // assign scale to value
   Scale sc2(grphLayer.Y);                     // Get X scale object from graph layer
   sc2.From = -20.0;                     // Assign scale from value
   sc2.To = 20.0;                       // assign scale to value
  }

}

Origin C-logplot for the dark current

#include <origin.h>
#include <math.h>
void logplot()
{
 int i;
 // define two Dataset variable and map them to the 1st and 2nd columns of the worksheet

 Dataset dsCol1("data1",0);
 Dataset dsCol2("data1",1);

 dsCol1.SetSize(251);
 dsCol2.SetSize(251);
  for(i=0;i<251;i++){
  if (dsCol2[i]<0){
  
  dsCol2[i]=log10(-dsCol2[i]*10000)
  }
  else dsCol2[i]=log10(dsCol2[i]*10000);
  
  }

}

Monday, February 18, 2013

Origin C

OriginLab is quite powerful. This software integrates Matlab, Labview, C, C++.
Awesome!

#include <origin.h>
void test()

{

printf("hello world\n");

}
 
imput:test

php

XAMPP and Notepad++

Start learning php.

Go Go Go

<?php
echo 'hello world.';
?>

;;;;;;;;;;;;;;;;;;;
; About php.ini   ;
;;;;;;;;;;;;;;;;;;;
; PHP's initialization file, generally called php.ini, is responsible for
; configuring many of the aspects of PHP's behavior.
; PHP attempts to find and load this configuration from a number of locations.
; The following is a summary of its search order:
; 1. SAPI module specific location.
; 2. The PHPRC environment variable. (As of PHP 5.2.0)
; 3. A number of predefined registry keys on Windows (As of PHP 5.2.0)
; 4. Current working directory (except CLI)
; 5. The web server's directory (for SAPI modules), or directory of PHP
; (otherwise in Windows)
; 6. The directory from the --with-config-file-path compile time option, or the
; Windows directory (C:\windows or C:\winnt)
; See the PHP docs for more specific information.
; http://php.net/configuration.file


<?php
$name='Alex';
$age=21;
if(strtolower($name)==='alex'){
 if($age>=21){
  echo 'you\'re over 21';
  if(1===1){
   echo 'Yes, 1 is equal to 1!';
 }
  }
}else{
 echo 'you\'re not Alex!';
}
?>


<?php
echo '<strong>Leize, Hello world!!<>';
?>

Saturday, February 16, 2013

Java Tutorial-Instant Message

Source code:
Class Client:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class Client extends JFrame{
private JTextField userText;
private JTextArea chatWindow;
private ObjectOutputStream output;
private ObjectInputStream input;
private String message="";
private String serverIP;
private Socket connection;

public Client(String host){
 super("Client mofo!");
 serverIP=host;
 userText=new JTextField();
 userText.setEditable(false);
 userText.addActionListener(
   new ActionListener(){
    public void actionPerformed (ActionEvent event){
     sendMessage(event.getActionCommand());
     userText.setText("");
   
    }
   }
   );
 add(userText, BorderLayout.NORTH);
 chatWindow=new JTextArea();
 add(new JScrollPane(chatWindow),BorderLayout.CENTER);
 setSize(300,150);
 setVisible(true);
}
public void startRunning(){
 try{
  connectToServer();
  setupStreams();
  whileChatting();
 }catch(EOFException eofException){
  showMessage("\n Client terminated connection");

 }catch(IOException ioException){
  ioException.printStackTrace();

 }finally{
  closeCrap();
 }

}
private void connectToServer() throws IOException{
 showMessage("Attempting connection...\n");
 connection=new Socket(InetAddress.getByName(serverIP),6789);
 showMessage("Connected to:"+connection.getInetAddress().getHostName());
}
private void setupStreams() throws IOException{
 output=new ObjectOutputStream(connection.getOutputStream());
 output.flush();
 input=new ObjectInputStream(connection.getInputStream());
 showMessage("\n Dude your streams are now good to go!\n");
}
private void whileChatting() throws IOException{
 ableToType(true);
 do{
  try{
   message=(String) input.readObject();
   showMessage("\n"+message);
  }catch(ClassNotFoundException classNotfoundException){
   showMessage("\n I don't know that object type");
  }

 }while(!message.equals("SERVER - END"));
 }
private void closeCrap(){
 showMessage("\n closing crap down...");
 ableToType(false);
 try{
  output.close();
  input.close();
  connection.close();
 }catch(IOException ioException){
  ioException.printStackTrace();
 }
}
private void sendMessage(String message){
 try{
  output.writeObject("CLIENT- "+ message);
  output.flush();
  showMessage("\nCLIENT- " + message);
 }catch(IOException ioException){
  chatWindow.append("\n something messed up sending message");
 }

}
private void showMessage(final String m){
 SwingUtilities.invokeLater(
new Runnable(){
 public void run(){
  chatWindow.append(m);

 }

}
   );


}
private void ableToType(final boolean tof){
 SwingUtilities.invokeLater(
   new Runnable(){
           public void run(){
            userText.setEditable(tof);

            }

              }
   );
}

}

Class ClientTest:
import javax.swing.JFrame;
public class ClientTest {
 public static void main(String[] args) {
  Client charlie;
  charlie=new Client("127.0.0.1");
  charlie.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  charlie.startRunning();
 }
}


Class Server:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Server extends JFrame{
private JTextField userText;
private JTextArea chatWindow;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
public Server(){
 super("Buckys Instant Messenger");
 userText=new JTextField();
 userText.setEditable(false);
 userText.addActionListener(
   new ActionListener(){
    public void actionPerformed(ActionEvent event){
     sendMessage(event.getActionCommand());
     userText.setText("");
    }  
  
   }
   );

 add(userText, BorderLayout.NORTH);
 chatWindow=new JTextArea();
 add(new JScrollPane(chatWindow));
 setSize(300,150);
 setVisible(true);
}
public void startRunning(){
 try{
  server=new ServerSocket(6789,100);
  while(true){
   try{
    waitForConnection();
    setupStreams();
    whileChatting();
   }catch(EOFException eofException){
    showMessage("\n Server ended the connection! ");
   }finally{
    closeCrap();
   }
 
  }

 }catch(IOException ioException){
  ioException.printStackTrace();

 }


}
private void waitForConnection() throws IOException{
 showMessage("Waiting for someone to connect...\n");
 connection=server.accept();
 showMessage(" Now connected to " + connection.getInetAddress().getHostName());
}
private void setupStreams() throws IOException{
 output=new ObjectOutputStream(connection.getOutputStream());
 output.flush();
 input=new ObjectInputStream(connection.getInputStream());
 showMessage("\n Stream are now setup! \n");
}
private void whileChatting() throws IOException{
 String message="You are now connected!";
 sendMessage(message);
 ableToType(true);
 do{
  try{
   message=(String) input.readObject();
   showMessage("\n"+message);
  }catch(ClassNotFoundException classNotFoundException){
   showMessage("\n i don't know that user send!!");
 
  }

 }while(!message.equals("CLIENT - END"));

}
private void closeCrap(){
 showMessage("\n Closing connections...\n");
 ableToType(false);
 try{
  output.close();
  input.close();
  connection.close();
 }catch(IOException ioException){
  ioException.printStackTrace();

 }
}
private void sendMessage(String message){
 try{
  output.writeObject("SERVER - " + message);
  output.flush();
  showMessage("\nServer -" + message);
 }catch(IOException ioException){
  chatWindow.append("\n ERROR: DUDE I CANT SEND THAT MESSAGE");

 }

}
private void showMessage(final String text){
 SwingUtilities.invokeLater(
   new Runnable(){
    public void run(){
     chatWindow.append(text);
   
    }
  
   }
   );

}
private void ableToType(final boolean tof){
 SwingUtilities.invokeLater(
   new Runnable(){
    public void run(){
     userText.setEditable(tof);
     }
    }
   );
 }
}

Class ServerTest:
import javax.swing.JFrame;

public class ServerTest {
 public static void main(String[] args) {
  Server sally=new Server();
  sally.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  sally.startRunning();
 }
}



Start PROGRAMMING

Original skllis:
Mathematica
Matlab

Skills need to be developed:
C++
OpenCL
JAVA
CSS
PhP