#include "stdafx.h"
#include
int main()
{
int a, b, c, d, e, i = 0;
for (a = 1; a <= 15; a++)
for (b = 1; b <= 15; b++)
for (c = 1; c <= 15; c++)
for (d = 1; d <= 15; d++)
for (e = 1; e <= 15; e++)
if (b - a > 0 && c - b > 0 && d - c > 0 && e - d > 0)
{
printf("A:%2d B:%2d C:%2d D:%2d E:%2d ", a, b, c, d, e);
i++;
if (i % 1 == 0)
printf("\n");
}
printf("一共%d种\n", i);
return 0;
}
把这一段代码的运行结果输出到,E盘上一个叫1.txt的文本文档上。
要求:1.用C语言。2,在我给的代码的基础上修改。
// Q689426.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
FILE *fpWrite=fopen("1.txt","w");
int a, b, c, d, e, i = 0;
for (a = 1; a <= 15; a++)
for (b = 1; b <= 15; b++)
for (c = 1; c <= 15; c++)
for (d = 1; d <= 15; d++)
for (e = 1; e <= 15; e++)
if (b - a > 0 && c - b > 0 && d - c > 0 && e - d > 0)
{
printf("A:%2d B:%2d C:%2d D:%2d E:%2d ", a, b, c, d, e);
fprintf(fpWrite, "A:%2d B:%2d C:%2d D:%2d E:%2d ", a, b, c, d, e);
//如果要换行 fprintf(fpWrite, "A:%2d B:%2d C:%2d D:%2d E:%2d \n", a, b, c, d, e);
i++;
if (i % 1 == 0)
printf("\n");
}
printf("total: %d\n", i);
fprintf(fpWrite, "total: %d\n", i);
fclose(fpWrite);
return 0;
}
运行效果。
但是非常强烈建议你用C#,我的代码,不但简洁,而且非常容易修改,可以一劳永逸解决你今后的问题。
你可以很灵活的指令输出文件,数字范围,以及输出格式,只需要在运行时添加额外的参数即可:
usage: prog-name [range] [path] [format]
其中prog-name是程序名, range是数字范围(>=5), path是文件路径, format是输出格式(必须包含5个%d). 第一个参数是必须的,后两个可选(有默认值).
关键是output_range()
#include <stdio.h>
#include <stdlib.h>
/*
Paramters:
@range: number range, travel numbers in [1, 2, ..., range]
@path: output file path, if path is NULL, use stdout
@format: output format which includes < "%d" * 5 >, if format
is NULL, use "A:%2d B:%2d C:%2d D: %2d E:%2d "
Example:
unsinged cnt;
cnt = output_range(range, path, format);
*/
unsigned output_range(unsigned range, char *path, char *format)
{
if (range < 5)
return 0;
FILE *out = NULL;
if (path)
{
out = fopen(path, "w");
}
if (!out)
{
/* Use stdout as default file */
out = stdout;
}
if (!format)
{
format = "A:%2d B:%2d C:%2d D:%2d E:%2d ";
}
/* Use type 'unsigned' instead of int */
unsigned a, b, c, d, e;
unsigned cnt;
for (a = 1; a <= range; a++)
for (b = a+1; b <= range; b++)
for (c = b+1; c <= range; c++)
for (d = c+1; d <= range; d++)
for (e = d+1; e <= range; e++)
{
fprintf(out, format, a, b, c, d, e);
cnt++;
fprintf(out, "\n");
}
fprintf(out, "Total: %d\n", cnt);
/* Flush file data */
fflush(out);
fclose(out);
return cnt;
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
fprintf(stderr, "Usage: %s <range> [output_path] [format]\n", argv[0]);
return -1;
}
int range = atoi(argv[1]);
char *path = NULL;
char *format = NULL;
if (argc > 2)
path = argv[2];
if (argc > 3)
format = argv[3];
output_range(range, path, format);
return 0;
}
改一改就可以用了,保存到e盘下1.txt:
int _tmain(int argc, _TCHAR* argv[])
{
FILE *fpWrite;
fopen_s(&fpWrite,"e:\\1.txt", "w");
int a, b, c, d, e, i = 0;
for (a = 1; a <= 15; a++)
for (b = 1; b <= 15; b++)
for (c = 1; c <= 15; c++)
for (d = 1; d <= 15; d++)
for (e = 1; e <= 15; e++)
if (b - a > 0 && c - b > 0 && d - c > 0 && e - d > 0)
{
printf("A:%2d B:%2d C:%2d D:%2d E:%2d ", a, b, c, d, e);
fprintf(fpWrite, "A:%2d B:%2d C:%2d D:%2d E:%2d ", a, b, c, d, e);
//如果要换行 fprintf(fpWrite, "A:%2d B:%2d C:%2d D:%2d E:%2d \n", a, b, c, d, e);
i++;
if (i % 1 == 0)
printf("\n");
}
printf("total: %d\n", i);
fprintf(fpWrite, "total: %d\n", i);
fclose(fpWrite);
return 0;
}
int main()
{
FILE *fp;
fp=fopen("1.txt","w");
int a, b, c, d, e, i = 0;
for (a = 1; a <= 15; a++)
for (b = 1; b <= 15; b++)
for (c = 1; c <= 15; c++)
for (d = 1; d <= 15; d++)
for (e = 1; e <= 15; e++)
if (b - a > 0 && c - b > 0 && d - c > 0 && e - d > 0)
{
printf("A:%2d B:%2d C:%2d D:%2d E:%2d ", a, b, c, d, e);
fprintf(fp,"A:%2d B:%2d C:%2d D:%2d E:%2d ", a, b, c, d, e); //同输出printf一样,以格式方式输出到文本中
i++;
if (i % 1 == 0)
printf("\n");
}
printf("一共%d种\n", i);
fprintf(fp,"一共%d种\n", i); 同输出printf一样,以格式方式输出到文本中
fclose(fp);//关闭流
printf("###");
return 0;
}
可以直接将你的c语言程序放在e盘,然后运行代码,会直接在E盘存储,可以在E盘直接找到。