2011-04-26 126 views
0

我在MATLAB中遇到了一些麻煩。我需要創建自己的小週期表,我想我迄今已經完成了很好的工作。但我的問題是,當我嘗試添加第二個元素時,它會寫入第一個元素。我怎樣才能解決這個問題?請看看我的代碼,我知道它有點長:D 如果你看到其他錯誤,我會很感激。感謝您的幫助matlab,結構,元素週期表創建

fprintf('Periodic table program - Main menu:\n (1) Add an element; (2) Search for an element; (3) Exit\n'); 
choose=input('Enter your choice: '); 
while choose~=3 
if choose==1 
    element.name=input('Enter the name of your element: ','s'); 
    element.symbol=input('Enter the symbol of your element: ','s'); 
    element.Anumber=input('Enter the atomic number of your element: '); 
    while element.Anumber<=0 || mod(element.Anumber,1)~=0 
     disp('This is not a positive integer, please enter a valid input!'); 
     eAnumber=input('Enter the atomic number of your element: '); 
    end 
elseif choose==2 
    fprintf('Search menu:\n (1) By name; (2) By symbol; (3) By atomic number; (4) Main menu\n'); 
    S_choice=input('Enter your choice: '); 
    while S_choice ~=4 
    if S_choice==1 
     alex=input('Enter the name of the element: ','s'); 
     if alex~=element.name 
      disp('No such element exists!'); 
     else 
      fprintf('Operations menu:\n (1) Show properties; (2) Edit; (3) Main menu\n'); 
      Op_choice=input('Enter your choice: '); 
      while Op_choice~=3 
       if Op_choice==1 
        element 
       elseif Op_choice==2 
        fprintf('Edit menu:\n (1) Edit name; (2) Edit symbol; (3) Edit atomic number; (4) Main menu\n'); 
        E_choice=input('Enter your choice: '); 
        while E_choice~=4 
         if E_choice==1 
          new_name=input('Enter the new name of the element: ','s'); 
          element.name=new_name; 
         elseif E_choice==2 
          new_sym=input('Enter the new symbol of the element: ','s'); 
          element.symbol=new_sym; 
         elseif E_choice==3 
          new_anumber=input('Enter the new atomic number: '); 
          while new_anumber<=0 || mod(new_anumber,1)~=0 
           disp('This is not a positive integer, please enter a valid input!'); 
           new_anumber=input('Enter the new atomic number: '); 
          end 
          element.Anumber=new_anumber; 
         end 
        fprintf('Edit menu:\n (1) Edit name; (2) Edit symbol; (3) Edit atomic number; (4) Main menu\n'); 
        E_choice=input('Enter your choice: '); 
        end    
       end 
       fprintf('Operations menu:\n (1) Show properties; (2) Edit; (3) Main menu\n'); 
       Op_choice=input('Enter your choice: '); 
      end 
     end 
    elseif S_choice==2 
     lugano=input('Enter the symbol of the element: ','s'); 
     if lugano~=element.symbol 
      disp('No such element exists!'); 
     else 
      fprintf('Operations menu:\n (1) Show properties; (2) Edit; (3) Main menu\n'); 
      Op_choice=input('Enter your choice: '); 
      while Op_choice~=3 
       if Op_choice==1 
        element 
       elseif Op_choice==2 
        fprintf('Edit menu:\n (1) Edit name; (2) Edit symbol; (3) Edit atomic number; (4) Main menu\n'); 
        E_choice=input('Enter your choice: '); 
        while E_choice~=4 
         if E_choice==1 
          new_name=input('Enter the new name of the element: ','s'); 
          element.name=new_name; 
         elseif E_choice==2 
          new_sym=input('Enter the new symbol of the element: ','s'); 
          element.symbol=new_sym; 
         elseif E_choice==3 
          new_anumber=input('Enter the new atomic number: '); 
          while new_anumber<=0 || mod(new_anumber,1)~=0 
           disp('This is not a positive integer, please enter a valid input!'); 
           new_anumber=input('Enter the new atomic number: '); 
          end 
          element.Anumber=new_anumber; 
         end 
        fprintf('Edit menu:\n (1) Edit name; (2) Edit symbol; (3) Edit atomic number; (4) Main menu\n'); 
        E_choice=input('Enter your choice: '); 
        end 
       end 
        fprintf('Operations menu:\n (1) Show properties; (2) Edit; (3) Main menu\n'); 
        Op_choice=input('Enter your choice: '); 
      end 
     end 
    elseif S_choice==3 
     niang=input('Enter the atomic number of the element: '); 
     if niang~=element.Anumber 
      disp('No such element exists!'); 
     else 
      fprintf('Operations menu:\n (1) Show properties; (2) Edit; (3) Main menu\n'); 
      Op_choice=input('Enter your choice: '); 
      while Op_choice~=3 
       if Op_choice==1 
        element 
       elseif Op_choice==2 
        fprintf('Edit menu:\n (1) Edit name; (2) Edit symbol; (3) Edit atomic number; (4) Main menu\n'); 
        E_choice=input('Enter your choice: '); 
        while E_choice~=3 
         if E_choice==1 
          new_name=input('Enter the new name of the element: ','s'); 
          element.name=new_name; 
         elseif E_choice==2 
          new_sym=input('Enter the new symbol of the element: ','s'); 
          element.symbol=new_sym; 
         elseif E_choice==3 
          new_anumber=input('Enter the new atomic number: '); 
          while new_anumber<=0 || mod(new_anumber,1)~=0 
           disp('This is not a positive integer, please enter a valid input!'); 
           new_anumber=input('Enter the new atomic number: '); 
          end 
          element.Anumber=new_anumber; 
         end 
        fprintf('Edit menu:\n (1) Edit name; (2) Edit symbol; (3) Edit atomic number; (4) Main menu\n'); 
        E_choice=input('Enter your choice: '); 
        end 
       end 
       fprintf('Operations menu:\n (1) Show properties; (2) Edit; (3) Main menu\n'); 
       Op_choice=input('Enter your choice: '); 
      end 
     end 
    end 
    fprintf('Search menu:\n (1) By name; (2) By symbol; (3) By atomic number; (4) Main menu\n'); 
    S_choice=input('Enter your choice: '); 
    end 
end 
fprintf('Periodic table program - Main menu:\n (1) Add an element; (2) Search for an element; (3) Exit\n'); 
choose=input('Enter your choice: '); 
end 

回答

0

您需要一個結構數組來容納不同的元素。嘗試如下:

element = struct 
i = 1; 

element(i).name = 'oxygen' 
element(i).symbol = 'O' 

i = i + 1 
element(i).name = 'carbon' 
element(i).symbol = 'C' 
+0

我可以把它們放在單元格數組中,但它們是1x1結構形式。 – 2011-04-26 21:53:31

+0

我需要訪問的結構形式也無法找出:( – 2011-04-26 21:54:08