鱼C论坛

 找回密码
 立即注册
查看: 5143|回复: 19

C语言字符串的问题,我学晕了! 希望版主以及高手解答!

[复制链接]
发表于 2012-4-9 17:38:08 | 显示全部楼层 |阅读模式
3鱼币
编写一个程序,读取输入,直到读取10个字符串或者遇到EOF,二者中最先满足的那个终止读取过程。
然后将读取的字符串按照ASCII码表顺序输出,并且这项功能在单独的函数中进行。


说明下我自己的理解吧:
读取输入10个字符串 我觉得可以这样用

  1. char a[10][100];
  2. int i;
  3. for(i = 0;i < 10 && gets(a[i]) && a[i][0] != EOF;i++){}
复制代码

然后,读取的字符串按照ASCII码表顺序输出应该是用  strcmp判断,然后交换位置,这里类似于冒泡,
可是我真的不知道字符串该怎么交换位置啊!  应该是交换指针的指向,可是我真的做不出来的。求高手指教!
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-4-9 17:38:09 | 显示全部楼层
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #define MAX_STRINGS 10
  5. #define MAX_CHARS 100

  6. //这是你要的函数
  7. void string_test(void);

  8. int main(void){
  9.         string_test();
  10. }

  11. struct StringGroup{
  12.         //存放输入的字符串
  13.         char* str_vec[MAX_STRINGS];
  14.         //已经输入的字符串
  15.         int numbers;
  16. };

  17. void init( struct StringGroup* str_group );
  18. void destroy( struct StringGroup* str_group );
  19. void input( struct StringGroup* str_group );
  20. void output( struct StringGroup* str_group );
  21. void sort( struct StringGroup* str_group );
  22. void swap( char** pstra, char** pstrb );

  23. void string_test(void) {
  24.         struct StringGroup sg;
  25.         init( &sg );
  26.         input( &sg );
  27.         sort( &sg );
  28.         output( &sg );
  29.         destroy( &sg );
  30. }

  31. void init( struct StringGroup* str_group ) {
  32.         str_group->numbers=0;
  33. }

  34. void destroy( struct StringGroup* str_group ) {
  35.         int i;
  36.         for( i=0; i<str_group->numbers; ++i )
  37.                 free( str_group->str_vec[i] );
  38. }

  39. void input( struct StringGroup* str_group ) {
  40.         char tmp[MAX_CHARS];
  41.         while( str_group->numbers!=MAX_STRINGS && scanf("%s",tmp)!=EOF ) {
  42.                 str_group->str_vec[str_group->numbers] = (char*)malloc( MAX_CHARS );
  43.                 strcpy( str_group->str_vec[str_group->numbers], tmp );
  44.                 ++str_group->numbers;
  45.         }
  46. }

  47. void output( struct StringGroup* str_group ) {
  48.         int i;
  49.         for( i=0; i<str_group->numbers; ++i ){
  50.                 printf("%d:\t%s\n",i,str_group->str_vec[i]);
  51.         }
  52. }

  53. void sort( struct StringGroup* str_group ) {
  54.         int i,j;
  55.         for( i=0;i<str_group->numbers;++i )
  56.                 for( j=i+1;j<str_group->numbers;++j )
  57.                         if( strcmp( str_group->str_vec[i], str_group->str_vec[j] )>0 )
  58.                                 swap( &str_group->str_vec[i], &str_group->str_vec[j] );
  59. }

  60. void swap( char** pstra, char** pstrb ) {
  61.         char* tmp;
  62.         tmp = *pstra;
  63.         *pstra = *pstrb;
  64.         *pstrb = tmp;
  65. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-4-9 17:39:28 | 显示全部楼层
希望高手解答呢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-4-9 18:55:23 | 显示全部楼层
这里提供下思路,主函数的输入要统计总共输入了多少字符串。定义的这个单独的函数有两个参数,一个是这字符串数组,一个是字符串个数,在这个单独的函数中定义一个指针数组,长度10(也可以用malloc根据传入的参数决定数组长度,但是个人觉得浪费程序的效率,malloc语句本身也占用了内存,也节约不了多少,malloc本身执行也要时间),接下来关于字符串数组的交换,用两个参数,一是开始的字符串是第几个,二是结束的字符串是第几个,第一次排序所有数组,然后取前N个第一个ascii相同的字符串返回上一步,再次排序,对于排序结果,取前N个第一个ascii相同的字符串返回上一步,接下来的所有排序方法以此类推
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-4-9 20:10:53 | 显示全部楼层
本帖最后由 湮汐 于 2012-4-9 20:12 编辑

哇!  光老师你太强了! 我就想到 先把字符串大小的顺序先整理出来,记下下标顺序放在 a【i】中,然后  string [a【i】],用这样方式输出,刚刚也在电脑上调了半天的。
但是看到你的程序,我甘拜下风啊!  好专业啊!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-4-9 20:27:51 | 显示全部楼层
扎实的 数据结构 的知识
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-4-9 20:49:08 | 显示全部楼层
呵呵,没有什么新意,就是基本功而已。用C++的话这题还能写出些令人激动的代码。谁有兴趣用C++试试。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-4-9 21:01:23 | 显示全部楼层
仰望天上的光 发表于 2012-4-9 20:49
呵呵,没有什么新意,就是基本功而已。用C++的话这题还能写出些令人激动的代码。谁有兴趣用C++试试。

看光老师的这个程序我才知道自己的差距还有多大!
相对我用自己的思维编出来的,有点投机取巧的性质。
其实,我有点看不懂光老师的这个程序,而且看的很吃力!应该是自己的基本功还不扎实吧!还要继续学习呢!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

头像被屏蔽
发表于 2012-4-9 21:50:13 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-4-9 22:50:49 | 显示全部楼层
前面的C程序显然有许多问题,比如某个字符串特别长就会有问题,还有9楼说的也是一个缺陷。这些缺陷的主要原因是C语言的模型过于低级。作为对比,可以看一下,用C++标准库来实现同样的功能代码是如何的:
  1. #include <string>
  2. #include <set>
  3. #include <iostream>
  4. #include <algorithm>
  5. //#include <iterator>
  6. using namespace std;

  7. //这是你要的函数
  8. void string_test();
  9. int main(){
  10.         string_test();
  11. }

  12. void string_test() {
  13.         const int max_times = 10;
  14.         multiset<string> strs;
  15.         string tmp;
  16.         for( int i=0; i!=max_times && cin>>tmp; ++i )
  17.                 strs.insert(tmp);
  18.         copy(strs.begin(), strs.end(), ostream_iterator<string>(cout,"\n"));
  19. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-4-9 23:57:16 | 显示全部楼层
这么长的程序也能写,强人啊
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-4-10 00:27:41 | 显示全部楼层
为你那 76行 代码 ,回复下
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-4-10 01:06:56 | 显示全部楼层
wa... I am newbie.
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-4-10 08:33:20 | 显示全部楼层
挺难的 我都看不明白哦  呵呵
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-4-10 16:55:50 | 显示全部楼层
本帖最后由 haye123 于 2012-4-10 16:58 编辑

我用了差不多1小时半写的.
半年没有写过,可能有些地方会出错.
不知道能不能帮到你!:L

  1. #include <stdio.h>

  2. #include <malloc.h>

  3. #include <stdlib.h>

  4. #define STRING_SIZE 100
  5. #define SM_SIZE sizeof(struct StringMember)

  6. struct StringMember
  7. {
  8.       char szString[STRING_SIZE];
  9.       int dwNumber;
  10.       struct StringMember * lpNext;
  11. };

  12. typedef struct StringMember stSM;

  13. stSM * Input(void);
  14. void Arrangement(char * lpString);
  15. stSM * Output(stSM * head);
  16. stSM * SaveFile(stSM * head);
  17. void ReleaseMemory(stSM * head);

  18. void main()

  19. {
  20.       ReleaseMemory(SaveFile(Output(Input())));
  21.       system("pause");
  22. }

  23. stSM * Input(void)

  24. {
  25.       int i=0;
  26.       stSM * head,* p1,* p2;
  27.       printf("输入若干个字符串(结束输入EOF,回车键确定).按照ASCII码表顺序输出.\n");
  28.       printf("提醒:字符串长度 <= %d字节,输入一次EOF没有结束,请再次输入EOF.\n\n",STRING_SIZE);
  29.       head = p1 = p2 = (stSM *) malloc (SM_SIZE);
  30.       printf("第 %d 个字符串: ",i+1);
  31.       while(scanf("%s",p1->szString) != EOF)
  32.       {
  33.             p2=p1;
  34.             p1->dwNumber=++i;
  35.             Arrangement(p1->szString);
  36.             p1=(stSM *) malloc (SM_SIZE);
  37.             p2->lpNext=p1;
  38.             printf("\n第 %d 个字符串: ",i+1);
  39.       }
  40.       if(!i)
  41.       {
  42.             printf("\n@_@:没有发现1个(或以上)的字符串,程序强制结束!\n\n");
  43.             free(head);
  44.             exit(0);
  45.       }
  46.       p2->lpNext=NULL;
  47.       return(head);
  48. }

  49. stSM * Output(stSM * head)
  50. {
  51.       stSM * p1;
  52.       p1=head;
  53.       printf("\n\n按照ASCII码表顺序如下:\n");
  54.       while(p1)
  55.       {
  56.             printf("\n\n排列后字符串 %d 如下:\n",p1->dwNumber);
  57.             printf("%s",p1->szString);
  58.             p1=p1->lpNext;
  59.       }
  60.       fputc(10,stdout);
  61.       fputc(10,stdout);

  62.       return(head);
  63. }

  64. stSM * SaveFile(stSM * head)
  65. {
  66.       FILE * fp;
  67.       stSM * p1;
  68.       char temp,FileName[STRING_SIZE];
  69.       printf("\n\n是否把已排序的字符串保存在文件?\n(Y/y or N/n): ");
  70. SSS:  temp=getchar();
  71.       if(temp == 'n' || temp == 'N')
  72.       {
  73.             return(head);
  74.       }
  75.       else if(!(temp == 'y' || temp == 'Y'))
  76.       {
  77.             printf("输入错误:(Y或y为需要保存到文件中,N或n则不保存!,请重新输入!\n\n(Y/y or N/n):");
  78.             getchar();
  79.             goto SSS;
  80.       }
  81.       printf("\n\n输入文件名字(例如:Name.txt):");
  82.       scanf("%s",FileName);
  83.       if(!(fp=fopen(FileName,"w+")))
  84.       {
  85.             printf("\n\n无法创建 %s 该文件,程序自动退出!\n",FileName);
  86.             exit(0);
  87.       }
  88.       p1=head;
  89.       while(p1)
  90.       {
  91.             fprintf(fp,"第 %d 次字符串处理后结果:",p1->dwNumber);
  92.             fputs(p1->szString,fp);
  93.             fputc(10,fp);
  94.             fputc(10,fp);
  95.             p1=p1->lpNext;
  96.       }
  97.       fclose(fp);
  98.       printf("\n\n温馨提醒:以上排序后的字符串内容已经保存在 %s 文件中,\n请在程序当前目录查找!\n\n",
  99.             FileName);
  100.       return(head);
  101. }

  102. void ReleaseMemory(stSM * head)
  103. {
  104.       stSM * p1;
  105.       printf("\n程序正在退出,请稍等....-_- .zZ\n草..程序太啰嗦了\n\n");
  106.       while(head)
  107.       {
  108.             p1=head->lpNext;
  109.             free(head);
  110.             head=p1;
  111.       }
  112.       return;
  113. }

  114. void Arrangement(char * lpString)
  115. {

  116.       char temp;
  117.       int i,j;
  118.       for(i=0;* (lpString+i) !=0 ;i++)
  119.       {
  120.             for(j=1;* (lpString+j) != 0;j++)
  121.             {
  122.                   if(*(lpString+j-1) > * (lpString+j) )
  123.                   {
  124.                         temp = * (lpString+j-1);
  125.                         * (lpString+j-1) = * (lpString+j);
  126.                         * (lpString+j) = temp;
  127.                   }
  128.             }
  129.       }
  130.       return;
  131. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-4-10 22:08:23 | 显示全部楼层
来学习的.....
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-4-10 22:33:28 | 显示全部楼层
strlen得出字符串长度,再逐个比较?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-4-12 20:38:29 | 显示全部楼层
好长攸    :L
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-4-12 20:48:38 | 显示全部楼层
作为新手 膜拜了!!~
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-9-27 16:12:53 | 显示全部楼层
呵呵,新人求支持
呵呵,新人求支持
呵呵,新人求支持
呵呵,新人求支持
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-6 12:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表