课题内容:
设计一个分数统计程序。包括学生信息的输入输出以及排序。通过该课题全面熟悉数组、字符串、文件的使用,掌握程序设计的基本方法及友好界面的设计。
课题要求:
(1)输入某班级学生的姓名、分数;
(2)对(1)的分数进行降幂排列并输出;
(3)具有输入输出界面。
利用VS自带的MFC固件库进行界面搭建,排序主要用到冒泡排序算法。
流程图1
voidC分数统计Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, St_name);
DDX_Text(pDX, IDC_EDIT2, St_score);
}
CString strMsg[50];
float strScore[50];
int i = 0;
void sort(floatstrScore[])//冒泡排序算法
{
float temp;
CString tempS;
for (int n = 0; n < i -1; n++)
{
for (int m = 0; m < i - 1- n; m++)
{
if (strScore[m] <strScore[m + 1])
{
temp= strScore[m + 1];
tempS= strMsg[m + 1];
strScore[m + 1] = strScore[m];
strMsg[m+ 1] = strMsg[m];
strScore[m] = temp;
strMsg[m]= tempS;
}
}
}
}
voidC分数统计Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CString strNum;
UpdateData(TRUE);
strScore[i]= St_score;
strNum.Format(_T("%.2f"), St_score);
strMsg[i]= St_name +":"+ strNum +_T("\r\n");
i++;
}
voidC分数统计Dlg::OnBtnButton1()
{
// TODO: 在此添加控件通知处理程序代码
CString strAll;
sort(strScore);
for (int j = 0; j < i;j++)
strAll+= strMsg[j];
AfxMessageBox(strAll);
//AfxMessageBox(_T("测试成功!"));
}
课题内容:
设计一个打字程序。包括随机产生字符串,以及字符串比较和统计。通过此课题,熟练掌握数组、格式输出、字符串处理等。
课题要求:
(1)随机产生一字符串,每次产生的字符串内容、长度都不同;
(2)根据(1)的结果,输入字符串,判断输入是否正确,输出正确率;
(3)具有输入输出界面。
用随机数产生字符串数组,并与之将输入的字符串数据进行比较,输出计算结果。
流程图2
voidC打字程序Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, strInput);
DDX_Text(pDX, IDC_EDIT2, strOutput);
DDX_Text(pDX, IDC_EDIT3, strGenerate);
}
int randRange(intmini,intmax)
{
int randnum;
int range;
range= max - mini;
randnum= rand()%range + mini;//int()*range+mini;
return randnum;
}
TCHAR strg[50] = { _T("/0") };
int num;
voidC打字程序Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
srand((unsigned)time(NULL));
num= randRange(5,20);
for (int i = 0; i < num;i++)
strg[i]= randRange(65, 122);
strGenerate= strg;
UpdateData(false);
}
float compare(TCHAR *strg,TCHAR *strI,intn)
{
int i;
int count=0;
float result;
for (i = 0; i < min(num, n); i++)
{
if (strg[i] == strI[i])
count++;
}
result= count / (float)num;
return result;
}
voidC打字程序Dlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(true);
externTCHAR strg[50];
float res;
TCHAR strI[50] = { _T("/0") };
int n;
n =strInput.GetLength();
for (int i = 0; i < n;i++)
strI[i]= strInput[i];
res= compare(strg, strI, n);
strOutput.Format(_T("%f"), res);
UpdateData(false);
//CStringToIntArr(strInput,strI);
}
课题内容:
设计一个简单的文本编辑器,该系统要求对一个文本文件中的内容进行各种常规操作,如:插入、删除、查找、替换等功能。通过此课题,熟练掌握文本文件的操作及用字符数组或字符指针实现字符串操作的功能。
课题要求:
(1)编辑文本;
(2)保存、打开指定位置的文本文件;
(3)具有输入输出界面。
插入/删除:利用MFC中对文本框数据的直接操作即可获得经插入删除操作后的数据流,仅需要对其作保存处理即可。
查找:利用CString类库中的TEXT.Find()函数即可在相关数据流中检索匹配字符串,此时我们所需要做的操作仅为统计字符串个数。
替换:利用CString类库中的TEXT.Replace()函数即可做出相应替换擦操作。
流程图3
voidC文本编辑器Dlg::OnBnClickedButton2()//打开文件
{
// TODO: 在此添加控件通知处理程序代码
CString str;
CFile f;
f.Open(_T("E:\\MyProject\\My Visual Studio Project\\文本编辑器\\文本.txt"), CFile::modeRead);
char* ptchBuffer = NULL;
int nCount = (int)f.GetLength();
ptchBuffer= newchar[nCount + 1];
ptchBuffer[nCount]= '';
f.Read(ptchBuffer,(int)f.GetLength());
f.Close();
str = ptchBuffer;
// 释放
if (NULL != ptchBuffer)
{
delete[] ptchBuffer;
ptchBuffer= NULL;
}
strText= str;
UpdateData(false);
}
voidC文本编辑器Dlg::OnBnClickedButton1()//插入删除
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(true);
CStdioFile f;
f.Open(_T("E:\\MyProject\\My Visual Studio Project\\文本编辑器\\文本.txt"), CFile::modeCreate | CFile::modeWrite | CFile::typeText);
CString str;
strText.Format(strText+'\0');
f.WriteString(strText);
f.Close();
AfxMessageBox(_T("已保存数据到文件中!"));
}
voidC文本编辑器Dlg::OnBnClickedButton3()//查找
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(true);
if (strFind.IsEmpty())
{
AfxMessageBox(_T("查找的字符为空"));
return;
}
int pos = 0;
int count = 0;
CString strpos, temp;
strpos.Format(_T("%s字符串在原字符串中个数为:\n"), strFind);
while ((pos >= 0)&& (pos < strText.GetLength()))
{
pos= strText.Find(strFind, pos);
if (pos >= 0)
{
count++;
pos+= strFind.GetLength();
}
}
temp.Format(_T("%d"), count);
strpos+= temp;
AfxMessageBox(strpos);
}
voidC文本编辑器Dlg::OnBnClickedButton4()//替换
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(true);
int num;
num= strText.Replace(strFind, strChange);
CString str;
str.Format(_T("共完成了%d处替换"),num);
AfxMessageBox(str);
UpdateData(false);
}
课题内容:
设计一个加密程序。包括明文与密钥的转换。通过此课题,熟练掌握数组、格式输出、字符串处理、类型转换等。
课题要求:
(1)输入任意一段明文M,以及密钥K;
(2)根据以下公式将其转换为密文C。
Ci = mi + K ,其中i= 0,1,……n-1 , K 为密钥;
(3)具有输入输出界面。
流程图4
void convert(TCHARa[], TCHARb[],intlength, intkey)//字符转换程序
{
int i;
for (i = 0; i < length; i++)
{
b[i] = a[i] + key;
}
}
voidC加密程序Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(true);
TCHAR strC[50] = { _T("/0") };
TCHAR strI[50] = { _T("/0") };
int n,key;
n =strInput.GetLength();
if (n == 0 || n >=25)
AfxMessageBox(_T("输入错误明文!请重新输入..."));
else
{
key= 0;
for (int i = 0; i <strKey.GetLength(); i++)
{
key= key*10+strKey[i] - '0';
}
key%= 26;
for (int i = 0; i < n;i++)
strI[i]= strInput[i];
convert(strI,strC, n, key);
}
strOutput= strC;
UpdateData(false);
}
课题内容:
设计一个进制转换器程序。包括二进制、八进制、十进制、十六进制数互相转换。通过此课题,熟练掌握字符串、格式输出、进制换算的各种操作。
课题要求:
(1)可输入二进制、八进制、十进制、十六进制数;
(2)将已输入的数转换成其余进制的数;
(3)具有输入输出界面。
采用先转为十进制再计算其余进制的方式,其中转为二进制则用到itoa()函数,其余进制则按格式化输出。
流程图5
voidC进制转换程序Dlg::OnBnClickedButton1()//二
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(true);
int strI[20] = {'\0'};
int n,res=0;
n =strInput.GetLength();
for (int i = 0; i < n;i++)
strI[i]= strInput[i] - '0';
for (int i = n; i > 0;i--)
{
if(strI[n - i] == 1)
res= (int)pow(2, i-1) + res;
}
str2= strInput;
str8.Format(_T("%o"), res);
str10.Format(_T("%d"), res);
str16.Format(_T("%x"), res);
UpdateData(false);
}
voidC进制转换程序Dlg::OnBnClickedButton2()//八
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(true);
int strI[20] = { '\0' };
char strI2[20] = {"\0"};
int n, res = 0;
n =strInput.GetLength();
for (int i = 0; i < n;i++)
strI[i]= strInput[i] - '0';
for (int i = n; i > 0;i--)
{
if (strI[n - i] != 0)
res= strI[n - i] * (int)pow(8, i - 1) + res;
}
_itoa(res,strI2, 2);
str2= strI2;
str8= strInput;
str10.Format(_T("%d"), res);
str16.Format(_T("%x"), res);
UpdateData(false);
}
voidC进制转换程序Dlg::OnBnClickedButton3()//十
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(true);
int strI[20] = { '\0' };
char strI2[20] = { "\0" };
int n, res = 0;
n =strInput.GetLength();
for (int i = 0; i < n;i++)
strI[i]= strInput[i] - '0';
for (int i = n; i > 0;i--)
{
if (strI[n - i] != 0)
res= strI[n-i]*(int)pow(10, i - 1) + res;
}
_itoa(res,strI2, 2);
str2= strI2;
str8.Format(_T("%o"), res);
str10= strInput;
str16.Format(_T("%x"), res);
UpdateData(false);
}
voidC进制转换程序Dlg::OnBnClickedButton4()//十六
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(true);
int strI[20] = { '\0' };
char strI2[20] = { "\0" };
int n, res = 0;
n =strInput.GetLength();
for (int i = 0; i < n;i++)
strI[i]= strInput[i] - '0';
for (int i = n; i > 0;i--)
{
if (strI[n - i] != 0)
res= strI[n - i] * (int)pow(16, i - 1) + res;
}
_itoa(res,strI2, 2);
str2= strI2;
str8.Format(_T("%o"), res);
str10.Format(_T("%d"), res);
str16= strInput;
UpdateData(false);
}
课题要求:
(1)按班级按课程从文件中读入相应的平时成绩、期中考试成绩和期末考试成绩。
(2)三个成绩对总评成绩的百分比被定义为常数,各占总成绩的30%、30%和40%。
(3)计算每位学生的总评成绩。
(4)计算该班级本课程的总平均成绩。
(5)计算处于优、良、中、及格、不及格的学生人数以及占总人数的百分比。其中100-90为优,89-80为良,79-70为中,69-60为及格,60分以下为不及格。
(6)按要求输出成绩在优、良、中、及格、不及格各区间的学生学号、成绩。
构建基本类利用MFC中得CFILE类来作基本的文件操作,利用CString作数据流的输入输出操作。
流程图6
CScore *stu[20];
int n;
voidC学生成绩管理系统Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
FILE *fp;
CString str;
char ID[20];
floatugrade,mgrade,lgrade;
CStringstradd,strscore,strid;
fp =fopen("E:\\My Project\\My Visual Studio Project\\学生成绩管理系统\\note.dat", "r");
if (fp==NULL)
{
AfxMessageBox(_T("文件打开失败!"));
exit(0);
}
fscanf(fp,"%d", &n);
stradd.Format(_T("总人数:%d\n"),n);
for (int i = 0; i < n;i++)
{
fscanf(fp,"%s%f %f %f", &ID,&ugrade,&mgrade,&lgrade);
stu[i]= newCScore(ID,ugrade,mgrade,lgrade);
strid= stu[i]->GetId();
strscore.Format(_T("%s %.2f %.2f %.2f"),strid,stu[i]->GetUGrade(), stu[i]->GetMGrade(), stu[i]->GetLGrade());
stradd= stradd + strscore +_T("\n");
}
fclose(fp);
AfxMessageBox(stradd);
}
void save()
{
CFile fp;
CString str,stradd,strscore, strid;
int len = 0;
if (!fp.Open(_T("E:\\MyProject\\My Visual Studio Project\\学生成绩管理系统\\out.dat"), CFile::modeCreate | CFile::modeReadWrite))
{
AfxMessageBox(_T("D:\\TXL.TXT \r\n Open failed when write."));
}
for (int i = 0; i < 15;i++)
{
strid= stu[i]->GetId();
strscore.Format(_T("%s %.2f %.2f %.2f %c\n"), strid,stu[i]->GetUGrade(), stu[i]->GetMGrade(), stu[i]->GetLGrade(),stu[i]->GetGrade());
str+= strscore;
}
len= str.GetLength()*2;
//fp.SeekToEnd();
fp.Write(str.GetBuffer(len),len);
fp.Close();
}
voidC学生成绩管理系统Dlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
float sum=0,cul=0;
char ch;
CString strC;
CString stradd, strscore,strid;
for (int i = 0; i < n;i++)
{
cul= float(stu[i]->GetLGrade()*0.3+ stu[i]->GetMGrade()*0.3 + stu[i]->GetLGrade()*0.4);
sum+= cul;
if ((int)cul < 60)
ch= 'E';
if (60<=(int)cul&&(int)cul < 70)
ch= 'D';
if (70<=(int)cul && (int)cul < 80)
ch= 'C';
if (80<=(int)cul && (int)cul < 90)
ch= 'B';
if (90<=(int)cul && (int)cul <=100)
ch= 'A';
stu[i]->SetGrade(ch);
}
strC.Format(_T("全班的总评成绩为:\n%.2f"), sum);
AfxMessageBox(strC);
for (int i = 0; i < n;i++)
{
strid= stu[i]->GetId();
strscore.Format(_T("%s %.2f %.2f %.2f %c"), strid,stu[i]->GetUGrade(), stu[i]->GetMGrade(),stu[i]->GetLGrade(),stu[i]->GetGrade());
stradd= stradd + strscore +_T("\n");
}
AfxMessageBox(stradd);
save();
stradd.ReleaseBuffer();
AfxMessageBox(_T("文件保存成功!"));
}
voidC学生成绩管理系统Dlg::OnBnClickedButton3()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(true);
CString str, strscore,strid;;
char CH1;
int flag = 0;
CH1= (char)strCHGRADE[0];
for (int i = 0; i < n;i++)
{
if(stu[i]->GetGrade() == CH1)
{
flag= 1;
strid= stu[i]->GetId();
strscore.Format(_T("%s %.2f %.2f %.2f %c\n"), strid,stu[i]->GetUGrade(), stu[i]->GetMGrade(), stu[i]->GetLGrade(), stu[i]->GetGrade());
str+= strscore;
}
}
if (flag)
{
AfxMessageBox(str);
flag= 0;
}
else
AfxMessageBox(_T("不存在该等级成员!"));
}
课题内容:
设计一个模拟电信计费系统。能实现从文件中读取通话以及费率资料,并提供计费、话费查询和话单查询等服务。通过此课题,熟练掌握文件读写、数组、结构体、格式输入输出的各种操作,以及友好界面的设计和一些算法思想的应用。
课题要求:
(1)计费功能。根据存放在源数据文件中的通话记录和长途费率文件对每一条通话记录计算其通话费用,并将结果保存在费用文件中。其中:
通话费的计算方法如下:
通话费=长途电话费+本地电话费
长途电话费=费率(元/分钟)×通话时长(分钟)
(通话时长不满1分钟的按1分钟计算)
本地电话费为:3分钟以内0.3元,以后每1分钟递增0.2元。
(2)话费查询。输入一个电话号码,从费用文件中统计该电话号码的所有本地话费、长途话费,并从用户文件中查找其用户名,最后在屏幕上显示:
用户名 电话号码 本地话费 长途话费 话费总计
(3)话单查询。输入一个电话号码,查询并在屏幕显示该用户的所有通话记录,格式为:
用户名 主叫电话号码 被叫电话号码 通话时长
与B1基本一致,无特别事项需要说明。
流程图7
Callist *list[25];
int n;
voidC模拟电信计费系统Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
FILE *fp;
CStringstr,stradd,strouta,stroutn,strina,strinn;
int t;
char outa[NUM], outn[AREA], ina[NUM], inn[AREA];
fp =fopen("E:\\My Project\\My Visual Studio Project\\模拟电信计费系统\\hd.dat", "r");
if (fp == NULL)
{
AfxMessageBox(_T("文件打开失败!"));
exit(0);
}
//fscanf(fp,"%d", &n);
//stradd.Format(_T("总人数:%d\n"), n);
for (int i = 0; fscanf(fp, "%s %s %s %s%d", &outa, &outn, &ina, &inn,&t)!=EOF; i++)
{
list[i]= newCallist(outa, outn, ina,inn, t);
strouta= outa;
stroutn= outn;
strina= ina;
strinn= inn;
str.Format(_T("%s %s %s %s %d\n"), strouta, stroutn,strina, strinn,t);
stradd= stradd + str;
n= i;
}
fclose(fp);
AfxMessageBox(stradd);
}
void save(CStringstr)
{
CFile fp;
int len = 0;
if (!fp.Open(_T("E:\\MyProject\\My Visual Studio Project\\模拟电信计费系统\\fy.dat"), CFile::modeCreate | CFile::modeReadWrite))
{
AfxMessageBox(_T("D:\\fy.dat \r\n Open failed when write."));
}
len= str.GetLength() * 2;
//fp.SeekToEnd();
fp.Write(str.GetBuffer(len),len);
fp.Close();
}
Charge *clist[25];
voidC模拟电信计费系统Dlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
char an[5][10] = { "010","020","021","025","0571" };
float cost;
int type,time;
CString str, stradd,strouta, stroutn, strina, strinn;
for (int i = 0; i < n;i++)
{
if(strcmp(list[i]->Getinarea(),an[0])==0)
{
cost= (float)1.2;
type= 1;
cost= (float)list[i]->Gettime()*cost;
clist[i]= newCharge(list[i]->Getoutarea(),list[i]->Getoutnum(), list[i]->Getinarea(), list[i]->Getinnum(),list[i]->Gettime(), type,cost);
}
elseif(strcmp(list[i]->Getinarea(), an[1])==0)
{
cost= (float)1.2;
type= 1;
cost= list[i]->Gettime()*cost;
clist[i]= newCharge(list[i]->Getoutarea(),list[i]->Getoutnum(), list[i]->Getinarea(), list[i]->Getinnum(),list[i]->Gettime(), type, cost);
}
elseif(strcmp(list[i]->Getinarea(), an[2])==0)
{
cost= (float)0.8;
type= 1;
cost= list[i]->Gettime()*cost;
clist[i]= newCharge(list[i]->Getoutarea(),list[i]->Getoutnum(), list[i]->Getinarea(), list[i]->Getinnum(),list[i]->Gettime(), type, cost);
}
elseif(strcmp(list[i]->Getinarea(), an[3])==0)
{
cost= (float)0.3;
type= 0;
cost= (float)((list[i]->Gettime()-3)*0.2+3*0.3);
clist[i]= newCharge(list[i]->Getoutarea(),list[i]->Getoutnum(), list[i]->Getinarea(), list[i]->Getinnum(),list[i]->Gettime(), type, cost);
}
elseif(strcmp(list[i]->Getinarea(), an[4])==0)
{
cost= (float)1;
type= 1;
cost= list[i]->Gettime()*cost;
clist[i]= newCharge(list[i]->Getoutarea(),list[i]->Getoutnum(), list[i]->Getinarea(), list[i]->Getinnum(),list[i]->Gettime(), type, cost);
}
}
for (int i = 0; i<n; i++)
{
strouta=clist[i]->Getoutarea();
stroutn=clist[i]->Getoutnum();
strina=clist[i]->Getinarea();
strinn=clist[i]->Getinnum();
time= clist[i]->Gettime();
type= clist[i]->Gettype();
cost= clist[i]->Getcharge();
str.Format(_T("%s %s %s %s %d Type:%d Charge:%.2f\r\n"), strouta, stroutn,strina, strinn, time, type, cost);
stradd= stradd + str;
}
AfxMessageBox(stradd);
save(stradd);
AfxMessageBox(_T("已成功保存到文件!"));
}
Cuser *ulist[10];
int m;
void cuser()
{
FILE *fp;
char unum[NUM], uname[20];
fp =fopen("E:\\My Project\\My Visual Studio Project\\模拟电信计费系统\\yh.dat", "r");
if (fp == NULL)
{
AfxMessageBox(_T("文件打开失败!"));
exit(0);
}
//fscanf(fp,"%d", &n);
//stradd.Format(_T("总人数:%d\n"), n);
for (int i = 0; fscanf(fp, "%s %s", &unum, &uname)!= EOF; i++)
{
ulist[i]= newCuser(uname,unum);
m= i;
}
fclose(fp);
}
voidC模拟电信计费系统Dlg::OnBnClickedButton3()
{
// TODO: 在此添加控件通知处理程序代码
cuser();
UpdateData(true);
CString str,strnam;
char num[NUM] = {"\0"};
char *name;
float cost1=0,cost2=0,cost;
for (int i = 0; i <strnum.GetLength(); i++)
num[i]= (char)strnum[i];
for (int i = 0; i < m;i++)
{
if(strcmp(ulist[i]->Getnum(), num) == 0)
name= ulist[i]->Getname();
strnam= name;
}
for (int i = 0; i < n;i++)
{
if ((strcmp(clist[i]->Getoutnum(),num) == 0)||(strcmp(clist[i]->Getinnum(),num)==0))
{
if(strcmp(clist[i]->Getoutarea(),clist[i]->Getinarea())==0)
cost1+= clist[i]->Getcharge();
else
cost2+= clist[i]->Getcharge();
}
}
cost= cost1 + cost2;
str.Format(_T("%s %s 本地:%.2f 长途:%.2f 总:%.2f"), strnam, strnum,cost1,cost2, cost);
AfxMessageBox(str);
}
voidC模拟电信计费系统Dlg::OnBnClickedButton4()
{
// TODO: 在此添加控件通知处理程序代码
char num[NUM] = { "\0" };
char *name;
CString str,strnam,strall,stroutn,strinn;
for (int i = 0; i <strnum.GetLength(); i++)
num[i]= (char)strnum[i];
for (int i = 0; i < m;i++)
{
if(strcmp(ulist[i]->Getnum(), num) == 0)
name= ulist[i]->Getname();
strnam= name;
}
for (int i = 0; i < n;i++)
{
if ((strcmp(clist[i]->Getoutnum(),num) == 0) || (strcmp(clist[i]->Getinnum(), num) == 0))
{
stroutn=clist[i]->Getoutnum();
strinn=clist[i]->Getinnum();
str.Format(_T("%s %s %s %d\n"),strnam,stroutn,strinn,clist[i]->Gettime());
strall+= str;
}
}
AfxMessageBox(strall);
}