C语言编程:100以内的进位加法和借位减法?

要求:
1.题目随机生成,每页34题,左右各17题,形如82-47= 23+18= 。
2.输出多页,页数由键盘输入。
3.每页有眉头,形如“班级: 姓名: 学号: 时间:。
4.参与计算的数字都在100以内,和及差也在100以内,每题都必须有进位或借位。

代码文本:

#include "stdio.h"

#include <stdlib.h>

#include "time.h"

int main(int argc,char *argv[]){

int i,a,b,x,y,n;

printf("How many pages...\n");

if(scanf("%d",&n) && n<1)

return 0;

srand((unsigned)time(NULL));

while(n--){

printf("班级:     姓名:        学号:    时间:\n");

for(i=0;i<17;i++){

if(rand()&1){

a=10*(x=rand()%9)+(y=rand()%9+1);

b=10*(rand()%(9-x))+9-rand()%y;

printf("%2d+%2d=",a,b);

}

else{

a=10*(x=rand()%9+1)+(y=rand()%9);

b=10*(rand()%x)+rand()%(9-y)+y+1;

printf("%2d-%2d=",a,b);

}

printf("                 ");

if(rand()&1){

a=10*(x=rand()%9)+(y=rand()%9+1);

b=10*(rand()%(9-x))+9-rand()%y;

printf("%2d+%2d=\n",a,b);

}

else{

a=10*(x=rand()%9+1)+(y=rand()%9);

b=10*(rand()%x)+rand()%(9-y)+y+1;

printf("%2d-%2d=\n",a,b);

}

}

}

return 0;

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-04-26

思路:直接生成两个两位随机数,应该更简单些。

#include "stdafx.h"

#include <time.h>

#include <iostream>

using namespace std;

int main() 

{

int a, b,n=0;

srand((unsigned int)time(NULL));

cout << "输入页数\n";

cin >> n;

while (n) 

{

cout << "姓名:" << "        " << "班级:" << endl;

for (int i = 0;i < 17;i++) 

{

for (int j = 0;j < 2;j++) 

{

a = rand() % 99 + 1;

b = rand() % 99 + 1;

if (a > b && ((a - a / 10 * 10) < (b - b / 10 * 10)))

cout << a << "-" << b << "=        ";

else if (((a - a / 10 * 10) + (b - b / 10 * 10)) > 9 && ((a / 10 + b / 10) < 9))

cout << a << "+" << b << "=        ";

else

j--;

}

cout << endl;

}

n--;

cout << endl;

}

system("pause");

return 0;

}

相似回答