張生工作室

          一切皆有可能

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            24 隨筆 :: 3 文章 :: 11 評論 :: 0 Trackbacks

          2007年9月27日 #

          import java.io.DataInputStream;
          import java.io.DataOutputStream;
          import java.io.IOException;
          import java.util.Vector;

          import javax.microedition.io.Connector;
          import javax.microedition.io.ServerSocketConnection;
          import javax.microedition.io.SocketConnection;
          import javax.microedition.midlet.MIDlet;
          import javax.microedition.midlet.MIDletStateChangeException;


          public class GroupServer extends MIDlet implements Runnable {
           
           private ServerSocketConnection ssc=null;
           private SocketConnection sc=null;
           private Vector v=new Vector();

           protected void startApp() throws MIDletStateChangeException {

              try {
             ssc=(ServerSocketConnection)Connector.open("socket://:9999");
              
             new Thread(this).start();
             
            } catch (IOException e) {
             
             e.printStackTrace();
            }
             

           }

           protected void destroyApp(boolean b) throws MIDletStateChangeException {


           }

           protected void pauseApp() {


           }

           public void run() {
            while(true)
            {
             
             try {
              sc=(SocketConnection)ssc.acceptAndOpen();
              ChatThread chat=new ChatThread(sc);
              v.addElement(chat);
              chat.start();
             } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
             }
            }
            
            
           }
           class ChatThread extends Thread
           {
            private SocketConnection sc=null;
            private DataOutputStream dos=null;
            private DataInputStream dis=null;
            public ChatThread(SocketConnection sc)
            {
             this.sc=sc;
             try
             {
                 dis=sc.openDataInputStream();
                 dos=sc.openDataOutputStream();
             }
             catch(Exception e)
             {
              
             }
             
            }
            public void run()
            {
             while(true)
             {
              try
              {
                 // dis=sc.openDataInputStream();
                 // dos=sc.openDataOutputStream();
                  String str=dis.readUTF();
                  for(int i=0;i<v.size();i++)
                  {
                   ChatThread chat= (ChatThread)v.elementAt(i);
                  //chat.dos.writeUTF(chat.dis.readUTF());
                   chat.dos.writeUTF(str);
                  // System.out.println(str+"\n");
                  
                  }
              }
              catch(Exception e)
              {
               
              }
             }
            }
           }

           

          }

          posted @ 2008-12-02 22:40 張生工作室 閱讀(313) | 評論 (0)編輯 收藏

          import java.io.DataInputStream;
          import java.io.DataOutputStream;
          import java.io.IOException;
          import javax.microedition.io.Connector;
          import javax.microedition.io.ServerSocketConnection;
          import javax.microedition.io.SocketConnection;
          import javax.microedition.lcdui.Command;
          import javax.microedition.lcdui.CommandListener;
          import javax.microedition.lcdui.Display;
          import javax.microedition.lcdui.Displayable;
          import javax.microedition.lcdui.Form;
          import javax.microedition.lcdui.StringItem;
          import javax.microedition.lcdui.TextField;
          import javax.microedition.midlet.MIDlet;
          import javax.microedition.midlet.MIDletStateChangeException;


          public class Client extends MIDlet  implements CommandListener {
           
           

           private DataInputStream dis=null;
           private DataOutputStream dos=null;
           private ServerSocketConnection ssc=null;
           //private StreamConnection sc=null;
           private SocketConnection sc=null;
           private TextField msg=null;
           private Form frm=null;
           private Command cmdSend=null;
           private StringItem imsg=null;
           
           protected void startApp() throws MIDletStateChangeException {
            
            frm=new Form("客戶端");
            cmdSend=new Command("發(fā)送",Command.OK,1);
            msg=new TextField("請輸入你的信息","",255,TextField.ANY);
            frm.append(msg);
            frm.addCommand(cmdSend);
            frm.setCommandListener(this);
            Display.getDisplay(this).setCurrent(frm);
            
            
            try {
             sc=(SocketConnection)Connector.open("socket://127.0.0.1:9999");
             //sc=ssc.acceptAndOpen();
             dis=sc.openDataInputStream();
             dos=sc.openDataOutputStream();
             Chat chat=new Chat();
             chat.start();
             
            } catch (IOException e) {
            
             e.printStackTrace();
            }
            

           }
           

           public void commandAction(Command c, Displayable d) {
            if(c==cmdSend)
            {
             try {
              dos.writeUTF(msg.getString()+"\n");
             } catch (IOException e) {
              // TODO Auto-generated catch block
              System.out.println(e.getMessage()+"\n");
             }
             msg.setString("");
            }
            
            
           }

           protected void destroyApp(boolean b) throws MIDletStateChangeException {
            

           }

           protected void pauseApp() {
           

           }
           
           class Chat extends Thread
           {
            public void run()
            {
             while(true)
             {
              
               try {
                imsg=new StringItem(dis.readUTF(),"");
                frm.insert(1, imsg);
              
             } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
             }
             }
             
             
            }
           }

           
           

           

          }

          posted @ 2008-12-01 20:28 張生工作室 閱讀(251) | 評論 (0)編輯 收藏

          import java.io.DataInputStream;
          import java.io.DataOutputStream;
          import java.io.IOException;
          import javax.microedition.io.Connector;
          import javax.microedition.io.ServerSocketConnection;
          import javax.microedition.io.SocketConnection;
          import javax.microedition.io.StreamConnection;
          import javax.microedition.lcdui.Command;
          import javax.microedition.lcdui.CommandListener;
          import javax.microedition.lcdui.Display;
          import javax.microedition.lcdui.Displayable;
          import javax.microedition.lcdui.Form;
          import javax.microedition.lcdui.StringItem;
          import javax.microedition.lcdui.TextField;
          import javax.microedition.midlet.MIDlet;
          import javax.microedition.midlet.MIDletStateChangeException;


          public class Server extends MIDlet  implements CommandListener {
           
           

           private DataInputStream dis=null;
           private DataOutputStream dos=null;
           private ServerSocketConnection ssc=null;
           //private StreamConnection sc=null;
           private SocketConnection sc=null;
           private TextField msg=null;
           private Form frm=null;
           private Command cmdSend=null;
           private StringItem imsg=null;
           
           protected void startApp() throws MIDletStateChangeException {
            
            frm=new Form("服務(wù)器");
            cmdSend=new Command("發(fā)送",Command.OK,1);
            msg=new TextField("請輸入你的信息","",255,TextField.ANY);
            frm.append(msg);
            frm.addCommand(cmdSend);
            frm.setCommandListener(this);
            Display.getDisplay(this).setCurrent(frm);
            
            
            try {
             ssc=(ServerSocketConnection)Connector.open("socket://:9999");
             sc=(SocketConnection)ssc.acceptAndOpen();
             dis=sc.openDataInputStream();
             dos=sc.openDataOutputStream();
             Chat chat=new Chat();
             chat.start();
             
            } catch (IOException e) {
            
             e.printStackTrace();
            }
            

           }
           

           public void commandAction(Command c, Displayable d) {
            if(c==cmdSend)
            {
             try {
              dos.writeUTF(msg.getString()+"\n");
             } catch (IOException e) {
              // TODO Auto-generated catch block
              System.out.println(e.getMessage()+"\n");
             }
             msg.setString("");
            }
            
            
           }

           protected void destroyApp(boolean b) throws MIDletStateChangeException {
            

           }

           protected void pauseApp() {
           

           }
           
           class Chat extends Thread
           {
            public void run()
            {
             while(true)
             {
              
               try {
                imsg=new StringItem( dis.readUTF(),"");
                frm.insert(1, imsg);
             } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
             }
             }
             
             
            }
           }

           
           

           

          }

          posted @ 2008-12-01 20:28 張生工作室 閱讀(365) | 評論 (0)編輯 收藏

          import javax.microedition.lcdui.Command;
          import javax.microedition.lcdui.CommandListener;
          import javax.microedition.lcdui.Display;
          import javax.microedition.lcdui.Displayable;
          import javax.microedition.lcdui.Form;
          import javax.microedition.lcdui.Item;
          import javax.microedition.lcdui.ItemStateListener;
          import javax.microedition.lcdui.TextField;
          import javax.microedition.midlet.MIDlet;
          import javax.microedition.midlet.MIDletStateChangeException;


          public class MessageMIDlet extends MIDlet {

           
           private welcome wcfrm=null;
           
           public MessageMIDlet() {

           }

           protected void destroyApp(boolean arg0) throws MIDletStateChangeException {


           }

           protected void pauseApp() {
           

           }

           protected void startApp() throws MIDletStateChangeException {
           
            wcfrm=new welcome(this);
            Display.getDisplay(this).setCurrent(wcfrm);
           }

          }

          class welcome extends Form implements CommandListener
          {

           
              private Command cmdExit=null;
              private Command cmdWrite=null;
              private writemsg wrfrm=null;
              private MIDlet let=null;
             
              public welcome(MIDlet mlet)
              {
               super("歡迎使用本程序");
               let=mlet;
           
               cmdExit=new Command("退出",Command.EXIT,1);
               cmdWrite=new Command("寫短信",Command.SCREEN,1);
               this.addCommand(cmdExit);
               this.addCommand(cmdWrite);
               this.setCommandListener(this);
               
              }
           public void commandAction(Command c, Displayable d) {
           
             if(c==cmdExit)
             {
            
              let.notifyDestroyed();
             }
             else
              if(c==cmdWrite)
              {
               wrfrm=new writemsg(let);
               Display.getDisplay(let).setCurrent(wrfrm);
             
              
              }
           
           }

          }
          class writemsg extends javax.microedition.lcdui.Form implements CommandListener,ItemStateListener
          {
           

           private Command cmdBack=null;
           private Command cmdwrNum=null;
           private MIDlet let=null;
           private TextField tf=null;
           private Command cmdDel=null;
           private writeNum2 wrnum=null;
           
           

           public writemsg(MIDlet mlet)
           {
           
            super("請輸入短信內(nèi)容");
            let=mlet;
            cmdBack=new Command("返回",Command.EXIT,1);
            cmdwrNum=new Command("發(fā)送",Command.SCREEN,1);
            cmdDel=new Command("刪除",Command.EXIT,1);
            this.addCommand(cmdBack);
            this.addCommand(cmdwrNum);
           
            tf= new  TextField("","",255,TextField.ANY);
            //tf.addCommand(cmdDel);
            this.append(tf);
            this.setItemStateListener(this);
            this.setCommandListener(this);
            tf.setLayout(TextField.LAYOUT_VEXPAND);
           }
           
           public void commandAction(Command c, Displayable d) {
           
            if(c==cmdBack)
            {
             Display.getDisplay(let).setCurrent(new welcome(let));
            
            }else if(c==cmdwrNum)
            {
            wrnum=new  writeNum2(let);
             Display.getDisplay(let).setCurrent(wrnum);
            
            }else if(c==cmdDel)
            {
             int p=tf.getCaretPosition();
             if(p>0)
             {
              tf.delete(p-1, 1);
             }
             if(tf.getString().length()==0)
             {
                this.removeCommand(cmdDel);
                this.addCommand(cmdBack);
             
             }
            
            }

           
           }

           public void itemStateChanged(Item i) {
           
            if(i==tf)
            {
              if( tf.getString().length()>0)
              {
              // this.addCommand(cmdDel);
               this.removeCommand(cmdBack);
               this.addCommand(cmdDel);
              }
              else
              {
               this.removeCommand(cmdDel);
               this.addCommand(cmdBack);
              }
            
            
            }
           
           
           }
           
           
          }

           

          class writeNum2 extends javax.microedition.lcdui.Form implements CommandListener,ItemStateListener
          {
           

           private Command cmdBack=null;
           private Command cmdwrNum=null;
           private MIDlet let=null;
           private TextField tf=null;
           private Command cmdDel=null;
           private Form preFrm=null;
           private boolean sendflag=false;

           
           

           public writeNum2(MIDlet mlet)
           {
           
            super("請輸入對方號碼");
            let=mlet;
            preFrm=(Form) Display.getDisplay(let).getCurrent();
            cmdBack=new Command("返回",Command.EXIT,1);
            cmdwrNum=new Command("發(fā)送",Command.SCREEN,1);
            cmdDel=new Command("刪除",Command.EXIT,1);
            this.addCommand(cmdBack);
            this.addCommand(cmdwrNum);
           
            tf= new  TextField("","",13,TextField.NUMERIC);
            //tf.addCommand(cmdDel);
            this.append(tf);
            this.setItemStateListener(this);
            this.setCommandListener(this);
           
           }
           
           public void commandAction(Command c, Displayable d) {
           
            if(c==cmdBack)
            {
            if (sendflag)
            Display.getDisplay(let).setCurrent(new welcome(let));
            else
             Display.getDisplay(let).setCurrent(this.preFrm);
            
            }else if(c==cmdwrNum)
            {

              String msg= ((TextField)(preFrm.get(0))).getString();
              String phonenum= tf.getString();
             
              this.append("信息內(nèi)容:"+ msg);
              this.append("發(fā)送號碼:"+ phonenum);
              this.append("發(fā)送狀態(tài):成功");
              this.delete(0);
              this.removeCommand(cmdwrNum);
              this.removeCommand(cmdDel);
              this.addCommand(cmdBack);
              sendflag=true;
            }else if(c==cmdDel)
            {
             int p=tf.getCaretPosition();
             if(p>0)
             {
              tf.delete(p-1, 1);
             }
             if(tf.getString().length()==0)
             {
                this.removeCommand(cmdDel);
                this.addCommand(cmdBack);
             
             }
            
            }

           
           }


           public void itemStateChanged(Item i) {
           
            if(i==tf)
            {
              if( tf.getString().length()>0)
              {
              // this.addCommand(cmdDel);
               this.removeCommand(cmdBack);
               this.addCommand(cmdDel);
              }
              else
              {
               this.removeCommand(cmdDel);
               this.addCommand(cmdBack);
              }
            
            
            }
           
           
           }
           
           
          }

          /*

          class writeNum extends Form implements CommandListener
          {

           private MIDlet let=null;
           private Command cmdSend=null;
           private Command cmdBack=null;
           private TextField num=null;
           private Form backfrm=null;
           public writeNum(MIDlet mlet) {
           
            super("請輸入要發(fā)送的號碼");
            let=mlet;
            num=new TextField("","",13,TextField.NUMERIC);
            this.append(num);
            cmdSend=new Command("發(fā)送",Command.OK,1);
            cmdBack=new Command("返回",Command.BACK,1);
            this.addCommand(cmdSend);
            this.addCommand(cmdBack);
            this.backfrm=(Form) Display.getDisplay(let).getCurrent();
            this.setCommandListener(this);
           
           
           }

           public void commandAction(Command c, Displayable d) {
            if(c==cmdSend)
            {
             try
             {
              String msg=((TextField)this.backfrm.get(0)).getString();//當跨幾個Form時應(yīng)該怎么辦?
              this.append("信息:"+ msg+"已發(fā)送到:"+ num.getString());
            
             this.delete(0);
             this.setTitle("信息已發(fā)送成功");
             }
             catch(Exception e)
             {
              this.append("信息成功發(fā)送");
             }
            }
            else if(c==cmdBack)
            {
             Display.getDisplay(let).setCurrent(this.backfrm);
            }
           
           
           }
           
          }
          **/

           

          posted @ 2008-11-28 13:06 張生工作室 閱讀(386) | 評論 (1)編輯 收藏

          import javax.microedition.lcdui.Command;
          import javax.microedition.lcdui.CommandListener;
          import javax.microedition.lcdui.Display;
          import javax.microedition.lcdui.Displayable;
          import javax.microedition.lcdui.Form;
          import javax.microedition.lcdui.Item;
          import javax.microedition.lcdui.ItemStateListener;
          import javax.microedition.lcdui.TextField;
          import javax.microedition.midlet.MIDlet;
          import javax.microedition.midlet.MIDletStateChangeException;


          public class MessageMIDlet extends MIDlet {

           
           private welcome wcfrm=null;
           
           public MessageMIDlet() {

           }

           protected void destroyApp(boolean arg0) throws MIDletStateChangeException {


           }

           protected void pauseApp() {
           

           }

           protected void startApp() throws MIDletStateChangeException {
           
            wcfrm=new welcome(this);
            Display.getDisplay(this).setCurrent(wcfrm);
           }

          }

          class welcome extends Form implements CommandListener
          {

           
              private Command cmdExit=null;
              private Command cmdWrite=null;
              private writemsg wrfrm=null;
              private MIDlet let=null;
             
              public welcome(MIDlet mlet)
              {
               super("歡迎使用本程序");
               let=mlet;
           
               cmdExit=new Command("退出",Command.EXIT,1);
               cmdWrite=new Command("寫短信",Command.SCREEN,1);
               this.addCommand(cmdExit);
               this.addCommand(cmdWrite);
               this.setCommandListener(this);
               
              }
           public void commandAction(Command c, Displayable d) {
           
             if(c==cmdExit)
             {
            
              let.notifyDestroyed();
             }
             else
              if(c==cmdWrite)
              {
               wrfrm=new writemsg(let);
               Display.getDisplay(let).setCurrent(wrfrm);
             
              
              }
           
           }

          }
          class writemsg extends javax.microedition.lcdui.Form implements CommandListener,ItemStateListener
          {
           

           private Command cmdBack=null;
           private Command cmdwrNum=null;
           private MIDlet let=null;
           private TextField tf=null;
           private Command cmdDel=null;
           private writeNum wrnum=null;
           
           

           public writemsg(MIDlet mlet)
           {
           
            super("請輸入短信內(nèi)容");
            let=mlet;
            cmdBack=new Command("返回",Command.EXIT,1);
            cmdwrNum=new Command("發(fā)送",Command.SCREEN,1);
            cmdDel=new Command("刪除",Command.EXIT,1);
            this.addCommand(cmdBack);
            this.addCommand(cmdwrNum);
           
            tf= new  TextField("","",255,TextField.ANY);
            //tf.addCommand(cmdDel);
            this.append(tf);
            this.setItemStateListener(this);
            this.setCommandListener(this);
           
           }
           
           public void commandAction(Command c, Displayable d) {
           
            if(c==cmdBack)
            {
             Display.getDisplay(let).setCurrent(new welcome(let));
            
            }else if(c==cmdwrNum)
            {
             wrnum=new writeNum(let);
             Display.getDisplay(let).setCurrent(wrnum);
            
            }else if(c==cmdDel)
            {
             int p=tf.getCaretPosition();
             if(p>0)
             {
              tf.delete(p-1, 1);
             }
             if(tf.getString().length()==0)
             {
                this.removeCommand(cmdDel);
                this.addCommand(cmdBack);
             
             }
            
            }

           
           }

           public void itemStateChanged(Item i) {
           
            if(i==tf)
            {
              if( tf.getString().length()>0)
              {
              // this.addCommand(cmdDel);
               this.removeCommand(cmdBack);
               this.addCommand(cmdDel);
              }
              else
              {
               this.removeCommand(cmdDel);
               this.addCommand(cmdBack);
              }
            
            
            }
           
           
           }
           
           
          }

          class writeNum extends Form implements CommandListener
          {

           private MIDlet let=null;
           private Command cmdSend=null;
           private Command cmdBack=null;
           private TextField num=null;
           private writemsg backfrm=null;
           public writeNum(MIDlet mlet) {
           
            super("請輸入要發(fā)送的號碼");
            let=mlet;
            num=new TextField("","",13,TextField.NUMERIC);
            this.append(num);
            cmdSend=new Command("發(fā)送",Command.OK,1);
            cmdBack=new Command("返回",Command.BACK,1);
            this.addCommand(cmdSend);
            this.addCommand(cmdBack);
            this.backfrm=(writemsg) Display.getDisplay(let).getCurrent();
            this.setCommandListener(this);
           
           
           }

           public void commandAction(Command c, Displayable d) {
            if(c==cmdSend)
            {
             try
             {
              String msg=((TextField)this.backfrm.get(0)).getString();//當跨幾個Form時應(yīng)該怎么辦?
              this.append("信息:"+ msg+"已發(fā)送到:"+ num.getString());
            
             this.delete(0);
             this.setTitle("信息已發(fā)送成功");
             }
             catch(Exception e)
             {
              this.append("信息成功發(fā)送");
             }
            }
            else if(c==cmdBack)
            {
             Display.getDisplay(let).setCurrent(this.backfrm);
            }
           
           
           }
           
          }

           

          posted @ 2008-11-28 00:04 張生工作室 閱讀(213) | 評論 (0)編輯 收藏

          import javax.microedition.lcdui.Command;
          import javax.microedition.lcdui.CommandListener;
          import javax.microedition.lcdui.Display;
          import javax.microedition.lcdui.Displayable;
          import javax.microedition.lcdui.Form;
          import javax.microedition.lcdui.Item;
          import javax.microedition.lcdui.ItemCommandListener;
          import javax.microedition.lcdui.ItemStateListener;
          import javax.microedition.lcdui.TextField;
          import javax.microedition.midlet.MIDlet;
          import javax.microedition.midlet.MIDletStateChangeException;

          public class MIDletItemCommmandListener extends MIDlet implements ItemCommandListener,CommandListener,ItemStateListener {
                                                                        
           
           private Form frm=new Form("Item監(jiān)聽器");
           private TextField uid=new TextField("用戶名","",20,TextField.ANY);
           private TextField pwd=new TextField("密碼","",20,TextField.PASSWORD);
           private Display dis=Display.getDisplay(this);
           private Command Del=new Command("刪除",Command.ITEM,1);
           
           public MIDletItemCommmandListener() {
            
           }

           protected void startApp() throws MIDletStateChangeException {
            
            
            
            frm.append(uid);
            uid.addCommand(Del);
            pwd.addCommand(Del);
            pwd.setItemCommandListener(this);
            uid.setItemCommandListener(this);
            frm.append(pwd);
            dis.setCurrent(frm);
            dis.setCurrentItem(pwd);
            frm.setItemStateListener(this);
            


           }
           
           protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
           

           }

           protected void pauseApp() {
            

           }

           public void commandAction(Command c, Item i) {
            
            if(c==Del)
            {
             TextField tf=(TextField)i;
             
             int p=tf.getCaretPosition();
             if(p>0)
             {
             tf.delete(p-1, 1);
             }
            }
           
            
           }

           public void commandAction(Command c, Displayable d) {
            
            
           }

           public void itemStateChanged(Item i) {
            
              if(i==uid)
              {
               frm.setTitle("用戶名"+uid.getString());
              } else if(i==pwd)
              {
               frm.setTitle("密碼"+uid.getString());
              }
            
           }

           

          }

          posted @ 2008-11-27 08:49 張生工作室 閱讀(415) | 評論 (0)編輯 收藏

          import javax.microedition.lcdui.Command;
          import javax.microedition.lcdui.CommandListener;
          import javax.microedition.lcdui.Display;
          import javax.microedition.lcdui.Displayable;
          import javax.microedition.lcdui.Form;
          import javax.microedition.lcdui.Item;
          import javax.microedition.lcdui.ItemCommandListener;
          import javax.microedition.lcdui.TextField;
          import javax.microedition.midlet.MIDlet;
          import javax.microedition.midlet.MIDletStateChangeException;

          public class MIDletItemCommmandListener extends MIDlet implements ItemCommandListener,CommandListener {
                                                                        
           
           private Form frm=new Form("Item監(jiān)聽器");
           private TextField uid=new TextField("用戶名","",20,TextField.ANY);
           private TextField pwd=new TextField("密碼","",20,TextField.PASSWORD);
           private Display dis=Display.getDisplay(this);
           private Command Del=new Command("刪除",Command.ITEM,1);
           
           public MIDletItemCommmandListener() {
            // TODO Auto-generated constructor stub
           }

           protected void startApp() throws MIDletStateChangeException {
            
            
            
            frm.append(uid);
            uid.addCommand(Del);
            pwd.addCommand(Del);
            pwd.setItemCommandListener(this);
            uid.setItemCommandListener(this);
            frm.append(pwd);
            dis.setCurrent(frm);
            dis.setCurrentItem(pwd);
            


           }
           
           protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
            // TODO Auto-generated method stub

           }

           protected void pauseApp() {
            

           }

           public void commandAction(Command c, Item i) {
            
            if(c==Del)
            {
             TextField tf=(TextField)i;
             
             int p=tf.getCaretPosition();
             if(p>0)
             {
             tf.delete(p-1, 1);
             }
            }
           
            
           }

           public void commandAction(Command c, Displayable d) {
            
            
           }

           

          }

          posted @ 2008-11-27 08:34 張生工作室 閱讀(580) | 評論 (0)編輯 收藏

          import javax.microedition.lcdui.Display;
          import javax.microedition.lcdui.Font;
          import javax.microedition.lcdui.Image;
          import javax.microedition.lcdui.List;
          import javax.microedition.midlet.MIDlet;
          import javax.microedition.midlet.MIDletStateChangeException;


          public class ListImage extends MIDlet {
           
           
           private List list;
           private Display dis;

           public ListImage() {
            // TODO Auto-generated constructor stub
           }

           protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
            // TODO Auto-generated method stub

           }

           protected void pauseApp() {
            // TODO Auto-generated method stub

           }

           protected void startApp() throws MIDletStateChangeException {
            
            
            list=new List("列表",List.MULTIPLE);
            Image img=null;
            try
            {
             
            img=Image.createImage("/mlogo.PNG");
            
            
            }
            catch(Exception e)
            {
             System.out.println("Error");
            }
            list.append("圖片", img);
            Font f=null;
            f=Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_UNDERLINED, Font.SIZE_LARGE);
            list.setFont(0, f);
            dis=Display.getDisplay(this);
            dis.setCurrent(list);
            // TODO Auto-generated method stub

           }

          }

          posted @ 2008-11-26 09:54 張生工作室 閱讀(252) | 評論 (0)編輯 收藏

          import javax.microedition.lcdui.Command;
          import javax.microedition.lcdui.CommandListener;
          import javax.microedition.lcdui.Display;
          import javax.microedition.lcdui.Displayable;
          import javax.microedition.lcdui.List;
          import javax.microedition.midlet.MIDlet;
          import javax.microedition.midlet.MIDletStateChangeException;


          public class MIDlet3 extends MIDlet implements CommandListener  {
           
           private List list=null;
           
           private Command cmdOk=null;
           
           private Display dis=null;

           public MIDlet3() {
            // TODO Auto-generated constructor stub
           }

           protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
            // TODO Auto-generated method stub

           }

           protected void pauseApp() {
            // TODO Auto-generated method stub

           }

           protected void startApp() throws MIDletStateChangeException {
            
              cmdOk=new Command("確定",Command.OK,1);
             
              list=new List("請選擇",List.MULTIPLE);
            
            for(int i=0;i<10;i++)
            {
             list.append("列表"+i, null);
             
            }
            list.addCommand(cmdOk);
            dis=Display.getDisplay(this);
            list.setCommandListener(new Operater());
            dis.setCurrent(list);
            // TODO Auto-generated method stub

           }

           public void commandAction(Command c, Displayable d) {
              //Method1
            int size=list.size();
            for(int i=0;i<size;i++)
            {
             if(list.isSelected(i))
             {
              System.out.print(list.getString(i));
             }
            }
            //Method 2
            /*
            boolean[] Flags=new boolean[list.size()];
               list.getSelectedFlags(Flags);
               for(int i=0;i<list.size();i++)
               {
                if(Flags[i])
                {
                 System.out.print(list.getString(i));
                }
               }
               */
            
           }

          }

           

           

          posted @ 2008-11-26 09:35 張生工作室 閱讀(187) | 評論 (0)編輯 收藏

          import javax.microedition.lcdui.Command;
          import javax.microedition.lcdui.CommandListener;
          import javax.microedition.lcdui.Display;
          import javax.microedition.lcdui.Displayable;
          import javax.microedition.lcdui.List;
          import javax.microedition.midlet.MIDlet;
          import javax.microedition.midlet.MIDletStateChangeException;


          public class MIDlet3 extends MIDlet implements CommandListener  {
           
           private List list=null;
           
           private Command cmdOk=null;
           
           private Display dis=null;

           public MIDlet3() {
            // TODO Auto-generated constructor stub
           }

           protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
            // TODO Auto-generated method stub

           }

           protected void pauseApp() {
            // TODO Auto-generated method stub

           }

           protected void startApp() throws MIDletStateChangeException {
            
              cmdOk=new Command("確定",Command.OK,1);
             
              list=new List("請選擇",List.MULTIPLE);
            
            for(int i=0;i<10;i++)
            {
             list.append("列表"+i, null);
             
            }
            //list.addCommand(cmdOk);
            
            list.setCommandListener(new Operater());
            dis.setCurrent(list);
            // TODO Auto-generated method stub

           }

           public void commandAction(Command c, Displayable d) {
              //Method1
            int size=list.size();
            for(int i=0;i<size;i++)
            {
             if(list.isSelected(i))
             {
              System.out.print(list.getString(i));
             }
            }
            //Method 2
            boolean[] Flags=new boolean[list.size()];
               list.getSelectedFlags(Flags);
               for(int i=0;i<list.size();i++)
               {
                if(Flags[i])
                {
                 System.out.print(list.getString(i));
                }
               }
            
           }

          }

           

           

          posted @ 2008-11-26 08:52 張生工作室 閱讀(197) | 評論 (0)編輯 收藏

          import javax.microedition.lcdui.Command;
          import javax.microedition.lcdui.CommandListener;
          import javax.microedition.lcdui.Display;
          import javax.microedition.lcdui.Displayable;
          import javax.microedition.lcdui.List;
          import javax.microedition.midlet.MIDlet;
          import javax.microedition.midlet.MIDletStateChangeException;


          public class MIDlet2 extends MIDlet  {
           
           private List list;
           
           private Command cmdOk;
           
           private Display dis;

           public MIDlet2() {
            // TODO Auto-generated constructor stub
           }

           protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
            // TODO Auto-generated method stub

           }

           protected void pauseApp() {
            // TODO Auto-generated method stub

           }

           protected void startApp() throws MIDletStateChangeException {
            
              cmdOk=new Command("確定",Command.OK,1);
             
              list=new List("請選擇",List.EXCLUSIVE);
            
            for(int i=0;i<10;i++)
            {
             list.append("列表"+i, null);
             
            }
            list.addCommand(cmdOk);
            dis=Display.getDisplay(this);
            list.setCommandListener(new Operater());
            dis.setCurrent(list);
            // TODO Auto-generated method stub

           }


            


          }

          class Operater implements javax.microedition.lcdui.CommandListener
          {


           public void commandAction(Command c, Displayable d) {
            
            
            if(c.getLabel().equals("確定"))
            {
             List l=(List)d;
               l.setTitle( l.getString(l.getSelectedIndex())); 
            }
            
           }
           
           
          }

          posted @ 2008-11-26 08:40 張生工作室 閱讀(126) | 評論 (0)編輯 收藏

          import javax.microedition.lcdui.Command;
          import javax.microedition.lcdui.Display;
          import javax.microedition.lcdui.Displayable;
          import javax.microedition.lcdui.List;
          import javax.microedition.midlet.MIDlet;
          import javax.microedition.midlet.MIDletStateChangeException;


          public class MIDlet1 extends MIDlet implements javax.microedition.lcdui.CommandListener {

           
           private Display dis;
           private List list;

           
           public MIDlet1() {
            // TODO Auto-generated constructor stub
           }

           protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
            // TODO Auto-generated method stub

           }

           protected void pauseApp() {
            // TODO Auto-generated method stub

           }

           protected void startApp() throws MIDletStateChangeException {
            

            list=new List("列表",List.IMPLICIT);

           

            list.setCommandListener(this);
            for(int i=0;i<100;i++)
            {
             list.append("表列"+ i, null);
            }
            

            
           
            
            dis=Display.getDisplay(this);
            
            dis.setCurrent(list);
            
            // TODO Auto-generated method stub

           }

           public void commandAction(Command cmd, Displayable dis) {
            
            
           if(cmd==list.SELECT_COMMAND)
           {
            list.setTitle(list.getString(list.getSelectedIndex()));
           }
           
            
            // TODO Auto-generated method stub
            
           }

          }

          posted @ 2008-11-26 08:16 張生工作室 閱讀(177) | 評論 (0)編輯 收藏

               摘要: 第四章 管理信息系統(tǒng)規(guī)劃與開發(fā)方法   開發(fā)管理信息系 統(tǒng)是一個投資巨大、歷時很長、涉及面很廣的工程項目,如果沒有進行規(guī)劃或規(guī)劃不合理,不僅造成開發(fā)過程的直接損失,由此而引起的企業(yè)運行不好的間接損失更 是難以估計。人們通常認為,假若一個操作錯誤可能損失幾萬元,那么一個設(shè)計錯誤就能損失幾十萬元,一個計劃錯誤就能損失幾百萬元,而一個規(guī)劃錯誤的損失則 能達到千萬元,甚至上億元。一個科學(xué)合理的系...  閱讀全文
          posted @ 2008-07-26 15:20 張生工作室 閱讀(6096) | 評論 (0)編輯 收藏

               摘要: Struts2+spring2+hibernate3整合方案 最近閑來無事可做,于是開始學(xué)習(xí)struts2。Struts2和struts1、webwork2有什么區(qū)別我也不說了,網(wǎng)上有很多這方面的資料。以前在項目中從未使用過struts,一直使用spring+hibernate,現(xiàn)在既然學(xué)習(xí)了Struts,也不能浪費,于是乎開始琢磨著怎么整合這3個框架。整合原理以spring為容器,管理hibe...  閱讀全文
          posted @ 2008-05-13 12:20 張生工作室 閱讀(320) | 評論 (0)編輯 收藏

          Matlab6.5使用
          第1章  Matlab6.5使用方法

          1.Matlab6.5簡介
          1.1  21世紀的科學(xué)計算語言
          ?功能強大
             數(shù)值計算、符號運算、圖形可視化
          ?語言簡單
          ?擴充能力強、可開發(fā)性好  
          ?編程容易、效率高

          1.2  Matlab6.5的新特點
          ?開發(fā)環(huán)境
            
          ?數(shù)值處理

          ?程序及數(shù)據(jù)結(jié)構(gòu)  
          ?圖形處理

          ?用戶圖形界面

          ?應(yīng)用程序接口

          1.3  Matlab網(wǎng)上資源

          http://www.mathsworks.com

          ftp.mathworks.com

          ……

          2.Matlab6.5桌面平臺
          ?Matlab主窗口

          ?命令窗口

          ?歷史窗口

          ?當前目錄窗口

          ?發(fā)明說明書窗口

          ?工作間管理窗口

          3.Matlab6.5幫助系統(tǒng)
          ?聯(lián)機幫助系統(tǒng)
          ? 選中Help下拉菜單的前四項中任何一項;
          ? 在命令窗口中執(zhí)行Helpwin、helpdesk、doc;
          ? 直接按主窗口的?按鈕.

          ?命令窗口查詢幫助
          ?  help系列----help、help+函數(shù)(類)名、helpwin、helpdesk;
          ? lookfor函數(shù);
          ? 其他幫助命令
          ? exist—變量檢驗函數(shù);
          ? what---目錄中文件列表;
          ? who---內(nèi)存變量列表;
          ? whos---內(nèi)存變量詳細信息;
          ? which---確定文件位置。

          ?聯(lián)機演示系統(tǒng)
          1.基本介紹窗口
          >> intro

          2.演示界面
          help--demos
          >>demo

          ?常用命令和技巧
          1.一些通用命令
          cd  顯示或改變工作目錄
          dir, ls  顯示目錄文件
          type  顯示文件內(nèi)容
          clear  清理內(nèi)存變量
          clf    清除圖形窗口
          pack  收集內(nèi)存碎片、擴大內(nèi)存空間
          clc   清除工作窗
          echo  工作窗信息顯示開關(guān)
          hold  圖形保持開關(guān)
          disp  顯示變量或文字內(nèi)容
          path  顯示搜索目錄
          save  保存內(nèi)存變量到指定文件
          load  加載指定文件的變量
          diary  日志文件命令
          quit   退出Matlab
          !     調(diào)用dos命令


          2. 標點
          : 多種應(yīng)用功能
          ; 區(qū)分行,取消行顯示等
          , 區(qū)分列,函數(shù)參數(shù)分隔符等
          ()  指定運算過程中的先后次序等
          []  矩陣定義的標志
          {}  用于構(gòu)成單元數(shù)組等
          .   小數(shù)點及域訪問等
          …  續(xù)行符
          %  注釋標記

          =  賦值標記
          ’   字符串的標志符等

           

           

          Help 程序(主題)名
          >> help sinh
           SINH   Hyperbolic sine.
             SINH(X) is the hyperbolic sine of the elements of X.
           Overloaded methods
              help sym/sinh.m

          >>lookfor complex
          ……

          顯示
          >>who  %顯示當前工作區(qū)中的所有變量名
          >>whos  %顯示當前工作區(qū)中的所有變量%名,變量的大小,字節(jié)數(shù)和類型。

          >>disp(x)  %顯示x的內(nèi)容,矩陣或字符串

          清除
          >>clear (變量名)


          存儲
          >>save (文件名)
          調(diào)出
          >> load (文件名)

          >> save
          Saving to: matlab.mat
          >> load
          Loading from: matlab.mat

          >> save temp
          -將當前系統(tǒng)中的所有變量存入temp.mat;
          >> save temp x
          -僅存入x變量到temp.mat;
          >> save temp x y z
          -僅存入x y z變量到temp.mat;
          >> load temp
          -重新從temp.mat中提出變量。

          搜索
          >>path %顯示目前的搜索路徑,可以用File菜單中的Set Path觀察和修改路徑
          管理
          what   返回目前目錄下的M,MAT,MEX文件列表
          dir、 ls  列出目前目錄下的所有文件

          cd path  改變目前目錄為path

          pwd、 cd、 chdir   顯示目前的工作目錄 
          退出
          >>quit


          第2章 數(shù)值計算功能

          2.1  Matlab的數(shù)據(jù)類型

          1. 變量與表達式
          Matlab命令的通常形式
            變量=表達式
          >> A=[1,3.0,5.6,sin(2.)]
          A =
            1.0000    3.0000    5.6000    0.9093

          >> a=[1 3.0 5.6 sin(2.)]
          a =
            1.0000    3.0000    5.6000    0.9093
          >> sin(pi/2)/3
          ans =
          0. 3333
          >> whos
            Name    Size    Bytes  Class
            A       1x4        32  double array
            a       1x4        32  double array
            ans     1x1         8  double array
          Grand total is 9 elements using 70 bytes

          2.預(yù)定義變量----常量
          系統(tǒng)的特殊常量
          特殊變量           取值
          pi                  
          eps                 計算機的最小正數(shù)
          flops                浮點運算次數(shù)
          i,j                   i=j=
          Inf                  無窮大
          NaN                 不定量
          Realmin      最小的正浮點數(shù) 2^(-1022)
          Realmax      最大的浮點數(shù) 2^(1023)


          >> z1=3+4i
          z1 =
             3.0000 + 4.0000i
          >> z1=3+4j
          z1 =
             3.0000 + 4.0000i

          3.顯示格式
          計算以雙精度計算;
          數(shù)值的有效范圍為10e-308~10e308;
          顯示可以不同的顯示格式顯示;
          缺省情況為短格式(short)顯示—若數(shù)據(jù)為整數(shù),則以整型表示;若為實數(shù),則保留小數(shù)點后4位的浮點數(shù)表示。
          >> x=[3.0 2]
          x =
          3 2
          >> x=[4/3 1.2345e-6]
          x =
          1.3333    0.0000

          >> format long
          >> x=[4/3 1.2345e-6]
          x =
             1.33333333333333   0.00000123450000

           

          >> format short e
          >> x=[4/3 1.2345e-6]
          x =
          1. 3333e+000  1.2345e-006

          >> format +
          >> x=[4/3 1.2345e-6]
          x =
          ++

          short
          long
          hex
          bank
          +
          short e
          long e
          short e
          short g
          long g
          rational


          4. 字符串
          --所有字符串用單引號設(shè)定后輸入或賦值
          >> s='matrix laboratory'
          s =
          matrix laboratory

          --字符串的每個字符(包括空格)均為字符數(shù)組的一個元素
          >>size(s)   %size命令查看字符數(shù)組的維數(shù)
          ans =
               1    17

          --字符串和字符數(shù)組(或矩陣)基本上等價
          >> s(3)
          ans =
          t

          >> s2=['matlab']
          s2 =
          matlab

          --字符數(shù)組的生成
          >> s3=char('s','y','m','b','o','l','i','c');
          >> s3'
          ans =
          symbolic

          --字符串和數(shù)組之間的轉(zhuǎn)換
          A. 字符串轉(zhuǎn)換為數(shù)值代碼
          >> double(s3)'
          ans =
             115   121   109    98   111   108   105    99

          B. 字符數(shù)組轉(zhuǎn)換為字符串
           >> cellstr(s3)'
          ans =
          's'  'y'  'm'   'b'   'o'   'l'   'i'   'c'

          C. 數(shù)值數(shù)組和字符串之間的轉(zhuǎn)換
          -num2str    數(shù)字轉(zhuǎn)換為字符串
          -int2str     整數(shù)轉(zhuǎn)換為字符串
          -mat2str    矩陣轉(zhuǎn)換為字符串
          -str2num   轉(zhuǎn)換字符串為數(shù)字
          -sprintf     將格式數(shù)據(jù)寫為字符串
          -sscanf     在格式控制下讀字符串

          >> a=[1:5];
          >> b=num2str(a);
          >> a*2
          ans =
               2     4     6     8    10
          >> b*2
          ans =
            Columns 1 through 11
              98    64    64   100    64    64   102    64    64   104    64
            Columns 12 through 13
              64   106


          >> str2num(b)*2
          ans =
               2     4     6     8    10

          D. 字符串操作
          -strcat   鏈結(jié)串
          -strvcat  垂直鏈結(jié)串
          -strcmp  比較串
          -strncmp  比較串的前n個字符
          -findstr   在其他串中尋找此串
          -strjust    證明字符數(shù)組
          -strmatch  查找可能匹配的字符串
          -strrep    以其他串代替此串
          -strtok    尋找串中記號
          -upper    轉(zhuǎn)換為大寫
          -lower    轉(zhuǎn)換為小寫
          -blanks   生成空串
          -deblank  移去串內(nèi)空串
            
          E. 執(zhí)行字符串
          --函數(shù)eval實現(xiàn)

          n=4;
          t='1/(i+j-1)';
          a=zeros(n);
          for i=1:n
          for j=1:n
          a(i,j)=eval(t);
          end
          end
          a
          a =
             1.0000    0.5000    0.3333    0.2500
             0.5000    0.3333    0.2500    0.2000
             0.3333    0.2500    0.2000    0.1667
             0.2500    0.2000    0.1667    0.1429

          >> d='cd';
          >> eval(d)
          C:\MATLAB6p1\work

          F. 一些基本數(shù)字轉(zhuǎn)換函數(shù)
          -hex2num   十六進制轉(zhuǎn)換為雙精度數(shù)
          -hex2dec    十六進制轉(zhuǎn)換為十進制整數(shù)
          -dec2hex    十進制整數(shù)轉(zhuǎn)換為十六進制串
          -bin2dec    二進制轉(zhuǎn)換為十進制整數(shù)
          -dec2bin    十進制整數(shù)轉(zhuǎn)換為二進制串
          -base2dec   轉(zhuǎn)換B底字符串為十進制整數(shù)
          -dec2base  轉(zhuǎn)換十進制整數(shù)為B底串
          -strings    strings函數(shù)的幫助


          5. 矩陣

          6. 單元型變量
          一種以任意形式的數(shù)組為元素的多維數(shù)組
          --定義
          (1)賦值語句直接定義
          (2)由cell函數(shù)預(yù)先分配存儲空間,再對單元元素逐個賦值
          >> A=[1, 2; 3, 4];
          >> B={1:4, A, 'abcd'}
          B =
             [1x4 double]    [2x2 double]    'abcd'
          >> c{1,1}=1:4;
          >> c{1,2}=A;
          >> c{1,3}='abcd';
          >> c
          c =
              [1x4 double]    [2x2 double]    'abcd'
          >> c{2}
          ans =
               1     2
               3     4
          >> c(2)
          ans =
              [2x2 double]

          注:
          1)單元型變量的元素不是以指針的方式保存。改變其元素原變量矩陣A的值不等于改變變量B的第2個元素的值。
          2)單元型變量自身可以嵌套。

          >> D={1:4,A,B}
          D =
          [1x4 double]    [2x2 double]    {1x3 cell}

          >> D{3}{3}
          ans =
          abcd

          >> A(1,2)=-10
          A =
               1   -10
               3     4

          >> c{2}
          ans =
               1     2
               3     4

          --單元型變量的相關(guān)函數(shù)
          -cell          生成單元型變量
          -cellfun   對單元型變量中元素作用的函數(shù)
          -celldisp   顯示單元型變量的內(nèi)容
          -cellplot    圖形顯示單元型的內(nèi)容
          -num2cell   數(shù)值數(shù)組轉(zhuǎn)換為單元型變量
          -deal       輸入輸出處理
          -cell2struct  單元型變量轉(zhuǎn)換為結(jié)構(gòu)型變量
          -struct2cell  結(jié)構(gòu)型變量轉(zhuǎn)換為單元型變量-iscell      判斷是否為單元型變量   
          -reshape    改變單元數(shù)組的結(jié)構(gòu)

          >> cellfun('islogical', B)
          % 其他函數(shù)isreal, isempty, length, ndims等
          ans =
               0     0     0

          >> celldisp(B)
          B{1} =
               1     2     3     4
          B{2} =
               1     2
               3     4
          B{3} =
          abcd

          >> size(D)
          ans =
               1     3
          >> reshape(D,3,1)
          ans =
              [1x4 double]
              [2x2 double]
              {1x3 cell  }
          >> size(ans)
          ans =
               3     1

          7. 結(jié)構(gòu)型變量
          以指針方式傳遞數(shù)據(jù),將不同數(shù)據(jù)類型組合在一起的數(shù)據(jù)類型。

          --定義
          (1)賦值語句直接定義,以指針操作符‘.’連接結(jié)構(gòu)變量名和屬性名;
          (2)由struct函數(shù)定義
             結(jié)構(gòu)變量名=struct(元素名1, 元素值1,元素名2, 元素值2, …)


          >> A.a1='abcd';
          >> A.a2=1;
          >> A.a3=[1,2,3,4];
          >> A
          A =
              a1: 'abcd'
              a2: 1
              a3: [1 2 3 4]

          >> B=[1,2;3,4];
          >> A(2).a1='efgh';
          >> A(2).a2=2;
          >> A(2).a3=B;
          >> A
          A =
          1x2 struct array with fields:
              a1
              a2
              a3

          >> A(1)
          ans =
              a1: 'abcd'
              a2: 1
              a3: [1 2 3 4]
          >> A(2)
          ans =
              a1: 'efgh'
              a2: 2
              a3: [2x2 double]

          >> C=struct('c1',1,'c2',B, 'c3','abcd')
          C =
              c1: 1
              c2: [2x2 double]
              c3: 'abcd'

          >> C.c1=A
          C =
              c1: [1x2 struct]
              c2: [2x2 double]
              c3: 'abcd'

          >> C.c1(1).a1
          ans =
          abcd

          --結(jié)構(gòu)型變量的相關(guān)函數(shù)
          -struct     創(chuàng)建或轉(zhuǎn)換結(jié)構(gòu)變量
          -findnames  得到結(jié)構(gòu)型變量的屬性名
          -getfield   得到結(jié)構(gòu)型變量的屬性值
          -setfield   設(shè)定結(jié)構(gòu)型變量的屬性值
          -rmfield   刪除結(jié)構(gòu)型變量的屬性
          -isfield    判斷是否為結(jié)構(gòu)型變量的屬性
          -isstruct   判斷是否為結(jié)構(gòu)型變量

          >> fieldnames(C)
          ans =
              'c1'
              'c2'
              'c3'
          >> iscell(ans)
          ans =
               1

          >> D=getfield(C,'c1')
          D =
          1x2 struct array with fields:
              a1
              a2
              a3

          >> C=setfield(C,'c1',2)
          C =
              c1: 2
              c2: [2x2 double]
              c3: 'abcd'

          >> C=rmfield(C,'c1')
          C =
              c2: [2x2 double]
              c3: 'abcd'

          >> isfield(C,'c2')
          ans =
               1

          >> isstruct(C)
          ans =
               1

           


          2.2 矩陣的基本運算

          I.數(shù)和算術(shù)表達式
          3 –90  0.0001  9.1234  1.6021e-20 
          5.02252e23

          數(shù)學(xué)運算符:
          +  -  *  /  (右除)   \  (左除)   ^

          II.數(shù)學(xué)函數(shù)

          基本三角函數(shù) sin cos sec csc tan cot
          反三角函數(shù) asin acos asec acsc atan acot
          雙曲函數(shù) sinh cosh sech csch tanh coth
          反雙曲函數(shù) asinh acosh asech acsch atanh acoth
          指數(shù)和對數(shù) exp log Log10 sqrt  
          復(fù)數(shù)運算 abs angle conj real imag 
          數(shù)值函數(shù) fix floor ceil round rem 
          整數(shù)函數(shù) lcm gcd    
                


          >> x=5.1
          x =
              5.1000
          >> fix(x)
          ans =
          5
          >> floor(x)
          ans =
          5
          >> ceil(x)
          ans =
          6
          >> round(x)
          ans =
          5
          >> rem(5,3)
          ans =
          2
          >> sin(1+2i)
          ans =
          3.1658 + 1.9596i

          III.矩陣及其元素

          1.矩陣輸入的基本方法
          >> A=[1 2 3]
          A =
               1     2     3
          >> A=[1 2 3;4 5 6;7 8 9]
          A =
               1     2     3
               4     5     6
               7     8     9
          >> A=[1, 2, 3;
          4,5,6;
          7, 8,9]
          A =
               1     2     3
               4     5     6
               7     8     9
          >>  A=[1, 2, 3
          4,5,6
          7, 8,9]

          A =
               1     2     3
               4     5     6
               7     8     9

          2.矩陣元素的存取
          Matlab的矩陣元素可以為任何數(shù)值表達式。
          >> x=[-1.3 sqrt(3) (1+2+3)*4/5]
          x =
             -1.3000    1.7321    4.8000

          >> A=[1+5i 2+6i]
          A =
             1.0000 + 5.0000i   2.0000 + 6.0000i

          >> A=[1 2; 3 4]+i*[5 6;7 8]
          A =
             1.0000 + 5.0000i   2.0000 + 6.0000i
             3.0000 + 7.0000i   4.0000 + 8.0000i


          Matlab的矩陣元素的引用:

          >> x=A(1,2)
          x =
             2.0000 + 6.0000i

          Matlab的矩陣的維數(shù)自動擴充:
          >> x=[-1.3 sqrt(3) (1+2+3)*4/5]
          x =
             -1.3000    1.7321    4.8000
          >> x(5)=abs(x(1))
          x =
             -1.3000  17321  4.8000   0   1.3000
          >> A=[1 2;3 4]
          A =
               1     2
               3     4
          >> A(3,1)=-1
          A =
               1     2
               3     4
          -1     0


          3.子矩陣的操作
          大的矩陣可以通過小的矩陣擴充得到:
          >> A=[A;2 1]
          A =
               1     2
               3     4
              -1     0
               2     1
          >> A=[A, [1 2 3 4]']
          A =
               1     2     1
               3     4     2
              -1     0     3
               2     1     4

          小的矩陣可以從大的矩陣抽取得到:
          A(:)—A的所有元素;
          A(:,J)—A的第J列所有元素;
          A(J:K)—A(J),A(J+1),…,A(K);
          A(:,J:K)—A(:,J),A(:,J+1),…A(:,K);

          A(J,:)—A的第J行所有元素;
          A(J:K,:)—A(J,:),A(J+1,:),…A(K.:);
          A =
               1     2     1
               3     4     2
              -1     0     3
               2     1     4
          >> y=A(1:3,:)
          y =
               1     2     1
               3     4     2
              -1     0     3

          >> x=A(:,1:2)
          x =
               1     2
               3     4
              -1     0
               2     1

          >> z=A(1:2,2:3)
          z =
               2     1
               4     2
          4. “:”運算符的用法
          用來產(chǎn)生相同增量的向量的方法:
          >> t=1:5
          t =
               1     2     3     4     5

          >> x=pi:-pi/3:0
          x =
             3.1416    2.0944    1.0472    0

          >> x=(0.0:0.2:1.0)';%‘'’表示轉(zhuǎn)置
          >> y=sin(x);  %計算函數(shù)值
          >> [x y]     %組合出矩陣
          ans =
                   0         0
              0.2000    0.1987
              0.4000    0.3894
              0.6000    0.5646
              0.8000    0.7174
              1.0000    0.8415


          5.生成特殊矩陣的函數(shù)
          >> rand(1,3)
          ans =
              0.9501    0.2311    0.6068

          >> rand(3)
          ans =
              0.4860    0.4565    0.4447
              0.8913    0.0185    0.6154
              0.7621    0.8214    0.7919

          >> eye(2,3)
          ans =
               1     0     0
               0     1     0

          >> ones(3,2)
          ans =
               1     1
               1     1
               1     1

          >> randn(3,2)  %標準正態(tài)分布
          ans =
             -0.4326    0.2877
             -1.6656   -1.1465
              0.1253    1.1909

          IV. 矩陣操作
          1.矩陣轉(zhuǎn)置
          >> A=[1,2,3;4,5,6]
          A =
               1     2     3
               4     5     6
          >> B=A'
          B =
             1     4
               2     5
               3     6

          >> C=[1+i,-i;2,1-i]
          C =
             1.0000 + 1.0000i        0 - 1.0000i
             2.0000             1.0000 - 1.0000i
          >> D=C'  %轉(zhuǎn)置共扼
          D =
             1.0000 - 1.0000i   2.0000         
          0 + 1.0000i   1.0000 + 1.0000i


          2.對角矩陣
          diag函數(shù)用來生成一個對角矩陣
          或提取一個矩陣的對角元素:
          >> diag(2:4)
          ans =
               2     0     0
               0     3     0
               0     0     4
          >> A
          A =
               1     2     3
               4     5     6
          >> diag(A)
          ans =
               1
               5
          3.上/下三角矩陣
          >> tril(A)
          ans =
               1     0     0
               4     5     0

          >> triu(A)
          ans =
               1     2     3
               0     5     6

          >> fliplr(A)
          ans =
               3     2     1
               6     5     4

          >> flipud(A)
          ans =
               4     5     6
               1     2     3


          V.矩陣運算
          1.加和減
          >> A
          A =
               1     2     3
               4     5     6
          >> B=[1 4 7;2 5 6]
          B =
               1     4     7
               2     5     6
          >> C=A+B
          C =
               2     6    10
               6    10    12

          >> D=A-1.0
          D =
               0     1     2
               3     4     5

           

          2.乘法
          >> A
          A =
               1     2     3
               4     5     6
          >> A'
          ans =
               1     4
               2     5
               3     6
          >> A*A'
          ans =
              14    32
              32    77

          3.矩陣除法
          標量運算時:
          矩陣運算時:
           --inv(A)*B;
           --A*inv(B);

          >> A=[1 2 3]
          A =
               1     2     3
          >> B=[1 2 -3; -2 5 6; 7 2 1]
          B =
               1     2    -3
              -2     5     6
               7     2     1
          >> A/B
          ans =
             -0.1818    0.3636    0.2727

          >> B\A'
          ans =
              0.2929
              0.4444
              0.0606

           

           

          4. 矩陣乘方
          >> B
          B =
               1     2    -3
              -2     5     6
               7     2     1
          >> B^3
          ans =
               6    -6   114
             258   309   150
             -98   134   118

          5.數(shù)組運算
          ----向量或矩陣間對應(yīng)元素的運算:
          .+  .-   .*   ./   .\   .^

          >> x=[1 2 3]
          x =
               1     2     3
          >> y=[4 5 6]
          y =
               4     5     6
          >> z=x.*y
          z =
               4    10    18
          >> z1=x.^y
          z1 =
              1    32   729
          >> z3=x.^2
          z3 =
               1     4     9
          >> z4=2.^x
          z4 =
               2     4     8

          >> [1 2; 3 4]./[2 2; 2 2]
          ans =
              0.5000    1.0000
              1.5000    2.0000
          >> [1 2; 3 4].\[2 2; 2 2]
          ans =
              2.0000    1.0000
              0.6667    0.5000


          常見數(shù)值問題介紹
          一、 線性方程組求解
          A*x=b
          A為m*m階矩陣, X和b為m階向量。
           
          >> A=[1,2,3;4,5,6;7,8,0];
          >> b=[366;804;351];
          >> det(A)
          ans =
              27
          >> x=inv(A)*b
          x =
             25.0000
             22.0000
             99.0000

           

          >> x=A\b
          x =
             25.0000
             22.0000
             99.0000

          廣義情況:方程數(shù)與未知量數(shù)目不等時,線性方程組通常不存在唯一解,但最小二乘解x*是存在唯一。
            
          >> A=[1 2 3; 4 5 6; 7 8 0; 2 5 8];
          >> b=[366 804 351 514]';
          >> x=A\b
          x =
            247.9818
           -173.1091
            114.9273

           


          二、矩陣函數(shù)
          I.矩陣基本性質(zhì)
          1)行列式
          >> A=rand(3)
          A =
              0.9501    0.4860    0.4565
              0.2311    0.8913    0.0185
              0.6068    0.7621    0.8214
          >> det(A)
          ans =
              0.4289

          2)秩
          >> rank(A)
          ans =
               3

          3)跡
          >> trace(A)
          ans =
              2.6628

          4)范數(shù)
          >> A=[1 1 1;0 0 0;1 1 1]
          A =
               1     1     1
               0     0     0
               1     1     1
          >> norm(A)   %2-范數(shù)
          ans =
              2.4495

          >> norm(A,inf)   % inf-范數(shù)
          ans =
               3

          >> norm(A,1)    %1-范數(shù)
          ans =
               2

           

           

          II.特征值計算
          >> A=[0 1;-1 0]
          A =
               0     1
              -1     0
          >> eig(A)
          ans =
                  0 + 1.0000i
                  0 - 1.0000i

          >> [x,D]=eig(A)
          x =
             0.7071           0.7071         
             0 + 0.7071i       0 - 0.7071i

          D =
                  0 + 1.0000i        0         
                  0                  0 - 1.0000i

          ---A*X=X*D


          III.三角分解
          >> A=[1 2 3;4 5 6; 7 8 9]
          A =
               1     2     3
               4     5     6
               7     8     9
          >> [L,U]=lu(A)
          L =
              0.1429    1.0000         0
              0.5714    0.5000    1.0000
              1.0000         0         0
          U =

              7.0000    8.0000    9.0000
                   0    0.8571    1.7143
                   0         0    0.0000

          >> L*U
          ans =
               1     2     3
               4     5     6
               7     8     9
          >> [L,U,P]=lu(A)
          L =
              1.0000         0         0
              0.1429    1.0000         0
              0.5714    0.5000    1.0000
          U =
              7.0000    8.0000    9.0000
                   0    0.8571    1.7143
                   0         0    0.0000
          P =
               0     0     1
               1     0     0
               0     1     0

          >> P*A
          ans =
               7     8     9
               1     2     3
               4     5     6

           

          >> L*U
          ans =
               7     8     9
               1     2     3
               4     5     6

          IV.正交分解
          >> A=[1 2 3; 4 5 6;7 8 9;10 11 12]
          A =
               1     2     3
               4     5     6
               7     8     9
              10    11    12

          >> [Q,R]=qr(A)
          Q =
             -0.0776   -0.8331    0.5456   -0.0478
             -0.3105   -0.4512   -0.6919    0.4704
             -0.5433   -0.0694   -0.2531   -0.7975
             -0.7762    0.3124    0.3994    0.3748


          R =
            -12.8841  -14.5916  -16.2992
                 0   -1.0413   -2.0826
                 0       0    -0.0000
                 0       0         0

          >> Q*R
          ans =
              1.0000    2.0000    3.0000
              4.0000    5.0000    6.0000
              7.0000    8.0000    9.0000
             10.0000   11.0000   12.0000

          -- QR分解可用來求超定方程的最小二乘解

          >> b=[1;3; 5; 7]
          b =
               1
               3
               5
               7

          >> x=A\b
          Warning: Rank deficient, rank = 2  tol =   1.4594e-014.
          x =
              0.5000
                   0
              0.1667

          >> y=Q'*b
          y =
             -9.1586
             -0.3471
              0.0000
             -0.0000
          >> x=R\y
          Warning: Rank deficient, rank = 2  tol =   1.4594e-014.
          x =

              0.5000
                   0
              0.1667
          三、方程求根
           

          >> p=[1 -12 0 25 116]
          p =
               1   -12     0    25   116
          >> r=roots(p)
          r =
            11.7473         
             2.7028         
            -1.2251 + 1.4672i
            -1.2251 - 1.4672i

          已知多項式的根,可用poly構(gòu)造相應(yīng)的多項式:

          >> pp=poly(r)
          pp =
              1.0000  -12.0000   -0.0000   25.0000  116.0000

          四、曲線擬和
          I.一維插值
          >> h=1:12;
          >> t=[5 8 9 15 25 29 31 30 22 25 27 24];
          >> plot(h,t,h,t,'+')
           
          interp1插值函數(shù)的格式:
          Y1=interp1(X,Y,X1,’method’)
          X,Y—原數(shù)據(jù)點;
          X1—加細的數(shù)據(jù)點,或希望得到插值數(shù)據(jù)的數(shù)據(jù)點;
          Method---‘linear’,  ‘spline’, ‘cubic’;

          >> x1=1:0.2:12;
          >> y1=interp1(h,t,x1,'spline');
          >> plot(h,t,'k+',x1,y1,'k-')
           

          II.曲線擬和
          設(shè)有一組數(shù)據(jù)點
           
          尋找一個多項式n次多項式P*(x):
           

          >> x=0:0.1:1;
          >> y=[-0.447 1.978 3.28 6.16 7.08 7.34 7.66 9.56 9.48 9.30 11.2];
          >> p=polyfit(x,y,2)
          p =
             -9.8108   20.1293   -0.0317

          >> xi=linspace(0,1,100);
          >> z=polyval(p,xi);
          >> plot(x,y,'ko',xi,z,'r-')
           


          >> p=polyfit(x,y,10);
          >> xi=linspace(0,1,100);
          >> z=polyval(p,xi);
          >> plot(x,y,'ko',xi,z,'r-')
           

          五、數(shù)值積分
          sum(x)—輸入數(shù)組x, 輸出為x的和;

          cumsum(x)—輸入數(shù)組x, 輸出為x的依次累加和;


          trapz(x)—輸入數(shù)組x, 輸出為x按梯形求積公式計算的積分(單位步長);

          trapz(x,y)—輸入數(shù)組x,y, 輸出y對x按梯形求積公式計算的積分(步長可以不相等);

          quad(‘fun’,a,b)—用辛普森公式(2階)計算以fun.m的函數(shù)在(a,b)上的積分,相對誤差為0.001;

          quad(‘fun’,a,b,tol)—用辛普森公式(2階)計算以fun.m的函數(shù)在(a,b)上的積分,相對誤差為tol;

          quad8(‘fun’,a,b,tol)—用辛普森公式(8階)計算以fun.m的函數(shù)在(a,b)上的積分,相對誤差為tol;

          rand(1,n)—產(chǎn)生n個(0,1)的隨機數(shù)。

           

          計算積分
          1)矩形公式和梯形公式:將
          >> h=pi/20;
          >> x=0:h:pi/2;
          >> y=sin(x);
          >> z1=sum(y(1:10))*h
          z1 =
              0.9194
          >> z2=sum(y(2:11))*h
          z2 =
              1.0765
          >> z=cumsum(y);
          >> z11=z(10)*h
          z11 =
              0.9194
          >> z12=(z(11)-z(1))*h
          z12 =
              1.0765
          >> z3=trapz(y)*h
          z3 =
              0.9979
          2)辛普森公式
          >> z4=quad('sin',0,pi/2)
          z4 =
              1.0000

          3)蒙特卡羅方法
          >> n=100000;
          >> x=rand(1,n);
          >> y=sin(x.*pi/2);
          >> z=sum(y)*pi/(2*n)
          z =
              1.0005

           

           

           

           


          六、數(shù)據(jù)分析
          max----最大值
          min----最小值
          mean----均值
          media----中值
          std----標準差
          sum----元素總和
          cumsum----累加和
          prod----元素乘積
          cumprod----累積
          corrcoef----互相關(guān)系數(shù)矩陣
          cov----協(xié)方差矩陣
          ……

          >> a=[ 1 3 5 9 3 2 7 6];
          >> mean(a)
          ans =
              4.5000
          >> median(a)
          ans =
               4

          >> std(a)
          ans =
              2.7255
          >> cumsum(a)
          ans =

               1     4     9    18    21    23    30    36

          >> b=[1 4 6 8 2 1 5 0];
          >> corrcoef(a,b)
          ans =
              1.0000    0.6771
              0.6771    1.0000
          >> cov(a,b)
          ans =
              7.4286    5.2143
              5.2143    7.9821

           

           

           

           

          編程基礎(chǔ)

          一、 關(guān)系運算和IF語句
          I. 關(guān)系運算和邏輯操作符
          關(guān)系運算符
          <   <=   >    >=   ==   ~=

          邏輯操作符
          &   |   ~

          y=all(x)
          若x 為向量,當所有元素非零時y=1,
          否則y=0;
          若x 為矩陣,all作用于列元素,y為行向量。

          y=any(x)
          若x 為向量,當有一個元素非零時y=1,
          否則y=0;
          若x 為矩陣,all作用于列元素,y為行向量。

           

          >> A=[1 2; 0 1]
          A =
               1     2
               0     1
          >> any(A)    %any表示矩陣的每一列中%是否有非零元素
          ans =
               1     1

          >> all(A)  %all表示矩陣的每一列中
          %是否全是非零元素
          ans =
               0     1


          IF語句:

          1)
          if <關(guān)系表達式>
          <語句1>
          end

          2)
          if <關(guān)系表達式>
          <語句1>
          else
          <語句2>
          end

          3)
          if <關(guān)系表達式1>
          <語句1>
          elseif <關(guān)系表達式1>
          <語句2>
          ……
          elseif <關(guān)系表達式n>
          <語句n>
          else
          <語句n+1>
          end

           


          二、for 循環(huán)
          for <循環(huán)參數(shù)>=<初值>:<步長>:<終值>
             <語句>
          end

          >> for i=1:3
          for j=1:4
          a(i,j)=1/(i+j-1);
          end
          end
          >> format rat
          >> a
          a =
              1      1/2        1/3       1/4    
             1/2     1/3        1/4        1/5    
             1/3     1/4        1/5        1/6 

          二、 while語句
          while <關(guān)系表達式>
             <語句>
          end

          求出滿足1+2+…+n<100的最大整數(shù)n:
          >> sum=0;n=0;
          >> while sum<100
          n=n+1;
          sum=sum+n;
          end;
          >> n=n-1;
          >> n
          n =
               13   

          三、 switch語句
          switch <表達式>
          case  value1
            <語句1>
          case  value2
            <語句2>

          otherwise
            <語句n>
          end

          四、 M文件:文本文件及函數(shù)文件
          I.文本文件: <M-文件名>.m
          fibo.m:

          %An M-file to calculate Fibonnacinumbers
          f=[1 1]; i=1;
          while i<15
            f(i+2)=f(i)+f(i+1);
            i=i+1;
          end
          plot(f, ‘*k’)

           
          II.函數(shù)文件
          M-文件的第一行包含function;
          變量和運算都在文件內(nèi)部,不在工作間;
          文件名為函數(shù)名;

          function <因變量>=<函數(shù)名>(<自變量>)

          function a=randint(m,n)
          %RANDINT Randomly generated integral %matrix
          %randint(m,n) returns an m-by-n matrix %with entries between 0 and 9
          a=floor(10*rand(m,n));

          >>x=randint(2,3)
          x=
            9  6  8
            2  3  5

           


          stat.m:
          function [mean,stdev]=stat(x)
          % STAT Mean and standard deviation
          % For a vector x, stat(x) returns the mean %and standard deviation of x
          % For a matrix x, stat(x) returns two row %vectors containing, respectively,
          % the mean and standard deviation of each %column.
          [m,n]=size(x);
          if m==1
              m=n;  % handle case of a row vector
          end
          mean=sum(x)/m;
          stdev=sqrt(sum(x.^2)/m-mean.^2);

          >> x=[2 4 -7 0 5 -1];
          >> [xm,xd]=stat(x)
          xm =
              0.5000
          xd =
              3.9476

          posted @ 2007-09-27 11:54 張生工作室 閱讀(2206) | 評論 (3)編輯 收藏

          主站蜘蛛池模板: 鲁甸县| 裕民县| 宿松县| 田阳县| 平南县| 五常市| 贵阳市| 深州市| 周口市| 琼结县| 五台县| 大方县| 荔浦县| 固镇县| 南康市| 长兴县| 浦城县| 东丰县| 年辖:市辖区| 莲花县| 永康市| 富川| 米易县| 金平| 湛江市| 宁都县| 青海省| 巴里| 三门峡市| 高唐县| 墨江| 龙口市| 定日县| 海安县| 秭归县| 三穗县| 三河市| 库伦旗| 蕲春县| 饶平县| 汕尾市|