| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 786 人关注过本帖
标题:只能输出数字
只看楼主 加入收藏
henyue
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2006-6-3
收藏
 问题点数:0 回复次数:5 
只能输出数字
有一个小问题想请问一下
程序里要求输出的字符只能是数字,否则做出相应处理
这个应该怎么实现呢,谢谢
搜索更多相关主题的帖子: 数字 输出 
2008-08-06 20:19
网客侠
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2008-7-27
收藏
得分:0 
%d输出就行啦
2008-08-06 20:58
floats
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-8-5
收藏
得分:0 
通过字符的ASCII的值来判断输出的字符是不是数字。数字字符0-9的ASCII的值的范围是 0x30—0x39
2008-08-06 22:09
Rand
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2008-7-2
收藏
得分:0 
if(ch>'0'&&ch<'9')
lalala~
2008-08-06 22:38
henyue
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2006-6-3
收藏
得分:0 
上面说的都只是0-9,如果要数字有多位的怎么办呢
2008-08-07 08:41
henyue
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2006-6-3
收藏
得分:0 
比如有一个文件内容是这样的:1,23,45,6,a,
                            2,12,47,9,B,
只能输出其中的数字,遇到非数字则进行处理,代码如下:帮忙改下,谢谢
#include <stdio.h>                                               
#include <stdlib.h>   
#include <string.h>
#include <curses.h>

#define SIZE 5
                                             
struct d_list
{                                                  
   int data;                                                      
   struct d_list *prior;                                          
   struct d_list *next;                                            
}dlist[SIZE];         
   
int Get_File_Content( char *filename );  
int spit( unsigned char *buf );
int write_buf( char *filename, unsigned char *buf, int len );

//读文件(读内容、解析内容)
int Get_File_Content( char *filename )
{
    FILE *fp;
    char buf[8192];    
    if( filename == NULL )
        return -1;        
    if( ( fp = fopen( filename, "r" ) ) == NULL )
        return -1;            
    while( 1 )
    {
        memset( buf, 0x00, sizeof(buf) );    
        fgets( buf, sizeof(buf), fp );            
        if( feof(fp) )
            break;                    
        //fwrite( "buf = %s",sizeof(buf),1,fp );
        printf( "buf = %s", buf );
        spit( buf );
    }    
    fclose(fp);
    return 0;
}

//建立双向循环链表                                                     
void create_d_list ( struct d_list **headp, int *p )                                                                                                                                                                  
{                                                               
   struct d_list *head = NULL,*tail;                                 
   if( p[0] == 0 )                                                     
   *headp = NULL;                                                
   else
   {                                                           
      head = ( struct d_list * ) malloc ( sizeof( struct d_list ) );            
      head->data = *p++;                                                
      tail = head;                                                   
      while( *p )
      {                                                      
         tail->next = ( struct d_list * ) malloc ( sizeof ( struct d_list ) );      
         tail->next->prior = tail;                                         
         tail = tail->next;                                                
         tail->data = *p++;                                                
      }                                                               
      tail->next = head;                                                
      head->prior = tail;                                               
   }                                                               
   *headp = head;              
   printf( "%s %d\n", __FILE__, __LINE__ );                                       
}
                                                     
//双向循环链表降序排列                                                                                             
void sort_d_list( struct d_list **headp1 )                        
{                                                               
   struct d_list *p, *q;                                            
   int i,j,k,n = 0;                                                  
   p = *headp1;                                       
   while( p != (*headp1)->prior )
   {                                    
      n++;                                                            
      p = p->next;                                                     
   }                                                              
   for( i = 0, p = *headp1; i < n; p = p->next, i++ )                           
   for( j = i, q = p->next; j < n; q = q->next, j++ )                           
   if( (p->data) < (q->data) )
   {                                       
      k = q->data;                                                      
      q->data = p->data;                                                
      p->data = k;                                                      
   }                                                           
}
 
//遍历双向循环链表 写文件                                   
void disp_d_list ( struct d_list *p1 )                             
{                                                               
   struct d_list *p = p1;
   char buffer[50];
   int i = 1;
   FILE *stream;  
   if( ( stream =fopen("file1","w+") ) == NULL )
     {
        printf( "cannot open the file\n" );
      return;
   }   
   do{ memset( buffer, 0x00, sizeof(buffer) );
          sprintf( buffer, "%-8d", p->data );
       fwrite( buffer, strlen(buffer), 1, stream );     
      /* if( (i % 4) == 0 )
       {
            printf("i = %d\n", i);
       }*/
       if( ((i % 6) == 0) )
            fwrite( "\n", 1, 1, stream );      
       i++;
       //printf( "buf = %8s ", buffer );
       p = p->next;                                         
    }while( p != p1 );  
    //while( p->next != p1 );  
   write_buf( stream, buffer, strlen(buffer) );
   fclose( stream );
   return;                                       
}  

//写文件                  
int write_buf( char *filename, unsigned char *buf, int len )
{
     int ret;
     char string[] = "This is a string";
     char msg[20];
   FILE *fp;      
   if( filename == NULL || buf == NULL )
      return -1;
   if((fp = fopen("filename","w+")) == NULL )
   {
      printf( "cannot open the file\n" );
      return -2;     
   }      
   ret = fwrite( dlist, sizeof( struct d_list ), 1, fp );
   if( ret != 1 )
         printf( "file write error\n" );          
   //printf( "%s %d\n", __FILE__, __LINE__ );     
   fread( string, strlen(string), 1, fp );   
   fseek( fp, 0, SEEK_SET );   
   fgets( msg, strlen(string)+1, fp );   
   fputs( "This is a test\n", fp );   
   fprintf( fp, "%s\n", msg );
   printf( "%s\n", msg );
   fclose( fp );
   return ret;
}                                                                                                   

//解析内容
int spit( unsigned char *buf )
{    
     //char string[] = "This is a string";
   char *p,*q = ",";
   p = strtok( buf, q );  
   while( p != NULL )
   {
         printf( "%s\n", p );
         p = strtok( NULL, q );
   }   
   return 0;
}

int main( int argc, char *argv[] )
{                  
   struct d_list *head;                                       
   //int a[] = {100,5,3,99,2,7,6,9,8,10,11,70,14,13,12,16,999,1000,0};         
   Get_File_Content( "num" );
   create_d_list(&head,"num" );                                                                                                                                                                                         
   sort_d_list( &head );                              
   disp_d_list( head );                              
   return 0;               
}
2008-08-07 08:45
快速回复:只能输出数字
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.052906 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved