生成50道100以内加法/减法算式的习题_软件构造_羊卓的杨

废话咱们就不多讲了,直接上代码,看完的老铁记得给个收藏关注❤~。

下面这两个平台也求一波关注,最近两年内有更新

bilibili:羊卓的杨(链接)
新浪微博:羊卓的杨(链接)

#include 
#include 
#include 
#include 
using namespace std;

void showMainMenu(int left, int right);//显示主菜单
void showExercise(int left, int right);//显示习题
void createExercise(int left, int right);//设计习题
void caculateAnswer(int left, int right);//计算答案
void showAnswer(int left, int right);//显示答案
int getRandom();//获取随机数
int same_judge[200];

int main() {
	int choice=0;
	memset(same_judge,0,sizeof(same_judge));//为查重数组赋初始值 
	do {
		if(choice==1)
			system("cls");
		int left, right;
		left = getRandom();
		right = left+1000;
		showMainMenu(left, right);
		cout << "\n\n\t\t\t\t    ========是否还要继续进行操作?(1:继续 | 0:退出)========" << endl;
		cin >> choice;
		memset(same_judge,0,sizeof(same_judge));
	} while(choice);
	return 0;
}

void showMainMenu(int left, int right) {//主菜单显示
	cout << "\t\t\t\t\t\t---------口算习题----------" << endl;
	cout << "\t\t\t\t\t\t|      1.生成口算题(两套) |" << endl;
	cout << "\t\t\t\t\t\t|      2.显示答案         |" << endl;
	cout << "\t\t\t\t\t\t|      3.退出             |" << endl;
	cout << "\t\t\t\t\t\t---------------------------" << endl;
	char choice;
	cin >> choice;
	while((choice-'0')<1 || (choice-'0')>3 || !(isdigit(choice))) {
		cout << "请正确输入:" << endl;
		cin >> choice;
	}
	switch(choice) {
		case '1':
			showExercise(left, right);
			break;
		case '2':
			showAnswer(left, right);
			break;
		case '3':
			exit(-1);
	}
}

int getRandom() {//获取随机数
	int left;
	do {
		srand(time(0));
		left = (rand()+26314)%10000;
	} while(left<=1000);
	return left;
}

void showExercise(int left, int right) {//习题显示
	cout << "\t\t\t\t\t\t================================" << endl;
	cout << "\t\t\t\t\t\t|50道100以内的混合加减运算(一)|" << endl;
	cout << "\t\t\t\t\t\t================================" << endl;
	createExercise(left, right);
	//cout << "QQ:653731317" << endl;
	cout << "\n\t\t\t\t\t\t================================" << endl;
	cout << "\t\t\t\t\t\t|50道100以内的混合加减运算(二)|" << endl;
	cout << "\t\t\t\t\t\t================================" << endl;
	createExercise(left+123, right+567);
	//羊卓的杨 -> 杨卓 -> 青岛大学Java班 
}

void createExercise(int left, int right) { //设计习题
	int a,b, total=1, flag=0;
	for(int i=left; i<right, total<51; i++) {
		srand(i);
		a = rand()%100;
		b = rand()/100%100;
		if(a==0 || b==0)
			continue;
		if(flag==0)//格式对齐
			cout << "\t\t\t\t";
		flag=1;
		if(same_judge[a+b]!=0)//查重
			continue;
		if(a+b>100 && a>b) {
			same_judge[a+b]=a-b;//记录下啦这组数
			printf("%2d:%2d-%2d=    ",total,a,b);
			if(total%5==0 && total!=50)//格式换行对齐
				cout << "\n\t\t\t\t";
			total++;
		} else if(a+b<100) {
			same_judge[a+b]=a-b;//记录下来这组数
			printf("%2d:%2d+%2d=    ",total,a,b);
			if(total%5==0 && total!=50)//格式换行对齐
				cout << "\n\t\t\t\t";
			total++;
		}
	}
	//cout << "微博:羊卓的杨" << endl;
}

void showAnswer(int left, int right) {//答案显示
	cout << "\t\t\t\t\t\t================================" << endl;
	cout << "\t\t\t\t\t\t|     第一套习题参考答案:     |" << endl;
	cout << "\t\t\t\t\t\t================================" << endl;
	caculateAnswer(left, right);
	cout << "\n\t\t\t\t\t\t================================" << endl;
	cout << "\t\t\t\t\t\t|     第二套习题参考答案:     |" << endl;
	cout << "\t\t\t\t\t\t================================" << endl;
	caculateAnswer(left+123, right+567);
	//羊卓的杨 -> 杨卓 -> 青岛大学Java班 
}

void caculateAnswer(int left, int right) { //计算答案
	int a,b, total=1, flag=0;
	for(int i=left; i<right, total<51; i++) {
		srand(i);
		a = rand()%100;
		b = rand()/100%100;
		if(a==0 || b==0)
			continue;
		if(flag==0)//格式对齐
			cout << "\t\t\t\t";
		flag=1;
		if(same_judge[a+b]!=0)//查重
			continue;
		if(a+b>100 && a>b) {
			same_judge[a+b]=a-b;//记录下来这组数
			printf("%2d:%2d-%2d=%2d    ",total,a,b,a-b);
			if(total%5==0 && total!=50)//格式换行对齐
				cout << "\n\t\t\t\t";
			total++;
		} else if(a+b<100) {
			same_judge[a+b]=a-b;//记录下来这组数
			printf("%2d:%2d+%2d=%2d    ",total,a,b,a+b);
			if(total%5==0 && total!=50)//格式换行对齐
				cout << "\n\t\t\t\t";
			total++;
		}
		//cout << "bilibili:羊卓的杨\n" << endl;
	}
}

你可能感兴趣的:(【软件构造_青岛大学】,c++)