如何编写一个能找到单词值的代码? [英] How to write a code that will find the value of a word ?

查看:85
本文介绍了如何编写一个能找到单词值的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如字符(例如:abck



gimetric值是:26



当我运行此代码,此消息出现(未定义referance to rangeindex)



我尝试过:



  #include   <   stdio.h  >  
#include < stdlib.h >
#include < stdbool.h >

解决方案

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



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

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

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



你有 main 的错误在

 tolowerCase(c); 


你可以使用整数除法和mo dulo操作,以便一起摆脱if语句。



考虑你有26个字符,你想分成三组。

3 * 9 = 27,所以使用九作为分红将适合账单。



您还必须通过减去'a'来标准化您的输入字符(或者值122),所以你开始计算0.



为了使这个工作正常,你必须检查角色是:

1.字母,所以你永远不会得到大于26的值。

2.小写,否则你最终会得到负值。

这个你已经在你的代码中做了,所以没有问题。



所以为了找到范围索引你只需从你的角色中减去'a'并除以9 。

 a到i => 0 
j到r => 1
s到z => 2



这是有效的,因为整数除法会截断答案,结果只给你整数。

  int  rangeindex( char  c)
{
return (c - ' a')/ 9 ;
}





对于字符值的计算,您可以使用模运算(%),这将给出你用整数除以提醒。

所以使用9将总是给你一个介于0 - 8之间的值。

因此,你需要加1以获得你想要的结果。

  int  letterGimatryValue( char  c)
{
// 结果值介于1到9之间有效字符
int place = 1 +(c - ' a')% 9 ;
return (place *( int )power( 10 ,rangeindex(c)));

// 或者你可以使用标准函数 double pow(double x,双y)
// 在< math.h>中声明;
// return(place *(int)pow(10,rangeindex(c) ));
}





这种方法消除了对if,switch或lookup表的需要。 />


注意:您仍然需要修复 ppolymorphe 指出的错误。

提示:使用返回值。



是的,我知道这是一项学校作业,但减少这么多代码真是太有趣了。 : - )


for example characters (ar example: abck

gimetric value is :26

when i run this code, this message appears(undefined referance to rangeindex)

What I have tried:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

解决方案

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[^]

You have a bug in main at

tolowerCase(c);


You could use integer division and modulo operations in order to get rid of the if statements all together.

Consider that you have 26 characters that you want to divide into three groups.
3 * 9 = 27, so using nine as the dividend will fit the bill.

You also have to normalize your input character by subtracting 'a' (or the value 122), so you start your calculations at 0.

For this to work properly you have to check that the character is a:
1. letter, so you never will get a value larger than 26.
2. lower case, otherwise you will end up with negative values.
This you already do in your code, so no problem there.

So in order to find the range index you only have to subtract 'a' from your character and divide by 9.

a to i => 0
j to r => 1
s to z => 2


This works because integer division will truncate the answer and only give you whole numbers as a result.

int rangeindex(char c)
{
    return (c - 'a') / 9;
}



For the calculation of the value of the character you can use the modulo operation (%), which will give you the reminder when dividing with an integer.
so using 9 will always give you a value between 0 - 8.
Hence, you need to add 1 in order to get the result you want.

int letterGimatryValue(char c)
{
    // Results in a value between 1 and 9 for any valid character
    int place = 1 + (c - 'a') % 9;
    return (place * (int)power(10, rangeindex(c)));

    // Alternatively you can use the standard function double pow(double x, double y)
    // which is declared in <math.h>
    //return (place * (int)pow(10, rangeindex(c)));
}



This approach eliminates the need for if, switch or lookup tables.

Note: You still have to fix the bug pointed out by ppolymorphe.
Hint: Use the return value.

And yes, I know this is a school assignment, but it was kind of fun to reduce so much code. :-)


这篇关于如何编写一个能找到单词值的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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