看看这个,是否符合要求?
程序代码:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<windows.h>
int main(int argc, char* argv[])
{
srand((unsigned)time(NULL));
int num1,num2,sign;
int n;
float m;
cout<<"您这次要出几道题?";
cin>>n;
FILE* fp1;
FILE* fp2;
fp1=fopen("f:\\试题.txt","w");
fp2=fopen("f:\\试题答案.txt","w");
for(int i=0;i<n;i++){
num1=rand()%100;
num2=rand()%100;
sign=rand()%4;
switch(sign){
case 0:
fprintf(fp1,"%2d+%2d=\n",num1,num2);
fprintf(fp2,"%2d+%2d=%d\n",num1,num2,num1+num2);
break;
case 1:
if(num1<num2){
m=num1;
num1=num2;
num2=m;
}
fprintf(fp1,"%2d-%2d=\n",num1,num2);
fprintf(fp2,"%2d-%2d=%d\n",num1,num2,num1-num2);
break;
case 2:
fprintf(fp1,"%2d*%2d=\n",num1,num2);
fprintf(fp2,"%2d*%2d=%d\n",num1,num2,num1*num2);
break;
case 3:
fprintf(fp1,"%2d/%2d=\n",num1,num2);
fprintf(fp2,"%2d/%2d=%.2f\n",num1,num2,(float)num1/num2);
break;
}
}
fclose(fp1);
fclose(fp2);
return 0;
}