我对变量的赋值和While函数有问题

时间:2019-04-18 12:19:13

标签: c variables while-loop

这是我的第一篇文章,很高兴成为这个社区的一员。我是这方面的初学者,可以从我发现的各种教程中学习,这是我的第一个“程序”。我来找你们,因为我的代码遇到两个问题,我的程序员朋友无法回答我(实际上,她告诉我在这里提问):

我敢肯定这是一个简单的答案,但我只是不知道,而且我在互联网上找不到任何合适的答案(或者我看起来不正确)。

谢谢大家!

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

int main()
{
// insert code here...
int num1;
int num3;
char ans[3];

printf("Enter first number: ");
scanf("%i", &num1);
printf("Enter second number: ");
scanf("%i", &num3);
while(!(strcmp(ans,"Sum") | (!(strcmp(ans,"Sub"))) | (!(strcmp(ans,"Mul"))) | (! 
(strcmp(ans,"Div"))) == 0));
printf("What kind of operation do you want to make? Type Sum, Sub, Mul or Div: 
");
scanf("%s", ans);
int Sum = num1 + num3;
int Sub = num1 - num3;
int Mul = num1 * num3;
//    int Div = num1 / num2;

if((strcmp(ans,"Sum") == 0))
{
    printf("The sum of both numbers give %d\n", Sum);
}
//    {printf("%d y %d\n", num1, num2);
//}
if((strcmp(ans,"Sub") == 0))
{
    printf("The substraction of both numbers give %d\n", Sub);
}
if((strcmp(ans,"Mul") == 0))
{
    printf("The multiplication of both numbers give %d\n", Mul);
}
if((strcmp(ans,"Div") == 0))
{
    printf("The division of both numbers give %d\n", num1 / num3);
}
{    if (!(strcmp(ans,"Sum") | (!(strcmp(ans,"Sub"))) | (!(strcmp(ans,"Mul"))) | 
(!(strcmp(ans,"Div"))) == 0))
{
    printf("This command does not apply, please type the correct option\n");
}
    //((strcmp(ans, "Sum")) | (strcmp(ans, "Sub")) | (strcmp(ans, "Mul")) | 
(strcmp(ans, "Div")) != 0) {



}
}
  • 在我运行程序时,它没有为“ num2”分配除0以外的任何值。

  • 我真正想要的“ while”功能是告诉您输入了错误的字符,然后回到主要问题,直到您正确输入为止。

P.S:现在,我已经发送了我编写的所有代码。

2 个答案:

答案 0 :(得分:0)

您不检查是否输入了两个数字的有效数字,因为您不检查 scanf 是否返回1

while 中的测试放置了错误的'('')',对于 if

while 中,您需要先比较 ans 以便阅读,以便使用,这是未定义的行为, while 可以无限循环

用于读取操作的 scanf 不会限制读取字符的数量,您可以写出 ans ,甚至输入有效的操作名称也无济于事记住最后一个空字符

缺少执行操作的程序的结尾


提案:

df['Installs'] = df['Installs'].str.replace('[,+]','').astype(float)
#alternative
#df['Installs'] = df['Installs'].replace('[,+]','', regex=True).astype(float)
print (df)
     Installs
0     10000.0
1    500000.0
2   5000000.0
3  50000000.0
4    100000.0
5     50000.0

如您所见,我没有单独的 while 来仅检查输入操作是否有效,因为这样做之后,您必须再次测试什么是Enter操作来对其进行管理。

编译和执行:

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

int main()
{
  int num1;
  int num2;
  char ans[4];

  printf("Enter first number: ");

  if (scanf("%i", &num1) != 1) {
    puts("invalid number");
    return -1;
  }

  printf("Enter second number: ");

  if (scanf("%i", &num2) != 1) {
    puts("invalid number");
    return -1;
  }

  for (;;) {
    printf("What kind of operation do you want to make? Type Sum, Sub, Mul or Div: ");
    if (scanf("%3s", ans) != 1)
      /* EOF */
      return -1;

    if (!strcmp(ans,"Sum")) {
      printf("%d+%d=%d\n", num1, num2, num1+num2);
      return 0;
    }
    if (!strcmp(ans,"Sub")) {
      printf("%d-%d=%d\n", num1, num2, num1-num2);
      return 0;
    }
    if (!strcmp(ans,"Mul")) {
      printf("%d*%d=%d\n", num1, num2, num1*num2);
      return 0;
    }
    if (!strcmp(ans,"Div")) {
      if (num2 == 0)
        puts("cannot divide by 0");
      else
        printf("%d/%d=%d\n", num1, num2, num1/num2);
      return 0;
    }
    puts("This command does not apply, please type the correct option");
  }
}

答案 1 :(得分:0)

resource "digitalocean_droplet" "web" {
  name               = "web"
  size               = "${var.size}"
  image              = "${var.image}"
  region             = "${var.region}"
  ssh_keys           = [23625200]
  private_networking = "true"

  provisioner "remote-exec" {
    connection {
      host        = "${self.ipv4_address}"
      user        = "root"
      type        = "ssh"
      private_key = "${file("/root/id_rsa")}"
      timeout     = "2m"
    }

    inline = [
      "yum -y install httpd",
    ]
  }
}

这没有任何意义。首先,逻辑或写为while(!( strcmp(ans,"Sum") | (!( strcmp(ans,"Sub"))) | (!( strcmp(ans,"Mul"))) | (!( strcmp(ans,"Div"))) == 0)); 而不是||。其次,末尾的|意味着永远循环,因为在循环内永远不会更改输入。您不应该使用;,而应该使用;并将其放在printf和scanf周围,从而在它们周围形成一个循环。

将其重写为类似的内容:

{ ... }

do { printf("What kind of operation do you want to make? Type Sum, Sub, 'Mul' or Div: "); scanf("%s", ans); }while( strcmp(ans,"Sum") && strcmp(ans,"Sub") && strcmp(ans,"Mul") && strcmp(ans,"Div") ); 如果字符串不匹配,则返回非零,这意味着该C可以用英语读取,如下所示:
“在输入不是Sum且输入不是Sub且输入不是Mul且输入不是Div的情况下,请输入”。