c - 将参数解析为数组

标签 c arrays parameter-passing command-line-arguments

我正在尝试创建拉米纸牌游戏的简化版本。我需要解析卡片缩写,例如 SA 是 Spades Ace。 DT 是 Diamond 10 等。我知道有一种更简单的方法可以做到这一点,但这就是我的任务想要完成的方式。

示例执行看起来像

rummy 3 S2 H9 C4...等包括全部52张牌

argv[1]中的数字是游戏中的玩家。我应该如何将卡片从数字后面开始并将它们放入数组中?

到目前为止我的代码

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

int main(int argc, char *argv[])
{
    int players = *argv[1];
    char deck[52];
    int i, j, pid, cid;
    if (players > '5' || players < '3')
    {
        printf("%c is not the allowed number of players, min is 3 and max is 5\n",*argv[1]); 
        exit(0);
    }

}

最佳答案

快速而肮脏的演示:

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

int main(int argc, char *argv[])
{
  int players = atoi(argv[1]);
  char deck[52][3];
  int i, j, pid, cid;

  if (players > 5 || players < 3)
  {
    printf("%d is not the allowed number of players, min is 3 and max is 5\n", players);
    exit(0);
  }

  for (i = 0; i < argc - 2; i++)
  {
    strcpy(deck[i], argv[i+2]);
  }

  for (i = 0; i < argc - 2; i++)
  {
    printf("%s\n", deck[i]);
  }
}

绝对不会对输入进行健全性检查。这只是为了演示。

关于c - 将参数解析为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35596448/

相关文章:

javascript - React Native - 动态图像源

c - 有没有办法预定义 C 函数参数的预期长度?

javascript - 无法将参数从ajax传递到php

无法让 HTTP POST 请求正常工作

c - 以编程方式查找受 Postgres COPY 影响的行数

c - 稳定的归并排序C

java - 在java中用单独的矩形渲染等轴测图

c - Solaris SPARC : How to see what hanging process is doing?

javascript - 当 child 的 parent 被过滤器排除时,如何在 array.filter() 回调函数中获取他们?

c - 传递值以进一步向下调用堆栈