这个简单的python代码怎么了?

时间:2018-10-13 13:54:48

标签: python

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <assert.h>
#include <sys/wait.h>
#include <stdint.h>

#define EINVAL       1
#define E2BIG        2
#define MAXMENUARGS  7

//structure job initialize
struct Job {
    //initializing name variable
    char *name;
    int executionTime;
};
//init the array
struct Job jobQueue[5] = {0};
//cmd function provided by intructor
int cmd_run(int nargs, char **args) {
    int execT;
    sscanf(args[2], "%d", &execT);
    run(args[1], execT);
    return 0;
}
//cmd function provided by intructor
void cmd_list() {
    list();
}
//cmd function provided by intructor
static struct {
    const char *name;
    int (*func)(int nargs, char **args);
} cmdtable[] = {
    /* commands: single command must end with \n */
    { "r", cmd_run},
    { "run", cmd_run},
    { "list\n", cmd_list}
};
//cmd function provided by intructor
//this is the function that handles the arguments entered by the user
//provided it just in case someone needs to see how arguments are being 
//processed
int cmd_dispatch(char *cmd) {
    time_t beforesecs, aftersecs, secs;
    u_int32_t beforensecs, afternsecs, nsecs;
    char *args[MAXMENUARGS];
    int nargs = 0;
    char *word;
    char *context;
    int i, result;
    void *Dispatcher(void *arg);
    for (word = strtok_r(cmd, " ", &context);
            word != NULL;
            word = strtok_r(NULL, " ", &context)) {

        if (nargs >= MAXMENUARGS) {
            printf("Command line has too many words\n");
            return E2BIG;
        }
        args[nargs++] = word;
    }
    if (nargs == 0) {
        return 0;
    }
    for (i = 0; cmdtable[i].name; i++) {
        if (*cmdtable[i].name && !strcmp(args[0], cmdtable[i].name)) {
            assert(cmdtable[i].func != NULL);

            /* Call function through the cmd_table */
            result = cmdtable[i].func(nargs, args);
            return result;
        }
    }
    printf("%s: Command not found\n", args[0]);
    return EINVAL;
}

//adds job to the array using user arguments
void run(char name[], int executionTime) {
    //creates a job using the arguments specified by user
    struct Job job = {name, executionTime};
    jobQueue[0] = job;
    printf("\nJob added to queue now please type 'list'\n");
}

//name will not print here
void list() {
    printf("\nSee how the name will not print below?\n");
    char executionTimeStr[5];
    for (int c = 0; c < sizeof (jobQueue) / sizeof (jobQueue[0]); c++) {
        //prints job info formatted 
        if (jobQueue[c].name != NULL) {
            sprintf(executionTimeStr, "%d", jobQueue[c].executionTime);
            //job name will not print here, output is just left blank
            printf("%s %20.8s", "Name", "ExecTime");
            printf("%-10.15s %11.3s\n",
                    jobQueue[c].name,
                    executionTimeStr
                    );
        }
    }
}
int main(int argc, char *argv[]) {

    printf("Welcome to our batch job scheduler\n");
    printf("Please enter the following exactly: 'run job1 10' \n");
    //ignore this, it handles my commandline parser
    char *buffer;
    size_t bufsize = 64;
    buffer = (char*) malloc(bufsize * sizeof (char));
    if (buffer == NULL) {
        perror("Unable to malloc buffer");
        exit(1);
    }
    while (1) {
        printf("User Input: ");
        getline(&buffer, &bufsize, stdin);
        cmd_dispatch(buffer);
    }
    //ignore this, it handles my commandline parser
    return 0;

}

1 个答案:

答案 0 :(得分:0)

您的缩进是错误的,您从未在x语句中引用变量elif,并且在第4行上缺少了一个冒号:

x = 5
if x < 2:
    print ('Below 2')
elif x > 2:
    print ('Two or more')
else:
    print ('Something Else')