功能不起作用...... [英] Fuction is not working...

查看:100
本文介绍了功能不起作用......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图计算一个名字的字符总和,例如a = 1,b = 2 ... z = 26

直到总和小于10.

计算器函数正在为第一个字符串返回,但是第二个字符串无法正常工作...请帮助... ...提前感谢..



我尝试了什么:



i was trying to calculate the summation of characters of a name such as a=1,b=2...z=26
untill summation is less than 10.
the value of calculator function is returning corrcectly for first string but it's not working correctly for 2nd string...help please...thanks in advance..

What I have tried:

#include <iostream>
#include
using namespace std;


double calculator(int n)
{
    int m=n;double num=0.0;
    while(m!=0)
    {
        int digit=m%10;
        num+=digit;
        m=m/10;

    }

    if(num>=10)
        calculator(num);
        else
        return num;
}


int main()
{
    string s1,s2;
    cin>>s1>>s2;
    transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
     transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
//cout<<s1<<" "<<s2;


        int len1=s1.size();
        int len2=s2.size();
        int number1=0,number2=0;
        int digit;
        for(int i=0;i<len1;i++)>
        {

            if(s1[i]=='a')
                digit=1;
            else if(s1[i]=='b')
            digit=2;
            else if(s1[i]=='c')
                digit=3;
            else if(s1[i]=='d')
                digit=4;
            else if(s1[i]=='e')
            digit=5;
            else if(s1[i]=='f')
                digit=6;
            else if(s1[i]=='g')
                digit=7;
                else if(s1[i]=='h')
            digit=8;
            else if(s1[i]=='i')
                digit=9;
            else if(s1[i]=='j')
                digit=10;
            else if(s1[i]=='k')
            digit=11;
            else if(s1[i]=='l')
                digit=12;
            else if(s1[i]=='m')
                digit=13;
                else if(s1[i]=='n')
            digit=14;
            else if(s1[i]=='o')
                digit=15;
            else if(s1[i]=='p')
                digit=16;
            else if(s1[i]=='q')
            digit=17;
            else if(s1[i]=='r')
                digit=18;
            else if(s1[i]=='s')
                digit=19;
                else if(s1[i]=='t')
            digit=20;
            else if(s1[i]=='u')
                digit=21;
            else if(s1[i]=='v')
                digit=22;
            else if(s1[i]=='w')
            digit=23;
            else if(s1[i]=='x')
                digit=24;
            else if(s1[i]=='y')
                digit=25;
                else if(s1[i]=='z')
                    digit=26;

            //int digit=(int)s1[i];
            number1+=digit;
        }

        double x=calculator(number1);
         for(int i=0;i<len2;i++)>
        {
            if(s2[i]=='a')
                digit=1;
            else if(s2[i]=='b')
            digit=2;
            else if(s2[i]=='c')
                digit=3;
            else if(s2[i]=='d')
                digit=4;
            else if(s2[i]=='e')
            digit=5;
            else if(s2[i]=='f')
                digit=6;
            else if(s2[i]=='g')
                digit=7;
                else if(s2[i]=='h')
            digit=8;
            else if(s2[i]=='i')
                digit=9;
            else if(s2[i]=='j')
                digit=10;
            else if(s2[i]=='k')
            digit=11;
            else if(s2[i]=='l')
                digit=12;
            else if(s2[i]=='m')
                digit=13;
                else if(s2[i]=='n')
            digit=14;
            else if(s2[i]=='o')
                digit=15;
            else if(s2[i]=='p')
                digit=16;
            else if(s2[i]=='q')
            digit=17;
            else if(s2[i]=='r')
                digit=18;
            else if(s2[i]=='s')
                digit=19;
                else if(s2[i]=='t')
            digit=20;
            else if(s2[i]=='u')
                digit=21;
            else if(s2[i]=='v')
                digit=22;
            else if(s2[i]=='w')
            digit=23;
            else if(s2[i]=='x')
                digit=24;
            else if(s2[i]=='y')
                digit=25;
                else if(s2[i]=='z')
                    digit=26;

            number2+=digit;
        }

        double y=calculator(number2);                  //why it's not returning exact value??

        if(x<y)>
        {
            double cal=(x/y)*100;
            printf("%.2lf%\n",cal);
        }
        else
        {
            double cal=y/x;
            cal*=100;
            printf("%.2lf%\n",cal);
        }
    //cout << "Hello world!" << endl;
    return 0;
}

推荐答案

首先,摆脱长 if..else if .. .else if ... 将char转换为int的代码:

First off, get rid of the long if..else if...else if... code for converting a char to an int:
if (s1[i] >= 'a' && s1[i] <= 'z')
   {
   digit = (int)(s1[i] - 'a') + 1;
   }

会这样做。

然后理清你的缩进!目前非常难以阅读,因为代码遍布整个地方并且根本不明显。

然后将用于处理字符串的代码提取到接受字符串的函数中并返回数字值 - 调用两次,每个字符串一次,就像你拥有计算器一样。

这样,你正在使用用于处理每个字符串的相同代码。

如果您仍然遇到问题,请使用调试器来查看代码并查看每个阶段的确切操作。

是的,我能为你做到这一点 - 但我不知道你输入的是两个字符串,这是你的功课! :笑:

试一试 - 看看你能找到什么!

如果你还是卡住了,请回到我们身边,准确解释它出错的地方,以及你得到的值。

Will do it.
Then sort out your indentation! At the moment that is very hard to read as the code is all over the place and it's not obvious at all.
Then extract your code for processing a string into a function which accepts a string and returns the number value - call that twice, once for each string in the same way that you have the calculator function.
That way, you are using the same code for processing each string.
If you still have a problem, then use the debugger to follow the code through and look at exactly what it is doing at each stage.
Yes, I could do that for you - but I don't know what you are typing as the two strings, and it's your homework! :laugh:
Give it a try - see what you can find!
If you are still stuck, come back to us and explain exactly where it goes wrong, and what values you are getting.


你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



使用调试器查看计算器正在做什么。



建议:想想你做了什么。你复制2个字符串的代码。

你会用3个字符串做什么?再次复制?

还有5个字符串?有10个字符串? 100个字符串?

通过创建一个函数,确保以相同的方式处理两个字符串。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Use the debugger to see what calculator is doing.

Advice: Think about what you do. you duplicate the code for 2 strings.
What would you do with 3 strings ? copy once more ?
And with 5 strings? with 10 strings ? with 100 strings ?
By making a function, you ensure that both strings are processed the same way.


计算器功能有缺陷。尝试

The calculator function is flawed. Try
int calculator(int n)
{
    int m = n,  num=0;
    while( m !=0 )
    {
        int digit= m % 10;
        num += digit;
        m = m / 10;

    }

    if( num >= 10 )
      return calculator(num);
    else
      return num;
}







[更新]

既然你'我已经使用了算法标题,您也可以尝试




[update]
Since you've already used the algorithm header, you may also try

 #include <iostream>
 #include <algorithm>
 #include <iomanip>
 using namespace std;

int accumulator(const int & cur_value, const char & c)
{
  return (cur_value + (::tolower(c) - 'a' + 1));
}

int modul(int n)
{
  int result = 0;
  while (n)
  {
    result += n % 10;
    n /= 10;
    result = result % 10 + result / 10;
  }
  return result;
}

int main()
{
    string s1,s2;
    cin >> s1 >> s2;

    int i1, i2;

    i1 = accumulate(s1.begin(), s1.end(), 0, accumulator);
    i2 = accumulate(s2.begin(), s2.end(), 0, accumulator);

    cout << i1 << ", " << i2 << endl;

    int x1, x2;
    x1 = modul(i1);
    x2 = modul(i2);

    cout << x1 << ", " << x2 << endl;

    if ( x1 > x2) swap(x1,x2);
    double cal = 100.0 * x1 / x2;

  cout << fixed << setprecision(2) <<  cal << "%" << endl;
}



[/ update]


[/update]


这篇关于功能不起作用......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆