A small program to solve the indention problem

View previous topic View next topic Go down

A small program to solve the indention problem

Post  viterbi on Thu Sep 08, 2011 9:47 am

I wrote a simple program which will add line number to the program and most importantly, which will replace TAB will six white underscores. This roughly solve the indention problem. See the following example for merge sort:

1____void my_msort(int* a, int begin, int end) {
2__________if (end-begin<1) return;
3__________int mid = (begin+end)/2;
4__________my_msort(a, begin, mid);
5__________my_msort(a, mid+1, end);
6__________int left = mid-begin+1;
7__________int right = end-mid;
8__________int *x = new int[left+1];
9__________int *y = new int[right+1];
10_________for (int i=0; i<left; i++) x[i] = a[begin+i];
11_________x[left] = ~(1<<31);
12_________for (int i=0; i<right; i++) y[i] = a[mid+1+i];
13_________y[right] = ~(1<<31);
14_________int l = 0;
15_________int r = 0;
16_________while (l<left || r<right) {
17_______________if (x[l] < y[r]) { a[begin+l+r] = x[l]; l++; }
18_______________else { a[begin+l+r] = y[r]; r++; }
19_________}
20___
21_________delete[] x;
22_________delete[] y;
23___}
24___

viterbi

Posts : 32
Join date : 2011-09-03

View user profile

Back to top Go down

The program I used

Post  viterbi on Thu Sep 08, 2011 9:56 am

1____#include <iostream>
2____#include <cstring>
3____
4____using namespace std;
5____
6____int main() {
7__________char s[256];
8__________char TAB = 9; // ASCII code for TAB key;
9__________int line_num = 1;
10_________char color[] = "white";
11_________while (cin.getline(s, 256)) {
12_______________cout << line_num;
13_______________cout << "[" << "color=" << color << "]";
14_______________if (line_num < 10) cout << "____";
15_______________else if (line_num < 100) cout << "___";
16_______________else cout << "__";
17_______________cout << "[" << "/color" << "]";
18_______________line_num++;
19_______________for (int i=0; i<strlen(s); i++) {
20_____________________if (s[i] == TAB) {
21___________________________cout << "[" << "color=" << color << "]";
22___________________________cout << "______";
23___________________________cout << "[" << "/color" << "]";
24_____________________}
25_____________________else cout << s[i];
26_______________}
27_______________cout << endl;
28_________}
29___}

viterbi

Posts : 32
Join date : 2011-09-03

View user profile

Back to top Go down

Re: A small program to solve the indention problem

Post  Admin on Thu Sep 08, 2011 2:03 pm

It looks much better!! I'll try to do this Very Happy

Admin
Admin

Posts : 131
Join date : 2011-08-16

View user profile http://codefornongeek.forumotion.com

Back to top Go down

Re: A small program to solve the indention problem

Post  Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum