复合数组是不是不能设太大?比如m[20000][200000],不过我要读的数据有可能达到2万,该怎么办?
// re.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define FILENAME_SIZE 512
#define LINE_SIZE 512
#define DATA_SIZE 20000
int main()
{
int i = 0;
int k = 0;
int m = 0;/* divident */
int n = 0;/* dividor */
int r = 0;/* remainder */
int a = 0;
int gcd = 0;/* great common divider */
int lcm = 0;/* least common multiplier */
int column = 0;
int row = 0;
int count = 0;
int Length1 = 0;/* length of first sequence */
int Length2 = 0;/* lencth of second sequence */
int this_line_is_data;
int degree = 0;/* degree of sequence */
int addresult =0;
char word[] = ".txt";/* file type */
char temp[100];
char Infile1[FILENAME_SIZE];/* first input file name */
char Infile2[FILENAME_SIZE];/* second input file name */
char Outfile[FILENAME_SIZE];/* output file name */
char buf[LINE_SIZE];
int sequence[DATA_SIZE][DATA_SIZE];/* new sequences */
int matrix[DATA_SIZE][DATA_SIZE];/* generate matrix */
int data1[DATA_SIZE];/* first sequence */
int data2[DATA_SIZE];/* second sequence */
FILE *fp = NULL;
char *ptr = NULL;
//Read the first sequence
printf("\nEnter first file path\n");/* appoint file */
gets(Infile1);
fp = fopen(Infile1, "r");/* open file */
if (fp == NULL)
{
printf("Failed to open file %s\n", Infile1);
return 1;
}
while (1)
{
if (fgets(buf, LINE_SIZE, fp) == NULL) /* reed one line of file buf */
break; /* if fgets==NULL,it is the end of file,then break */
this_line_is_data = 1;
for (i = 0; buf[i]; i++) /* judge the content of the read line*/
{
if (!isdigit(buf[i]) && !isspace(buf[i]))
{ /* if there is any character of this line is not digit or space, ignore this line */
this_line_is_data = 0;
break;
}
}
if (!this_line_is_data)
continue; /* swith to next line */
ptr = buf;/* now is sequence line */
while (1)
{ /* read digits */
while (isspace(*ptr))
ptr++; /*ignore space*/
if (sscanf(ptr, "%d", data1+Length1) <= 0) /* if it is the end of this line read another line */
break;
Length1++;
while (isdigit(*ptr))
ptr++;
}
}
fclose(fp);/* close file */
printf("The length of first sequence is %d.\n\nAnd the sequence is\n", Length1);/* print lenght of sequence on screen */
for (i = 0; i < Length1; i++)/* print sequence */
printf("%d ", data1[i]);
printf("\n\n");
//Read the second sequence using the same technique as first sequence
printf("\nEnter second file path\n");
gets(Infile2);
fp = fopen(Infile2, "r");
if (fp == NULL)
{
printf("Failed to open file %s\n", Infile2);
return 1;
}
while (1)
{
if (fgets(buf, LINE_SIZE, fp) == NULL)
break;
this_line_is_data = 1;
for (i = 0; buf[i]; i++)
{
if (!isdigit(buf[i]) && !isspace(buf[i]))
{
this_line_is_data = 0;
break;
}
}
if (!this_line_is_data)
continue;
ptr = buf;
while (1)
{
while (isspace(*ptr))
ptr++;
if (sscanf(ptr, "%d", data2+Length2) <= 0)
break;
Length2++;
while (isdigit(*ptr))
ptr++;
}
}
fclose(fp);
printf("The length of second sequence is %d.\n\nAnd the sequence is\n", Length2);
for (i = 0; i < Length2; i++)
printf("%d ", data2[i]);
printf("\n\n");
//Porcess sequences
printf("Enter the degree of the sequences\n");/* set degree of sequence */
scanf("%d",degree);
if(Length1==Length2)
{/* Tow squences can not in the same length */
printf("\n\nWrong sequences! These tow sequence can not be same length\n");
return 1;
}
//Put sequences into matrix
for(i = 0; i < Length2; i++)
{/* first sequence to column */
for(k = 0; k < Length1; k++)
{/* second sequence to row */
addresult = data1[k]+data2[i];/* adding digit of sequences */
if(addresult < degree)
matrix[i][k] = addresult;
else
matrix[i][k] = addresult-degree;
}
}
//Set first column as degree/2 or (degree-1)/2
for(i = 0; i < Length1; i++)
{
if(degree%2 == 0)
matrix[1][i] = degree/2;
else
matrix[1][i] = (degree-1)/2;
}
//Set first row as 0
for(i = 0; i < Length2; i++)
matrix[i][0] = 0;
//Use Euclid arithmetic to calculate gcd & lcm
m = Length1;
n = Length2;
if(n > m)/* Let m alway larger than n */
{
a = n;
n = m;
m = a;
}
if(m%n == 0)/* calculate the gcd and lcm of length1 and length2 */
{
lcm = m;
gcd = n;
}
else
{
while(m%n == 0)/* gcd(m,n)=gcd(n,r) when r is the remain */
{
r = m%n;
m = n;
n = r;
}
gcd = n;
lcm = (Length1*Length2)/n;/* lcm=(m*n)/gcd */
}
//Get new sequences
column = 0;
row = 0;
for(i = 0; i < gcd; i++)
{/* the number of sequences is gcd */
for(k = 0; k < lcm; k++)
{/* the length of each sequence is lcm */
sequence[i][k] = matrix[column][row];/* get sequences slanting */
column++;
row++;
if(row = Length1)
row = 0;
if(column = Length2)
column = 0;
}
}
//Output sequences to files
printf("\nEnter the path to store new sequences without file name or \".txt\"\n");/* set output file path */
gets(Outfile);
for(i = 0; (Outfile[i] = tolower(Outfile[i])) != '/0'; i++);/* to lower the path namen */
while(strstr(Outfile, word) != NULL)/* search ".txt" in the file path, if it is exist, print fail and enter again */
{
printf("Wrong path!\n");
printf("\nEnter the path to store new sequences without file name or \".txt\"\n");/* set output file path again */
gets(Outfile);
for(i = 0; (Outfile[i] = tolower(Outfile[i])) != '/0'; i++);
}
while(Outfile[count] != '\0')/* count the number of letters of the path */
count++;
for(i=0; i < gcd; i++)
{/* output each sequence to one file */
temp = itoa(i+1, temp, 10);/* integer to array */
Outfile = strcat(Outfile, temp);/* copy the file number to the end of the path */
Outfile = strcat(Outfile, word);/* set the file as txt file */
fp = fopen(Outfile, "w");/* open file */
if (fp == NULL)
{
printf("Failed to open file %s\n", Infile1);
return 1;
}
fprintf(fp, "!New sequence is generated, this is the %d sequence and its degree = %d\n\n", i, degree);
for(k=0; k < lcm; k++)/* sequence write on the file */
fprintf(fp, "%d ", sequence[i][k]);
fprintf(fp, "\n\n!Length of this sequence is %d", lcm);
}
}