简单测试数据生成器

  出编程题目的过程中,经常遇到数据的生成,很多时候需要大量的数据,要自己写太麻烦,所以想到了写一个数据生成器,不过写的有些简陋,哈哈,有共同困难的一起讨论下写个更完善的。

下面是代码:

View Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_LENGTH    1000
#define MAX_LENGTH_MAT    200
int choice( );
void select( int ch );
void Integer( );
void Char( );
void String( );
void Float();
void Matrices( );
void UserDefine( );

int main()
{
    select( choice() );
    system("pause");
    return 0;
}

int choice( )
{
    char ch;
    printf("pleas chose what type of data you want to create!\n\n");
    printf("Integer                             1\n\n");
    printf("char                                2\n\n");
    printf("string                              3\n\n");
    printf("float                               4\n\n");
    printf("matrices                            5\n\n");
    printf("userdefine                          6\n\n");
    printf("Please chose:");
    scanf("%c",&ch);
    if((int)ch <= 48 || (int)ch >= 56 ){
        printf("Your choice isn't true!");
        return 0;
    }
    return (int)ch - '0';
}
void select( int ch )
{
    switch( ch )
    {
        case 1:
            Integer( );
            break;
        case 2:
            Char( );
            break;
        case 3:
            String( );
            break;
        case 4:
            Float( );
            break;
        case 5:
            Matrices( );
            break;
        case 6:
            UserDefine( );
            break;
        default:
            break;
    }
}
void Integer( )
{
// Open file
    FILE *fp;
    int rewrite = 0;
    if( (fp = fopen("data.out","r")) != 0 ){
        printf("rewrite it ? ( 1 = yes, 2 = no)");
        scanf("%d",&rewrite);
        if( rewrite == 1 )
            fp = fopen("data.out","w");
        else{
            printf("clean the data !\n");
            system("pause");
            exit(1);    
        }
    }
    else{
        fp = fopen("data.out","w");
    }
// Create data
    long max_num;
    long    numbers[MAX_LENGTH] = {0};
    int line_num = 0;
    srand( time(0));
    printf("Please input the max integer of the data !\n");
        scanf("%d",&max_num);
    printf("How many number in one line ?\nPlase input:");
        scanf("%d",&line_num);
    for( long i = 0; i < MAX_LENGTH; i++ )
    {
        numbers[ i ] = rand( ) % max_num;
    }
    
// Write data into file
    for( long i = 1; i <= MAX_LENGTH; i++ ){
        fprintf(fp,"%ld ",numbers[ i - 1]);
        if( i % line_num == 0 )
        fprintf(fp,"\n");
        }
// close file
    fclose(fp);
}
void Char( )
{
// Open file
    FILE *fp;
    int rewrite = 0;
    if( (fp = fopen("data.out","r")) != 0 ){
        printf("rewrite it ? ( 1 = yes, 2 = no)");
        scanf("%d",&rewrite);
        if( rewrite == 1 )
            fp = fopen("data.out","w");
        else{
            printf("clean the data !\n");
            system("pause");
            exit(1);    
        }
    }
    else{
        fp = fopen("data.out","w");
    }
// Create and puts data
    int chars[MAX_LENGTH];
    long ch;
    long chose;
    srand( time(0));    
    for( long i = 0; i < MAX_LENGTH; i++ )
    {
        chars[ i ] = rand( ) % 127;
    }
    int line_num, isEnter = 1,isEnterFlag = 1;
    printf("How many characters in a single line ?\nPlease input:");
        scanf("%d",&line_num);
    printf("You want to create only letter or all ascii characters ?( 1 = Letter, 2 = ascii )\nPlease input:");
        scanf("%d",&chose);
    if( chose == 1 ){
        for( long i = 1; i <= MAX_LENGTH; i++ )
        {
            if( ( chars[ i - 1 ] >= 65 && chars[ i - 1 ] <= 90 ) || ( chars[ i - 1 ]) >= 97 && chars[ i - 1 ] <= 122 )
            {
                fprintf(fp,"%c ",(char)chars[i-1] );
                isEnter++;
                isEnterFlag = 1;
            }
            if( i % line_num == 0 && isEnterFlag ){
                fprintf(fp,"\n");
                isEnterFlag = !isEnterFlag;
            }
        }
    }
    else if( chose == 2 ){
        for( long i = 1; i <= MAX_LENGTH; i++ )
        {
            fprintf(fp,"%c ",(char)chars[ i - 1 ]);
            if( i % line_num == 0 )
                fprintf(fp,"\n");    
        }
    }
    else{
        printf("Invalid input\n");
        exit(1);
    }
//close file
    fclose(fp);
}
void String( )
{
// Open file
    FILE *fp;
    int rewrite = 0;
    if( (fp = fopen("data.out","r")) != 0 ){
        printf("rewrite it ? ( 1 = yes, 2 = no)");
        scanf("%d",&rewrite);
        if( rewrite == 1 )
            fp = fopen("data.out","w");
        else{
            printf("clean the data !\n");
            system("pause");
            exit(1);    
        }
    }
    else{
        fp = fopen("data.out","w");
    }
// Create and puts data
    int chars[MAX_LENGTH];
    srand( time(0));    
    for( long i = 0; i < MAX_LENGTH; i++ )
    {
        chars[ i ] = rand( ) % 127;
    }
    int strlong = 0,chosed,esStringlong = 1,isEnter = 1;
    printf("how long the string is ? \nPlease input:");
        scanf("%d",&strlong);
    printf("only upper letters or only lower letters or mixed ?( 1 = upper , 2 = lower ,3 = mixed )\nPlease input:");
        scanf("%d",&chosed);
    if(chosed == 1 )
    {
        for( long i = 1; i <= MAX_LENGTH; i++ )
        {
            if( chars[ i - 1 ] >= 97 && chars[ i - 1] <= 122 )
            {
                fprintf(fp,"%c",(char)chars[i-1] );
                esStringlong++;
                isEnter = 1;
            }
            if( esStringlong % strlong == 0 && isEnter )
            {
                fprintf(fp,"\n");
                isEnter = !isEnter;
            }
            if( i >= MAX_LENGTH - 2)
                esStringlong = 0;
        }
    }
    else if( chosed == 2 )
    {
        for( long i = 1; i <= MAX_LENGTH; i++ ){
            if(  chars[ i - 1 ] >= 65 && chars[ i - 1 ] <= 90 )
            {
                fprintf(fp,"%c",(char)chars[i-1] );
                esStringlong++;
                isEnter = 1;
            }
            if( esStringlong % strlong == 0 && isEnter )
            {
                fprintf(fp,"\n");
                isEnter = !isEnter;
            }
            if( i >= MAX_LENGTH - 2)
                esStringlong = 0;
        }
    }
    else if( chosed == 3 )
    {
        for( long i = 1; i <= MAX_LENGTH; i++ )
        {
            if( (chars[ i - 1 ] >= 65 && chars[ i - 1 ] <= 90 ) || ( chars[ i - 1 ] >= 97 && chars[ i - 1 ] <= 122 ) )
            {
                fprintf(fp,"%c",(char)chars[i-1] );
                esStringlong++;
                isEnter = 1;
            }
            if( esStringlong % strlong == 0 ){
                fprintf(fp,"\n");
                isEnter = !isEnter;
            }
        }    
    }
//close file
    fclose(fp);
}

void Float( )
{
// Open file
    FILE *fp;
    int rewrite = 0;
    if( (fp = fopen("data.out","r")) != 0 ){
        printf("rewrite it ? ( 1 = yes, 2 = no)");
        scanf("%d",&rewrite);
        if( rewrite == 1 )
            fp = fopen("data.out","w");
        else{
            printf("clean the data !\n");
            system("pause");
            exit(1);    
        }
    }
    else{
        fp = fopen("data.out","w");
    }
// Create and puts data
    long max_num;
    long    numbers[MAX_LENGTH] = {0},point[MAX_LENGTH];
    srand( time(0));
    printf("Please input the max integer of the data !\n");
    scanf("%d",&max_num);
    for( long i = 0; i < MAX_LENGTH; i++ )
    {
        numbers[ i ] = rand( ) % max_num;
        point[ i ] = rand( ) % max_num;
    }
    for( long i = 1; i <= MAX_LENGTH; i++ )
    {
        fprintf(fp,"%ld.%ld\t",numbers[ i - 1 ],point[ i - 1]);
        if( i % 20 == 0 )
        fprintf(fp,"\n");
    }
//close file
    fclose(fp);
    
}

void Matrices( )
{
// Open file
    FILE *fp;
    int rewrite = 0;
    if( (fp = fopen("data.out","r")) != 0 ){
        printf("rewrite it ? ( 1 = yes, 2 = no)");
        scanf("%d",&rewrite);
        if( rewrite == 1 )
            fp = fopen("data.out","w");
        else{
            printf("clean the data !\n");
            system("pause");
            exit(1);    
        }
    }
    else{
        fp = fopen("data.out","w");
    }
// Create and puts data
    srand( time(0));
    int mat_num = 0;
    int rows = 0, cols = 0,max_num = 0;
    long mat[MAX_LENGTH_MAT][MAX_LENGTH_MAT];
    printf("How many matrices do you want to create (two dimentional)?\nPlase input:");
        scanf("%d",&mat_num);
    printf("Please input the rows and cols of the matrices:");
        scanf("%d%d",&rows, &cols);
    printf("plese select the matrices max number:");
        scanf("%d",&max_num);
    int k = 0;
    int bigOrShort = 0;
    while( k++ < mat_num )
    {
        for( int i = 0; i < rows; i++ )
        {
            for( int j = 0; j < cols; j++ )
            {
                bigOrShort = rand( ) % 10000;
                if( bigOrShort > 5000 )
                    mat[i][j] = rand() % max_num;
                else
                    mat[i][j] = -(rand() % max_num);
            }
        }
        for( int i = 0; i < rows; i++ )
        {
            for( int j = 0; j < cols; j++ )
            {
                fprintf(fp,"%d\t",mat[i][j]);    
            }
            fprintf(fp,"\n");
        }
        fprintf(fp,"\n");
    }
//close file
    fclose(fp);
}
void UserDefine( )
{
    printf("sorry,function undeveloped,any question? contact lavaric.msc@gmail.com\n");
}

转载于:https://www.cnblogs.com/lavaric/archive/2012/09/04/2669791.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值