struct transaction * sortedArraysCommonElements(struct transaction *a, int alen, struct transaction *b, int blen)
{
	if (a == NULL || b == NULL)
		return NULL;
	int min = (alen>blen) ? blen : alen;
	struct transaction *c = (struct transaction *)malloc(min*sizeof(struct transaction));
	min = 0;
	int i = 0, j = 0;
	for (; i<alen && j<blen;)
	{
		reverse1(a[i].date);
		reverse1(b[j].date);
		if (strcmp(a[i].date, b[j].date) == 0)
		{
			strcpy(c[i].date, a[i].date);
			reverse1(c[i].date);
			strcpy(c[i].description, a[i].description);
			c[i].amount = a[i].amount;
			i++;
			j++;
			min++;
		}
		else if (strcmp(a[i].date, b[j].date)<0)
			i++;
		else
			j++;
	}
	if (min)
		return c;
	return NULL;
}
Ejemplo n.º 2
0
sexpr reverse(sexpr x)
{
#   ifdef TEST_WITH_SYSTEM_MALLOC
      malloc(100000);
#   endif
    return( reverse1(x, nil) );
}
Ejemplo n.º 3
0
Archivo: e8-5.cpp Proyecto: hizcode/ppp
int main()
try
{
	vector<int> val;

	cout << "Please enter a sequence of integers ending with any non-digit character: ";
	int i;
	while (cin>>i) val.push_back(i);
	print("\nInput:\n",val);
	reverse1(val);
	print("\nReversed once:\n",val);
	reverse2(val);
	print("\nReversed again:\n",val);


	keep_window_open("~");	// For some Windows(tm) setups
}
catch (runtime_error e) {	// this code is to produce error messages; it will be described in Chapter 5
	cout << e.what() << '\n';
	keep_window_open("~");	// For some Windows(tm) setups
}
catch (...) {	// this code is to produce error messages; it will be described in Chapter 5
	cout << "exiting\n";
	keep_window_open("~");	// For some Windows(tm) setups
}
Ejemplo n.º 4
0
/* Return reverse(x) concatenated with y */
sexpr reverse1(sexpr x, sexpr y)
{
    if (is_nil(x)) {
        return(y);
    } else {
        return( reverse1(cdr(x), cons(car(x), y)) );
    }
}
Ejemplo n.º 5
0
void test_reverse1_to_reverse_the_string_itself(CuTest *tc)
{
    /*
     * The string block "str" is readonly on  C, so we must
     * create an "array" of chars with null char at end
     */
    char text1[] = { 'b', 'u', 't', '\0' };
    char text2[] = { 'd', 'o', '\0' };
    
    CuAssertStrEquals(tc, "but", text1);
    CuAssertStrEquals(tc, "do", text2);
    
    reverse1(text1);
    reverse1(text2);
    
    CuAssertStrEquals(tc, "tub", text1);
    CuAssertStrEquals(tc, "od", text2);
}
Ejemplo n.º 6
0
void test_reverse(n)
{
	printf("Testing reverse with a linked list containing %d node(s) ...\n", n);
	List_Pointer head = init(n);
	print(head);
	List_Pointer reversed = reverse1(head);
	//List_Pointer reversed = reverse(head);
	print(reversed);
	printf("\n");
}
Ejemplo n.º 7
0
int main(int argc, char** argv) {

    char c[] = "hello"; // note: NOT char* c = "hello"; -> UB
    printf( "%s\n", c);
    reverse1( c);
    printf( "%s\n", c);
    
    inplace_reverse( c);
    printf( "%s\n", c);
    return 0;
}
Ejemplo n.º 8
0
int main() {
	int len;
	char line[MAX_LINE_NUM];

	while((len = getline1(line, MAX_LINE_NUM)) > 0) {
		if(line[len - 1] == '\n') {
			line[len - 1] = '\0';
		}
		reverse1(line);
		printf("reverse output:%s\n", line);
	}
}
Ejemplo n.º 9
0
int main(int argc, char *argv[])
{
    char str1[] = "hahaya"; //char *p = "hahaya"字符串常量 不可修改
    reverse1(str1);
    std::cout << str1 << std::endl;

    char str2[] = "hahaya";
    reverse2(str2);
    std::cout << str2 << std::endl;

    return 0;
}
Ejemplo n.º 10
0
/*
No extra space, done inplace with two variables
to store the start and the end
*/
void reverse1(char *a, int start, int end)
{
	if (start >= end)
		return;

	reverse1(a, start+1, end-1);

	char temp = a[start];
	a[start] = a[end];
	a[end] = temp;
	
	return;
}
Ejemplo n.º 11
0
/*
 * Runs the machine and returns true or false if the machine accepts
 * the input.
 *
 * The token read is saved on token, so it expects that the size is
 * bigger enough to put the token. (this is not safe, and must be
 * refactored)
 */
bool fa_run(FiniteAutomata *m, char *token, BufferedInputStream *in)
{
    state_t state = m->initial_state;
    int c, j = 0;;
    mark(in);
    while (true) {
        c = read(in);
        //printf("%c", c);
        switch (m->actions[state][c]) {
        case ERROR:
            //printf(" -> ERROR\n");
            token[j++] = c;
            token[j] = '\0';
            reverse1(token);
            while (j > 0) {/* pushback token consumed */
                unread(token[--j], in);
                token[j] = '\0'; /* cleaning token */
            }
            unmark(in);
            return false;
        case MOVEAPPEND:
            //printf(" -> MOVEAPPEND\n");
            state = m->transitions[state][c];
            token[j++] = c;
            break;
        case MOVENOAPPEND:
            //printf(" -> MOVENOAPPEND\n");
            state = m->transitions[state][c];
            break;
        case HALTAPPEND:
            //printf(" -> HALTAPPEND\n");
            token[j++] = c;
            token[j] = '\0';
            unmark(in);
            return true;
        case HALTNOAPPEND:
            //printf(" -> HALTNOAPPEND\n");
            token[j] = '\0';
            unmark(in);
            return true;
        case HALTREUSE:
            //printf(" -> HALTREUSE\n");
            unread(c, in);
            token[j] = '\0';
            unmark(in);
            return true;
        }
    }
}
Ejemplo n.º 12
0
/*
 * Reverse a chain of CM ops
 */
static NODE *
reverse(NODE *p)
{
	NODE *l = p->n_left;
	NODE *r = p->n_right;

	p->n_left = r;

	if (l->n_op == CM)
		return reverse1(l, p);

	p->n_right = l;

	return p;
}
Ejemplo n.º 13
0
static NODE *
reverse1(NODE *p, NODE *a)
{
	NODE *l = p->n_left;
	NODE *r = p->n_right;

	a->n_right = r;
	p->n_left = a;

	if (l->n_op == CM) {
		return reverse1(l, p);
	} else {
		p->n_right = l;
		return p;
	}
}
Ejemplo n.º 14
0
int main()
{
	vector<string> v;
	
	cout << "Enter a sequence of strings (enter \"quit\" to exit loop): ";
	string str;
	while (cin >> str && str != "quit")
		v.push_back(str);
	
	cout << "Reversing with reverse1(): ";
	v = reverse1(v);
	print(v);

	cout << "Reversing with reverse2(): ";
	reverse2(v);
	print(v);

	return 0;
}
Ejemplo n.º 15
0
int main()
{
	vector<int> v;
	
	cout << "Enter a sequence of integers (enter | to quit loop): ";
	int n;
	while (cin >> n)
		v.push_back(n);
	
	cout << "Reversing with reverse1(): ";
	v = reverse1(v);
	print(v);

	cout << "Reversing with reverse2(): ";
	reverse2(v);
	print(v);

	return 0;
}
Ejemplo n.º 16
0
void    autonomous_routine1(void)

{
    unsigned int            sonar_distance = 0;
    static unsigned long    elapsed_time,
			    old_time = 0,
			    start_time;
    
    controller_begin_autonomous_mode();
    elapsed_time = start_time = SYSTEM_TIMER_SECONDS();
    
    /*
     *  An autonomous loop with sensor input.  Loop terminates when
     *  a button sensor on port 5 is pressed, or the 20 second time
     *  period runs out.
     */
    
    forward1(MOTOR_POWER1); 
    while ( (elapsed_time - start_time) < ROUTINE1_DURATION )
    {
	sonar_distance = sonar_read(SONAR_INTERRUPT_PORT);
	
	if ( (sonar_distance < ROUTINE1_SONAR_DISTANCE) ||
	     (io_read_digital(BUMPER_LEFT_PORT) == 0) ||
	     (io_read_digital(BUMPER_RIGHT_PORT) == 0) )
	{
	    reverse1(MOTOR_POWER1);
	    delay_msec(ROUTINE1_BACKUP_MS);
	    sonar_distance = sonar_read(SONAR_INTERRUPT_PORT);
	    spin1(MOTOR_POWER1, ROUTINE1_SPIN_MS);
	    forward1(MOTOR_POWER1);
	    sonar_distance = sonar_read(SONAR_INTERRUPT_PORT);
	}
	
	elapsed_time = SYSTEM_TIMER_MS();
	
	/* Adjust sonar direction every 40 ms */
	if ( elapsed_time % ROUTINE1_SONAR_SERVO_DELAY == 0 )
	    sonar_scan(SONAR_SERVO_PORT);
	
	/* Produce debug output once per second */
	elapsed_time /= MS_PER_SEC;
	if ( elapsed_time > old_time )
	{
	    old_time = elapsed_time;
	    /*
	    if ( rc_new_data_available() )
	    {
		DPRINTF("ET: %ld  RC: %x  A[1,2]: %x %x  "
		    "D[5,6]: %d %d  Shaft I[%d,%d]: %d %d Sonar[%d]: %d\n",
		    elapsed_time - start_time, rc_read_status(),
		    io_read_analog(1),  io_read_analog(2),
		    io_read_digital(5), io_read_digital(6),
		    LEFT_ENCODER_INTERRUPT_PORT, RIGHT_ENCODER_INTERRUPT_PORT,
		    shaft_encoder_read_std(LEFT_ENCODER_INTERRUPT_PORT),
		    shaft_encoder_read_std(RIGHT_ENCODER_INTERRUPT_PORT),
		    SONAR_INTERRUPT_PORT, sonar_distance);
	    }
	    */
	}
    }
    pwm_write(RIGHT_DRIVE_PORT, MOTOR_STOP);
    pwm_write(LEFT_DRIVE_PORT, MOTOR_STOP);
    controller_submit_data(NO_WAIT);

    controller_end_autonomous_mode();
}
Ejemplo n.º 17
0
int main(){
    std::cout << reverse(1234567893) << '\n';
    std::cout << reverse1(1234567893) << '\n';
}
Ejemplo n.º 18
0
char * strrev4(char *s)
{
	reverse1(s, 0, strlen(s)-1);
	return s;
}