class="MsoNormal">Win32控制台程序界面类
我相信不少同学也和我一样,在刚学习c++的时候都会尝试做简单的图书管理系统,学生成绩管理系统等,做这些实验项目可以加深你对文件操作,vector,list等容器的熟悉度,还可以让你学会设计系统结构。
随着学习的逐渐深入,同学们也许会发现,很多时候我们只要调用标准库函数里的类,代码量就会少很多,如果我们在平时的练习里有意识地去封装一些也许会比较常用的类,那么是不是我们平时编程的效率也会快好多呢。
答案是肯定的,特别对于XXXX系统之类的程序来说,可供应的地方就更多了,要重复使用这些我们自己封装好的类,只要将我们做好的类的.h和.cpp文件与我们做好想做的程序的代码放在同一个文件夹里就好。
下面我就将我在做好的界面类与大家分享,由于时间仓促,所做的类的调用方法也比较少,
接下来的时间里,我会继续扩充该类的方法,以便适应未来工作学习的需要,同时也方便大家
头文件:windesign.h//划线类(专供windowDesigner.h使用)
??????? wndowDesigner.h//画界面类
其他要包含的cpp文件
questionwin.cpp
windesign.cpp
windowDesigner.cpp
?
?
windesign.h的方法:
???????? winDesign& leftSide(int,char);//首行缩进
???????? winDesign& leftSidec(int,char,char);//首行缩进做加左边框
???????? winDesign& Sidesc(int,char);//边框左
???????? winDesign& SidescR(int,int,char);//边框右
???????? winDesign& SidescL(int,int,char);//边框左2
???????? winDesign& centre(int,int,char,string);//中部内容
???????? winDesign& centre(int ,char );//中部标题,上下部边框
(绘制一行的内容需要用到其中的几个方法)
?
windowDesigner.h的方法:
?? ?? void mainwin(string sentence[],int n,int s);//画主界面//各行内容,选项数,缩进(选项数要大于sentence[]的元素数)
???????? void questionwin(string sentence[],int n,int s);//对话框//各行内容,选项数,缩进
?? void needinput(string sentence[],string input[],int n,int s);//需要输入的界面//
?
?
各文件源代码:
windesign.h文件
#ifndef WINDESIGN_H
#define WINDESIGN_H
#include<string>
using std::string;
#include<iomanip>
using std::setfill;
using std::setw;
class winDesign
{
public:
???????? winDesign& leftSide(int,char);//首行缩进
???????? winDesign& leftSidec(int,char,char);//首行缩进做加左边框
???????? winDesign& Sidesc(int,char);//边框左
???????? winDesign& SidescR(int,int,char);//边框右
???????? winDesign& SidescL(int,int,char);//边框左2
???????? winDesign& centre(int,int,char,string);//中部内容
???????? winDesign& centre(int ,char );//中部标题,上下部边框
};
#endif
wndowDesigner.h文件
#ifndef WINDOWDESIGNER_H
#define WINDOWDESIGNER_H
#include"windesign.h"
#include<string>
using std::string;
class windowDesigner
{
public:
???????? windowDesigner(winDesign)
???????? {
?
???????? };//实验品(组成)
???????? void mainwin(string sentence[],int n,int s);//画主界面//各行内容,选项数,缩进
???????? void questionwin(string sentence[],int n,int s);//对话框//各行内容,选项数,缩进
???????? void needinput(string sentence[],string input[],int n,int s);//需要输入的界面//
private:
???????? winDesign Line;
};
#endif
questionwin.cpp
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
#include<iomanip>
using std::setfill;
using std::setw;
#include"windowDesigner.h"
void windowDesigner::questionwin(string sentence[],int n,int s)
{
???????? int a=1;//边框粗细
???????? int h1=25;//整体缩进(数值越大越靠右)
???????? int k1=30;//窗口宽度(数值越大越靠下)
???????? int i;
???????? int n1=10;
???????? for(i=0;i<n1;i++)
???????? {
?????????????????? cout<<endl;
???????? }
???????? Line.leftSide(h1,' ').SidescR(a,a,'=').centre(k1,'=').SidescL(a,a,'=');//上边
???????? cout<<endl;
???????? Line.leftSide(h1,' ').SidescR(a,a,'=').centre(7,k1,' ',sentence[0]).SidescL(a,a,'=');
???????? cout<<endl;//标题
???????? Line.leftSide(h1,' ').SidescR(a,a,'=').centre(k1,' ').SidescL(a,a,'=');
???????? cout<<endl;
?????????????????? for(i=1;i<n;i++)//选项
???????? {
?????????????????? Line.leftSide(h1,' ').SidescR(a,a,'=').centre(s,k1,' ',sentence[i]).SidescL(a,a,'=');
???????? ??? cout<<endl;
?????????????????? Line.leftSide(h1,' ').SidescR(a,a,'=').centre(k1,' ').SidescL(a,a,'=');
?????????????????? cout<<endl;
?????????????????? }
?????????????????? if(i<n-1)
?????????????????? {cout<<endl;}//(如果没有这句话,最后一个选项会有写不协调)
??? Line.leftSide(h1,' ').SidescR(a,a,'=').centre(k1,'=').SidescL(a,a,'=');//底边
???????? cout<<endl;
????????
}
windesign.cpp
#include<iostream>
using std::cout;
using std::endl;
#include"windesign.h"
?
winDesign &winDesign::leftSide(int n1,char s3)
{
???????? cout<<setfill(s3)<<setw(n1)<<s3;
???????? return *this;
}
winDesign& winDesign::leftSidec(int n1,char s3,char s4)
{
???????? cout<<setfill(s3)<<setw(n1)<<s4;
???????? return*this;
}//首行缩进做加左边框
winDesign &winDesign::Sidesc(int n2,char a1)
{
???????? cout<<setfill(a1)<<setw(n2)<<a1;
???????? return *this;
}
winDesign &winDesign::centre(int n4,int n3,char s2,string str1)
{
???????? int i;
???????? i=str1.length();
???????? cout<<setfill(s2)<<setw(n4)<<s2;
???????? cout<<str1;
???????? cout<<setfill(s2)<<setw(n3-n4-i)<<s2;
???????? return *this;
}
winDesign &winDesign::centre(int n3,char s2)
{
???????? cout<<setfill(s2)<<setw(n3)<<s2;
???????? return *this;
}
winDesign &winDesign::SidescR(int n2,int n21,char s1)
{
???????? cout<<setfill(' ')<<setw(n2-n21)<<' ';
???????? cout<<setfill(s1)<<setw(n21)<<s1;
???????? return *this;
???????? }
???????? winDesign &winDesign::SidescL(int n2,int n21,char s1)
{
???????? cout<<setfill(s1)<<setw(n21)<<s1;
???????? cout<<setfill(' ')<<setw(n2-n21)<<' ';
???????? return *this;
???????? }
?
windowDesigner.cpp文件
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
#include<iomanip>
using std::setfill;
using std::setw;
#include"windowDesigner.h"
void windowDesigner::mainwin(string sentence[],int n,int s)
{
???????? int h1=12;/*缩进*/
???????? constint n1=2;//头两行列数
???????? int h2=7,h3=45,h4=4;//头两行
???????? int y[4]={8,7,39,9};//第三行标题
???????? int k[3]={7,36,13};//
???????? constint n2=5,n3=8;
???????? int m2=8,m3=40,m4=8;//选项行间隔
???????? int mouth2=8,mouth3=10,mouth4=36,mouth5=12;//(窗口装饰)嘴
???????? int empty2=4,empty3=45,empty4=7;//空行
???????? int legL=4;//腿长=legL-1;
???????? int leg2=25,leg3=6,leg4=25;//腿行
???????? int a=1;//边框粗细
???????? int i;
???????? for(i=0;i<n1;i++)//绘制头顶部边框,行数n1=2
???????? {
?????????????????? Line.leftSide(h1,' ').Sidesc(h2,'=').centre(h3,'=').Sidesc(h4,'=');
?????????????????? cout<<endl;
???????? }
???????? Line.leftSide(h1,' ').Sidesc(y[0],'=').centre(y[1],y[2],'=',sentence[0]).Sidesc(y[3],'=');
???????? cout<<endl;//标题
???????? Line.leftSide(h1,' ').SidescL(8,n2-i+4,'=').centre(40,' ').SidescR(8,n2-i+4,'=');
???????? cout<<endl;//空白行
???????? for(i=1;i<n;i++)//选项
???????? {
?????????????????? Line.leftSide(h1,' ').SidescL(k[0],a,'=').centre(s,k[1],' ',sentence[i]).SidescR(k[2],a,'=');
???????? ??? cout<<endl;
?????????????????? Line.leftSide(h1,' ').SidescL(5,a,'=').centre(45,' ').SidescR(6,a,'=');
?????????????????? cout<<endl;
???????? }
?????????????????? if(i<n-1)
?????????????????? {cout<<endl;}//(如果没有这句话,最后一个选项会有写不协调)
?????????????????? for(i=0;i<n1;i++)
???????? {
??????????????????????????? Line.leftSide(h1,' ').SidescL(empty2,a,'=').centre(empty3,' ').SidescR(empty4,a,'=');
?????????????????? cout<<endl;
???????? }
???????? Line.leftSide(h1,' ').SidescL(mouth2,a,'=').centre(mouth3,mouth4,' ',"*********************").SidescR(mouth5,a,'=');
???????? cout<<endl;
???????? Line.leftSide(h1,' ').SidescL(mouth2,a,'=').centre(mouth3,mouth4,' ',"*?????????????????? *").SidescR(mouth5,a,'=');
???????? cout<<endl;
???????? Line.leftSide(h1,' ').SidescL(mouth2,a,'=').centre(mouth3,mouth4,' ',"*??????????? ???????*").SidescR(mouth5,a,'=');
???????? cout<<endl;
?????????????????? for(i=0;i<n1;i++)
???????? {
?????????????????? Line.leftSide(h1,' ').Sidesc(h2,'=').centre(h3,'=').Sidesc(h4,'=');
?????????????????? cout<<endl;
???????? }
?????????????????? for(i=1;i<legL;i++)
?????????????????? {
???????? Line.leftSide(h1,' ').SidescL(leg2,leg2-2*i,'=').centre(leg3,' ').SidescR(leg4,leg4-2*i,'=');
?????????????????? ?cout<<endl;
?????????????????? }
???????? cout<<endl;
?
}
?
?效果:
?
?
?
?
?