| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1080 人关注过本帖
标题:刚入门C,搞试验的,要做资料,写了个含水率的程序,有点小问题,求指导
只看楼主 加入收藏
whukeming
Rank: 2
等 级:论坛游民
帖 子:76
专家分:51
注 册:2008-8-24
结帖率:0
收藏
 问题点数:0 回复次数:10 
刚入门C,搞试验的,要做资料,写了个含水率的程序,有点小问题,求指导
mac系统,只能用Xcode写,
下面是输出结果和程序,就是为什么重复程序时出现2行 “程序是否要继续执行(Y/N)”

程序是否要继续执行(Y/N)
y
请输入配合比序号:
1
砂的含水率:
4.3
碎石(1)含水率:
1.5
碎石(2)含水率:
1.0
输出水泥, 输出砂, 碎石(1), 碎石(2), 应加水含量, 外加剂 :
水泥 = 369
砂 = 717
碎石(1) = 238
碎石(2) = 945
应加水含量 = 121
外加剂 = 3.69
程序是否要继续执行(Y/N)

程序是否要继续执行(Y/N)
//
//  main.c
//  含水率计算
//
//  Created by Hu Keming on 13-3-26.
//  Copyright (c) 2013年 Hu Keming. All rights reserved.
//



# include <stdio.h>

int round (float x)

{
    x = x +0.5;
   
    return x;
}


int main(void)
{
    int i = 1;
    char ch;
   
    while (i == 1)
    {
        
        printf("程序是否要继续执行(Y/N)\n");
        
        scanf("%c", &ch);
        
        printf("\n");
        
        if(ch == 'y' || ch == 'Y')
            
            
        {
            int val;
            
            
            printf("请输入配合比序号: \n");
            scanf("%d", &val);
            
            float W1, W2, W3;
            float S1, S2, S3;
            int water;
            
            
            switch (val)
            {
                case 1:                   //立柱、盖梁含、墙身(1)含水率 C30
                {
                    int cement_1 = 369;
                    float additive_1 = 3.69;
                    
                    printf("砂的含水率: \n");
                    scanf("%f", &W1);
                    
                    printf("碎石(1)含水率: \n");
                    scanf("%f", &W2);
                    
                    printf("碎石(2)含水率: \n");
                    scanf("%f", &W3);
                    
                    S1 = 687 * (1 + W1*0.01);
                    
                    S2 = 234 * (1 + W2*0.01);
                    
                    S3 = 936 * (1 + W3*0.01);
                    
                    S1 = round(S1);
                    
                    S2 = round(S2);
                    
                    S3 = round(S3);
                    
                    water = 164 - ((S1 + S2 + S3) - 1857);
                    
                    printf("输出水泥, 输出砂, 碎石(1), 碎石(2), 应加水含量, 外加剂 :\n");
                    printf("水泥 = %d\n", cement_1);
                    printf("砂 = %.f\n", S1);
                    printf("碎石(1) = %.f\n", S2);
                    printf("碎石(2) = %.f\n", S3);
                    printf("应加水含量 = %d\n", water);
                    printf("外加剂 = %.2f\n", additive_1);
                    
                    break;
                }
                    
                case 2:               //桩基含水率 C25
                {
                    int cement_2 = 380;
                    float additive_2 = 3.80;
                    
                    printf("砂的含水率: \n");
                    scanf("%f", &W1);
                    
                    printf("碎石(1)含水率: \n");
                    scanf("%f", &W2);
                    
                    printf("碎石(2)含水率: \n");
                    scanf("%f", &W3);
                    
                    S1 = 778 * (1 + W1*0.01);
                    
                    S2 = 207 * (1 + W2*0.01);
                    
                    S3 = 826 * (1 + W3*0.01);
                    
                    S1 = round(S1);
                    
                    S2 = round(S2);
                    
                    S3 = round(S3);
                    
                    water = 179 - ((S1 + S2 + S3) - 1811);
                    
                    printf("输出水泥, 输出砂, 碎石(1), 碎石(2), 应加水含量, 外加剂 :\n");
                    printf("水泥 = %d\n", cement_2);
                    printf("砂 = %.f\n", S1);
                    printf("碎石(1) = %.f\n", S2);
                    printf("碎石(2) = %.f\n", S3);
                    printf("外加剂 = %.2f\n", additive_2);
                    
                    break;
                }
                    
                case 3:                          //系梁、墙身(2) C25
                {
                    int cement_3 = 343;
                    float additive_3 = 2.74;
                    
                    printf("砂的含水率: \n");
                    scanf("%f", &W1);
                    
                    printf("碎石(1)含水率: \n");
                    scanf("%f", &W2);
                    
                    printf("碎石(2)含水率: \n");
                    scanf("%f", &W3);
                    
                    S1 = 696 * (1 + W1*0.01);
                    
                    S2 = 237 * (1 + W2*0.01);
                    
                    S3 = 949 * (1 + W3*0.01);
                    
                    S1 = round(S1);
                    
                    S2 = round(S2);
                    
                    S3 = round(S3);
                    
                    water = 165 - ((S1 + S2 + S3) - 1882);
                    
                    printf("输出水泥, 输出砂, 碎石(1), 碎石(2), 应加水含量, 外加剂 :\n");
                    printf("水泥 = %d\n", cement_3);
                    printf("砂 = %.f\n", S1);
                    printf("碎石(1) = %.f\n", S2);
                    printf("碎石(2) = %.f\n", S3);
                    printf("应加水含量 = %d\n", water);
                    printf("外加剂 = %.2f\n", additive_3);
                    
                    break;
                }
                    
                case 4:              //基础(1) C20
                {
                    int cement_4 = 300;
                    float additive_4 = 2.40;
                    
                    printf("砂的含水率: \n");
                    scanf("%f", &W1);
                    
                    printf("碎石(1)含水率: \n");
                    scanf("%f", &W2);
                    
                    printf("碎石(2)含水率: \n");
                    scanf("%f", &W3);
                    
                    S1 = 733 * (1 + W1*0.01);
                    
                    S2 = 239 * (1 + W2*0.01);
                    
                    S3 = 957 * (1 + W3*0.01);
                    
                    S1 = round(S1);
                    
                    S2 = round(S2);
                    
                    S3 = round(S3);
                    
                    water = 161 - ((S1 + S2 + S3) - 1929);
                    
                    printf("输出水泥, 输出砂, 碎石(1), 碎石(2), 应加水含量, 外加剂 :\n");
                    printf("水泥 = %d\n", cement_4);
                    printf("砂 = %.f\n", S1);
                    printf("碎石(1) = %.f\n", S2);
                    printf("碎石(2) = %.f\n", S3);
                    printf("应加水含量 = %.2f\n", additive_4);
                    
                    break;
                }
                    
                case 5:              //基础(2) C15
                {
                    int cement_5 = 267;
                    float additive_5 = 2.40;
                    
                    printf("砂的含水率: \n");
                    scanf("%f", &W1);
                    
                    printf("碎石(1)含水率: \n");
                    scanf("%f", &W2);
                    
                    printf("碎石(2)含水率: \n");
                    scanf("%f", &W3);
                    
                    S1 = 746 * (1 + W1*0.01);
                    
                    S2 = 243 * (1 + W2*0.01);
                    
                    S3 = 974 * (1 + W3*0.01);
                    
                    S1 = round(S1);
                    
                    S2 = round(S2);
                    
                    S3 = round(S3);
                    
                    water = 160 - ((S1 + S2 + S3) - 1963);
                    
                    printf("输出水泥, 输出砂, 碎石(1), 碎石(2), 应加水含量, 外加剂 :\n");
                    printf("水泥 = %d\n", cement_5);
                    printf("砂 = %.f\n", S1);
                    printf("碎石(1) = %.f\n", S2);
                    printf("碎石(2) = %.f\n", S3);
                    printf("应加水含量 = %d\n", water);
                    printf("外加剂 = %.2f\n", additive_5);
                    
                    break;
                }
                    
            }
        }
        else if(ch == 'n' || ch == 'N')
            break;
        
    }
   
   
    return 0;
}
搜索更多相关主题的帖子: 碎石 含水率 资料 
2013-03-26 00:38
whukeming
Rank: 2
等 级:论坛游民
帖 子:76
专家分:51
注 册:2008-8-24
收藏
得分:0 
求知道,看了半天没看出来
2013-03-26 00:38
a271885843
Rank: 2
等 级:论坛游民
帖 子:291
专家分:58
注 册:2011-11-24
收藏
得分:0 
以下是引用whukeming在2013-3-26 00:38:49的发言:

求知道,看了半天没看出来

你在
程序代码:
 while (i == 1)
    {
        
          fflush(stdin);
        printf("程序是否要继续执行(Y/N)\n");
        
        scanf("%c", &ch);
        printf("\n");

加入清空键盘缓存区函数 fflush(stdin);就可以解决了
因为你第一次输入y的时候,键盘缓存区里是这样的y\n,scanf读取了y,第一次循环之后,他会读取你swich里sacnf里的其他遗留字符\n,字符所以循环的时候是是不经过swich的,然后再循环while,所以你每次输入前,用fflush(stdin);清空缓存区,就会保证scanf不会读取之前缓存遗留字符的影响。

[ 本帖最后由 a271885843 于 2013-3-26 08:45 编辑 ]

认为事物非黑即白是缺智慧的表现……
2013-03-26 08:44
whukeming
Rank: 2
等 级:论坛游民
帖 子:76
专家分:51
注 册:2008-8-24
收藏
得分:0 
回复 3楼 a271885843
加了这个函数貌似还是老问题。。我到VC++里试试看
2013-03-26 09:06
韶志
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:斗气大陆
等 级:贵宾
威 望:44
帖 子:2223
专家分:13592
注 册:2013-3-22
收藏
得分:0 
清空缓存区,fflush(stdin); 函数的位置要放对

三十年河东,三十年河西,莫欺少年穷!
2013-03-26 10:24
whukeming
Rank: 2
等 级:论坛游民
帖 子:76
专家分:51
注 册:2008-8-24
收藏
得分:0 
回复 5楼 韶志
确定是放在了while里面你指的位置了,我再试试吧,VC++打开直接N个错误。。。算了
2013-03-26 10:36
韶志
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:斗气大陆
等 级:贵宾
威 望:44
帖 子:2223
专家分:13592
注 册:2013-3-22
收藏
得分:0 
回复 6楼 whukeming
我并没有指出位置...   而且你的程序不加那个清空函数在VC里调试还是一大堆错误

三十年河东,三十年河西,莫欺少年穷!
2013-03-26 10:38
whukeming
Rank: 2
等 级:论坛游民
帖 子:76
专家分:51
注 册:2008-8-24
收藏
得分:0 
回复 7楼 韶志
这是为什么呢,我在Xcode里调试没问题的。

还有你那函数貌似不起作用
2013-03-26 14:44
a271885843
Rank: 2
等 级:论坛游民
帖 子:291
专家分:58
注 册:2011-11-24
收藏
得分:0 
以下是引用a271885843在2013-3-26 08:44:27的发言:


你在
 while (i == 1)
    {
        
          fflush(stdin);
        printf("程序是否要继续执行(Y/N)\n");
        
        scanf("%c", &ch);
        printf("\n");
加入清空键盘缓存区函数 fflush(stdin);就可以解决了
因为你第一次输入y的时候,键盘缓存区里是这样的y\n,scanf读取了y,第一次循环之后,他会读取你swich里sacnf里的其他遗留字符\n,字符所以循环的时候是是不经过swich的,然后再循环while,所以你每次输入前,用fflush(stdin);清空缓存区,就会保证scanf不会读取之前缓存遗留字符的影响。

我在C-free5里调试通过,不过你那整型和浮点型数据之间转换有点警告,你在试试看,你这代码的问题就出在没清空前一个循环里隐含字符\n,所以第二次循环,scanf首先读取的是遗留的\n.

认为事物非黑即白是缺智慧的表现……
2013-03-26 15:01
whukeming
Rank: 2
等 级:论坛游民
帖 子:76
专家分:51
注 册:2008-8-24
收藏
得分:0 
谢谢了
用函数重新写了段,把yes和no去掉了,这样清爽多了
程序代码:
//
//  main.c
//  函数求含水率
//
//  Created by Hu Keming on 13-3-28.
//  Copyright (c) 2013年 Hu Keming. All rights reserved.
//

# include <stdio.h>

int roun(float x)            //定义四舍五入函数

{
    x = x+0.5;
    
    return x;
}                  

void moisture_capacity(float sand, float detritus_1, float detritus_2)  //定义求含水率函数
{
    float S1, S2, S3;
    int water;
    int a, b, c, w1, w2, d;
    float e;
    int i;
    printf("请输入对应构建编号: \n");
    scanf("%d", &i);
    if (i == 1)                  //桩基
    {
        a = 778, b = 207, c = 826 , w1 = 179, w2 = 1811;
        d = 380, e = 3.80;
    }
    else if (i == 2)             //立柱、盖梁、墙身(1)
    {
        a = 687, b = 234, c = 936 , w1 = 164, w2 = 1857;
        d = 369, e = 3.69;
    }
    else if (i == 3)             //系梁 墙身(2)
    {
        a = 696, b = 237, c = 949 , w1 = 165, w2 = 1882;
        d = 343, e = 2.74;
    }
    else if (i == 4)             //基础(1)
    {
        a = 733, b = 239, c = 957 , w1 = 161, w2 = 1929;
        d = 300, e = 2.40;
    }
    else                         //基础(2)
    {
        a = 746, b = 243, c = 974 , w1 = 160, w2 = 1963;
        d = 267, e = 2.40;
    }
    
    S1 = (sand*0.01+1)*a;
    S2 = (detritus_1*0.01+1)*b;
    S3 = (detritus_2*0.01+1)*c;
    
    S1 = roun(S1);
    S2 = roun(S2);
    S3 = roun(S3);
    
    water = w1 - (S1 + S2 + S3 - w2);
    
    printf("水泥 = %d\n\n", d);
    printf("砂 = %.f\n\n碎石(1) = %.f\n\n碎石(2) = %.f\n\n", S1, S2, S3);
    printf("水 = %d\n\n", water);
    printf("外加剂 = %.2f\n\n", e);
}


int main()

{
    while(1) // while死循环使程序重复运行
    {
        float sand, detritus_1, detritus_2;
        
        printf("请输入含水率百分比: \n");
    
        scanf("%f %f %f", &sand, &detritus_1, &detritus_2); 
    
        moisture_capacity (sand, detritus_1, detritus_2);
    }
    
    return 0;
}
2013-03-28 11:30
快速回复:刚入门C,搞试验的,要做资料,写了个含水率的程序,有点小问题,求指 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030229 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved