鱼C论坛

 找回密码
 立即注册
查看: 1806|回复: 6

调用函数后,得到结果不对

[复制链接]
发表于 2015-3-29 09:48:55 | 显示全部楼层 |阅读模式
20鱼币
本帖最后由 无情修罗 于 2015-3-29 15:56 编辑
  1. #include <stdio.h>
  2. #include <string.h>

  3. /* 本部分代码功能建议:函数原型声明 */
  4. /* User Code Begin(Limit: lines<=2, lineLen<=80, 考生可在本行后添加代码、最多2行、行长<=80字符) */
  5. int input(char *from[],char (*p)[9]);
  6. void find(char *from[],int n,int *findmax);
  7. /* User Code End(考生添加代码结束。注意:空行和单独为一行的{与}均不计行数、行长不计行首tab缩进) */

  8. int main(void)
  9. {
  10.         char *pStr[50], str[50][9];
  11.         int Count=0, max;
  12.         
  13.         printf("****Input strings****\n");
  14.         Count = input(pStr, str);

  15.         printf("\nmax = ");
  16.         find(pStr, Count, &max);
  17.         printf("%s\n", pStr[max]);

  18.         return 0;
  19. }

  20. /* User Code Begin:考生在此后完成自定义函数的设计,行数不限 */
  21. int input(char *from[],char (*p)[9])
  22. {
  23.         int i,j=0;
  24.         char *temp;
  25.         
  26.         for(i=0;i<50;i++)
  27.         {
  28.                 gets(*(p+i));

  29.                 if(strcmp(*(p+i),"*End*")==0)
  30.                 {
  31.                         break;
  32.                 }
  33.                 else
  34.                 {
  35.                         from[j++] = *(p+i);
  36.                
  37.                 }
  38.                
  39.         
  40.          
  41.         }
  42.         
  43.         return j;
  44. }

  45. void find(char *from[],int n,int *findmax)
  46. {
  47.         
  48.         int i,j;
  49.         i=j=0;
  50.         char *temp;
  51.         *temp = *from[i];
  52.         findmax = &j;
  53.         for(i=1;i<n;i++)
  54.         {
  55.                 if(strcmp(temp,from[i])<0)
  56.                
  57.                 {
  58.                         temp = from[i];
  59.                         findmax=&i;
  60.                 }
  61.                
  62.         }

  63.         *findmax = *findmax -1;
  64.         printf("%d\n",*findmax);
  65.         
  66.         
  67. }
复制代码
我想问一下为什么调用find函数后,max的值与函数中最后的findmax的值不一样

最佳答案

查看完整内容

改了下,可以运行了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-3-29 09:48:56 | 显示全部楼层
本帖最后由 haiouda 于 2015-3-30 10:42 编辑
  1. #include <stdio.h>
  2. #include <string.h>

  3. /* 本部分代码功能建议:函数原型声明 */
  4. /* User Code Begin(Limit: lines<=2, lineLen<=80, 考生可在本行后添加代码、最多2行、行长<=80字符) */

  5. int max=0;
  6. int input(char *from[],char p[50][9]);
  7. void find(char *from[],int n,int *findmax);
  8. /* User Code End(考生添加代码结束。注意:空行和单独为一行的{与}均不计行数、行长不计行首tab缩进) */



  9. int main(void)
  10. {
  11.         char *pStr[50], str[50][9];
  12.         int Count=0, max;
  13.         
  14.         printf("****Input strings****\n");
  15.         Count = input(pStr, str);
  16.         
  17.         printf("\nmax = ");
  18.         find(pStr, Count, &max);
  19.         printf("%s\n", pStr[max]);
  20.         
  21.         return 0;
  22. }

  23. /* User Code Begin:考生在此后完成自定义函数的设计,行数不限 */
  24. int input(char *from[],char p[50][9])
  25. {
  26.         int i,j=0;
  27.         char *q="*END*";
  28.         
  29.         
  30.         for(i=0;i<50;i++)
  31.         {
  32.                 gets(p[i]);
  33.                
  34.       if(       strcmp(p[i],q)==0   )
  35.        break;
  36.                
  37.                 else
  38.                 {
  39.                         from[j++] = *(p+i);
  40.                         
  41.                 }
  42.                
  43.         
  44.                
  45.         }
  46.         
  47.         return j;
  48. }

  49. void find(char *from[],int n,int *findmax)
  50. {
  51.         char *temp1;
  52.         int i,j;
  53.         i=j=0;
  54.         
  55.         
  56.         for(i=0;i+1<n;i++)
  57.         {
  58.                 if(strcmp(from[i],from[i+1])<0)
  59.                         
  60.                 {
  61.                         temp1 = from[i+1];
  62.                         from[i+1]=from[i];
  63.                         from[i]=temp1;
  64.                          *findmax=i+1;
  65.                        
  66.                
  67.                 }

  68.             
  69.                
  70.         }
  71.         
  72.                printf("%d\n",*findmax);
  73.         
  74.         
  75. }
复制代码

改了下,可以运行了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-3-29 17:29:29 | 显示全部楼层
本帖最后由 haiouda 于 2015-4-1 23:02 编辑

  1. 没用的删除了


复制代码

试着调了下,没运行不知道,还有没有错的地方;

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-4-1 09:55:27 | 显示全部楼层
{:1_1:}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-4-1 10:16:58 | 显示全部楼层
学习了一下
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-4-1 21:15:46 | 显示全部楼层
haiouda 发表于 2015-3-29 17:29
试着调了下,没运行不知道,还有没有错的地方;

还是有问题
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-4-1 22:49:02 | 显示全部楼层
本帖最后由 haiouda 于 2015-4-1 23:01 编辑

什么问题?
二楼的,我在我机器上运行了。
比较大小这里,实际大小的应该比算出来的少1,因为二楼的程序 i 是0开始的。*pStr[50]; 也是从0 开始的,这里不算问题哟。




小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-19 11:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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