Thứ Sáu, 4 tháng 12, 2015

Chương trình kiểm tra dữ liệu nhập là số.

Kiểm tra dữ liệu có phải là số nguyên, số thực (cả âm) hay không, nếu đúng thì in ra kết quả là số đó nhân 2, còn không thì nhập lại.

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main(void)
{
    char n[10];
    int i, a, c=0;
    float b;
    FILE *f1, *f2;
again:
    printf("\nNhap so: ");
    gets(n);

    i = strlen(n);

    if ((n[0] == 45 && i == 1) || (n[0] == 46))
    {
        printf("Nhap sai. Nhap lai.");
        goto again;
    }

    for (int j = 0; j < i; j++)
    {
        if (n[j]<45 || n[j]>57)
        {
            printf("\nNhap sai. Nhap lai.");
            goto again;
        }
        if (n[j] == 46)
            c = 1;
    }

    if ( (f1 = fopen("test1.txt", "w")) != NULL)
        printf("\nMo duoc file 1.");
    else printf("\nKhong mo dc file 1.");

    fprintf(f1, "%s", n);
    fclose(f1);

    if ((f2 = fopen("test1.txt", "r")) != NULL)
        printf("\nMo duoc file 2.");
    else printf("\nKhong mo dc file 2.");
   
    if (c == 1)
    {
        fscanf(f2, "%f", &b);
        printf("So da nhap nhan 2: %f", b*2);
    }
    else
    {
        fscanf(f2, "%d", &a);
        printf("So da nhap nhan 2: %d", a*2);
    }
    fclose(f2);
    getch();
}

1 nhận xét:

  1. Harrah's Casino & Racetrack - Mapyro
    Search for 포항 출장샵 Harrah's Casino 사천 출장샵 & 안동 출장안마 Racetrack in Elgin, Illinois. Find reviews, hours, directions, coupons and 안산 출장샵 more for Harrah's Casino & Racetrack 공주 출장안마 in Elgin, IL.

    Trả lờiXóa