示例#1
0
文件: D.cpp 项目: leonacwa/code
int main() {
	int runs;
	scanf("%d", &runs);
	while (runs--) {
		scanf("%d%d", &n, &m);
		for (int i = 0; i < n; ++i) scanf("%s", mat[i]);
		bool ans = true;
		memset(vis, 0, sizeof(vis));
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < m; ++j) {
				if (mat[i][j] == 'F') {
					if (!check_x(i, j)) {
						ans = false;
						break;
					}
				} else {
					if (count_f(i, j) != mat[i][j] - '0') {
						ans = false;
						break;
					}
				}
			}
			if (!ans) break;
		}
		int cnt_vis = 0, cnt_flag = 0;
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < m; ++j) {
				if (vis[i][j]) ++cnt_vis;
				if (mat[i][j] == 'F') ++cnt_flag;
			}
		}
		if (ans && cnt_vis != cnt_flag) ans = false;
		puts(ans ? "Well done Clark!" : "Please sweep the mine again!");
	}
	return 0;
}
示例#2
0
//main funcation starts
int main(int argc, char **argv)
{
        //buffer is to hold the commands that the user will type in
        char buffer[80];
        time_t mytime;
        mytime = time(NULL);
        char *s_close = "exit\n";

		char pathname[80];
    	getcwd(pathname,sizeof(pathname));
 
        while(1)
        {
                //print the prompt, including time and welcome
                printf("%s",ctime(&mytime));
                printf("Jeremy's Shell:~%s$",pathname);
                
                //get input
                fgets(buffer, 80, stdin);
                
                if (strcmp (buffer,s_close) == 0)
                {
                	exit(0);
                }

                //fork!
                int pid = fork();
                //Error checking to see if fork works
                //If pid !=0 then it's the parent
                if(pid!=0)
                {
                        wait(NULL);
                }
                else
                {
                    int no_of_args = count_f(buffer);
                    //printf("no_of_args = %d\n",no_of_args);
                    //Add one to the no_of_args so that the last one array is NULL
                    char** array_of_strings = malloc((sizeof(char*)*(no_of_args+1)));
 
                    //break the string up and create an array of pointers that point to each of the arguments.          
                    int count=0;
                    char* pch2;

                    if (no_of_args>1)
                    {
                        pch2 = strtok (buffer," ");
                        while (pch2 != NULL)
                        {
                            array_of_strings[count]=(char*)malloc((sizeof(char)*strlen(pch2)));
                            strcpy(array_of_strings[count], pch2);
                            pch2 = strtok (NULL, " ");
                            count++;
                        }
                        //printf("count = %d\n",count);
                    }
                    else
                    {
                        count=1;
                        array_of_strings[0] = buffer;
                        array_of_strings[1]=NULL;
                    }
                    //deal with the '\n' in the string
                    count--;
                    int i=0;
                    while(1)
                    {
                        if (array_of_strings[count][i] == '\n')
                        {
                            array_of_strings[count][i] = 0;
                            break;
                        }
                        i++;
                    }

                    //format for command is eg. ls -a -l
                    //the first element in the array will be the program name
          
                    char *prog = malloc(sizeof(char)*(strlen(array_of_strings[0])));
                    prog = array_of_strings[0];
                    
                      //  int k=0;
                      // for(k=0; k<=(no_of_args); k++)
                      //      printf(">>>>%s\n", array_of_strings[k]);
                    
                    char *s_cd = "cd";
                    char *s_pwd = "pwd";
                    char *s_ls = "ls";

                    if (strcmp (prog,s_cd) == 0)
                    {
                        cd(array_of_strings);
                    }
                    else if (strcmp (prog,s_pwd) == 0)
                    {
                        pwd();
                    }
                    else if (strcmp (prog,s_ls) == 0)
                    {
                        ls();
                    }
                    else
                    {
                        //pass the prepared arguments to execv and we're done!
                        int rv = 0;
                        rv = execvp(prog, array_of_strings);
                        printf("No command \"%s\" found!\n",prog);
                        exit(0);
                        //printf("%s\n",strerror(errno));
                    }
                }
        }
        return 0;
}