佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1236|回复: 10

C programming 急

[复制链接]
发表于 26-10-2008 01:28 PM | 显示全部楼层 |阅读模式
C programming 急Hi

我的menu 有
(D)epth :
(S)ave
(L)oad
(P)rint
(Q)uite

当我click D  的时候会 load 去double depth(double seconds2Bottom);  请问在 我的char menuSelect(const char prompt[]);  switchstatment 的 case 'd'  那边要怎样写。。。。。   还有 我这样的 code  对吗??

谢谢

double depth(double seconds2Bottom);
char menuSelect(const char prompt[]);
bool yesNo(const char prompt[]);
int main(void)
{
char selection;
printf("(D)epth: Enter pit location. Compute depth \n");
printf("(S)ave : Write pits date to file \n");
printf("(L)oad : Read mine shaft information from file. \n");
printf("(P)rint: Display mine shaft information.\n");
printf("(Q)uit : Exit program.\n");
printf("enter your choice : ", menuSelect);
scanf("%c", &selection);

}




double depth(double seconds2Bottom)
{
/* Acceleration of gravity */
const double G = 9.81;
    double toBottom; /* Time to reach the bottom of the shaft */
do
{

  printf("\nEnter time to bottom: ");
  scanf("%lf", &toBottom);
  fflush(stdin);
  printf("\nShaft depth is %.2f m\n", depth(toBottom));
} while( yesNo("Do you want to take another measure"));


return -0.5 * G * seconds2Bottom * seconds2Bottom;
}
char menuSelect(const char prompt[])
{
char selection;
switch(selection)
{
case 'd':
case 'D':
depth(seconds2Bottom);
}
}
回复

使用道具 举报


ADVERTISEMENT

发表于 26-10-2008 03:05 PM | 显示全部楼层

回复 1# vzhen 的帖子

1. double seconds2Bottom ,  seconds2Bottom 的值是多少? 从那里拿的?

2. char menuSelect(const char prompt[]) , 这个是要做什么的? 我猜你要得是用来做namu selection 吧, 那么array 就去除吧。

3. char menuSelect(const char prompt[]) 者个好像没return 东西, 给他void 吧。

4. printf("enter your choice : ", menuSelect); menuSelect 是什么来的?而且好像这个variable 没出现过的, 去除吧

5. 你要将你的selecttion pass 给 menuSelect,那么就将你的argument 写进去。

6. bool yesNo(const char prompt[]); c programming 没有 bool 的。。

7. } while( yesNo("Do you want to take another measure")); 这个是什么来的?


基本上, 除了seconds2Bottom的问题 ( 因为我不懂哪里来得) 之外, 应该是这样写。
  1. double depth(double seconds2Bottom);
  2. void menuSelect(const char prompt);
  3. //bool yesNo(const char prompt); NO BOOL IN C PROGRAMMING
  4. int yesNo(const char prompt);
  5. int main(void)
  6. {
  7. char selection;
  8. printf("(D)epth: Enter pit location. Compute depth \n");
  9. printf("(S)ave : Write pits date to file \n");
  10. printf("(L)oad : Read mine shaft information from file. \n");
  11. printf("(P)rint: Display mine shaft information.\n");
  12. printf("(Q)uit : Exit program.\n");
  13. printf("enter your choice : ");
  14. scanf("%c", &selection);

  15. menuSelect(selection);

  16. return 0;


  17. }




  18. double depth(double seconds2Bottom)
  19. {
  20. /* Acceleration of gravity */
  21. const double G = 9.81;
  22.     double toBottom; /* Time to reach the bottom of the shaft */
  23. do
  24. {

  25.   printf("\nEnter time to bottom: ");
  26.   scanf("%lf", &toBottom);
  27.   fflush(stdin);
  28.   printf("\nShaft depth is %.2f m\n", depth(toBottom));
  29. } while( yesNo("Do you want to take another measure")); // wrong logic


  30. return -0.5 * G * seconds2Bottom * seconds2Bottom;
  31. }

  32. void menuSelect(const char prompt)
  33. {

  34.     // double seconds2Bottom = ????? ;
  35.     //
  36. switch(prompt)
  37. {
  38. case 'd':
  39. case 'D':
  40. depth(seconds2Bottom);
  41. break;
  42. }
  43. }
复制代码
回复

使用道具 举报

 楼主| 发表于 26-10-2008 03:38 PM | 显示全部楼层

回复 2# onlylonly 的帖子

嗨,
谢谢你的回复。。。。


那个yesNo  其实是没有问题的。。。因为有另一部分没paste 上来。。。。

那个 double depth  是 return -0.5 * G * seconds2Bottom * seconds2Bottom;  计算出来的。。。。


我的问题是, 一开始时menu ....  然后click D   就会出现 double depth 的function

Enter the time to bottom :

可是 我的case 'D':
这里 link 不到 double depth 的function.


seconds2bottom 就是 我输入的 号码  double toBottom ............

对了 seconds2bottom = ??    这里应该放什么。。。。。。。。。

[ 本帖最后由 vzhen 于 26-10-2008 03:45 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 26-10-2008 03:49 PM | 显示全部楼层
OnlyOnly


你方便留个msn 吗??
回复

使用道具 举报

发表于 26-10-2008 03:49 PM | 显示全部楼层

回复 3# vzhen 的帖子

我的问题你没看懂啊。。

你在 argument passing, seconds2Bottom ( 不是 double depth, 在看多一次)你的argument 振么可能为uninitialize?

而且你的 yesno 为 bool, 在c里面更本没有bool 的, 怎么可能没问题的?

至于如何输入d 就可以渠道function, 我的code哪里已经写了, 直接呼唤menuSelect就可以了
回复

使用道具 举报

 楼主| 发表于 26-10-2008 04:05 PM | 显示全部楼层

回复 5# onlylonly 的帖子

如果我要这样,  你可以帮我修正吗
#include <stdio.h>
#include <stdbool.h>

double depth(double seconds2Bottom);
void menuSelect(char selection);
bool yesNo(const char prompt[]);


int main(void)
{
char selection;
printf("(D)epth: Enter pit location. Compute depth \n");
printf("(S)ave : Write pits date to file \n");
printf("(L)oad : Read mine shaft information from file. \n");
printf("(P)rint: Display mine shaft information.\n");
printf("(Q)uit : Exit program.\n");
printf("enter your choice : ");

scanf("%c", &selection);
menuSelect(selection);

return 0;
}

    double toBottom;
do
{
     
  printf("\nEnter time to bottom: ");
  scanf("%lf", &toBottom);
  fflush(stdin);
  printf("\nShaft depth is %.2f m\n", depth(toBottom));
} while( yesNo("Do you want to take another measure"));            



// 我想把这个do while 放在 int main 应该怎么修改 。。。。。就是说 选了 D 才会跳出 Enter time to bottom :

double depth(double seconds2Bottom)
{
const double G = 9.81;

return -0.5 * G * seconds2Bottom * seconds2Bottom;
}




void menuSelect(char selection)
{
double seconds2Bottom = ???;                   // 这里应该放什么
switch(selection)
{
case 'd':
case 'D':
depth(seconds2Bottom);
break;
case 'q':
case 'Q':
                              // exit 是 return 0;  吗?
}
}

/* Display a question and returns true if the answer is 'y' or 'Y' */
bool yesNo(const char prompt[])
{
char answer;
printf("\n%s (Y/N)? ", prompt);
answer = getchar();
fflush(stdin);
return (answer == 'y' || answer == 'Y');
}

[ 本帖最后由 vzhen 于 26-10-2008 04:11 PM 编辑 ]
回复

使用道具 举报

Follow Us
发表于 26-10-2008 04:12 PM | 显示全部楼层

回复 6# vzhen 的帖子

你的second2bottom 都没用到, 没什么不在argument哪里放void呢?

这几天不是很得空, 过些时候才写个sample code 给你行吗? 大约明天或后天, 你这个是否紧急的?
回复

使用道具 举报

 楼主| 发表于 26-10-2008 04:38 PM | 显示全部楼层
相当急。。。。
星期3交。。。。。。。

明天你有时间吗?

可不可以在msn 教我, 我send 题目和 code 给你。。。。好吗
  
我也是奇怪为什么 secondstoBottom 没用到但是能算出答案。。。。
回复

使用道具 举报


ADVERTISEMENT

发表于 26-10-2008 06:24 PM | 显示全部楼层

回复 8# vzhen 的帖子

看看这个code是否是你要的
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <stdlib.h>

  4. void depth(void);
  5. void menuSelect(const char prompt);
  6. bool yesNo(const char prompt[]);

  7. int main(void)
  8. {
  9.     char selection;
  10.     printf("(D)epth: Enter pit location. Compute depth \n");
  11.     printf("(S)ave : Write pits date to file \n");
  12.     printf("(L)oad : Read mine shaft information from file. \n");
  13.     printf("(P)rint: Display mine shaft information.\n");
  14.     printf("(Q)uit : Exit program.\n");
  15.     printf("enter your choice : ");
  16.     scanf(" %c", &selection);

  17.     menuSelect(selection);

  18.     return 0;


  19. }




  20. void depth(void)
  21. {
  22.     /* Acceleration of gravity */
  23.     const double G = 9.81;
  24.       double toBottom; /* Time to reach the bottom of the shaft */
  25.     do
  26.     {

  27.         printf("\nEnter time to bottom: ");
  28.           scanf(" %lf", &toBottom);
  29.           fflush(stdin);
  30.           printf("\nShaft depth is %.2f m\n", -0.5 * G * toBottom * toBottom);
  31.     } while( yesNo("Do you want to take another measure"));


  32. }

  33. void menuSelect(char selection)
  34. {

  35.     switch(selection)
  36.     {
  37.         case 'd':
  38.         case 'D':
  39.         depth();
  40.         break;

  41.         case 'q':
  42.         case 'Q':
  43.         exit(0);
  44.                               // exit
  45.     }
  46. }

  47. /* Display a question and returns true if the answer is 'y' or 'Y' */
  48. bool yesNo(const char prompt[])
  49. {
  50.     char answer;
  51.     printf("\n%s (Y/N)? ", prompt);
  52.     getchar(); // eliminate linebreak
  53.     answer = getchar();
  54.     fflush(stdin);
  55.     return (answer == 'y' || answer == 'Y');
  56. }
复制代码

[ 本帖最后由 onlylonly 于 26-10-2008 06:26 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 27-10-2008 01:40 AM | 显示全部楼层
OnlyOnly,
那个问题我解决廖。。。。。就把do while 放上去int main 就可以了。

我又有三个问题。
1. exit(0);   =  compile 后出现warning  因为required standard code 所以warning message 是不可以的,我试过 return 0;   有error.  在这里要放什么才对。

2. 如果在 输入data 的时候 required 一个 space
Example :  abc 123    必须要有一个space between abc 123 在 printf 还是 scanf 要加些什么?

3 .如果在menu 的时候输入没有固定 的 char 要怎样loop back 回 menu 从新输入到正确为字?  

char selection;
double toBottom;
char name;
        printf("(D)epth: Enter pit location. Compute depth \n");
        printf("(S)ave : Write pits date to file \n");
        printf("(L)oad : Read mine shaft information from file. \n");
        printf("(P)rint: Display mine shaft information.\n");
        printf("(Q)uit : Exit program.\n");
        printf("enter your choice : ");
       

        scanf("%c", &selection);
        menuSelect(selection);
        if((selection) !='d' && (selection) != 'D')
        printf("invalid entry");
       
        else
        这里应该要怎样loopback 回去?


THANKS
回复

使用道具 举报

发表于 18-11-2008 11:55 PM | 显示全部楼层
原帖由 vzhen 于 27-10-2008 01:40 AM 发表
OnlyOnly,
那个问题我解决廖。。。。。就把do while 放上去int main 就可以了。

我又有三个问题。
1. exit(0);   =  compile 后出现warning  因为required standard code 所以warning message 是不可以的,我试 ...


那个function 的 return 是 void, 当然不能回传 value. 用 "return;"
loopback 的话就用个while loop就可以了,选对了就invalidate那个 statement
回复

使用道具 举报

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

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 22-12-2025 06:17 PM , Processed in 0.140811 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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