题目
原题:
使用程序打印九九乘法表
样例输出:
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
….
代码
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
void In(double* a); //对输入数据进行判断
int main()
{
double _num; //用于接收输入的数字
int num; //最高为num×num
int i, j; //用于循环
int result; //运算结果
int choice; //检验是否退出
system("title 【婳骨】の乘法表系统");
while (1) {
printf("欢迎来到【婳骨】の乘法表系统\n请输入需要几几乘法表?\n");
while (1) {
In(&_num);
num = (int)_num;
if (num < 1) {
printf("输入范围不对啦,请重新输入!\n");
}
else break;
}
for (i = 1; i < num + 1; i++) {
for (j = 1; j < i + 1; j++) {
result = i * j;
printf("%d × %d = %d", i, j, result);
printf("\t");
}
printf("\n");
}
printf("是否重新运行本程序?如需重新运行请输入0,否则按任意键退出\n");
choice = getch();
if (choice == '0') {
system("cls");
}
else break;
}
printf("谢谢使用!推荐您背好乘法表呢亲~");
system("pause");
return 0;
}
void In(double* a) //对输入数据进行判断
{
do
{
if (scanf_s("%lf", a) == 1)
{
break;
}
char choice;
while ((choice = getchar()) != '\n' && choice != EOF);
printf("输入的不是数字!\n");
} while (scanf_s("%lf", a) != 1);
}
部分运行结果