int is_ind(char *s) { int i; i = 0; if (is_all_num(&s[0]) == 1) return (1); if (s[i] == ':') { while (s[++i] != '\0') { if (in_str(s[i], LABEL_CHARS) != 1) { while (s[i] == ' ' || s[i] == '\t') i++; if (s[i] == '\0' || in_str(s[i], COMMENT_CHAR)) return (1); return (0); } if (s[i + 1] == '\0') return (1); } } return (0); }
int is_dir(char *s) { int i; i = 0; if (s[i] != '%') return (0); i++; if (s[i] != ':' && is_all_num(&s[i]) != 1) return (0); else if (is_all_num(&s[i]) == 1) return (1); if (s[i] == ':') while (s[++i] != '\0') { if (in_str(s[i], LABEL_CHARS) != 1) { while (s[i] == ' ' || s[i] == '\t') i++; if (s[i] == '\0' || in_str(s[i], COMMENT_CHAR)) return (1); return (0); } } else return (0); return (1); }
void round_trip_tab() { std::string in_str("\"xx\\txx\""); saru_assert( fastjson::dom::parse_string(in_str, &token, &chunk, 0, &ErrorGetter::on_error, &error_getter ) ); saru_assert_equal( in_str, fastjson::as_string( &token ) ); }
int my_str_isalpha(char *str) { char *alpha = "abcdefghijklmnopqrstuvwxyz"; char *alpha_up = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char *curChar; if (my_strlen(str) == 0) return (1); curChar = str; while (*curChar != '\0') { if (!in_str(*curChar, alpha) && !in_str(*curChar, alpha_up)) return (0); curChar = curChar + 1; } return (1); }
void read_tab() { std::string in_str("\"xx\\txx\""); saru_assert( fastjson::dom::parse_string(in_str, &token, &chunk, 0, &ErrorGetter::on_error, &error_getter ) ); saru_assert_equal( fastjson::Token::ValueToken, token.type ); saru_assert_equal( 5u, token.value.size ); saru_assert( token.value.ptr ); saru_assert_equal( "xx\txx", std::string( token.value.ptr, token.value.size ) ); }
void round_trip_tab() { std::string in_str("\"xx\\txx\""); fastjson::dom::Chunk chunk; fastjson::Token token; std::string error_message; saru_assert( fastjson::dom::parse_string(in_str, &token, &chunk, &error_message ) ); saru_assert_equal( in_str, fastjson::as_string( &token ) ); }
int loadtaginfo(ifstream &in , vector<string> & index2tag) { int index; string tag; string s; while(getline(in,s)) { istringstream in_str(s); in_str>>index>>tag; index2tag[index]=(tag); } return 0; }
int loadmidinfo(ifstream &in , vector<int> & index2mid) { int index; int mid; string s; while(getline(in,s)) { istringstream in_str(s); in_str>>index>>mid; index2mid[index]=mid; } return 0; }
void read_tab() { std::string in_str("\"xx\\txx\""); fastjson::dom::Chunk chunk; fastjson::Token token; std::string error_message; saru_assert( fastjson::dom::parse_string(in_str, &token, &chunk, &error_message ) ); saru_assert_equal( fastjson::Token::ValueToken, token.type ); saru_assert_equal( 5u, token.data.value.size ); saru_assert( token.data.value.ptr ); saru_assert_equal( "xx\txx", std::string( token.data.value.ptr, token.data.value.size ) ); }
int check_char(char c) { extern char special_chars[]; extern int in_comment; // 是否在注释中 extern int pre_comment_char; // 上次输入是否为注释符第一位 "/" int i=0; if (in_str()) { return is_str_tag(c); } else if (in_comment) { // 有可能将关闭注释1 if (in_comment == 1 && c == '*') { pre_comment_char = '*'; return 0; } // 关闭注释 1 2 else if ((in_comment == 1 && pre_comment_char == '*' && c == '/') || (in_comment == 2 && c == '\n')) { in_comment = 0; pre_comment_char = 0; return 0; } } else { if (!pre_comment_char && c == '/') { pre_comment_char = '/'; return 0; } else if (pre_comment_char == '/' && c == '*') { pre_comment_char = '*'; in_comment = 1; return 0; } else if (pre_comment_char == '/' && c == '/') { pre_comment_char = 0; in_comment = 2; return 0; } else { pre_comment_char = 0; return index_special_char(c); } } return 0; }
int pipe_parsing(char *str) { char **tab_pipe; int ret; int tab_len; if (!valid_str(str) || !last_verif(str)) return (0); if (in_str(str, token_val("|")) && (tab_pipe = str_to_wordtab(str, token_val("|")))) { tab_len = count_tab(tab_pipe); ret = exec_pipe(tab_pipe, tab_len); free(tab_pipe); } else ret = parse_command_redir(str); return (ret); }
int main(int argc, char* argv[]) { srand(time(NULL)); assert(argc>3); std::ifstream in_str(argv[1]); if (!in_str) throw 1; float alpha = atof(argv[2]); s_t l = atoi(argv[3]); double pstar=0; std::clock_t start_ps = std::clock(); for (s_t i =ceil(alpha*l);i<=l;i++) { pstar+=nChoosek(l,i)*pow(alpha,i)*pow(1-alpha,l-i); } std::cout<<pstar<<'\n'; std::cout<<"Computing p* took "<<(std::clock()-start_ps)/(double)CLOCKS_PER_SEC<<"seconds\n"; std::clock_t start_reading = std::clock(); MyNum num; std::vector<MyNum> numbers; while (num.read(in_str)) { numbers.push_back(num); } std::cout<<"Reading in numbers took "<<(std::clock()-start_reading)/(double)CLOCKS_PER_SEC<<"seconds\n"; std::clock_t start_running = std::clock(); unsigned int m = 100000; unsigned int worked=0; for (int i=0;i<m;i++) { worked+=testSubsequence(numbers,l,alpha); } double p = worked*1.0/m; std::cout<<p<<'\n'; if (p>pstar) std::cout<<"YES\n"; else std::cout<<"NO\n"; std::cout<<"Running algorithm took "<<(std::clock()-start_running)/(double)CLOCKS_PER_SEC<<"seconds\n"; return 0; }
int main() { int c; int i = 0; int open_c, close_c; extern int last_nest_char; extern char nest_chars[]; int code; int now_in_str = 0; for(i=0; i < MAXNESTLEN; i++) { nest_chars[i] = '\0'; } while((c = getchar()) != EOF) { // 开启符判断 code = check_char(c); open_c = is_open_char(code); close_c = is_close_char(code); now_in_str = in_str(); if (c == '"' || c == '\'') { } if ((now_in_str && last_nest_char == close_c) || (!now_in_str && match(last_nest_char, close_c))) { if (!(last_nest_char = pull_nest_char())) { printf("Error: Unmached right syntax: %c \n", close_c); exit(0); } } else if ((!(now_in_str == '\'' && open_c == '"')) && ((now_in_str != '"' && open_c) || (now_in_str == '"' && open_c == '\''))) { last_nest_char = open_c; if (!push_nest_char(last_nest_char)) { printf("Error: Reach max nest len \n"); exit(0); } } } //printf("Remain unclosed chars: %s \n", nest_chars); }
int main(int argc,char* argv[]) { assert(argc==2||argc==3); std::ifstream in_str(argv[1]); if (!in_str) { std::cout<<"ERROR cannot read input file\n"; return 1; } std::string space; in_str>>space; assert(space=="board"); int r,c; in_str>>r>>c; Board board(r,c,in_str); std::vector<Ship*> ships; while (in_str>>space) { try { Ship* ship = new Ship(space); ships.push_back(ship); } catch(std::runtime_error& e) { std::cout<<e.what(); return 1; } } std::sort(ships.begin(),ships.end(),sortShip); for (int i=0;i<ships.size();i++) { std::cout<<ships[i]->getLen()<<"\n"; } board.print(); int count = recurse(board,ships,0); for (int i=0;i<ships.size();i++) delete ships[i]; return 0; }
int main(int argc, char* argv[]) { if (!(argc == 3 || (argc == 4 && std::string(argv[3]).compare("-s") == 0))) { std::cerr << "Usage: " << argv[0] << " ScoreFile OutputFile " << " [-s] " << std::endl; return 1; } std::ifstream in_str(argv[1]); if (!in_str) { std::cerr << "Could not open " << argv[1] << " to read.\n"; return 1; } std::ofstream out_str(argv[2]); if (!out_str) { std::cerr << "Could not open " << argv[2] << " to write.\n"; return 1; } // read in the file and update the data std::string line; std::vector<std::string> tokens; std::vector<Player> players; unsigned int max_name_len = std::string("player").size(); while (std::getline(in_str, line)) { tokens = split(line, ' '); if (tokens.size() < 5) { std::cerr << "Wrong input format in file " << argv[1] << ".\n"; return 1; } int winner; int loser; if (!find_player(players, tokens[0], tokens[1], winner)) { players.push_back(Player(tokens[0], tokens[1])); winner = players.size() - 1; } if (!find_player(players, tokens[3], tokens[4], loser)) { players.push_back(Player(tokens[3], tokens[4])); loser = players.size() - 1; } max_name_len = std::max(std::max(max_name_len, players[winner].name_length()), players[loser].name_length()); // update the match win/lose information players[winner].win_match_by(1); players[loser].lose_match_by(1); // handle the sets information for (unsigned int i = 5; i < tokens.size(); i++) { int games_won; int games_lost; parse_set(tokens[i], games_won, games_lost); // update the game win/lose information players[winner].win_game_by(games_won); players[loser].win_game_by(games_lost); players[winner].lose_game_by(games_lost); players[loser].lose_game_by(games_won); // update the surprise match where // winner loses first set or // loser wins first set. if (i == 5 && games_won < games_lost) { // a match winner loses first set players[winner].surprise_match_by(1); players[winner].lose_win_match_by(1); // a match loser wins first set players[loser].surprise_match_by(1); players[loser].win_lose_match_by(1); } } } // output the match information std::sort(players.begin(), players.end(), more_matches); out_str << "MATCH STATISTICS" << std::endl; output_header(out_str, max_name_len); for (unsigned int i = 0; i < players.size(); i++) { players[i].output_match(out_str, max_name_len); } out_str << std::endl; std::sort(players.begin(), players.end(), more_games); out_str << "GAME STATISTICS" << std::endl; output_header(out_str, max_name_len); for (unsigned int i = 0; i < players.size(); i++) { players[i].output_game(out_str, max_name_len); } // with a 4th argument, output the surprise ranking // of all players if (argc == 4 && std::string(argv[3]).compare("-s") == 0) { out_str << std::endl; std::sort(players.begin(), players.end(), more_surprises); out_str << "SURPRISE STATISTICS" << std::endl; output_header(out_str, max_name_len); for (unsigned int i = 0; i < players.size(); i++) { players[i].output_surprise(out_str, max_name_len); } } }
TEST(JSON, storeobject) { string in_str("Test"); JSON j; j.storeobject (&in_str); }
static int awk_main_2 (MyAwk& awk, int argc, qse_char_t* argv[]) { MyAwk::Run* run; cmdline_t cmdline; int n; awk.setTrait (awk.getTrait() | QSE_AWK_FLEXMAP | QSE_AWK_RWPIPE | QSE_AWK_NEXTOFILE); // ARGV[0] if (awk.addArgument (QSE_T("awk25")) <= -1) { print_error (awk); return -1; } if ((n = handle_cmdline (awk, argc, argv, &cmdline)) <= 0) return n; MyAwk::Source* in, * out; MyAwk::SourceString in_str (cmdline.ins); MyAwk::SourceFile in_file (cmdline.inf); MyAwk::SourceFile out_file (cmdline.outf); in = (cmdline.ins)? (MyAwk::Source*)&in_str: (MyAwk::Source*)&in_file; out = (cmdline.outf)? (MyAwk::Source*)&out_file: &MyAwk::Source::NONE; run = awk.parse (*in, *out); if (run == QSE_NULL) { print_error (awk); return -1; } if (cmdline.fs) { MyAwk::Value fs (run); if (fs.setStr (cmdline.fs) <= -1) { print_error (awk); return -1; } if (awk.setGlobal (QSE_AWK_GBL_FS, fs) <= -1) { print_error (awk); return -1; } } if (cmdline.outc) { if (awk.addConsoleOutput (cmdline.outc) <= -1) { print_error (awk); return -1; } } MyAwk::Value ret; if (awk.loop (&ret) <= -1) { print_error (awk); return -1; } return 0; }
int main(int argc, char* argv[]) { // spit out error if less then two elements if (argc < 3) { usage(argv[0]); exit(1); } // Load ppm image of floor textures to use Image<Color> floor_texture; std::string image_file = std::string(argv[1]); std::cout << "Loading " << argv[1] << std::endl; floor_texture.Load(image_file); // Load in location of furniture std::ifstream in_str(argv[2]); // Is this file is good if (!in_str.good()) { std::cerr << "Can't open " << argv[2] << " to read.\n"; exit(1); } //Load into vector as furnature objects std::vector<Furniture> furnitureVec; int itemCount = 0; loadFurnitureState(in_str,furnitureVec, itemCount); // INITAL RANDOMIZATION if(std::string(argv[3])=="start"){ std::cout << "Initial Randomization" << std::endl; setupInitalRandomPos(furnitureVec,floor_texture); // is file good to write std::ofstream out_str(argv[2]); if (!out_str.good()) { std::cerr << "Can't open " << argv[4] << " to write.\n"; exit(1); } saveFurnitureState(out_str,furnitureVec, itemCount); return 0; } // Randomly moves my furniture until they are in good starting positions std::cout <<"OLD COST " <<calculateCost(furnitureVec, floor_texture) << std::endl; std::cout << furnitureVec.size() <<std::endl; double current_cost = calculateCost(furnitureVec, floor_texture); unsigned int HEAT = 100; unsigned int SAVES = 0; while(0 < HEAT){ for(int i = 0; i < furnitureVec.size(); i ++){ Furniture curItem = furnitureVec[i]; float orginal_x = curItem.getPos().x; float orginal_y = curItem.getPos().y; // try and jitter cur item, this will change the value deskMove(furnitureVec[i],HEAT,floor_texture); // check new cost double new_cost = calculateCost(furnitureVec, floor_texture); // I got a better cost if(new_cost < current_cost){ current_cost = new_cost; // I did worst }else{ // place item back furnitureVec[i].setPos(orginal_x,orginal_y); } }//for HEAT--; if(HEAT%5 == 0){ std::string save_str = convertInt(SAVES); save_str = "saved_state_" + save_str + ".st"; const char *cstr = save_str.c_str(); std::ofstream out_str(cstr); if (!out_str.good()) { std::cerr << "Can't open " << argv[2] << " to write.\n"; exit(1); } saveFurnitureState(out_str,furnitureVec, itemCount); out_str.close(); SAVES++; }//sav }//while }//main
/* * Play one game of Hangperson. The secret word is passed as a * parameter. The function should return true if the player won, * and false otherwise. */ bool one_game(const char *word) { char wordLength = get_length(word); char num_missed = 0; bool game_over = false; char input[1024]; // Create a string containing the secret word char secretWord[wordLength+1]; strcpy(&secretWord, word); printf("The secret word is: %s\n",secretWord); printf("Length of word is: %d\n", wordLength); // Create a string representing the guessing state char currWord[wordLength+1]; for (char i = 0; i < wordLength; i++) { currWord[i] = '_'; } currWord[wordLength] = '\0'; // Create an array to track guessed character char guessedChar[27]; guessedChar[0] = '\0'; while (!game_over) { bool good_input = false; print_gallows(num_missed); printf("%s\n", currWord); char guess = 'a'; while (!good_input) { // Loop until a single alphabet char is typed in print_prompt(guessedChar); fgets(input, 1024, stdin); if (feof(stdin)) { clearerr(stdin); print_tryagain(); continue; } else if (input[0] == '\0' || input[1] != '\n') { print_tryagain(); continue; } else if (!isalpha(input[0])) { print_tryagain(); continue; } else { guess = toupper(input[0]); if (in_str(guess, &guessedChar)) { printf("You already guessed %c\n", guess); continue; } good_input = true; } } append(guess, &guessedChar); if (in_str(guess, &secretWord)) { printf("Good guess.\n"); update_curr(guess, &secretWord, &currWord); if (is_guessed(&currWord)){ game_over = true; break; } } else { printf("Bad guess.\n"); num_missed += 1; if (num_missed >= 7) { game_over = true; print_gallows(num_missed); break; } } printf("Missed: %d\n",num_missed); good_input = true; } if (num_missed >= 7) { printf("You lost.\n"); } else { printf("You won.\n"); } printf("The word is: %s\n", secretWord); }
//main executable //assumes input file will be list of directed edges (u, v), where each line is of form: u v (no commas) int main(int argc, char* argv[]){ srand(clock()); ofstream adj_dif("adjacency_group_differences.txt"); //reads in arguments if(argc != 4 && argc != 5 && argc != 6 && argc != 7){cout << "Please include input file, k value, edge_add/label_swap, output type (overlap, edge_frequency, both, neither), [repeats, det_merge/nondet_merge] \n"; return 0;} ifstream in_str(argv[1]); int k = atoi(argv[2]); bool edge_add = false; string test_profs("edge_add"); if(argv[3] == test_profs){edge_add = true;} string output_type = "neither"; if(argc >= 5){output_type = argv[4];} int repeats = 1; if(argc >= 6){repeats = atoi(argv[5]);} string merge = "none"; if(argc >= 7){merge = argv[6];} //declares basic variables string s; vector<list<int> > Adjacency_graph; vector<list<int> > Final_graph; list<int> l; set<int> a_set; //reads in all the edges, and creates the adjacency graph while(!in_str.eof()){ in_str >> s; int source = atoi(s.c_str()); in_str >> s; int target = atoi(s.c_str()); while(Adjacency_graph.size() <= source){Adjacency_graph.push_back(l);} Adjacency_graph[source].push_back(target); } //declares more variables vector<set<int> > K_neighborhood; Create_K_Graph(Adjacency_graph, k, K_neighborhood); //Problem should be here! list<frequency_edge> edge_frequencies; vector<list<int> > K_neighborhood_lists; vector<int> clique_lookup; vector<int> group_size; vector<int> temp_vec; list<vector<int> >adj_groups; adj_groups.push_back(temp_vec); vector<vector<int> > same_cliques; vector<adj_itr> adj_map; //finds adjacency_groups for label-swapping algorithm if(!edge_add){ for(int i=0; i<K_neighborhood.size(); i++){ list<int> l; for(set<int>::iterator sitr = K_neighborhood[i].begin(); sitr != K_neighborhood[i].end(); sitr++){ l.push_back(*sitr); } K_neighborhood_lists.push_back(l); } kSort(K_neighborhood, adj_groups, adj_map); //super bad. fix later for(adj_itr itr1 = adj_groups.begin(); itr1 != adj_groups.end(); itr1++){ same_cliques.push_back(*itr1); } //assembles clique_lookup (allows constant time checking if two nodes are in all same cliques) //clique_lookup[i]=j if node i is in the jth element of adj_groups clique_lookup.insert(clique_lookup.begin(), K_neighborhood.size(), 0); group_size.insert(group_size.begin(), K_neighborhood.size(), 0); for(int i=0; i<same_cliques.size(); i++){ for(int j=0; j<same_cliques[i].size(); j++){ clique_lookup[same_cliques[i][j]] = i; group_size[same_cliques[i][j]] = same_cliques[i].size(); } } } for(int i=0; i<repeats; i++){ if(edge_add){ Final_graph.insert(Final_graph.begin(), K_neighborhood.size(), l); Edge_Adding(K_neighborhood, Final_graph, k); } else{ list<vector<int> > temp_adj_groups = adj_groups; if(merge == "nondet_merge"){ int maximum_difference = 5; //the maximum difference between any two adjacency groups that can be merged int total_difference = 1000000; //the sum of the differences between merged adjacency groups //non-deterministically merges similar adjacency groups //algorithm stops when all adjacency groups have difference greater than maximum_difference, or when sum of differences //of merged adjacency groups is greater than total_differences merge_similar_adj_groups(maximum_difference, total_difference, Adjacency_graph, K_neighborhood, temp_adj_groups, adj_map); } else if(merge == "det_merge"){ int maximum_difference = 5; //deterministically merges closest adjacency groups until all remaining groups have a difference greater than maximum difference deterministic_merge2(Adjacency_graph, K_neighborhood, temp_adj_groups, adj_map, maximum_difference); } ofstream merged_adj("merged_adj"); for(adj_itr i=temp_adj_groups.begin(); i!=temp_adj_groups.end(); i++){ for(int j=0; j<(*i).size(); j++){ merged_adj << (*i)[j] << " "; } merged_adj << endl; } Label_Swap(Adjacency_graph, Final_graph, temp_adj_groups); } //updates list of edge-frequencies based on Final_graph list<frequency_edge>::iterator freq_itr = edge_frequencies.begin(); for(int j=1; j<Final_graph.size(); j++){ Final_graph[j].sort(); list<int>::iterator itr = Final_graph[j].begin(); while(*itr < j && itr != Final_graph[j].end()){itr++;} while(itr != Final_graph[j].end()){ //cout << j << " " << *itr << endl; //cout << freq_itr -> edge_type; if(freq_itr != edge_frequencies.end() && freq_edge_less(*freq_itr, j, *itr)){freq_itr++;} else if(freq_itr != edge_frequencies.end() && freq_itr->node1==j && freq_itr->node2==*itr){(freq_itr->frequency)++;freq_itr++;itr++;} else{//adds new frequency edge to list //assigns type to frequency edge string edge_type = "original"; bool in_original = false; for(list<int>::iterator listitr = Adjacency_graph[j].begin(); listitr != Adjacency_graph[j].end(); listitr++){ if(*listitr == *itr){in_original = true; break;} } if(!in_original){ if(K_neighborhood[j].find(*itr) == K_neighborhood[j].end()){edge_type = "invalid";} else{edge_type = "valid";} } frequency_edge t(j, *itr, 1, edge_type); edge_frequencies.insert(freq_itr, t); itr++; } } } if(i+1 < repeats){Final_graph.clear();} } //outputs overlap from last Final_graph if(output_type == "both" || output_type == "overlap"){ ofstream overlap_out("overlap.txt"); vector<set<int> > New_K_neighborhood; Create_K_Graph(Final_graph, k, New_K_neighborhood); output_overlap(overlap_out, K_neighborhood, New_K_neighborhood); } //outputs frequencies from edge_frequencies //calculates relevant statistics if(output_type == "both" || output_type == "edge_frequency"){ ofstream freq_out("edge_frequencies.txt"); ofstream edge_out("all_edge_frequencies.txt"); vector<int> frequencies; while(frequencies.size() <= repeats){frequencies.push_back(0);} for(list<frequency_edge>::iterator itr=edge_frequencies.begin(); itr!=edge_frequencies.end(); itr++){ frequencies[itr->frequency]++; edge_out << itr->node1 << " " << itr->node2 << " " << itr->frequency << " " << itr -> edge_type << endl; } } //outputs last final_graph ofstream out_str("output.txt"); for(int i=0; i<Final_graph.size(); i++){ for(list<int>::iterator listitr = Final_graph[i].begin(); listitr != Final_graph[i].end(); listitr++){ out_str << i << " " << *listitr << endl; } } }