调试运行冒泡法小球显示和矩形显示两个程序。因为要超过30个字符不知道打什么了所以后面全是废话。
把你的程序贴出来,就不用凑字数了
这里提供一份使用冒泡法在控制台中显示小球和矩形的示例代码,供参考:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 20 // 控制台行数
#define COLS 60 // 控制台列数
// 小球结构体
typedef struct ball {
int x, y; // 小球的位置坐标
int dx, dy; // 小球的移动速度
char symbol; // 小球的字符表示
} Ball;
// 矩形结构体
typedef struct rect {
int x, y; // 矩形的位置坐标
int width, height; // 矩形的大小
char symbol; // 矩形的字符表示
} Rect;
// 在控制台上显示小球
void showBall(Ball *ball, char screen[ROWS][COLS]) {
// 在原位置上显示空格
screen[ball->y][ball->x] = ' ';
// 计算新位置
ball->x += ball->dx;
ball->y += ball->dy;
// 碰到边缘时反弹
if (ball->x <= 0 || ball->x >= COLS - 1) {
ball->dx = -ball->dx;
}
if (ball->y <= 0 || ball->y >= ROWS - 1) {
ball->dy = -ball->dy;
}
// 在新位置上显示小球
screen[ball->y][ball->x] = ball->symbol;
}
// 在控制台上显示矩形
void showRect(Rect *rect, char screen[ROWS][COLS]) {
int x, y;
// 在原位置上显示空格
for (y = rect->y; y < rect->y + rect->height; y++) {
for (x = rect->x; x < rect->x + rect->width; x++) {
screen[y][x] = ' ';
}
}
// 计算新位置
rect->x += rand() % 3 - 1;
rect->y += rand() % 3 - 1;
// 碰到边缘时反弹
if (rect->x < 0) {
rect->x = 0;
}
if (rect->x + rect->width > COLS) {
rect->x = COLS - rect->width;
}
if (rect->y < 0) {
rect->y = 0;
}
if (rect->y + rect->height > ROWS) {
rect->y = ROWS - rect->height;
}
// 在新位置上显示矩形
for (y = rect->y; y < rect->y + rect->height; y++) {
for (x = rect->x; x < rect->x + rect->width; x++) {
screen[y][x] = rect->symbol;
}
}
}
int main() {
char screen[ROWS][COLS];
int i, j, k;
Ball ball = {COLS / 2, ROWS / 2, 1, 1, 'o'};
Rect rect = {COLS / 2, ROWS / 2, 10, 5, '#'};
srand(time(NULL)); // 初始化随机数种子
// 清空控制台屏幕
system("cls");
// 开始主循环
while (1) {
// 在控制台屏幕上显示小球和矩形
showBall(&ball, screen);
showRect(&rect, screen);
// 在控制台屏幕上输出小球和矩形
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
putchar(screen[i][j]);
}
putchar('\n');
}
// 暂停一段时间,以便观察
for (k = 0; k < 10000000; k++);
// 清空控制台屏幕,准备下一次显示
system("cls");
}
return 0;
}
注意:存在
scanf("%d",&number);
scanf(" %c",&c);
第二个输入语句的条件控制要加空格,因为上一个结束标记是回车,这个时候在输入缓存里就把这个回车字符存在里面了。当你要再读入一个字符时,
就会默认先把缓存里的回车符读入(如果不加空格)
#include<stdio.h>
#define N 15
int main()
{
int i,number,top,bott,mid,flag,loca,sign,a[N]={0};
char c;
printf("enter date:\n");
scanf("%d",&a[0]); //输出第一个数
i=1;
while(i<N) //是否输入完毕
{
scanf("%d",&a[i]);
if(a[i]>=a[i-1]) //判断输出的数是不是比前一个大
i++;
else
printf("enter this data again:\n");
}
printf("\n");
for(i=0;i<N;i++) //输出数组
printf("%5d",a[i]);
printf("\n");
while(flag)
{
printf("input number to look for:");
scanf("%d",&number); //输入要查找的数
top=0; //查找区间的起始位置
bott=N-1; //查找区间的最末位置
loca=0;sign=0; //表示是否存在
if(number<a[0] || number>a[N-1]) //判断是否在区间内
loca=1;
else
while(top<=bott) //循环查找条件
{
mid=(bott+top)/2; //中间元素下标
if(number==a[mid])
{
printf("Has found %d, its position is %d\n",number,mid+1);
sign=1;
break;
}
else if(number>a[mid]) //如果数大于中间下标
top=mid+1; //在mid+1~bott中寻找
else
bott=mid-1; //否则在top~mid-1中寻找
}
if(loca || !sign) //判断是否存在
printf("cannot find %d.\n",number);
printf("continue or not(Y/N)?"); //提示是否继续查找
scanf(" %c",&c); //注意:有空格
if(c=='n'||c=='N')
flag=0;
}
return 0;
}
抱歉,参考资料中没有与冒泡法展示小球和矩形相关的内容,因此我无法提供具体的解决方案。可以尝试在其他编程资料中寻找相关内容或进行相关算法的研究和实验,祝你好运!