| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 674 人关注过本帖
标题:很多问题应该 二级指针啊等
只看楼主 加入收藏
烟雾中的迷茫
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:621
专家分:1069
注 册:2011-2-9
结帖率:100%
收藏
已结贴  问题点数:100 回复次数:18 
很多问题应该 二级指针啊等
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(int argc,char *argv[])
{
    unsigned int num;                             // num up to 100000
    int i,j,n,mark=0,k,len;                               // 各标记
    char **ch=NULL,*pstr=NULL;                               // 字符指针存放指针

    printf("请输入电话号码数!\n");
    scanf("%d\n",&num);

    for(i=0;i<num;i++)
    {
        *(ch+i)=(char*)malloc(20*sizeof(char));     // 动态分配内存
        gets(*(ch+i));

     // 处理号码中的大写字母
        len=strlen(ch[i])-1;
        for(j=0;j<=len;j++)
        {
            n=ch[i][j];
            switch(n)
            {
            case 65: case 66: case 67:
                ch[i][j]='2';break;
            case 68: case 69: case 70:
                ch[i][j]='3';break;
            case 71: case 72: case 73:
                ch[i][j]='4';break;
            case 74: case 75: case 76:
                ch[i][j]='5';break;
            case 77: case 78: case 79:
                ch[i][j]='6';break;
            case 80: case 82: case 83:
                ch[i][j]='7';break;
            case 84: case 85: case 86:
                ch[i][j]='8';break;
            case 87: case 88: case 89:
                ch[i][j]='9';break;
            default:  break;
            
        }
        // 除去字符串中的'-'
        for(j=0;j<=len;j++)
            if(ch[i][j]=='-')
                for(k=j+1;k<=len;k++)
                    ch[i][k-1]=ch[i][k];
        // 插入连接符
        for(j=len;j>=4;j--)
            ch[i][j+1]=ch[i][j];
        ch[i][j]='-';
        ch[i][len+1]='\0';
    }
        // 按照字典中字母顺序输出
        for(i=0;i<num;i++)
        {
            pstr=ch[i];
            for(j=i;j<num;j++)
            {
                if(strcmp(ch[i],ch[j+1])<0)
                    pstr=ch[j+1];
                if(strcmp(ch[i],ch[j+1])==0)
                    mark++;
            }

            if(mark==1)
                printf("No duplicates");
            else
                printf("%s  %d\n",ch[i]);
            mark=0;
        }
        return 0;
}


上段代码运行输入后回崩溃题目是http://
 里面有很大问题应该  我感觉理解错意思了 请大侠指教
献上第一个百分 呵呵
搜索更多相关主题的帖子: 问题 
2011-09-06 18:11
czsbc
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:469
专家分:1700
注 册:2008-12-13
收藏
得分:20 
找到一个小问题:
ch 没有给它分配内存呀
ch=(char **)malloc(num*sizeof(char*));
2011-09-06 18:41
czsbc
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:469
专家分:1700
注 册:2008-12-13
收藏
得分:20 
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(int argc,char *argv[])
{
    unsigned int num;                             // num up to 100000
    int i,j,n,mark=0,k,len;                               // 各标记
    char **ch=NULL,*pstr=NULL;                               // 字符指针存放指针

    printf("请输入电话号码数!\n");
    scanf("%d\n",&num);
    ch=(char **)malloc(num*sizeof(char*));
    for(i=0;i<num;i++)
    {
        *(ch+i)=(char*)malloc(20*sizeof(char));     // 动态分配内存
        gets(*(ch+i));

     // 处理号码中的大写字母
        len=strlen(ch[i]);
        for(j=0;j<len;j++)
        {
            n=ch[i][j];
            switch(n)
            {
            case 65: case 66: case 67:
                ch[i][j]='2';break;
            case 68: case 69: case 70:
                ch[i][j]='3';break;
            case 71: case 72: case 73:
                ch[i][j]='4';break;
            case 74: case 75: case 76:
                ch[i][j]='5';break;
            case 77: case 78: case 79:
                ch[i][j]='6';break;
            case 80: case 82: case 83:
                ch[i][j]='7';break;
            case 84: case 85: case 86:
                ch[i][j]='8';break;
            case 87: case 88: case 89:
                ch[i][j]='9';break;
            default:  break;
           
            }
        }
        // 除去字符串中的'-'
        for(j=0;j<len;j++)
        {
            if(ch[i][j]=='-')
            {
                for(k=j+1;k<len;k++)
                {
                    ch[i][k-1]=ch[i][k];
                }
            }
        }

        // 插入连接符
        for(j=len;j>3;j--)
            ch[i][j]=ch[i][j-1];
        ch[i][j]='-';
        ch[i][len+1]='\0';
    }
        // 按照字典中字母顺序输出
        for(i=0;i<num-1;i++)
        {
            pstr=ch[i];
            for(j=i;j<num-1;j++)
            {
                if(strcmp(ch[i],ch[j+1])<0)
                    pstr=ch[j+1];
                if(strcmp(ch[i],ch[j+1])==0)
                    mark++;
            }

            if(mark==1)
                printf("No duplicates");
            else
                printf("%s  %d\n",ch[i]);
            mark=0;
        }
        return 0;
}
暂时让它运行起来了,至于符不符合题意就不知道了,题目是一大段的英文,难得看呀
2011-09-06 19:19
烟雾中的迷茫
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:621
专家分:1069
注 册:2011-2-9
收藏
得分:0 
呵呵 不错
2011-09-06 21:27
烟雾中的迷茫
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:621
专家分:1069
注 册:2011-2-9
收藏
得分:0 
不过  num是指输入的电话号码个数 而不是输入的数据长度
2011-09-06 21:41
pauljames
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:千里冰封
威 望:9
帖 子:1555
专家分:10000
注 册:2011-5-8
收藏
得分:10 
printf("%s  %d\n",ch[i]);,这个参数个数不匹配
最后少了一个}

经常不在线不能及时回复短消息,如有c/单片机/运动控制/数据采集等方面的项目难题可加qq1921826084。
2011-09-06 21:57
waterstar
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:5
帖 子:984
专家分:2810
注 册:2010-2-12
收藏
得分:10 
为啥电话号码里会有65以上这么高的值出来啊

冰冻三尺,非一日之寒;士别三日,不足刮目相看!
2011-09-06 21:58
pauljames
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:千里冰封
威 望:9
帖 子:1555
专家分:10000
注 册:2011-5-8
收藏
得分:10 
用splint检测后结果如下,供参考
Splint 3.1.2 --- 03 May 2009
test.c: (in function main)
test.c:12:18: Format argument 1 to scanf (%d) expects int * gets unsigned int
                 *: &num
  To ignore signs in type comparisons use +ignoresigns
   test.c:12:13: Corresponding format code
test.c:12:5: Return value (type int) ignored: scanf("%d\n", &num)
  Result returned by function call is not used. If this is intended, can cast
  result to (void) to eliminate message. (Use -retvalint to inhibit warning)
test.c:14:13: Operands of < have incompatible types (int, unsigned int):
                 i < num
test.c:17:9: Use of gets leads to a buffer overflow vulnerability.  Use fgets
                instead: gets
  Use of function that may lead to buffer overflow. (Use -bufferoverflowhigh to
  inhibit warning)
test.c:17:9: Return value (type char *) ignored: gets(*(ch + i))
  Result returned by function call is not used. If this is intended, can cast
  result to (void) to eliminate message. (Use -retvalother to inhibit warning)
test.c:20:20: Index of null pointer ch: ch
  A possibly null pointer is dereferenced.  Value is either the result of a
  function which may return null (in which case, code should check it is not
  null), or a global, parameter or structure field declared with the null
  qualifier. (Use -nullderef to inhibit warning)
   test.c:9:15: Storage ch becomes null
test.c:20:9: Assignment of arbitrary unsigned integral type to int:
                len = strlen(ch[i]) - 1
  To allow arbitrary integral types to match any integral type, use
  +matchanyintegral.
test.c:23:13: Assignment of char to int: n = ch[i][j]
  To make char and int types equivalent, use +charint.
test.c:57:17: Operands of < have incompatible types (int, unsigned int):
                 i < num
test.c:60:21: Operands of < have incompatible types (int, unsigned int):
                 j < num
test.c:71:17: No argument corresponding to printf format code 2 (%d):
                 "%s  %d\n"
  Types are incompatible. (Use -type to inhibit warning)
   test.c:71:30: Corresponding format code
test.c:76:2: Path with no return in function declared to return int
  There is a path through a function declared to return a value on which there
  is no return statement. This means the execution may fall through without
  returning a meaningful result to the caller. (Use -noret to inhibit warning)
test.c:5:14: Parameter argc not used
  A function parameter is not used in the body of the function. If the argument
  is needed for type compatibility or future plans, use /*@unused@*/ in the
  argument declaration. (Use -paramuse to inhibit warning)
test.c:5:25: Parameter argv not used
Finished checking --- 14 code warnings

经常不在线不能及时回复短消息,如有c/单片机/运动控制/数据采集等方面的项目难题可加qq1921826084。
2011-09-06 21:59
小偌
Rank: 4
来 自:成都
等 级:业余侠客
帖 子:170
专家分:241
注 册:2011-8-15
收藏
得分:10 
.....看不懂啊~

不是很好么..比起关在笼子里的可怜小鸟..我成为乌鸦已足矣
2011-09-06 22:28
烟雾中的迷茫
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:621
专家分:1069
注 册:2011-2-9
收藏
得分:0 
回复 7楼 waterstar
看题目就知道了  他好像是为了将相应字符映射成数字
2011-09-06 22:43
快速回复:很多问题应该 二级指针啊等
数据加载中...
 
   



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

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