2011-04-26 129 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