c++怎么定向清屏?system用一下就全清了,我要清指定的行。问有没有这样的函数功能的。。

如题所述

/*
其实有一条 system("cls"); 就能完成任务了。
核心思想:
1)建立一个屏幕输出缓冲区,里面保存当前输出到屏幕的数据
2)读入用户操作指令
3)根据用户操作指令更新屏幕输出缓冲区数据
4)清除屏幕数据
5)将屏幕输出缓冲区数据重新输出到屏幕

下面的是为你写的一个示例程序,该程序有如下功能:
1)开始时显示一个 "九九乘法口诀" 表
2)在原"九九乘法口诀" 表的基础上,可以
添加行,
删除一个乘法式子,
添加一个乘法式子,
修改一个乘法式子
3)在屏幕重新显示"九九乘法口诀" 表

下面的程序建议拷贝到文本编辑器,如记事本中观看。

*/
#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
using namespace std;
//屏幕输出缓冲区大小
const int MAX_SCREEN_BUFFER_ROWS = 100;
const int MAX_SCREEN_BUFFER_COLS = 100;
//用户输入命令最大长度
const int MAX_COMMAND_LEN = 200;

class My_Screen
{
public :
My_Screen();
//删除一行
void deleteRow(int row);
//删除指定位置
void deleteContent(int row,int col);
//添加到指定位置
void insertContent(int row,int col,string content);
//更新指定位置
void updateContent(int row,int col,string content);
//输出到屏幕
void print();
//数字到字符串格式转换
static string FormatNumber(int num,int width);
private:
//屏幕输出缓冲区
string screen_buffer[MAX_SCREEN_BUFFER_ROWS][MAX_SCREEN_BUFFER_COLS];
//数据行数
int row_num;
//每行中数据列数
int row_content_num[MAX_SCREEN_BUFFER_ROWS];
};

My_Screen::My_Screen()
{
//数据初始化为九九乘法表,用来测试
int i,j;
this->row_num = 9;
for(i = 1 ; i <= 9 ; i++)
{
for(j = 1 ; j <= i ; j++)
{
this->screen_buffer[i-1][j-1] = "";
//一个式子的标准格式定义为:
//"dd * dd = dd"
//一个数字不足两位的高位补空格
this->screen_buffer[i-1][j-1]
.append(My_Screen::FormatNumber(j,2))
.append(" * ")
.append(My_Screen::FormatNumber(i,2))
.append(" = ")
.append(My_Screen::FormatNumber(i*j,2))
;

}
this->row_content_num[i-1] = i;
}
}

void My_Screen :: deleteRow(int row)
{
int i,j;
row --;
if( (0 <= row ) && ( row < this->row_num ))
{
for(i = row;i < this->row_num - 1;i++)
{
for(j = 0;j < this->row_content_num[i+1];j++)
{
this->screen_buffer[i][j]=this->screen_buffer[i+1][j];
}
this->row_content_num[i] = this->row_content_num[i+1];
}

this->row_num --;
}

print();
}
void My_Screen :: deleteContent(int row,int col)
{
int i,j;
row--,col--;
if(0 <= row && row < this->row_num)
{
if(0 <= col && col < this->row_content_num[row])
{
if(this->row_content_num[row] == 1)
{
deleteRow(row+1);
}
else
{
for(i = col;i < this->row_content_num[row] - 1;i++)
{
this->screen_buffer[row][i] = this->screen_buffer[row][i+1];
}

this->row_content_num[row]--;
}

}
}
print();
}
void My_Screen :: insertContent(int row,int col,string content)
{
int i,j;
row--,col--;
if(row == -1)
{
row = 0;
col = 0;
for(i = this->row_num ; i >= 1 ; i--)
{
for(j = 0 ;j < this->row_content_num[i-1]; j++)
{
this->screen_buffer[i][j] = this->screen_buffer[i-1][j];
}
this->row_content_num[i] = this->row_content_num[i-1];
}
this->row_content_num[0] = 0;
this->row_num ++;
}

if(0 <= row && row < this->row_num)
{
if(0 <= col && col <= this->row_content_num[row])
{
if( col < this->row_content_num[row])
{
for(i = this->row_content_num[row];i >= (col+1);i--)
{
this->screen_buffer[row][i] = this->screen_buffer[row][i-1];
}
}

this->screen_buffer[row][col] = content;
this->row_content_num[row]++;
}
}
print();
}
void My_Screen :: updateContent(int row,int col,string content)
{
row--,col--;
if(0 <= row && row < this->row_num)
{
if(0 <= col && col < this->row_content_num[row])
{
this->screen_buffer[row][col] = content;
}
}
print();
}

void My_Screen :: print()
{
int i,j;
system("cls");
for(i = 0 ; i < this->row_num ; i++)
{
cout.width( 5 );
cout<<(i+1)<<" : ";
for(j = 0 ; j < this->row_content_num[i] ; j++)
{
cout<<this->screen_buffer[i][j]<< " | ";
}
cout<<endl;
}
}

// 将数字 num 格式化成长度为 width 的字符串形式
string My_Screen:: FormatNumber(int num,int width)
{
string result = "";
int num_len = (int)log10(num) + 1;
int p = (int)pow(10,num_len-1);
if((width-num_len)>0)
{
result.append(width-num_len,' ');
}

for(int i = 0;i < num_len;i++)
{
result += '0' + num/p;
num -= p*(num/p);
p/=10;
}

return result;

}
int main(int argc, char *argv[])
{
My_Screen screen;

//用户输入
string user_command;
int selected_row,selected_col;
string content;
char temp_content[MAX_COMMAND_LEN];

screen.print();
//提示输入命令
cout<<"Input a comment : ";
while(cin>>user_command)
{
//注意命名区分大小写
//命令可以简写,如 "deleteRow" 简写为 "dR
if(user_command == "deleteRow" || user_command == "dR")
{
cin>>selected_row;
screen.deleteRow(selected_row);
}
else if(user_command == "deleteContent" || user_command == "dC")
{
cin>>selected_row>>selected_col;
screen.deleteContent(selected_row,selected_col);
}
else if(user_command == "insertContent" || user_command == "iC")
{
cin>>selected_row>>selected_col;
cin.getline(temp_content,MAX_COMMAND_LEN);
content = temp_content;
screen.insertContent(selected_row,selected_col,content);

}
else if(user_command == "updateContent" || user_command == "uC")
{
cin>>selected_row>>selected_col;
cin.getline(temp_content,MAX_COMMAND_LEN);
content = temp_content;
screen.updateContent(selected_row,selected_col,content);
}
else if(user_command == "help")
{
cout<<" deleteRow(dR) [row]"<<endl;
cout<<" deleteContent(dC) [row col]"<<endl;
cout<<" insertContent(iC) [row col]"<<endl;
cout<<" updateContent(uC) [row col]"<<endl;
}
else
{
cout<<"Ilegal command!"<<endl;
}

cout<<"Input a comment : ";
}
}

/*
样例输入(1)【帮助菜单】:

1 : 1 * 1 = 1 |
2 : 1 * 2 = 2 | 2 * 2 = 4 |
3 : 1 * 3 = 3 | 2 * 3 = 6 | 3 * 3 = 9 |
4 : 1 * 4 = 4 | 2 * 4 = 8 | 3 * 4 = 12 | 4 * 4 = 16 |
5 : 1 * 5 = 5 | 2 * 5 = 10 | 3 * 5 = 15 | 4 * 5 = 20 | 5 * 5 = 25 |
6 : 1 * 6 = 6 | 2 * 6 = 12 | 3 * 6 = 18 | 4 * 6 = 24 | 5 * 6 = 30 | 6 * 6 = 36 |
7 : 1 * 7 = 7 | 2 * 7 = 14 | 3 * 7 = 21 | 4 * 7 = 28 | 5 * 7 = 35 | 6 * 7 = 42 | 7 * 7 = 49 |
8 : 1 * 8 = 8 | 2 * 8 = 16 | 3 * 8 = 24 | 4 * 8 = 32 | 5 * 8 = 40 | 6 * 8 = 48 | 7 * 8 = 56 | 8 * 8 = 64 |
9 : 1 * 9 = 9 | 2 * 9 = 18 | 3 * 9 = 27 | 4 * 9 = 36 | 5 * 9 = 45 | 6 * 9 = 54 | 7 * 9 = 63 | 8 * 9 = 72 | 9 * 9 = 81 |
Input a comment : help

样例输出(1)【帮助菜单】:

1 : 1 * 1 = 1 |
2 : 1 * 2 = 2 | 2 * 2 = 4 |
3 : 1 * 3 = 3 | 2 * 3 = 6 | 3 * 3 = 9 |
4 : 1 * 4 = 4 | 2 * 4 = 8 | 3 * 4 = 12 | 4 * 4 = 16 |
5 : 1 * 5 = 5 | 2 * 5 = 10 | 3 * 5 = 15 | 4 * 5 = 20 | 5 * 5 = 25 |
6 : 1 * 6 = 6 | 2 * 6 = 12 | 3 * 6 = 18 | 4 * 6 = 24 | 5 * 6 = 30 | 6 * 6 = 36 |
7 : 1 * 7 = 7 | 2 * 7 = 14 | 3 * 7 = 21 | 4 * 7 = 28 | 5 * 7 = 35 | 6 * 7 = 42 | 7 * 7 = 49 |
8 : 1 * 8 = 8 | 2 * 8 = 16 | 3 * 8 = 24 | 4 * 8 = 32 | 5 * 8 = 40 | 6 * 8 = 48 | 7 * 8 = 56 | 8 * 8 = 64 |
9 : 1 * 9 = 9 | 2 * 9 = 18 | 3 * 9 = 27 | 4 * 9 = 36 | 5 * 9 = 45 | 6 * 9 = 54 | 7 * 9 = 63 | 8 * 9 = 72 | 9 * 9 = 81 |
Input a comment : help
deleteRow(dR) [row]
deleteContent(dC) [row col]
insertContent(iC) [row col]
updateContent(uC) [row col]

********************************************************************************

样例输入(2)【删除指定位置,这里是第二行第一列】:

1 : 1 * 1 = 1 |
2 : 1 * 2 = 2 | 2 * 2 = 4 |
3 : 1 * 3 = 3 | 2 * 3 = 6 | 3 * 3 = 9 |
4 : 1 * 4 = 4 | 2 * 4 = 8 | 3 * 4 = 12 | 4 * 4 = 16 |
5 : 1 * 5 = 5 | 2 * 5 = 10 | 3 * 5 = 15 | 4 * 5 = 20 | 5 * 5 = 25 |
6 : 1 * 6 = 6 | 2 * 6 = 12 | 3 * 6 = 18 | 4 * 6 = 24 | 5 * 6 = 30 | 6 * 6 = 36 |
7 : 1 * 7 = 7 | 2 * 7 = 14 | 3 * 7 = 21 | 4 * 7 = 28 | 5 * 7 = 35 | 6 * 7 = 42 | 7 * 7 = 49 |
8 : 1 * 8 = 8 | 2 * 8 = 16 | 3 * 8 = 24 | 4 * 8 = 32 | 5 * 8 = 40 | 6 * 8 = 48 | 7 * 8 = 56 | 8 * 8 = 64 |
9 : 1 * 9 = 9 | 2 * 9 = 18 | 3 * 9 = 27 | 4 * 9 = 36 | 5 * 9 = 45 | 6 * 9 = 54 | 7 * 9 = 63 | 8 * 9 = 72 | 9 * 9 = 81 |
Input a comment : dC 2 1

样例输出(2)【删除指定位置,这里是第二行第一列】:

1 : 1 * 1 = 1 |
2 : 2 * 2 = 4 |
3 : 1 * 3 = 3 | 2 * 3 = 6 | 3 * 3 = 9 |
4 : 1 * 4 = 4 | 2 * 4 = 8 | 3 * 4 = 12 | 4 * 4 = 16 |
5 : 1 * 5 = 5 | 2 * 5 = 10 | 3 * 5 = 15 | 4 * 5 = 20 | 5 * 5 = 25 |
6 : 1 * 6 = 6 | 2 * 6 = 12 | 3 * 6 = 18 | 4 * 6 = 24 | 5 * 6 = 30 | 6 * 6 = 36 |
7 : 1 * 7 = 7 | 2 * 7 = 14 | 3 * 7 = 21 | 4 * 7 = 28 | 5 * 7 = 35 | 6 * 7 = 42 | 7 * 7 = 49 |
8 : 1 * 8 = 8 | 2 * 8 = 16 | 3 * 8 = 24 | 4 * 8 = 32 | 5 * 8 = 40 | 6 * 8 = 48 | 7 * 8 = 56 | 8 * 8 = 64 |
9 : 1 * 9 = 9 | 2 * 9 = 18 | 3 * 9 = 27 | 4 * 9 = 36 | 5 * 9 = 45 | 6 * 9 = 54 | 7 * 9 = 63 | 8 * 9 = 72 | 9 * 9 = 81 |
Input a comment :

********************************************************************************

样例输入(3)【添加到指定位置,这里是第二行第一列】
1 : 1 * 1 = 1 |
2 : 2 * 2 = 4 |
3 : 1 * 3 = 3 | 2 * 3 = 6 | 3 * 3 = 9 |
4 : 1 * 4 = 4 | 2 * 4 = 8 | 3 * 4 = 12 | 4 * 4 = 16 |
5 : 1 * 5 = 5 | 2 * 5 = 10 | 3 * 5 = 15 | 4 * 5 = 20 | 5 * 5 = 25 |
6 : 1 * 6 = 6 | 2 * 6 = 12 | 3 * 6 = 18 | 4 * 6 = 24 | 5 * 6 = 30 | 6 * 6 = 36 |
7 : 1 * 7 = 7 | 2 * 7 = 14 | 3 * 7 = 21 | 4 * 7 = 28 | 5 * 7 = 35 | 6 * 7 = 42 | 7 * 7 = 49 |
8 : 1 * 8 = 8 | 2 * 8 = 16 | 3 * 8 = 24 | 4 * 8 = 32 | 5 * 8 = 40 | 6 * 8 = 48 | 7 * 8 = 56 | 8 * 8 = 64 |
9 : 1 * 9 = 9 | 2 * 9 = 18 | 3 * 9 = 27 | 4 * 9 = 36 | 5 * 9 = 45 | 6 * 9 = 54 | 7 * 9 = 63 | 8 * 9 = 72 | 9 * 9 = 81 |
Input a comment : iC 2 1 2 * 2 = 4

样例输出(3)【添加到指定位置,这里是第二行第一列】

1 : 1 * 1 = 1 |
2 : 2 * 2 = 4 | 2 * 2 = 4 |
3 : 1 * 3 = 3 | 2 * 3 = 6 | 3 * 3 = 9 |
4 : 1 * 4 = 4 | 2 * 4 = 8 | 3 * 4 = 12 | 4 * 4 = 16 |
5 : 1 * 5 = 5 | 2 * 5 = 10 | 3 * 5 = 15 | 4 * 5 = 20 | 5 * 5 = 25 |
6 : 1 * 6 = 6 | 2 * 6 = 12 | 3 * 6 = 18 | 4 * 6 = 24 | 5 * 6 = 30 | 6 * 6 = 36 |
7 : 1 * 7 = 7 | 2 * 7 = 14 | 3 * 7 = 21 | 4 * 7 = 28 | 5 * 7 = 35 | 6 * 7 = 42 | 7 * 7 = 49 |
8 : 1 * 8 = 8 | 2 * 8 = 16 | 3 * 8 = 24 | 4 * 8 = 32 | 5 * 8 = 40 | 6 * 8 = 48 | 7 * 8 = 56 | 8 * 8 = 64 |
9 : 1 * 9 = 9 | 2 * 9 = 18 | 3 * 9 = 27 | 4 * 9 = 36 | 5 * 9 = 45 | 6 * 9 = 54 | 7 * 9 = 63 | 8 * 9 = 72 | 9 * 9 = 81 |

********************************************************************************

样例输入(4)【修到指定位置,这里是第二行第一列】

1 : 1 * 1 = 1 |
2 : 2 * 2 = 4 |
3 : 1 * 3 = 3 | 2 * 3 = 6 | 3 * 3 = 9 |
4 : 1 * 4 = 4 | 2 * 4 = 8 | 3 * 4 = 12 | 4 * 4 = 16 |
5 : 1 * 5 = 5 | 2 * 5 = 10 | 3 * 5 = 15 | 4 * 5 = 20 | 5 * 5 = 25 |
6 : 1 * 6 = 6 | 2 * 6 = 12 | 3 * 6 = 18 | 4 * 6 = 24 | 5 * 6 = 30 | 6 * 6 = 36 |
7 : 1 * 7 = 7 | 2 * 7 = 14 | 3 * 7 = 21 | 4 * 7 = 28 | 5 * 7 = 35 | 6 * 7 = 42 | 7 * 7 = 49 |
8 : 1 * 8 = 8 | 2 * 8 = 16 | 3 * 8 = 24 | 4 * 8 = 32 | 5 * 8 = 40 | 6 * 8 = 48 | 7 * 8 = 56 | 8 * 8 = 64 |
9 : 1 * 9 = 9 | 2 * 9 = 18 | 3 * 9 = 27 | 4 * 9 = 36 | 5 * 9 = 45 | 6 * 9 = 54 | 7 * 9 = 63 | 8 * 9 = 72 | 9 * 9 = 81 |
Input a comment : uC 2 1 2 * 2 = 99

样例输出(4)【修到指定位置,这里是第二行第一列】

1 : 1 * 1 = 1 |
2 : 2 * 2 = 99 | 2 * 2 = 4 |
3 : 1 * 3 = 3 | 2 * 3 = 6 | 3 * 3 = 9 |
4 : 1 * 4 = 4 | 2 * 4 = 8 | 3 * 4 = 12 | 4 * 4 = 16 |
5 : 1 * 5 = 5 | 2 * 5 = 10 | 3 * 5 = 15 | 4 * 5 = 20 | 5 * 5 = 25 |
6 : 1 * 6 = 6 | 2 * 6 = 12 | 3 * 6 = 18 | 4 * 6 = 24 | 5 * 6 = 30 | 6 * 6 = 36 |
7 : 1 * 7 = 7 | 2 * 7 = 14 | 3 * 7 = 21 | 4 * 7 = 28 | 5 * 7 = 35 | 6 * 7 = 42 | 7 * 7 = 49 |
8 : 1 * 8 = 8 | 2 * 8 = 16 | 3 * 8 = 24 | 4 * 8 = 32 | 5 * 8 = 40 | 6 * 8 = 48 | 7 * 8 = 56 | 8 * 8 = 64 |
9 : 1 * 9 = 9 | 2 * 9 = 18 | 3 * 9 = 27 | 4 * 9 = 36 | 5 * 9 = 45 | 6 * 9 = 54 | 7 * 9 = 63 | 8 * 9 = 72 | 9 * 9 = 81 |
Input a comment :

********************************************************************************
样例输入(5)【删除指定行,这里是第二行】

1 : 1 * 1 = 1 |
2 : 2 * 2 = 99 | 2 * 2 = 4 |
3 : 1 * 3 = 3 | 2 * 3 = 6 | 3 * 3 = 9 |
4 : 1 * 4 = 4 | 2 * 4 = 8 | 3 * 4 = 12 | 4 * 4 = 16 |
5 : 1 * 5 = 5 | 2 * 5 = 10 | 3 * 5 = 15 | 4 * 5 = 20 | 5 * 5 = 25 |
6 : 1 * 6 = 6 | 2 * 6 = 12 | 3 * 6 = 18 | 4 * 6 = 24 | 5 * 6 = 30 | 6 * 6 = 36 |
7 : 1 * 7 = 7 | 2 * 7 = 14 | 3 * 7 = 21 | 4 * 7 = 28 | 5 * 7 = 35 | 6 * 7 = 42 | 7 * 7 = 49 |
8 : 1 * 8 = 8 | 2 * 8 = 16 | 3 * 8 = 24 | 4 * 8 = 32 | 5 * 8 = 40 | 6 * 8 = 48 | 7 * 8 = 56 | 8 * 8 = 64 |
9 : 1 * 9 = 9 | 2 * 9 = 18 | 3 * 9 = 27 | 4 * 9 = 36 | 5 * 9 = 45 | 6 * 9 = 54 | 7 * 9 = 63 | 8 * 9 = 72 | 9 * 9 = 81 |
Input a comment : dR 2

样例输出(5)【删除指定行,这里是第二行】

1 : 1 * 1 = 1 |
2 : 1 * 3 = 3 | 2 * 3 = 6 | 3 * 3 = 9 |
3 : 1 * 4 = 4 | 2 * 4 = 8 | 3 * 4 = 12 | 4 * 4 = 16 |
4 : 1 * 5 = 5 | 2 * 5 = 10 | 3 * 5 = 15 | 4 * 5 = 20 | 5 * 5 = 25 |
5 : 1 * 6 = 6 | 2 * 6 = 12 | 3 * 6 = 18 | 4 * 6 = 24 | 5 * 6 = 30 | 6 * 6 = 36 |
6 : 1 * 7 = 7 | 2 * 7 = 14 | 3 * 7 = 21 | 4 * 7 = 28 | 5 * 7 = 35 | 6 * 7 = 42 | 7 * 7 = 49 |
7 : 1 * 8 = 8 | 2 * 8 = 16 | 3 * 8 = 24 | 4 * 8 = 32 | 5 * 8 = 40 | 6 * 8 = 48 | 7 * 8 = 56 | 8 * 8 = 64 |
8 : 1 * 9 = 9 | 2 * 9 = 18 | 3 * 9 = 27 | 4 * 9 = 36 | 5 * 9 = 45 | 6 * 9 = 54 | 7 * 9 = 63 | 8 * 9 = 72 | 9 * 9 = 81 |
Input a comment :
*/
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-03-03
应该没有,只能全部清掉,再输出你要保留的,挺麻烦。。。
相似回答