求C语言程序!答好追加500分`
发布网友
发布时间:2024-01-01 12:58
我来回答
共4个回答
热心网友
时间:2024-02-27 21:37
点评:
1、一楼的功能基本实现了,不过,要把void main()改为int main(),不然编绎通不过.
2、二楼的似乎不合楼主要求.因为在输入错误时,不能及时回显*号,不过加上了时间统计,功强了功能.
楼主肯拿分砸,咱也不能让楼主失望。下面是我的程序,虽然是C++写的,不过全是面向过程的程序,改成C语言的起来很简单,如果楼主实在不想改了,可以写纸条给我。
//VC++ 6.0 + win2003下调试通过.
//main.cpp
#include <time.h>
#include <conio.h>
#include <iostream>
using namespace std;
const TEXTLEN = 50;
static char text[TEXTLEN+1];
void printWelcome(void);
void printBye(void);
void printMenu(void);
void typeClass(bool isA, int len=TEXTLEN);
void makeText(char* buf, bool isA, int len);
void main()
{
char key;
bool isExit = false;
printWelcome();
while (!isExit)
{
printMenu();
fflush(stdin);
cin>>key;
switch (key)
{
case '1':
typeClass(true);
break;
case '2':
typeClass(false);
break;
case '3':
isExit = true;
break;
}
}
printBye();
}
void printWelcome(void)
{
cout<<"|=============================================================================|"<<endl;
cout<<"| |"<<endl;
cout<<"| 欢迎您使用打字练习系统 |"<<endl;
cout<<"| 作者: 沙支夸依 2008-7-2 |"<<endl;
cout<<"| |"<<endl;
cout<<"|=============================================================================|"<<endl;
}
void printBye(void)
{
cout<<"|=============================================================================|"<<endl;
cout<<"| 感谢您的使用!再见! |"<<endl;
cout<<"|=============================================================================|"<<endl;
}
void printMenu(void)
{
cout<<"-------------------------------------------------------------------------------"<<endl;
cout<<"- 1、*练习; 2、B级练习; -"<<endl;
cout<<"- 3、退出; -"<<endl;
cout<<"-------------------------------------------------------------------------------"<<endl;
cout<<">>";
}
void typeClass(bool isA, int len)
{
char ch;
int err = 0;
if (isA)
{
cout<<">>"<<"*练习: 随机产生一串小写字符. 任意键开始!";
_getch();
fflush(stdin);
cout<<endl;
makeText(text, true, len);
cout<<">>"<<text<<endl<<">>";
}
else
{
cout<<">>"<<"B级练习: 随机产生一串小写或大写字符. 任意键开始!"<<endl;
_getch();
fflush(stdin);
makeText(text, false, len);
cout<<">>"<<text<<endl<<">>";
}
clock_t start= clock();
for (int i=0; i<len; i++)
{
ch=_getch();
if (text[i] != ch)
{
cout<<'*';
err++;
}
else
cout<<ch;
}
clock_t finish= clock();
double time=double((finish-start))/CLOCKS_PER_SEC;
cout<<endl;
cout<<time<<endl;
cout<<">> 字符串总长: "<<TEXTLEN<<endl;
cout<<">> 错识数 : "<<err<<endl;
cout<<">> 正确率 : "<<(float)(TEXTLEN-err)*100/(float)TEXTLEN<<"%"<<endl;
cout<<">> 打字速度 : "<<(float)TEXTLEN/time<<"字/秒"<<endl;
0==err? cout<<"厉害!全都打对了!"<<endl:cout<<"加把油!你会做的更好!"<<endl;
}
void makeText(char* buf, bool isA, int len)
{
memset(buf, 0, len+1);
srand((unsigned)time(NULL));
if (isA)
{
for (int i=0; i<len; i++)
{
buf[i] = (char)(rand()%26 + 0x61);
}
}
else
{
for (int i=0; i<len; i++)
{
int temp = rand()%52;
buf[i] = temp < 26? (char)(temp + 0x61):(char)(temp + 0x27);
}
}
}
热心网友
时间:2024-02-27 21:37
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "time.h"
void main()
{
int key=0,i,flag=0,ncrc=0,n;
char s[10],c;
printf("输入要打的字符个数n:");
scanf("%d",&n);
printf("设置打字练习难度:A或B(默认为A)");
c=getchar();
while (c!='A' && c!='B') c=getchar();
if (c=='B')
flag=1;
printf("打字练习开始,按esc退出\n");
while (key!=27){
ncrc=0;
srand((unsigned int)time(0));
for (i=0;i<n;i++){
s[i]=rand()%26+97-flag*32*rand()%2;
printf("%c",s[i]);
}
printf("\n");
for (i=0;i<n;i++){
key=getch();
if (key==27) return 0;
if (key==s[i]){
putchar(s[i]);
ncrc++;
}else
putchar('*');
}
printf("\n打对了%d个字符,打错了%d个字符.\n 按任意键继续,esc退出",ncrc,n-ncrc);
key=getch();
}
getch();
return 0;
}
热心网友
时间:2024-02-27 21:38
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <malloc.h>
char *str,*scr;
long start,finish;
void random(int i,int s){
int j;
srand((unsigned)time(NULL));
str=(char*)malloc(sizeof(char)*(s+1));
scr=(char*)malloc(sizeof(char)*(s+1));
for(j=0;j<s;j++){
if(i){
str[j]=rand()%26;
if(rand()%2)str[j]+='A';
else str[j]+='a';
}else str[j]=rand()%26+'a';
}
str[j]='\0';
}void finishs(int sum){
long times=finish-start;
int h,m,s,ms,i,y=0,n=0;
h=(int)times/3600000;
m=(int)((times-(long)h*3600000)/60000);
s=(int)((times-(long)h*3600000-(long)m*60000)/1000);
ms=(int)((times%1000)/10);
printf("use time:\n %d:%d:%d.%d\n\n",h,m,s,ms);
for(i=0;i<sum;i++){
if(str[i]==scr[i]){y++;printf("%c",str[i]);}
else{n++;printf("*");}
}printf("\n\nright value:%d\nright:%d%%\nwrong number:%d\nwrong:%d%%\n",y,(int)(((double)y/(double)sum)*100),n,(int)(((double)n/(double)sum)*100));
}
int main(void) {
char ch;
int i,s;
printf("heard or easy(A或B):");
ch=getchar();
if(ch=='A'||ch=='a'){
i=0;
}else i=1;
printf("how long :");
scanf("%d",&s);
random(i,s);
printf("%s\nenter:\n",str);
start=clock();
scanf("%s",scr);
finish=clock();
finishs(s);
}
热心网友
时间:2024-02-27 21:38
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <malloc.h>
char *str,*scr;
long start,finish;
void random(int i,int s){
int j;
srand((unsigned)time(NULL));
str=(char*)malloc(sizeof(char)*(s+1));
scr=(char*)malloc(sizeof(char)*(s+1));
for(j=0;j<s;j++){
if(i){
str[j]=rand()%26;
if(rand()%2)str[j]+='A';
else str[j]+='a';
}else str[j]=rand()%26+'a';
}
str[j]='\0';
}void finishs(int sum){
long times=finish-start;
int h,m,s,ms,i,y=0,n=0;
h=(int)times/3600000;
m=(int)((times-(long)h*3600000)/60000);
s=(int)((times-(long)h*3600000-(long)m*60000)/1000);
ms=(int)((times%1000)/10);
printf("用时:\n %d:%d:%d.%d\n\n",h,m,s,ms);
for(i=0;i<sum;i++){
if(str[i]==scr[i]){y++;printf("%c",str[i]);}
else{n++;printf("*");}
}printf("\n\n正确字符数:%d\n正确率:%d%%\n错误字符数:%d\n错误率:%d%%\n",y,(int)(((double)y/(double)sum)*100),n,(int)(((double)n/(double)sum)*100));
}
int main(void) {
char ch;
int i,s;
printf("请输入难度(A或B):");
ch=getchar();
if(ch=='A'||ch=='a'){
i=0;
}else i=1;
printf("请输入字符串长度:");
scanf("%d",&s);
random(i,s);
printf("%s\n请输入:\n",str);
start=clock();
scanf("%s",scr);
finish=clock();
finishs(s);
}