鱼C论坛

 找回密码
 立即注册
查看: 4603|回复: 18

不知道哪里有错啊,帮忙看下吧,虽然长但是没什么含量

[复制链接]
发表于 2013-8-30 16:58:46 | 显示全部楼层 |阅读模式
5鱼币
1.jpg 2.jpg 这是题目,代码编写如下
这是问题,无论输入什么都是这个响应,郁闷得慌
#include<stdio.h>
char get_choice();
char get_first();
void count1();
void count2();
void count3();
void count4();
float get_float();
int main()
{
char choice;
void count1();
    void count2();
    void count3();
    void count4();
   
    while((choice=get_choice())!='q')
    {
     switch(choice)
     {
      case 'a':count1();
               continue;
      
   case 's':count2();
            continue;
            
   case 'm':count3();   
      continue;
      
   case 'd':count4();
            continue;
            
   case 'q':break;         
   
   default:printf("Program error.\n");
           continue;            
     }   
    }
    printf("Done.");
    return 0;
}
char get_choice()
{
char ch;
printf("Enter the operation of your choice:\n");
printf("a.add       s.subtract\n");
printf("m.multiply  d.divide\n");
printf("q.quit\n");

ch=get_first();

while(ch!='a'||ch!='s'||ch!='m'||ch!='d'&&ch!='q')
  {
      printf("Please enter the character followed the tip.\n");
  
         ch=get_first();
  }
  

return ch;
}
void count1()
{
float num1,num2,num;
printf("Enter the first number:");
num1=get_float();
printf("Enter second number:");
num2=get_float();

num=num1+num2;
printf("%.1f + %.1f=%.1f.",num1,num2,num);

}
void count2()
{
float num1,num2,num;
printf("Enter the first number:");
num1=get_float();
printf("Enter second number:");
num2=get_float();

num=num1-num2;
printf("%.1f - %.1f=%.1f.",num1,num2,num);

}
void count3()
{
float num1,num2,num;
printf("Enter the first number:");
num1=get_float();
printf("Enter second number:");
num2=get_float();

num=num1*num2;
printf("%.1f * %.1f=%.1f.",num1,num2,num);

}
void count4()
{
float num1,num2,num;
printf("Enter the first number:");
num1=get_float();
printf("Enter second number:");
num2=get_float();
while(num2==0)
printf("Enter a number other than 0:");
num2=get_float();

num=num1/num2;
printf("%.1f / %.1f=%.1f.",num1,num2,num);

}
float get_float()
{
float input;
char ch;

while(scanf("%f",&input)!=1)
{
  while((ch=getchar())!='\n')
  putchar(ch);
  printf(" is not an number.\n");
  printf("Please enter a number,such as 2.5,-1.78E8,or 3.1.\n");
}
return input;
}
char get_first()
{
int ch;

ch=getchar();
while(getchar()!='\n')
continue;

return ch;
}

1.jpg

最佳答案

查看完整内容

发现没有 while(ch!='a'||ch!='s'||ch!='m'||ch!='d'&&ch!='q') 你这么写的话 绝对是真的 所以一直会在这个循环里 应该是 while(ch!='a' && ch!='s' && ch!='m' && ch!='d' && ch!='q' )
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-30 16:58:47 | 显示全部楼层
发现没有 while(ch!='a'||ch!='s'||ch!='m'||ch!='d'&&ch!='q')
你这么写的话 绝对是真的 所以一直会在这个循环里
应该是
while(ch!='a' && ch!='s' && ch!='m' && ch!='d' && ch!='q' )
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-30 17:11:43 | 显示全部楼层
稍微按照我自己的习惯改了下
  1. #include<stdio.h>
  2. char get_choice();
  3. char get_first();
  4. void count1();
  5. void count2();
  6. void count3();
  7. void count4();
  8. float get_float();
  9. int main()
  10. {
  11.         char choice;
  12.         void count1();
  13.     void count2();
  14.     void count3();
  15.     void count4();
  16.    
  17.     while((choice=get_choice())!='q')
  18.     {
  19.                 switch(choice)
  20.                 {
  21.                 case 'a':count1();
  22.                         break;
  23.                        
  24.                 case 's':count2();
  25.             break;
  26.             
  27.                 case 'm':count3();   
  28.                         break;
  29.                        
  30.                 case 'd':count4();
  31.             break;
  32.             
  33.                 case 'q':break;         
  34.                        
  35.                 default:printf("Program error.\n");
  36.                         break;            
  37.                 }   
  38.     }
  39.     printf("Done.");
  40.     return 0;
  41. }
  42. char get_choice()
  43. {
  44.         char ch;
  45.         printf("Enter the operation of your choice:\n");
  46.         printf("a.add       s.subtract\n");
  47.         printf("m.multiply  d.divide\n");
  48.         printf("q.quit\n");
  49.        
  50.         //ch=get_first();
  51.         ch = getchar();
  52.         //while(ch!='a'||ch!='s'||ch!='m'||ch!='d'&&ch!='q')
  53.         while(ch!='a' && ch!='s' && ch!='m' && ch!='d' && ch!='q' )
  54.         {
  55.                 printf("Please enter the character followed the tip.\n");
  56.                
  57.                 ch=get_first();
  58.         }
  59.        
  60.        
  61.         return ch;
  62. }
  63. void count1()
  64. {
  65.         float num1,num2,num;
  66.         printf("Enter the first number:");
  67.         //num1=get_float();
  68.         scanf("%f",&num1);
  69.         printf("Enter second number:");
  70.         //num2=get_float();
  71.         scanf("%f",&num2);
  72.        
  73.         num=num1+num2;
  74.         printf("%.1f + %.1f=%.1f.",num1,num2,num);
  75.        
  76. }
  77. void count2()
  78. {
  79.         float num1,num2,num;
  80.         printf("Enter the first number:");
  81.         num1=get_float();
  82.         printf("Enter second number:");
  83.         num2=get_float();
  84.        
  85.         num=num1-num2;
  86.         printf("%.1f - %.1f=%.1f.",num1,num2,num);
  87.        
  88. }
  89. void count3()
  90. {
  91.         float num1,num2,num;
  92.         printf("Enter the first number:");
  93.         num1=get_float();
  94.         printf("Enter second number:");
  95.         num2=get_float();
  96.        
  97.         num=num1*num2;
  98.         printf("%.1f * %.1f=%.1f.",num1,num2,num);
  99.        
  100. }
  101. void count4()
  102. {
  103.         float num1,num2,num;
  104.         printf("Enter the first number:");
  105.         num1=get_float();
  106.         printf("Enter second number:");
  107.         num2=get_float();
  108.         while(num2==0)
  109.                 printf("Enter a number other than 0:");
  110.         num2=get_float();
  111.        
  112.         num=num1/num2;
  113.         printf("%.1f / %.1f=%.1f.",num1,num2,num);
  114.        
  115. }
  116. float get_float()
  117. {
  118.         float input;
  119.         char ch;
  120.        
  121.         while(scanf("%f",&input)!=1)
  122.         {
  123.                 while((ch=getchar())!='\n')
  124.                         putchar(ch);
  125.                 printf(" is not an number.\n");
  126.                 printf("Please enter a number,such as 2.5,-1.78E8,or 3.1.\n");
  127.         }
  128.         return input;
  129. }
  130. char get_first()
  131. {
  132.         int ch;
  133.        
  134.         ch=getchar();
  135.         while(getchar()!='\n')
  136.                 continue;
  137.        
  138.         return ch;
  139. }
复制代码
主要是while的条件写错了
还有你干嘛放着好好的break不用 用continue
还有像 getchar不用 非 自己定义一个 get_first??
首先 这种预防等你功能实现之后再去弄吧
还有我和你说的你就是不听是吧
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-30 17:12:39 | 显示全部楼层
我自己 乱改一通 测试加法没问题
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-8-30 17:13:27 | 显示全部楼层

注释又忘了..
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-30 17:14:36 | 显示全部楼层
吃饭回来看。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-8-30 17:20:04 | 显示全部楼层
诸葛暗 发表于 2013-8-30 17:13
注释又忘了..

还是有问题的,当测试除法的时候,分母为零的情况没法解决
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-8-30 17:21:17 | 显示全部楼层
牡丹花下死做鬼 发表于 2013-8-30 17:12
我自己 乱改一通 测试加法没问题

应该把while改为if,我把循环和分支弄混了,╮(╯▽╰)╭
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-30 17:26:05 | 显示全部楼层
诸葛暗 发表于 2013-8-30 17:21
应该把while改为if,我把循环和分支弄混了,╮(╯▽╰)╭

  哦哦 sorry && 和 ||的优先级我看错了 sorry
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-30 17:27:49 | 显示全部楼层
诸葛暗 发表于 2013-8-30 17:20
还是有问题的,当测试除法的时候,分母为零的情况没法解决

while 只能控制一个语句啊 你要用 { } 括起来 那两句代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-30 17:33:01 | 显示全部楼层
牡丹花下死做鬼 发表于 2013-8-30 17:27
while 只能控制一个语句啊 你要用 { } 括起来 那两句代码
  1. void count4()
  2. {
  3.         float num1,num2,num;
  4.         printf("Enter the first number:");
  5.         num1=get_float();
  6.         printf("Enter second number:");
  7.         num2=get_float();
  8.         while(num2==0)
  9.         {//加上大括号
  10.                 printf("Enter a number other than 0:");
  11.         num2=get_float();
  12.         }
  13.         num=num1/num2;
  14.         printf("%.1f / %.1f=%.1f.",num1,num2,num);
  15.        
  16. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-8-30 17:39:24 | 显示全部楼层
3.jpg 再看下这个吧,不在发帖子了,算法没什么问题,问题在我注释的地方,我新定义的函数是想提示输入是一个字符,并且只读取首字符,结果这样
#include<stdio.h>
#include<ctype.h>
char get_choice(void);
char get_char();
int main()
{
char response;
int high=100;
int low=1;
int guess=(high+low)/2;

printf("Please choose an ingeter from 1 to 100.I will try to guess.");
printf("it.\nResponsed with a Y if my guess is right ,");
printf(" with a L if my guess is large,and with a S if it is small.\n");
printf("OK,is it %d?\n",guess);

while((response=get_choice())!='Y')
{
  if(response=='S')
  {
   low=guess+1;
   guess=(low+high)/2;
   printf("Well,is it %d?",guess);
   continue;
  }
  else if(response=='L')
  {
  high=guess-1;
  guess=(low+high)/2;
  printf("Well,is it %d?",guess);
  continue;
  }
  else{
   printf("I only understand L,S or Y.Enter again. ");
  continue;
  }
  
  
}

printf("Done");
}
char get_choice()
{
int ch;



ch=getchar();
while(getchar()!='\n')//只读取首字符
continue;

ch=get_char();

return ch;
}
char get_char()//提示输入一个字符
{
char input;
int ch;

input=toupper(input);
while(scanf("%c",&input)!=1)
{
  while((ch=getchar())!='\n')
  putchar(ch);
     printf(" is not a character.\nPlease enter a \
  charanter,such as L,S or Y.");
}
     
return input;
}

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

使用道具 举报

 楼主| 发表于 2013-8-30 17:42:31 | 显示全部楼层

嗯,while和if都可以
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-30 17:43:40 | 显示全部楼层
诸葛暗 发表于 2013-8-30 17:42
嗯,while和if都可以

我觉得还是 循环好
不要用continue
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-30 18:53:28 | 显示全部楼层
把主函数前面的整形去掉试试,
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-30 19:06:12 | 显示全部楼层
读者 发表于 2013-8-30 18:53
把主函数前面的整形去掉试试,

规定就是要
  1. #include<stdio.h>

  2. int main()
  3. {
  4.         return 0;
  5. }
复制代码
不能
  1. #include<stdio.h>

  2. void main()
  3. {

  4. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-31 11:12:35 | 显示全部楼层
(*^__^*) 嘻嘻…… 我说最佳答案是不是给我啊 O(∩_∩)O~
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-9-1 10:32:42 | 显示全部楼层
牡丹花下死做鬼 发表于 2013-8-31 11:12
(*^__^*) 嘻嘻…… 我说最佳答案是不是给我啊 O(∩_∩)O~

给你,这两天没上电脑忘了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-5 23:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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