存储和跳过输入 - 字符串

时间:2012-03-24 16:34:57

标签: c string input

所以我正在尝试将以下内容实现为输入命令

score string number 
Record number as the score for the player whose name is string. No output is produced. Assume number is an integer between -999999999 and 999999999.

best string 
Output a line containing best string number where number is the best score recorded so far for the player whose name is string. 
Output ? instead of number if no score has been recorded for the player.

highscore 
Output a line containing highscore number where number is the highest score recorded for any player so far. Output highscore ? if no score has been recorded.

Any command other than score, best, highscore. Ignore the command and proceed to the next one.

这是一些示例输入/输出

Sample Input

score FredFlintstonefromBedrock 10
score Wilma 20
score FredFlintstonefromBe 20
highscore
score Betty 30
highscore
best FredFlintstonefromBedrock
score FredFlintstonefromBedrock 25
best FredFlintstonefromBeyond
best Barney

Output for Sample Input

highscore 20
highscore 30
best FredFlintstonefromBe 20
best FredFlintstonefromBe 25
best Barney ?

我给出了以下函数readtring,http://ideone.com/jTq3l ...如果它可以成功读取字符串,则生成true。我正在考虑将分数存储在一个数组中...以获取更多信息http://www.student.cs.uwaterloo.ca/~cs136/assignments/a10/。我也不能使用堆存储。

我的问题是如何存储名称和分数。另外,我如何区分数字和字符串?

这是我到目前为止/ psuodocode:

    #include <stdio.h>
    #include <string.h>
    #include "readstring.h"


    int main(void) {
    char *i = "";  
    int x = 0;     
    char s[1000];
    char* a[100];
    int b[100];

    while (readstring(s,20) != 0) {
     //if(s == "score")
     // void; (do nothing)

     // if s is a number;
     // a[x] = i;
     // b[x] = s;
     // x++;

     //if (s == "highscore");
     // printf("highscore %d\n", (maxof b);

     //if (i == "best") {
     // printf("best %s %d\n", s, (maxof a's correspoding score);

     // else  i = s;
}
}

我觉得上面的^非常错误...有什么建议吗? :)

1 个答案:

答案 0 :(得分:0)

  

如何存储姓名及其分数?

如果您使用的是AVL树,请在节点中存储名称和分数。根据分数对树进行排序。但是,名称查找将是O(n)。

  

我如何区分数字和字符串?

使用stdlib.h中的strtol

  

我觉得[代码]非常错误......有什么建议吗?

不要硬编码字符串的长度。而是写:

while (readstring(s, (sizeof s) - 1))

由于当您从输入中读取单个单词时未保持状态,因此您的伪代码将不会检测到格式错误的输入,如下所示:

score Wilma
highscore 100
best