环境 VC6.0 控制台
代码
#include<stdio.h>
#include<stdlib.h>
#include <memory.h>
#define SIZE_NODE 100
#define SIZE_BLOCK 15
void main()
{
int n = 0, m = 0;
printf("请输入 节点数(小于等于%d的正整数):",SIZE_NODE);
scanf("%u", &n);
while ( n > SIZE_NODE )
{
printf("节点数超出了范围!\n");
printf("请输入 节点数(小于等于%d的正整数):",SIZE_NODE);
scanf("%u", &n);
fflush(stdin);
}
fflush(stdin);
printf("请输入文件块数(小于等于%d的正整数):",SIZE_BLOCK);
scanf("%u", &m);
while ( m > SIZE_BLOCK )
{
printf("文件块数超出了范围!\n");
printf("请输入文件块数(小于等于%d的正整数):",SIZE_BLOCK);
scanf("%u", &m);
fflush(stdin);
}
printf("\n节点数:%d,文件块数:%d\n",n,m);
/*动态分配二维数组*/
bool isFormatErr = false; //判断格式错误
int max_antiFactor = 0; //最大反分布式系数
int dist_coefficient = 0; //分布式系数
int *antiFactor_arrary = (int*)malloc(sizeof(int)*m); //反分布式系数数组
memset(antiFactor_arrary, 0, m*sizeof(antiFactor_arrary));//初始化数组值为0
char **node_array = (char**) malloc(sizeof(char *)*n);//节点数组
for ( int i = 0; i < n; ++i)
{
node_array[i]=(char *)malloc(sizeof(char)*m);
do
{
printf("请输入第%d个节点(%d位0 1数):",i+1,m);
fflush(stdin);
scanf("%s",node_array[i]);
for (int j = 0 ; j < m ; ++j)
{
if (node_array[i][j] != '0' && node_array[i][j] != '1')
{
isFormatErr = true;
printf("第%d个节点输入了非0 1的非法字符!请重新输入\n",i+1);
break;
}
}
if ( j == m )
{
for (j = 0 ; j < m ; ++j)
{
if (node_array[i][j] == '0')
antiFactor_arrary[j]++;
}
isFormatErr = false;
}
} while (isFormatErr);
}
printf("\n%d %d\n",n,m);
for ( i = 0; i < n; ++i)
{
printf("%s\n",node_array[i]);
free(node_array[i]);
#include<stdlib.h>
#include <memory.h>
#define SIZE_NODE 100
#define SIZE_BLOCK 15
void main()
{
int n = 0, m = 0;
printf("请输入 节点数(小于等于%d的正整数):",SIZE_NODE);
scanf("%u", &n);
while ( n > SIZE_NODE )
{
printf("节点数超出了范围!\n");
printf("请输入 节点数(小于等于%d的正整数):",SIZE_NODE);
scanf("%u", &n);
fflush(stdin);
}
fflush(stdin);
printf("请输入文件块数(小于等于%d的正整数):",SIZE_BLOCK);
scanf("%u", &m);
while ( m > SIZE_BLOCK )
{
printf("文件块数超出了范围!\n");
printf("请输入文件块数(小于等于%d的正整数):",SIZE_BLOCK);
scanf("%u", &m);
fflush(stdin);
}
printf("\n节点数:%d,文件块数:%d\n",n,m);
/*动态分配二维数组*/
bool isFormatErr = false; //判断格式错误
int max_antiFactor = 0; //最大反分布式系数
int dist_coefficient = 0; //分布式系数
int *antiFactor_arrary = (int*)malloc(sizeof(int)*m); //反分布式系数数组
memset(antiFactor_arrary, 0, m*sizeof(antiFactor_arrary));//初始化数组值为0
char **node_array = (char**) malloc(sizeof(char *)*n);//节点数组
for ( int i = 0; i < n; ++i)
{
node_array[i]=(char *)malloc(sizeof(char)*m);
do
{
printf("请输入第%d个节点(%d位0 1数):",i+1,m);
fflush(stdin);
scanf("%s",node_array[i]);
for (int j = 0 ; j < m ; ++j)
{
if (node_array[i][j] != '0' && node_array[i][j] != '1')
{
isFormatErr = true;
printf("第%d个节点输入了非0 1的非法字符!请重新输入\n",i+1);
break;
}
}
if ( j == m )
{
for (j = 0 ; j < m ; ++j)
{
if (node_array[i][j] == '0')
antiFactor_arrary[j]++;
}
isFormatErr = false;
}
} while (isFormatErr);
}
printf("\n%d %d\n",n,m);
for ( i = 0; i < n; ++i)
{
printf("%s\n",node_array[i]);
free(node_array[i]);
}
max_antiFactor = antiFactor_arrary[0];
for (int j = 1 ; j < m ; ++j)
{
if (antiFactor_arrary[j] > max_antiFactor)
max_antiFactor = antiFactor_arrary[j];
}
dist_coefficient = n - max_antiFactor;
printf("分布式系数为:%d\n",dist_coefficient);
free(antiFactor_arrary);
free(node_array);
}
max_antiFactor = antiFactor_arrary[0];
for (int j = 1 ; j < m ; ++j)
{
if (antiFactor_arrary[j] > max_antiFactor)
max_antiFactor = antiFactor_arrary[j];
}
dist_coefficient = n - max_antiFactor;
printf("分布式系数为:%d\n",dist_coefficient);
free(antiFactor_arrary);
free(node_array);
}