📄 a2打字程序.cpp
字号:
#include<iostream.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<iomanip.h>
#define L1 5//最小长度
#define L2 20//可变长度,即随机性
char str[L1+L2];//用于接收随机串
unsigned int length;//字符串实际长度
bool back()//返回主菜单函数
{
char c[10];
cout<<"按N退出系统,或按其他键继续练习打字:";
cin>>c;
if((c[0]=='n')||(c[0]=='N')) return 0;
else
{
system("cls");
return 1;
}
}
void produce()//产生随机字符串,返回该串
{
unsigned int i;
srand((unsigned)time(NULL));//设置随机数起点
length=rand()%L2+L1;//取随机长度
for(i=0;i<length;i++)
str[i]=rand()%94+33;//按长度取随机字符
for(;i<(L1+L2);i++)//填充'\0'可避免出错
str[i]='\0';
}
void compare(char *m,char *n)//字符串比较,输出正确率
{
unsigned int i,k=0;
for(i=0;i<length;i++)
{
if(*(m+i)!=*(n+i)&&(i<strlen(n)))//已经输入的
{
if(k==0)//第一个错误时
cout<<"----------------------------"
<<"错误列表----------------------------"<<endl;
cout<<" "<<*(n+i)<<"应为"<<*(m+i);
if((k>0)&&(((k+1)%5)==0))
cout<<endl;
k++;
}
else if(i>=strlen(n))//根本没输入的
{
if(k==0)//第一个错误时
cout<<"----------------------------"
<<"错误列表----------------------------"<<endl;
cout<<" "<<*(m+i)<<"没打!";
if((k>0)&&(((k+1)%5)==0))
cout<<endl;
k++;
}
}
if(k>0)//有打错的情况下
{
cout<<endl;
cout<<"-------------------------------"
<<"---------------------------------"<<endl;
}
if(k==length)//完全错误
cout<<"您的输入完全不对哦!请一定要加油哦!"<<endl;
else if(k==0)//全部正确
cout<<"真是太棒了,您的输入一字不差呢!"<<endl;
else//部分正确
{
cout<<"您的输入一共有"<<k<<"处错误,输入正确率为:";
cout<<setprecision(4)<<float(100*length-100*k)
/float(length)<<"%"<<endl;
}
}
void main()//主函数
{
char st[L1+L2+10];//长度加长可减少cin流溢出的错误
loo:produce();//产生随机字符串
cout<<"随机字串:"<<str<<endl;
cout<<"请您输入:";
cin>>st;
compare(str,st);//统计正确率
if(back()) goto loo;//是否继续
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -