关于这个问题的简单问题是,为什么它不打印字符串上的第二个值(转换后的第二个值)?

发布于 2024-08-24 10:15:50 字数 1538 浏览 6 评论 0原文

快速提问,我在这里做错了什么。此代码的目的是将输入获取为字符串,输入为“12 34”,“12”和“32”之间有一个空格,并转换并打印来自整数变量的两个单独的数字数字。为什么对函数 copyTemp 的第二次调用不产生值 34?我有一个 index_counter 变量,它跟踪字符串索引及其意味着跳过“空格”字符?我做错了什么?

谢谢。

#include <stdio.h>
#include <string.h>
int index_counter = 0;
int number;
void copyTemp(char *expr,char *temp);

int main(){
 char exprstn[80]; //as global?
 char tempstr[80];

 gets(exprstn);
 copyTemp(exprstn,tempstr);
 printf("Expression: %s\n",exprstn);
 printf("Temporary: %s\n",tempstr);
 printf("number is: %d\n",number);
 copyTemp(exprstn,tempstr);      //second call produces same output shouldnt it now produce 34 in the variable number?
 printf("Expression: %s\n",exprstn);
 printf("Temporary: %s\n",tempstr);
 printf("number is: %d\n",number);

 return 0;
}
void copyTemp(char *expr,char *temp){
 int i;
 for(i = index_counter; expr[i] != '\0'; i++){
  if (expr[i] == '0'){
   temp[i] = expr[i];
  }
  if (expr[i] == '1'){
   temp[i] = expr[i];
  }
  if (expr[i] == '2'){
   temp[i] = expr[i];
  }
  if (expr[i] == '3'){
   temp[i] = expr[i];
  }
  if (expr[i] == '4'){
   temp[i] = expr[i];
  }
  if (expr[i] == '5'){
   temp[i] = expr[i];
  }
  if (expr[i] == '6'){
   temp[i] = expr[i];
  }
  if (expr[i] == '7'){
   temp[i] = expr[i];
  }
  if (expr[i] == '8'){
   temp[i] = expr[i];
  }
  if (expr[i] == '9'){
   temp[i] = expr[i];
  }
  if (expr[i] == ' '){ 
   temp[i] = '\0';
   sscanf(temp,"%d",&number); 
   index_counter = i+1; //skips?
  }
 }
 // is this included here? temp[i] = '\0'; 
}

Quick question, What have I done wrong here. The purpose of this code is to get the input into a string, the input being "12 34", with a space in between the "12" and "32" and to convert and print the two separate numbers from an integer variable known as number. Why doesn't the second call to the function copyTemp, not produce the value 34?. I have an index_counter variable which keeps track of the string index and its meant to skip the 'space' character?? what have i done wrong?

thanks.

#include <stdio.h>
#include <string.h>
int index_counter = 0;
int number;
void copyTemp(char *expr,char *temp);

int main(){
 char exprstn[80]; //as global?
 char tempstr[80];

 gets(exprstn);
 copyTemp(exprstn,tempstr);
 printf("Expression: %s\n",exprstn);
 printf("Temporary: %s\n",tempstr);
 printf("number is: %d\n",number);
 copyTemp(exprstn,tempstr);      //second call produces same output shouldnt it now produce 34 in the variable number?
 printf("Expression: %s\n",exprstn);
 printf("Temporary: %s\n",tempstr);
 printf("number is: %d\n",number);

 return 0;
}
void copyTemp(char *expr,char *temp){
 int i;
 for(i = index_counter; expr[i] != '\0'; i++){
  if (expr[i] == '0'){
   temp[i] = expr[i];
  }
  if (expr[i] == '1'){
   temp[i] = expr[i];
  }
  if (expr[i] == '2'){
   temp[i] = expr[i];
  }
  if (expr[i] == '3'){
   temp[i] = expr[i];
  }
  if (expr[i] == '4'){
   temp[i] = expr[i];
  }
  if (expr[i] == '5'){
   temp[i] = expr[i];
  }
  if (expr[i] == '6'){
   temp[i] = expr[i];
  }
  if (expr[i] == '7'){
   temp[i] = expr[i];
  }
  if (expr[i] == '8'){
   temp[i] = expr[i];
  }
  if (expr[i] == '9'){
   temp[i] = expr[i];
  }
  if (expr[i] == ' '){ 
   temp[i] = '\0';
   sscanf(temp,"%d",&number); 
   index_counter = i+1; //skips?
  }
 }
 // is this included here? temp[i] = '\0'; 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀里藏娇 2024-08-31 10:15:50

您的程序中存在一些问题:

  • 您正在使用相同的索引
    exprtemp 数组。这适用于
    第一次,两者都将为 0
    首先但是当你想要的时候
    处理2nd数字,你需要
    将索引重置到 temp 数组中
    返回0。显然这不可能
    使用单个索引完成。你会
    必须使用两个索引,ij
  • 当您完成时
    处理第二个数字(34
    “12 34”)您将到达
    字符串,因此 sscanf 永远不会
    第二次运行(在
    一般用于最后一次)。所以
    在 for 循环之后你需要另一个
    sscanf 提取最后一个数字。此外,一旦您从字符串中提取了数字并增加了 i,您就应该从函数返回。
  • 您应该避免使用 gets() 并使用
    出于安全考虑,用 fgets() 代替
    原因。
  • 您可以将多个测试结合起来
    将数字放入单个测试中作为
    显示:

类似这样的东西。

void copyTemp(char *expr,char *temp){
    int i;
    int j = 0;
    for(i = index_counter; expr[i] != '\0'; i++){

        if (expr[i] >= '0' && expr[i]<='9'){
            temp[j++] = expr[i]; // copy the digit into temp..increment j.
        }    
        else if (expr[i] == ' '){ // space found..time to extract number.
            temp[j] = '\0'; // terminate the temp.
            sscanf(temp,"%d",&number); // extract.
            index_counter = i+1; // skip the space.
                    return; // done converting...return..must not continue.
        }
    }
    // have reached the end of the input string..and still need to extract a 
    // the last number from temp string.
    temp[j] = '\0';
    sscanf(temp,"%d",&number);
}

经过这些更改后,它按预期工作:

$ gcc b.c 2> /dev/null && ./a.out
12 34
Expression: 12 34
Temporary: 12
number is: 12
Expression: 12 34
Temporary: 34
number is: 34

您的方法非常脆弱......如果用户在输入数字之间给出多个空格......您的程序将失败。

There are a few problems in your program:

  • You are using the same index into
    expr and temp arrays. This works for
    the first time since both will be 0
    to start with but when you want to
    process the 2nd number, you need to
    reset the index into the temp array
    back to 0. Clearly this cannot be
    done using a single index. You'll
    have to use two indices, i and j.
  • By the time you complete the
    processing of the 2nd number ( 34 in
    "12 34") you'll reach the end of the
    string and hence the sscanf never
    gets run on the second occasion ( in
    general for the last occasion). So
    after the for loop you need another
    sscanf to extract the last number. Also you should return from the function once you've extracted the number from the string and incremented i.
  • You should avoid using gets() and use
    fgets() instead because of security
    reasons.
  • You can combine the multiple test for
    the digits into a single test as
    shown:

Something like this.

void copyTemp(char *expr,char *temp){
    int i;
    int j = 0;
    for(i = index_counter; expr[i] != '\0'; i++){

        if (expr[i] >= '0' && expr[i]<='9'){
            temp[j++] = expr[i]; // copy the digit into temp..increment j.
        }    
        else if (expr[i] == ' '){ // space found..time to extract number.
            temp[j] = '\0'; // terminate the temp.
            sscanf(temp,"%d",&number); // extract.
            index_counter = i+1; // skip the space.
                    return; // done converting...return..must not continue.
        }
    }
    // have reached the end of the input string..and still need to extract a 
    // the last number from temp string.
    temp[j] = '\0';
    sscanf(temp,"%d",&number);
}

After these changes it works as expected:

$ gcc b.c 2> /dev/null && ./a.out
12 34
Expression: 12 34
Temporary: 12
number is: 12
Expression: 12 34
Temporary: 34
number is: 34

Your approach is very fragile...if a user gives multiple spaces between the input numbers..your program will fail.

怎会甘心 2024-08-31 10:15:50

主要问题是 copyTemp 写入 temp[i],但每次调用 copyTemp 都会将 i 初始化为 < code>index_counter,而不是 0。这意味着每次调用 copyTemp 都会追加到现有的 temp 缓冲区,而不是覆盖旧内容,并且 因此 sscanf 总是重新读取相同的字符串。您需要使用单独的索引来跟踪从输入缓冲区读取的位置以及写入输出缓冲区的位置。

附加问题:
* 永远不要**使用ggets曾经。请改用 fgets
* 您在copyTemp 中复制了大量代码。相反,您可以这样做:

if (expr[i] == '0' || expr[i] == '1' || ...)

或者更好:

if (isdigit(expr[i]))
  • copyTemp 应该采取一些预防措施,以免溢出其目标缓冲区。 (请注意,copyTemp 甚至不需要将目标缓冲区作为参数。)

  • 您应该避免使用全局变量。 copyTemp 最好采用一个参数来指定从输入字符串开始读取的位置以及是否返回停止位置的索引。

The main problem is that copyTemp writes to temp[i], but each call to copyTemp initializes i to index_counter, not to 0. This means that each call to copyTemp appends to the existing temp buffer instead of overwriting the old contents, and sscanf thus always re-reads the same string. You need to use separate indices to keep track of where to read from the input buffer and where to write to the output buffer.

Additional problems:
* Never** use ggets. Ever. Use fgets instead.
* You duplicate a lot of code in copyTemp. You instead could do:

if (expr[i] == '0' || expr[i] == '1' || ...)

or better:

if (isdigit(expr[i]))
  • copyTemp should take some precautions to not overflow its destination buffer. (Note that copyTemp shouldn't even need to take a destination buffer as an argument.)

  • You should avoid using global variables. It'd be better for copyTemp to take an argument specifying where to start reading from the input string and if it returned the index where it left off.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文