各位高手帮帮忙吧,高分悬赏(50分)

direren 2009-12-13 11:29:11
用户输入一字符串(里面可以有数字,字母,和特殊符号)
要求把这一字符串分成(数字,字母,特殊符号)这三组输出来,而且每组中都必须按顺序输出
例子
1qaz2!wsx$#&* ==> 12 qazwsx !$#&*

条件:1.不能用类和继承
2. 不能用循环


各位大哥大姐,帮帮忙吧。

如果回答的好,我在加分!!!




...全文
172 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
pexwin 2010-01-07
  • 打赏
  • 举报
回复
果真牛人啊
120680451 2009-12-13
  • 打赏
  • 举报
回复
是否是这结果:

please input the string:
1qaz2!wsx$#&*
12 aqswxz !#$&*
Press any key to continue
120680451 2009-12-13
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 direren 的回复:]
你这个程序没有排序呀,只是分类输出了。。怎么排序呢
[/Quote]
加个order函数即可:

#include <stdio.h>
#include <string.h>
#define MAXLENGTH 255
#define NUMBER 0
#define LETTER 1
#define SPECIAL 2

char string [3][MAXLENGTH] ;

void order (char *) ;
void judge (int, char *) ;

void main ()
{
char str[MAXLENGTH] ;
printf ("please input the string:\n") ;
scanf ("%s", str) ;
if (str[0] >= '0' && str[0] <= '9')
judge (NUMBER, str) ;
else if (str[0] >= 'a' && str[0] <= 'z'
|| str[0] >= 'A' && str[0] <= 'Z')
judge (LETTER, str) ;
else judge (SPECIAL, str) ;
printf ("%s %s %s\n", string[0], string[1], string[2]) ;
}

void judge (int sort, char *str)
{
static int num = 0 ;
static int lett = 0 ;
static int spec = 0 ;
static int location = 0 ;

if (NUMBER == sort)
string [sort][num ++] = str[location++] ;
else if (LETTER == sort)
string [sort][lett++] = str[location++] ;
else
string [sort][spec++] = str[location++] ;
order (string [sort]) ;

if (!str[location]) return ;
else if (str[location] >= '0' && str[location] <= '9')
judge (NUMBER, str) ;
else if (str[location] >= 'a' && str[location] <= 'z'
|| str[location] >= 'A' && str[location] <= 'Z')
judge (LETTER, str) ;
else judge (SPECIAL, str) ;
}

void order (char *string)
{
int len = strlen (string) ;
for (int i = 0; i < len - 1; i++)
if (string[len - 1] < string[i])
{
char temp = string[len - 1] ;
for (int j = len - 1; j > i; j--)
string[j] = string[j-1] ;
string[i] = temp ;
}
}
mstlq 2009-12-13
  • 打赏
  • 举报
回复
完全不懂楼主所说的“排序”是什么概念……
再给楼主一个无聊的goto版本……
输出是
1qaz2!wsx$#&* ==> 12 qazwsx !$#&*


#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

char estr[3][256];
int inds[3];

int main()
{
char *str="1qaz2!wsx$#&*";
char *src = str;
int ind;

loop:
if(isdigit(*src)) ind=0;
else if(isalpha(*src)) ind=1;
else ind=2;
estr[ind][inds[ind]++] = *src++;
if(*src != '\0') goto loop;

printf("%s ==> %s %s %s",str,estr[0],estr[1],estr[2]);
return 0;
};

direren 2009-12-13
  • 打赏
  • 举报
回复
你这个程序没有排序呀,只是分类输出了。。怎么排序呢
mstlq 2009-12-13
  • 打赏
  • 举报
回复
如果直接用全局数组,那就是

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

char num[256];
char alp[256];
char otr[256];

void extract(const char* str,int np, int ap, int op)
{
if (*str=='\0') return;
if (isdigit(*str))
*(num+np++) = *str++;
else if(isalpha(*str))
*(alp+ap++) = *str++;
else
*(otr+op++) = *str++;
return extract(str,np,ap,op);
}


int main()
{
char *str="1qaz2!wsx$#&*";
extract(str,0,0,0);
puts(num);
puts(alp);
puts(otr);
return 0;
};
mstlq 2009-12-13
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

char *num;
char *alp;
char *otr;
void init();
void finish();
void shows();

void extract(const char* str,int np, int ap, int op)
{
if (*str=='\0') return;
if (isdigit(*str))
*(num+np++) = *str++;
else if(isalpha(*str))
*(alp+ap++) = *str++;
else
*(otr+op++) = *str++;
return extract(str,np,ap,op);
}


int main()
{
char *str="1qaz2!wsx$#&*";
init();
extract(str,0,0,0);
shows();
finish();
return 0;
}

void init()
{
num = calloc(256,sizeof(char));
alp = calloc(256,sizeof(char));
otr = calloc(256,sizeof(char));
};

void finish()
{
free(num);
free(alp);
free(otr);
};

void shows()
{
puts(num);
puts(alp);
puts(otr);
};
120680451 2009-12-13
  • 打赏
  • 举报
回复
合并了三个函数后:

#include <stdio.h>
#include <string.h>
#define MAXLENGTH 255
#define NUMBER 0
#define LETTER 1
#define SPECIAL 2

char string [3][MAXLENGTH] ;

void judge (int, char *) ;

void main ()
{
char str[MAXLENGTH] ;
printf ("please input the string:\n") ;
scanf ("%s", str) ;
if (str[0] >= '0' && str[0] <= '9')
judge (NUMBER, str) ;
else if (str[0] >= 'a' && str[0] <= 'z'
|| str[0] >= 'A' && str[0] <= 'Z')
judge (LETTER, str) ;
else judge (SPECIAL, str) ;
printf ("%s %s %s\n", string[0], string[1], string[2]) ;
}

void judge (int sort, char *str)
{
static int num = 0 ;
static int lett = 0 ;
static int spec = 0 ;
static int location = 0 ;

if (NUMBER == sort)
string [sort][num ++] = str[location++] ;
else if (LETTER == sort)
string [sort][lett++] = str[location++] ;
else
string [sort][spec++] = str[location++] ;

if (!str[location]) return ;
else if (str[location] >= '0' && str[location] <= '9')
judge (NUMBER, str) ;
else if (str[location] >= 'a' && str[location] <= 'z'
|| str[location] >= 'A' && str[location] <= 'Z')
judge (LETTER, str) ;
else judge (SPECIAL, str) ;
}
120680451 2009-12-13
  • 打赏
  • 举报
回复
这是结果:

please input the string:
1qaz2!wsx$#&*
12 qazwsx !$#&*
Press any key to continue


这是代码:

#include <stdio.h>
#include <string.h>
#define MAXLENGTH 255

char numberstr [MAXLENGTH] ;
char letterstr [MAXLENGTH] ;
char specialstr[MAXLENGTH] ;

void number (char *, int) ;
void letter (char *, int) ;
void special (char *, int) ;

void main ()
{
char str[MAXLENGTH] ;
printf ("please input the string:\n") ;
scanf ("%s", str) ;
if (str[0] >= '0' && str[0] <= '9')
number (str, 0) ;
else if (str[0] >= 'a' && str[0] <= 'z'
|| str[0] >= 'A' && str[0] <= 'Z')
letter (str, 0) ;
else special (str, 0) ;
printf ("%s %s %s\n", numberstr, letterstr, specialstr) ;
}

void number (char *str, int location)
{
static int i = 0 ;
numberstr[i++] = str[location++] ;
if (!str[location]) return ;
else if (str[location] >= '0' && str[location] <= '9')
number (str, location) ;
else if (str[location] >= 'a' && str[location] <= 'z'
|| str[location] >= 'A' && str[location] <= 'Z')
letter (str, location) ;
else special (str, location) ;
}

void letter (char *str, int location)
{
static int i = 0 ;
letterstr[i++] = str[location++] ;
if (!str[location]) return ;
else if (str[location] >= '0' && str[location] <= '9')
number (str, location) ;
else if (str[location] >= 'a' && str[location] <= 'z'
|| str[location] >= 'A' && str[location] <= 'Z')
letter (str, location) ;
else special (str, location) ;
}

void special (char *str, int location)
{
static int i = 0 ;
specialstr[i++] = str[location++] ;
if (!str[location]) return ;
else if (str[location] >= '0' && str[location] <= '9')
number (str, location) ;
else if (str[location] >= 'a' && str[location] <= 'z'
|| str[location] >= 'A' && str[location] <= 'Z')
letter (str, location) ;
else special (str, location) ;
}
direren 2009-12-13
  • 打赏
  • 举报
回复
不知道呀,之前只自学过,老师讲的都是概念型的课,很无奈。。。。
alan001 2009-12-13
  • 打赏
  • 举报
回复
不用循环?用goto?
direren 2009-12-13
  • 打赏
  • 举报
回复
谢谢,如果会就交交我吧,我一直在这等着,期待有结果。
direren 2009-12-13
  • 打赏
  • 举报
回复
这是我的一点想法( 但是还不懂,不用循环怎样把字符串存入, 怎么样输出,和怎么样排序)
int lex(){
getchar();
switch (charClass){
case Letter:
addChar();
getChar();
While(charClass==Letter)
{ addChar();
getChar();
}
return lookup(lexeme);
break;}
}
mstlq 2009-12-13
  • 打赏
  • 举报
回复
用递归咯……
虽然本质上是循环,不过可以不出现循环语句……

吃饭去,吃完饭还没人弄,我弄……
kouwenlong 2009-12-13
  • 打赏
  • 举报
回复
2. 不能用循环
那怎么遍历字符串啊?
direren 2009-12-13
  • 打赏
  • 举报
回复
题目是对的,我只是想在了解一下要是排序会怎么做而已。
jernymy 2009-12-13
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 mstlq 的回复:]
排序还是用快排吧……
不然的话又出现for循环了……

此外,楼主1楼给的例子根本就没排序……
[/Quote]

支持
mstlq 2009-12-13
  • 打赏
  • 举报
回复
排序还是用快排吧……
不然的话又出现for循环了……

此外,楼主1楼给的例子根本就没排序……
direren 2009-12-13
  • 打赏
  • 举报
回复
谢谢beanjoy~~~~十分感谢。。。。
direren 2009-12-13
  • 打赏
  • 举报
回复
谢谢大家,尤其是beanJoy的帮助。。

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧