// test.cpp : 定義控制檯應用程式的入口點。
// 在vc2008下除錯透過
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
int main()
{
FILE * myfile;
myfile = fopen("e:\\test.txt","w");
char result[50];
char op;
int i = 0;
while( i++
int a = rand() % 10;
int b = rand() % 19; // 生成 0 - 18 b為 0 - 9做加法 10 - 18 做減法
b = 9 - b;
int r = a + b;
if( b > 0)
op = "+";
else
op = "-";
b = abs(b);
sprintf(result,"%d %c %d = %d\n",a,op,b,r);
fputs(result,myfile);
}
fclose(myfile);
return 0;
部分結果如下
1 - 9 = -8
4 - 5 = -1
9 - 2 = 7
8 + 6 = 14
2 - 2 = 0
5 + 3 = 8
1 - 3 = -2
1 - 7 = -6
5 - 1 = 4
7 + 7 = 14
2 + 8 = 10
2 - 4 = -2
不過建議自己多思考,祝你成功
// test.cpp : 定義控制檯應用程式的入口點。
// 在vc2008下除錯透過
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
int main()
{
FILE * myfile;
myfile = fopen("e:\\test.txt","w");
char result[50];
char op;
int i = 0;
while( i++
{
int a = rand() % 10;
int b = rand() % 19; // 生成 0 - 18 b為 0 - 9做加法 10 - 18 做減法
b = 9 - b;
int r = a + b;
if( b > 0)
op = "+";
else
op = "-";
b = abs(b);
sprintf(result,"%d %c %d = %d\n",a,op,b,r);
fputs(result,myfile);
}
fclose(myfile);
return 0;
}
部分結果如下
1 - 9 = -8
4 - 5 = -1
9 - 2 = 7
8 + 6 = 14
2 - 2 = 0
5 + 3 = 8
1 - 3 = -2
1 - 7 = -6
5 - 1 = 4
7 + 7 = 14
1 - 3 = -2
2 + 8 = 10
2 - 4 = -2
不過建議自己多思考,祝你成功