鱼C论坛

 找回密码
 立即注册
查看: 865|回复: 8

[已解决]C语言字符串比较的问题

[复制链接]
发表于 2020-4-5 22:12:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
打字程序,在屏幕上输出一行英文字符串(带空格),然后提示用户原样输入这行字符串,并给出用户输入的正确率。
最佳答案
2020-4-6 21:12:46
那我重发:


#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[len+1]='\0';
   
    int sum=0,err=0;
    for(int i=0;i<len;i++)
    {
        if(p[i]==a[i])
        {
            sum++;
            cout<<"ok "<<p[i]<<" "<<(int)p[i]<<" -- "<<(int)a[i]<<endl;
            //system("pause");
        }
        else
        {
            err++;
            cout<<"the error is "<<(int)p[i]<<" right there "<<(int)a[i]<<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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-4-6 19:10:54 | 显示全部楼层
本帖最后由 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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-6 19:11:36 | 显示全部楼层
试试,要是行,赏个分.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-6 19:22:55 | 显示全部楼层
我要改一下,遇空格竟然退出了....
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-6 21:08:43 | 显示全部楼层
本帖最后由 大河之jian 于 2020-4-6 21:26 编辑

#include<iostream>
#include<string>
using namespace std;
int main()
{
        char str[100]="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;
}
你试试我这个。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-6 21:10:53 | 显示全部楼层
howzyao 发表于 2020-4-6 19:11
试试,要是行,赏个分.

这个不能啊,兄弟,正确率求不出来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-6 21:12:46 | 显示全部楼层    本楼为最佳答案   
那我重发:


#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[len+1]='\0';
   
    int sum=0,err=0;
    for(int i=0;i<len;i++)
    {
        if(p[i]==a[i])
        {
            sum++;
            cout<<"ok "<<p[i]<<" "<<(int)p[i]<<" -- "<<(int)a[i]<<endl;
            //system("pause");
        }
        else
        {
            err++;
            cout<<"the error is "<<(int)p[i]<<" right there "<<(int)a[i]<<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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-7 18:43:38 From FishC Mobile | 显示全部楼层
本帖最后由 howzyao 于 2020-4-7 18:45 编辑

我想我写的还是有点小毛病,就是最后一个句号,不知道是什么原因,导致它不参与比较,难道是cin.getline不好用吗?
请哪位vip,第九坛坛主李大鬼,来指点一下。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-7 21:00:59 From FishC Mobile | 显示全部楼层
我想起来了一个事:
cin.getline(p,len);   
//下面增加一排:
cin.get( );
//用这个吃掉回车键,试试。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-26 12:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表