如何在 C 中使用带有用户输入的循环函数?

2024-03-14

这是我创建的平均程序。

#include <stdio.h>
#include <stdlib.h>

 int main()
 {
    float f1,f2,f3;

    /* Program to calculate averages. */

    /*Asks for the numbers.*/

    printf(" Please enter three numbers.\n");
    printf ("\t" "First number please.\n");
    scanf("%f", &f1);
    printf ("\t" "Second number please.\n");
    scanf ("%f", &f2);
    printf("\t" "Third number please.\n");
    scanf("%f", &f3);

    /* Now it averages it.*/
    printf(" Thank you, wait one.\n");
    printf(" Excellent, your sum is.\n");
    printf("%f""\n", f1+f2+f3);


    printf("Your average of the sum is now!!!!\n");
    printf("%f", (f1+f2+f3)/3);

    return 0;
}

现在我会把这变成一个闲暇时光吗?或者是 if else ?


如果您想重复整个输入和平均过程,您可以在代码中包含一个循环:

#include <stdio.h>

int main(void)
{
    float f1,f2,f3;

    while (1)
    {
        printf("Please enter three numbers.\n");
        printf("\tFirst number please.\n");
        if (scanf("%f", &f1) != 1)
            break;
        printf("\tSecond number please.\n");
        if (scanf("%f", &f2) != 1)
            break;
        printf("\tThird number please.\n");
        if (scanf("%f", &f3) != 1)
            break;

        printf("Your sum     is %f\n", f1+f2+f3);
        printf("Your average is %f\n", (f1+f2+f3)/3);
    }

    return 0;
}

请注意,此代码检查返回值scanf()每次使用时,如果出现问题就中断循环。不需要字符串连接,单个printf()当然可以打印一个字符串和一个值。

这是一个简单的第一阶段;可以使用更复杂的技术。例如,您可以创建一个函数来提示并读取数字:

#include <stdio.h>

static int prompt_and_read(const char *prompt, float *value)
{
    printf("%s", prompt);
    if (scanf("%f", value) != 1)
        return -1;
    return 0;
}

int main(void)
{
    float f1,f2,f3;

    while (printf("Please enter three numbers.\n") > 0 &&
           prompt_and_read("\tFirst number please.\n", &f1) == 0 &&
           prompt_and_read("\tSecond number please.\n", &f2) == 0 &&
           prompt_and_read("\tThird number please.\n", &f3) == 0)
    {
        printf("Your sum     is %f\n", f1+f2+f3);
        printf("Your average is %f\n", (f1+f2+f3)/3);
    }

    return 0;
}

如果您想摆脱一组固定的三个值,那么您可以迭代直到遇到 EOF 或错误:

#include <stdio.h>

static int prompt_and_read(const char *prompt, float *value)
{
    printf("%s", prompt);
    if (scanf("%f", value) != 1)
        return -1;
    return 0;
}

int main(void)
{
    float value;
    float sum = 0.0;
    int   num = 0;

    printf("Please enter numbers.\n");

    while (prompt_and_read("\tNext number please.\n", &value) == 0)
    {
        sum += value;
        num++;
    }

    if (num > 0)
    {
        printf("You entered %d numbers\n", num);
        printf("Your sum     is %f\n", sum);
        printf("Your average is %f\n", sum / num);
    }

    return 0;
}

您还可能决定将提示字符串末尾的换行符替换为空格,以便在与提示相同的行上键入该值。

如果您想检查是否重复计算,可以对第一个或第二个版本的代码使用较小的变体:

#include <stdio.h>

static int prompt_and_read(const char *prompt, float *value)
{
    printf("%s", prompt);
    if (scanf("%f", value) != 1)
        return -1;
    return 0;
}

static int prompt_continue(const char *prompt)
{
    printf("%s", prompt);
    char answer[2];
    if (scanf("%1s", answer) != 1)
        return 0;
    if (answer[0] == 'y' || answer[0] == 'Y')
    {
        int c;
        while ((c = getchar()) != EOF && c != '\n')      // Gobble to newline
            ;
        return 1;
    }
    return 0;
}

int main(void)
{
    float f1,f2,f3;

    while (printf("Please enter three numbers.\n") > 0 &&
           prompt_and_read("\tFirst number please.\n", &f1) == 0 &&
           prompt_and_read("\tSecond number please.\n", &f2) == 0 &&
           prompt_and_read("\tThird number please.\n", &f3) == 0)
    {
        printf("Your sum     is %f\n", f1+f2+f3);
        printf("Your average is %f\n", (f1+f2+f3)/3);
        if (prompt_continue("Do you want to try again?") == 0)
            break;
    }

    return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 C 中使用带有用户输入的循环函数? 的相关文章

  • CMake 找不到请求的 Boost 库

    既然我已经浏览了其他人的解决方案几个小时 但找不到适合我的问题的正确答案 我想将我的具体问题带给您 我正在尝试使用 CMake 构建 vsomeip 为此 我之前构建了 boost 1 55 但是 我在 CMake 中收到以下错误 The
  • 将 new 与 decltype 一起使用

    T t T is an implementation detail t new T want to avoid naming T to allow for flexibility t new decltype t error cannot
  • 您可以从基本 Win32 控制台模板应用程序中的 C#/Winrt 组件调用(不是 WinForm/abstractions/wrappers 或使用 C++/Winrt 模板)吗?)

    我有一个现有的程序 win32 x86 控制台应用程序 需要调用托管代码 来自 Net 的 C dll The dll不暴露给 COM 但可以从 C WinRT 组件调用并由 C WinRT 控制台模板应用引用 BUT即使安装了 C Win
  • 单元测试验证失败

    我正在运行我的单元测试PostMyModel路线 然而 在PostMyModel 我用的是线Validate
  • 有没有办法在 xcode 上使用 c++0x ?我想使用 gcc 4.4 或更高版本

    我想使用 gcc 4 4 或更高版本进行 iphone 开发 有人知道怎么做吗 不 你不知道 相信我 你不会 Apple 仍保留 gcc 4 2 1 因为 4 2 2 及更高版本使用 GPLv3 这意味着他们必须放弃对其平台的控制 对于 i
  • 在 C# 中调用 C++ 库 [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我有很多用 C 编写的库 我想从 C 调用这些库 但是 我遇到了很多问题 我想知道是否有书籍或指南告诉我如何做到这一点 Dll导入 htt
  • 从模板切换传递的类型

    在 C 中是否可以检查传递给模板函数的类型 例如 template
  • 运行需要 MySql.Data 的内置 .NET 应用程序

    我在运行我编写的内置 NET 应用程序时遇到问题 我的应用程序使用最新的 MySql 连接器 该连接器安装在我的系统上 当我尝试将其添加为引用时 该连接器显示为 NET 4 Framwork 组件 当我在环境中以调试模式运行应用程序时 一切
  • Gwan C#,如何获取HTTP标头?

    我需要它来重写 url 以了解我正在处理哪个友好的 url 用于用户代理和其他东西 EDIT public class Gwan MethodImplAttribute MethodImplOptions InternalCall exte
  • 获取 boost Spirit 语法中的当前行

    我正在尝试使用 boostspirit 获取正在解析的文件的当前行 我创建了一个语法类和结构来解析我的命令 我还想跟踪在哪一行找到命令并将其解析到我的结构中 我将 istream 文件迭代器包装在 multi pass 迭代器中 然后将其包
  • 使用循环将对象添加到列表(python)

    我正在尝试使用 while 循环将对象添加到列表中 基本上这就是我想做的 class x pass choice raw input pick what you want to do while choice 0 if choice 1 E
  • 如何对 NServiceBus.Configure.WithWeb() 进行单元测试?

    我正在构建一个 WCF 服务 该服务接收外部 IP 上的请求并将其转换为通过 NServiceBus 发送的消息 我的单元测试之一调用Global Application Start 它执行应用程序的配置 然后尝试将 Web 服务解析为 验
  • 如何在三个 IEnumerable 上使用 Zip [重复]

    这个问题在这里已经有答案了 可能的重复 使用 Linq 从 3 个集合创建项目 https stackoverflow com questions 5284315 create items from 3 collections using
  • 从浏览器访问本地文件?

    您好 我想从浏览器访问系统的本地文件 由于涉及大量安全检查 是否可以通过某种方式实现这一目标 或使用 ActiveX 或 Java Applet 的任何其他工作环境 请帮帮我 要通过浏览器访问本地文件 您可以使用签名的 Java Apple
  • 在 OpenGL 中渲染纹理 1 到 1

    所以我想做的是使用 OpenGL 和 C 将纹理渲染到平面上 作为显示图像的一种方式 但是我需要确保在渲染纹理时没有对纹理进行任何处理 抗锯齿 插值 平滑 模糊等 这是 OpenGL 处理渲染纹理的默认方式吗 或者是否需要设置一些标志才能禁
  • 如何停止无限循环?

    我正在编写一个程序 该程序将计算三角形或正方形的面积 然后提示用户是否希望计算另一个 我的代码已经运行到可以计算任一形状的面积的程度 但随后不再继续执行代码的其余部分 例如 如果选择了正方形 则计算面积 然后返回到正方形边长的提示 我假设这
  • 需要提取字符串中点后的最后一个数字,如“7.8.9.1.5.1.100”

    我需要提取 C 字符串中最后一个点后面的最后一个数字 例如 7 8 9 1 5 1 100 并将其存储在整数中 Added 该字符串也可以是 7 8 9 1 5 1 1 或 7 8 9 1 5 1 0 我还想验证它在最后一个点之前恰好是 7
  • 通过 Tab 键浏览 XML 文档字段

    In VB NET you can move through the fields in the XML member documentation with the Tab key 这在 C 中不起作用 还有其他方法吗 除了用鼠标将光标放在
  • 如何得知客户端从服务器的下载速度?

    根据客户的下载速度 我想以低质量或高质量显示视频 任何 Javascript 或 C 解决方案都是可以接受的 Thanks 没有任何办法可以确定 您只能测量向客户端发送数据的速度 如果没有来自客户端的任何类型的输入来表明其获取信息的速度 您
  • INotifyPropertyChanged 和 propertyName

    我一直不确定它的含义propertyName实施时INotifyPropertyChanged 所以一般来说你实现INotifyPropertyChanged as public class Data INotifyPropertyChan

随机推荐