#include <stdio.h> #define MAXSIZE 6 int data[MAXSIZE]={ 1,3,5,2,4,6 }; typedef struct LNode { int data; struct LNode *next; }* SLink; bool sort(SLink L) { if(!L||!L->next) return false; int temp; L=L->next; SLink p=L->next; while(L){ p=L->next; while(p){ if(L->data > p->data) //123 { temp=L->data; L->data=p->data; p->data=temp; } //if p=p->next; } //end while L=L->next; } // end while return true; } int main() { SLink L= new LNode; L->data=0; L->next=0; SLink head = L; for(int i=0;i<MAXSIZE;i++) { SLink p= new LNode; p->data= data[i]; p->next= 0; L->next=p; L=L->next; } L=head; bool b=sort(L); while(L->next){ printf("%d ",L->next->data); L=L->next; } return 1; } void BubbleSort_2(int a[], int size) { bool bSwaped = true; for (int i = 0; i < size -1; i++) { // 每次先重置为false bSwaped = false; for (int j = size - 1; j > i ; j--) { if (a[j-1] > a[j]) { int temp = a[j-1]; a[j-1] = a[j]; a[j] = temp; bSwaped = true; } } // 如果上一次扫描没有发生交换,则说明数组已经全部有序,退出循环 if (!bSwaped) break; } } void BubbleSort_3(int a[], int size) { int lastSwapPos = 0,lastSwapPosTemp = 0; for (int i = 0; i < size - 1; i++) { lastSwapPos = lastSwapPosTemp; for (int j = size - 1; j >lastSwapPos; j--) { if (a[j - 1] > a[j]) { int temp = a[j - 1]; a[j - 1] = a[j]; a[j] = temp; lastSwapPosTemp = j; } } if (lastSwapPos == lastSwapPosTemp) break; } } #include <stdlib.h> #include <stdio.h> void Merge(int sourceArr[],int tempArr[], int startIndex, int midIndex, int endIndex) { int i = startIndex, j=midIndex+1, k = startIndex; while(i!=midIndex+1 && j!=endIndex+1) { if(sourceArr[i] >= sourceArr[j]) tempArr[k++] = sourceArr[j++]; else tempArr[k++] = sourceArr[i++]; } while(i != midIndex+1) tempArr[k++] = sourceArr[i++]; while(j != endIndex+1) tempArr[k++] = sourceArr[j++]; for(i=startIndex; i<=endIndex; i++) sourceArr[i] = tempArr[i]; } //内部使用递归 void MergeSort(int sourceArr[], int tempArr[], int startIndex, int endIndex) { int midIndex; if(startIndex < endIndex) { midIndex = (startIndex + endIndex) / 2; MergeSort(sourceArr, tempArr, startIndex, midIndex); MergeSort(sourceArr, tempArr, midIndex+1, endIndex); Merge(sourceArr, tempArr, startIndex, midIndex, endIndex); } } int main(int argc, char * argv[]) { int a[8] = {50, 10, 20, 30, 70, 40, 80, 60}; int i, b[8]; MergeSort(a, b, 0, 7); } #include <errno.h> #include <stdio.h> typedef enum State { C_BEGIN, C_END, CPP_BEGIN, CPP_END, }State; void AnnotationConvert( const char* inputFile, const char* outputFile ) { FILE* fOut, *fIn; fIn = fopen( inputFile, "r" ); fOut = fopen( outputFile, "w" ); if ( fIn == NULL || fOut == NULL ) { if ( fIn == NULL ) printf( "打开文件%s失败, errno: %d\n", inputFile, errno ); printf( "打开文件%s失败, errno: %d\n", inputFile, errno ); return; } char first, second; State tag = C_END; do { first = fgetc( fIn ); switch ( first ) { case '/': second = fgetc( fIn ); if ( second == '*' ) { if ( tag == C_END ) { fputc( '/', fOut ); fputc( '/', fOut ); tag = C_BEGIN; }else { fputc( '/', fOut ); fputc( '*', fOut ); } }else if ( second == '/' ) { char next; fputc( '/', fOut ); fputc( '/', fOut ); do { next = fgetc( fIn ); fputc( next, fOut ); if ( next == EOF ) return; } while ( next != '\n' ); }else { fputc( first, fOut ); fputc( second, fOut ); } break; case '\n': fputc( '\n', fOut ); if ( tag == C_BEGIN ) { fputc( '/', fOut ); fputc( '/', fOut ); } break; case '*': second = fgetc( fIn ); if ( second == '/' ) { char next = fgetc( fIn ); if ( next == '/' ) { fputc( '\n', fOut ); fseek( fIn, -1, SEEK_CUR ); }else if ( next != '\n' && next != EOF ) { fputc( '\n', fOut ); fputc( next, fOut ); }else { fputc( '\n', fOut ); } tag = C_END; }else if ( second == '*' ) { fputc( first, fOut ); fseek( fIn, -1, SEEK_CUR ); }else { fputc( first, fOut ); fputc( second, fOut ); } break; default: if ( first != EOF ) fputc( first, fOut ); break; } } while ( first != EOF ); if ( tag != C_END ) return; fclose( fIn ); fclose( fOut ); } int main() { AnnotationConvert( "input.txt", "output.c" ); system( "pause" ); return(0); } # include<iostream> using namespace std; #define STACKSIZE 1024 #define UL unsigned long typedef enum { NO_COMMENT_STATE, C_COMMENT_STATE, CPP_COMMENT_STATE, STR_STATE, END_STATE }STATE_ENUM; /* 状态列表 */ typedef struct { FILE *inputfile; FILE *outputfile; STATE_ENUM ulstate; }STATE_MACHINE; /* 状态机 */ /* */ STATE_MACHINE g_state = { 0 }; /* ///////////////////////////////////////////////// */ void EventPro( char ch ); /* 事件驱动 */ void EventProAtNo( char ch ); void EventProAtC( char ch ); void EventProAtCpp( char ch ); void EventProAtStr( char ch ); /* ////////////////////////////////////////////////// */ int CommentConvert( FILE *inputfile, FILE *outputfile ) { if ( inputfile == NULL || outputfile == NULL ) { cout << "input argument Invalid!" << endl; return(-1); } g_state.inputfile = inputfile; g_state.outputfile = outputfile; g_state.ulstate = NO_COMMENT_STATE; /* 初始状态为无注释状态 */ char ch; while ( g_state.ulstate != END_STATE ) { ch = fgetc( g_state.inputfile ); /* */ EventPro( ch ); } return(0); } void EventPro( char ch ) /* 事件驱动模型 */ { switch ( g_state.ulstate ) /*不同的事件状态使用不同的状态函数 */ { case NO_COMMENT_STATE: EventProAtNo( ch ); break; case C_COMMENT_STATE: EventProAtC( ch ); break; case CPP_COMMENT_STATE: EventProAtCpp( ch ); break; case STR_STATE: EventProAtStr( ch ); break; case END_STATE: break; } } void EventProAtNo( char ch ) { char nextch; switch ( ch ) { case '/': /* // / * */ nextch = fgetc( g_state.inputfile ); if ( nextch == '/' ) /* C++ */ { fputc( '/', g_state.outputfile ); fputc( '*', g_state.outputfile ); /* 将CPP的//转化为/ * */ g_state.ulstate = CPP_COMMENT_STATE; /* 转换为CPP状态 */ }else if ( nextch == '*' ) /* C */ { fputc( ch, g_state.outputfile ); fputc( nextch, g_state.outputfile ); g_state.ulstate = C_COMMENT_STATE; /* 转换为C状态 */ }else { } break; case EOF: g_state.ulstate = END_STATE; break; case '"': g_state.ulstate = STR_STATE; default: fputc( ch, g_state.outputfile ); break; } } void EventProAtC( char ch ) { char nextch; switch ( ch ) { case '*': nextch = fgetc( g_state.inputfile ); if ( nextch == '/' ) { fputc( ch, g_state.outputfile ); fputc( nextch, g_state.outputfile ); g_state.ulstate = NO_COMMENT_STATE; } break; case '/': nextch = fgetc( g_state.inputfile ); if ( nextch == '/' ) { fputc( ' ', g_state.outputfile ); fputc( ' ', g_state.outputfile ); /* 嵌套注释用两个空格代替 */ } break; default: fputc( ch, g_state.outputfile ); break; } } void EventProAtCpp( char ch ) { /* 123 / *123 */ char nextch; switch ( ch ) { case '\n': /* 处理多行 */ fputc( '*', g_state.outputfile ); fputc( '/', g_state.outputfile ); fputc( '\n', g_state.outputfile ); g_state.ulstate = NO_COMMENT_STATE; break; case EOF: fputc( '*', g_state.outputfile ); fputc( '/', g_state.outputfile ); g_state.ulstate = END_STATE; break; case '/': nextch = fgetc( g_state.inputfile ); if ( nextch == '/' ) /* (嵌套//) */ { fputc( ' ', g_state.outputfile ); fputc( ' ', g_state.outputfile ); }else if ( nextch == '*' ) /* (嵌套 / *) */ { fputc( ' ', g_state.outputfile ); fputc( ' ', g_state.outputfile ); }else { fputc( ch, g_state.outputfile ); } break; case '*': nextch = fgetc( g_state.inputfile ); if ( nextch == '/' ) /* 嵌套// */ { fputc( ' ', g_state.outputfile ); fputc( ' ', g_state.outputfile ); }else { fputc( ch, g_state.outputfile ); } break; case '"': g_state.ulstate = STR_STATE; default: fputc( ch, g_state.outputfile ); break; } } void EventProAtStr( char ch ) { char nextch; switch ( ch ) { case '\0': nextch = fgetc( g_state.inputfile ); if ( nextch == '"' ) /* 读取到 \0 和 " 说明字符串结束 */ g_state.ulstate = NO_COMMENT_STATE; /* 状态切换 */ break; case EOF: g_state.ulstate = END_STATE; break; default: fputc( ch, g_state.outputfile ); break; } } int main() { FILE *fpIn = NULL; /* inputfile */ FILE *fpOut = NULL; /* outputfile */ errno_t err; err = fopen_s( &fpIn, "input.txt", "r" ); if ( (err = fopen_s( &fpIn, "input.txt", "r" ) ) != 0 ) { cout << "Open input file fail!" << endl; /* return -1; */ } if ( (err = fopen_s( &fpOut, "output.c", "w" ) ) != 0 ) { cout << "Open output file fail!" << endl; /* return -1; */ } CommentConvert( fpIn, fpOut ); /* */ fclose( fpIn ); fclose( fpOut ); return(0); }