📄 strbubbl.cpp
字号:
//这个程序在本书所带软盘中。文件名为STRBUBBL.CPP
//这个程序利用子程序将名单按字母顺序排队。
#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define line 30
#define size 25
void main(void)
{
void bubble_sort(char[][size], int);//定义一个有两维字符数组参数的子程序
void output(char[][size], int); //定义一个有两维字符数组参数的子程序
char record[line][size]; //定义两维字符型数组
int row = 0;
while (gets(record[row]) != NULL)
++row;
bubble_sort(record, row); //调用冒泡排序子程序
output(record, row); //调用输出子程序
}
/************子程序bubble_sort()************************/
void bubble_sort(char charray[][size], int r)
{
int top, j, pos;
char temp[line];
top = 0;
pos = 0;
while (top < r-1) {
j = r-1;
do {
if (charray[j][pos] < charray[j-1][pos]) { //交换单元的值
strcpy(temp, charray[j]);
strcpy(charray[j], charray[j-1]);
strcpy(charray[j-1], temp);
}
j--;
} while (j > top);
top++;
} //while循环结束
}
/***********子程序output()****************************/
void output(char charray[][size], int r)
{
puts("排队后的名单为:");
for (int i = 0; i < r; i++)
puts(charray[i]);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -