从TXT阵列在C(长数字) [英] arrays from txt in c (long numbers)

查看:109
本文介绍了从TXT阵列在C(长数字)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

4000亿; 499999999999; VISA;

400000000000;499999999999;VISA;

5000; 59999999; MASTERCARD;

50000000;59999999;MASTERCARD;

6700; 67999999; MAESTRO;

67000000;67999999;MAESTRO;

字段:1.范围开始2.范围结束,3名

fields: 1. Range Start 2. Range End, 3 Name.

[开始范围]和[结束范围]字段可以由长度为1至16个字符(数字)。

[Start Range] and [End Range] fields can be from 1 to 16 characters (digits) in length.

该计划的目标如下:

第一次请求输入16位卡号。

First Request to enter 16-digit card number.

 Card number input, verification and processing use char [n] type (Simbol array)

二:检查可以在文本文件中找到对应输入卡号的条目,如果我进入450亿是4000亿和499999999999之间,所以我需要把一个文本在autput名签证。
我不能用很长很长......类型,因为我undrstand我需要使用数组...
第三请求,要求其格式为nnnn.mm,其中NNNN-1至4位数字金额拉特,但是毫米输入金额 - 2位量santims

Second:Check for an entry corresponding to the entered card number can be found in a text file if I enter 45000000000 it's between 400000000000 and 499999999999 so i need to put a text in a autput name VISA. And i can't use long long types... as i undrstand i need to use arrays... Third Request to enter the amount in the format "nnnn.mm", where nnnn-1 to 4 digits long amount of lats, but mm - 2-digit amount santims.

char input[32]; // = "100;200;first";
char name[10];
int min, max, c, i, k;

FILE *file;
file = fopen("gg.txt","r");

i=0;

while ((c=getc(file))!= EOF)
{
    k=(int)c;
    input[i]=k;
    i++;
}

char* result = NULL;
char delims[] = ";";

result = strtok(input, delims);

// atoi() converts ascii to integer.
min = atoi(result);
result = strtok(NULL, delims);
max = atoi(result);

result = strtok(NULL, delims);

strcpy(name, result);

printf("Min=%d, Max=%d, Name=%s\n", min, max, name);
printf("input=%s\n",input);
printf("%d\n",i);

getch();
return 0;

这code。通过varunl给了我工作的圣维特SMaL公司数量(包含gg.txt文件为:100; 200;第一),但需要其他的SMT,enybody,能不能帮我。

this code given me by varunl works vith smal numbers ( the containing of gg.txt file is: 100;200;first), but a need smt else, enybody, can help me?

推荐答案

诀窍是在填充的号码16位数,所以你可以为字符串比较它们。所以,如果你这样说的:

The trick is in padding the numbers to 16 digits, so you can compare them as strings. So if you read this:

67000000;67999999;MAESTRO;

在现实中,你不得不考虑这样的:

in reality you have to consider it like this:

0000000067000000;0000000067999999;MAESTRO;

同为用户的输入。

The same for the user input.

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

void FindCCType(const char *pnumber);

int main(int argc, char *argv[])
{
    FindCCType("67000001");
    return 0;
}

#define MAXLENGTH 16

char* PadLeft(const char *pnumber)
{
    char *pnumber2 = (char*)malloc(MAXLENGTH + 1);
    size_t len = strlen(pnumber);

    if (len > MAXLENGTH)
    {
        /* Too many digits in credit card number */
        exit(1);
    }

    strcpy(pnumber2 + MAXLENGTH - len, pnumber);

    char *pbegin = pnumber2, *plast = pnumber2 + MAXLENGTH - len;

    while (pbegin < plast)
    {
        *pbegin = '0';
        pbegin++;
    }

    return pnumber2;
}

void FindCCType(const char *pnumber)
{
    printf("Input: %s\n", pnumber);

    char *pnumber2 = PadLeft(pnumber);

    FILE *file = fopen("gg.txt","r");

    char pline[1000];

    while (fgets(pline, sizeof(pline), file) != NULL)
    {
        if (strlen(pline) + 1 == sizeof(pline)) 
        {
            /* Line too much long */
            exit(2);
        }

        char *pmin = strtok(pline, ";");
        char *pmax = strtok(NULL, ";");
        char *pname = strtok(NULL, ";");

        printf("Min: %s, Max: %s, Name: %s", pmin, pmax, pname);

        char *pmin2 = PadLeft(pmin);
        char *pmax2 = PadLeft(pmax);

        if (strcmp(pnumber2, pmin2) >= 0 && strcmp(pnumber2, pmax2) <= 0)
        {
            printf(" ** FOUND **\n");

            free(pmin2);
            free(pmax2);
            break;
        }

        printf(" ** NO GOOD **\n");

        free(pmin2);
        free(pmax2);
    }

    fclose(file);
    free(pnumber2);
}

这篇关于从TXT阵列在C(长数字)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆