C语言字符串比较的问题
打字程序,在屏幕上输出一行英文字符串(带空格),然后提示用户原样输入这行字符串,并给出用户输入的正确率。 本帖最后由 howzyao 于 2020-4-6 19:47 编辑#include <cstdlib>
#include <iostream>
#include <stdio.h>
using namespace std;
int main(int argc, char *argv[])
{
char *a="hello world, this is a typing game.";
printf("%s",a);
int len= strlen( a );
char *p = new char[ len ];
cout<<"\nNow there were "<<len
<<" words.\nplease input these again:"<<endl;
//scanf("%c",p);
cin.getline(p,len);
int sum=0;
for(int i=0;i<len;i++)
{
if(p==a)
;
else
{
cout<<"the error is "<<p<<" right there"<<endl;
sum++;
}
}
cout<<"the total errors were "<<sum<<endl;
double dsum=sum;
double dlen=len;
double r = dsum/dlen;
cout<<"the right percent is "<<r<<endl;
system("PAUSE");
return EXIT_SUCCESS;
} 试试,要是行,赏个分. 我要改一下,遇空格竟然退出了.... 本帖最后由 大河之jian 于 2020-4-6 21:26 编辑
#include<iostream>
#include<string>
using namespace std;
int main()
{
char str="I love fishc.com";
cout<<str<<endl;
int len,count=0;
len=strlen(str);
cout<<"please enter the same sentence"<<endl;
char ch;
for(int i=0;i<len;i++)
{
while((ch=cin.get())!='\n')
{
if(ch==str)
{
count++;
break;
}
else
{
break;
}
}
}
float m=float (count)/len;
cout<<"输入正确率为:"<<m*100<<"%"<<endl;
return 0;
}
你试试我这个。 howzyao 发表于 2020-4-6 19:11
试试,要是行,赏个分.
这个不能啊,兄弟,正确率求不出来 那我重发:
#include <cstdlib>
#include <iostream>
#include <stdio.h>
using namespace std;
int main(int argc, char *argv[])
{
char *a="hello world. this is a typing game.";
printf("%s",a);
int len= strlen( a );
char *p = new char[ len ];
cout<<"\nNow there were "<<len
<<" words.\nplease input these again:"<<endl;
//scanf("%c",p);
cin.getline(p,len);
//p='\0';
int sum=0,err=0;
for(int i=0;i<len;i++)
{
if(p==a)
{
sum++;
cout<<"ok "<<p<<" "<<(int)p<<" -- "<<(int)a<<endl;
//system("pause");
}
else
{
err++;
cout<<"the error is "<<(int)p<<" right there "<<(int)a<<endl;
}
}
cout<< sum<<" "<<err<<endl;
double dsum=sum;
double dlen=len;
double r = dsum/dlen;
cout<<"the right percent is "<<r<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
本帖最后由 howzyao 于 2020-4-7 18:45 编辑
我想我写的还是有点小毛病,就是最后一个句号,不知道是什么原因,导致它不参与比较,难道是cin.getline不好用吗?
请哪位vip,第九坛坛主李大鬼,来指点一下。 我想起来了一个事:
cin.getline(p,len);
//下面增加一排:
cin.get( );
//用这个吃掉回车键,试试。
页:
[1]