ety
                         
                      
                    
                            
                      
                     
                    
                      
                    
                   | 
                  
                    
                    分享: 
                     
					 
						  
					 
					 
						  
					 
					 
						  
					 
                    
                    
                    	
                    
                      ▲
                    
                      ▼
                    
                    
                    
                      
                     
                     
                     
                     
                    
                    
                    
                      
                          
                          
                      
                       
                      
                      
                      
        
                          為什麼還沒教的就不能用? 你猜的還是老師親口說的? 看你寫的程式, 只用到 stdio.h, 那我假設你們老師只教到 BIOS ( Basic Input and Output System ) 這個範圍好了, 我們不用 isdigit(), 以 scanf 來實作: 複製程式
#include <stdio.h>
int main(void)
{
    int a, b, c, d;
    char e[168];
    c = 0;
    for (a = 1; a <= 10; a++)
    {
        b = 0;
        printf("請輸入第%d個字串:\n", a);
        scanf("%s", &e);
        for (d = 0; d < strlen(e); d++)
        {
            if (e[d] > 47 && e[d] < 58)
            {b += (e[d] - 48);}
        }
        printf("這個字串的數字總和為: %d\n\n", b);
        c += b;
    }
    printf("這十個字串的數字總和為: %d", c);
    return 0;
}
 變數 a 是用來計數第一個迴圈的, 因為你們老師要十個, 所以就命令它執行十次. 變數 b 是用來計數單一字串中的數字總和. 變數 c 是用來計數十個字串中的數字總和. 變數 d 是用來計數第二個迴圈的. 這個變數的值是每個字串的長度. 例如輸入一個長度為五的字串, 那這個變數的值就是5! 變數 e 是用來儲存字串用的一個字元陣列. 要特別注意的是: 我假設你們老師每次輸入的字串長度皆不超過一佰六十八個字元! 若你認為你們老師可能會輸入更長的字串, 你可以修改為大一個的陣列! 我之所以用一六八純粹只是... 這個數字很吉利 ~~~     
 
 [ 此文章被ety在2007-06-02 23:44重新編輯 ] 
                    
                    
                     
        
                   | 
                 
                
                  
              
                
                   
                    
                   | 
                 
                
                  
                    
                      x0
                      
                     
                    
                  
                    [11 樓]
                    
                    
                     From:臺灣和信超媒體寬帶網 |  Posted:2007-06-02 23:36 | 
                    
                     | 
                 
               
                   | 
                 
                 
        
        
                
        
        
                
        
        
              
                
                  
                    ety
                         
                      
                    
                            
                      
                     
                    
                      
                    
                   | 
                  
                    
                    分享: 
                     
					 
						  
					 
					 
						  
					 
					 
						  
					 
                    
                    
                    	
                    
                      ▲
                    
                      ▼
                    
                    
                    
                      
                     
                     
                     
                     
                    
                    
                    
                      
                          
                          
                      
                       
                      
                      
                      
        
                          當然可以呀, 不過想想看, 要一次輸入十個字串, 中間都沒有什麼訊息顯示的話, 挺無趣的! 不過, 我也不知道你們老師的問題是如何說明的, 畢竟你是他的學生, 你應該猜得出來是不是十個字串算出數字總和還是一個十個字的字串! 附帶一提, 我之前所寫的程式碼不夠嚴謹, 你在 compile 的時候可能會出現 warning ... 如果你確定要用這個程式碼交作業的話, 請寫成這樣: 複製程式
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
  int a, b, c, d;
  char e[256];
  c = 0;
  for (a = 1; a <= 10; a++)
  {
      b = 0;
      printf("請輸入第%d個字串:\n", a);
      scanf("%s", e);
      for (d = 0; d < strlen(e); d++)
      {
        if (e[d] > 47 && e[d] < 58)
        {b += (e[d] - 48);}
      }
      printf("這個字串的數字總和為: %d\n\n", b);
      c += b;
  }
  printf("這十個字串的數字總和為: %d\n", c);
  system("pause");
  return 0;
}
 注意, e 的前面不要加 &, 另外, 使用 strlen 時要 include <string.h>  
                    
                    
                     
        
                   | 
                 
                
                  
              
                
                   
                    
                   | 
                 
                
                  
                    
                      x1
                      
                     
                    
                  
                    [14 樓]
                    
                    
                     From:臺灣和信超媒體寬帶網 |  Posted:2007-06-03 14:38 | 
                    
                     | 
                 
               
                   | 
                 
                 
        
        
              
                
                  
                    ety
                         
                      
                    
                            
                      
                     
                    
                      
                    
                   | 
                  
                    
                    分享: 
                     
					 
						  
					 
					 
						  
					 
					 
						  
					 
                    
                    
                    	
                    
                      ▲
                    
                      ▼
                    
                    
                    
                      
                     
                     
                     
                     
                    
                    
                    
                      
                          
                          
                      
                       
                      
                      
                      
        
                          如果你擔心 string.h 並不包含於 BIOS (也就是 stdio 或 stdlib 的範圍內), 那麼你也可以不用 strlen, 自己寫出一個副程式來替代: 複製程式
#include <stdio.h>
#include <stdlib.h>
int length(char string[])
{
    int index;
    for (index = 0; string[index] != '\0' index++)
        continue;
    return index;
}
int main(void)
{
  int a, b, c, d;
  char e[256];
  c = 0;
  for (a = 1; a <= 10; a++)
  {
      b = 0;
      printf("請輸入第%d個字串:\n", a);
      scanf("%s", e);
      for (d = 0; d < length(e); d++)
      {
        if (e[d] > 47 && e[d] < 58)
        {b += (e[d] - 48);}
      }
      printf("這個字串的數字總和為: %d\n\n", b);
      c += b;
  }
  printf("這十個字串的數字總和為: %d\n", c);
  system("pause");
  return 0;
}
 一次給了你兩個版本, 希望對你的課業有所幫助! 有問題的話寄封信給我, 我不常來這兒的, 若你又有問題再發問, 我可能會沒注意到! ^^  
                    
                    
                     
        
                   | 
                 
                
                  
              
                
                   
                    
                   | 
                 
                
                  
                    
                      x0
                      
                     
                    
                  
                    [15 樓]
                    
                    
                     From:臺灣和信超媒體寬帶網 |  Posted:2007-06-03 14:52 | 
                    
                     | 
                 
               
                   | 
                 
                 
        
        
                
        
        
                
        
        
                
        
        
                
        
       |