c - 长方形的面积万无一失

标签 c foolproof-validation

谁能帮我算一下长方形的面积。我无法做到万无一失,即禁止输入字母和负数。我只是无法在创建引用字母的第一个条件时创建第二个条件。我该怎么办呢,或者我什至必须从头开始更改程序。我只需要求出矩形的面积即可。

#include "stdafx.h"
#include <stdio.h>
#include <math.h>
#include <locale.h>
#include <Windows.h>

int _tmain(int argc, char* argv[])
{
    printf("Area of a Rectangle. Press Enter to start\n");
    system("pause");

    float a, s, d;

    do
    {
        fflush(stdin);
        system("cls");
        printf("Enter the lengths of the rectangle\n");
        scanf_s("%f", &a);
        scanf_s("%f", &s);

        if (getchar() != '\n') {
            while (getchar() != '\n')
                ;
            printf("Your data is wrong\n");
            system("pause");
            continue;
        }

        break;
    } while (true);

    d = a*s;
    printf(" Area is %.1f ", d);
    system("pause");
    return 0;
}

最佳答案

该程序将要求输入一个数值,并且只接受正值。输入字母或符号将导致 scanf() 失败,程序将清除输入缓冲区并重试。在此示例中,输入字母“q”将退出程序。您可以根据您的情况进行调整。

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


int main()
{
    int i = 0;
    float n = 0.0f;

    do {
        printf("enter numeric value or q to quit\n");
        if ( scanf("%f",&n) != 1) {// scan one float
            while ( ( i = getchar ( )) != '\n' && i != 'q') {
                //clear buffer on scanf failure
                //stop on newline
                //quit if a q is found
            }
            if ( i != 'q') {
                printf ( "problem with input, try again\n");
            }
        }
        else {//scanf success
            if ( n == fabs ( n)) {
                printf("number was %f\n", n);
            }
            else {
                printf("positive numbers only please\n");
            }
        }
    } while ( i != 'q');

    return 0;
}

这已将上述内容改编为函数。

#include <stdio.h>

float getfloat ( char *prompt, int *result);

int main ( int argc, char* argv[])
{
    float width, height, area;
    int ok = 0;

    do {
        printf("\n\tArea of a Rectangle.\n");
        width = getfloat ( "Enter the width of the rectangle or q to quit\n", &ok);
        if ( ok == -1) {
            break;
        }
        height = getfloat ( "Enter the height of the rectangle or q to quit\n", &ok);
        if ( ok == -1) {
            break;
        }

        area = width * height;
        printf(" Area is %.1f\n", area);
    } while (1);

    return 0;
}

float getfloat ( char *prompt, int *result)
{
    int i = 0;
    float n = 0.0f;

    *result = 0;

    do {
        printf("%s", prompt);
        if ( scanf("%f",&n) != 1) {// scan one float
            while ( ( i = getchar ( )) != '\n' && i != 'q') {
                //clear buffer on scanf failure
                //stop on newline
                //quit if a q is found
            }
            if ( i != 'q') {
                printf ( "problem with input, try again\n");
            }
            else {
                *result = -1;
                n = 0.0f;
            }
        }
        else {//scanf success
            if ( n == fabs ( n)) {
                *result = 1;
            }
            else {
                printf("positive numbers only please\n");
            }
        }
    } while ( *result == 0);

    return n;
}

关于c - 长方形的面积万无一失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32444898/

相关文章:

c - postgresql 的 Windows 安装错误。 make命令出错

c - 为什么动态分配的int数组在scanf、c语言中使用&?

c - main() 是否可以接受任何其他参数?

asp.net-mvc - 手动触发万无一失的验证

c# - 将万无一失的 RequiredIf 与枚举一起使用

javascript - 添加自定义验证时出现"Sys is not defined"错误(MVC万无一失的验证)

c - 当您引用数组的未定义元素时,C 中会发生什么?

c - string FILE stdio 兼容吗?