今天受朋友所托用C语言写了两个面向对象的程序。 请大家多指教:
/*************************************************************** * 功能:将一个 5 * 5矩阵中的最大的元素放在中心,四个角分别放四 * 个 最小的元素(按从左到右从上到下的顺序依次从小到大存放) * 示例如下 : * * 处理前的矩阵 * 1, 2 , 3, 4, 5 * 6, 7, 8, 9, 10 * 11, 12, 13, 14,15 * 16, 17, 18, 19,20 * 21, 22, 23, 24,25 * * 处理后的矩阵 * 1, 5, 21, 13,2 * 6, 7, 8, 9, 10 * 11, 12, 25, 14,15 * 16, 17, 18, 19,20 * 3, 22, 23, 24,4 * * 作者:challengehope * 日期:2008/06/09 ***************************************************************/ #include <stdio.h>
/* fixed length 5 */ const int FIXED_LENGTH = 5;
/* power fixed length 25 */ const int POWER_FIXED_LENGTH = 5 * 5;
/* input data which need to deal wtih*/ int sourceData = { {1, 2 , 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14,15}, {16, 17, 18, 19,20}, {21, 22, 23, 24,25} } /* source list data */ int sourceListData[POWER_FIXED_LENGTH];
/* target data */ int tartgetData [][FIXED_LENGTH];
/* source list data */ int targetListData[POWER_FIXED_LENGTH];
//************************************************************ // * Method Name : fillDataToList // * Description : fill source data to list // * args : none // * return : none // *********************************************************** void convertArrayToList() { int i,j; for(i = 0; i < FIXED_LENGTH; i++) { for(j = 0; j < FIXED_LENGTH; j++) { sourceListData[i * FIXED_LENGTH + j] = sourceData[i][j]; targetListData[i * FIXED_LENGTH + j] = sourceData[i][j]; } } } //************************************************************ // * Method Name : fillDataToList // * Description : fill source data to list // * args : none // * return : none // *********************************************************** void convertListDataToArray() { int i = 0; int j = 0; while (i < FIXED_LENGTH) { tartgetData[i][0]= targetListData[i * FIXED_LENGTH]; tartgetData[i][1]= targetListData[i * FIXED_LENGTH + 1]; tartgetData[i][2]= targetListData[i * FIXED_LENGTH + 2]; tartgetData[i][3]= targetListData[i * FIXED_LENGTH + 3]; tartgetData[i][4]= targetListData[i * FIXED_LENGTH + 4]; i++; } } //************************************************************ // * Method Name : fillDataToList // * Description : fill source data to list // * args : int a, int b // * return : none // *********************************************************** void swap(int a, int b) { int temp = a; a= b; b = temp; } //************************************************************ // * Method Name : bubbleSort // * Description : sort data // * args : data // * return : none // *********************************************************** void bubbleSort(int data[]) { int i,j; for(i = 0; i < POWER_FIXED_LENGTH; i++) { for(j = 0; j < POWER_FIXED_LENGTH; j++) { if(data[j] > data[i]) { swap(data[j], data[i]); } } } } //************************************************************ // * Method Name : bubbleSort // * Description : sort data // * args : data // * return : none // *********************************************************** void analysemagiccube() { // init data convertArrayToList(); //sort data bubbleSort(sourceListData); //fill data //0,0 int index = findData(sourceListData[0]); swap(targetListData[0], targetListData[index]); //0,4 index = findData(sourceListData[1]); swap(targetListData[4], targetListData[index]); //4,0 index = findData(sourceListData[2]); swap(targetListData[4 * FIXED_LENGTH -1], targetListData[index]); //4,4 index = findData(sourceListData[3]); swap(targetListData[4 * FIXED_LENGTH + 4 -1], targetListData[index]); //2,2 index = findData(sourceListData[POWER_FIXED_LENGTH -1]); swap(targetListData[2 * FIXED_LENGTH + 2 -1], targetListData[index]); //convert List Data To Array convertListDataToArray(); } //************************************************************ // * Method Name : findData // * Description : findData // * args : data // * return : index of the data // *********************************************************** int findData(int data) { for(int i = 0; i < POWER_FIXED_LENGTH; i++) { if(data == targetListData[i]) { return i; } else { continue; } } } //*********************************** // * Method Name : main function // * Description : just for test // * args : none // ********************************** int main(){ //test analysemagiccube(); }
/*************************************************************** * 功能:输入一行文字,计算其中的大写字母小写字母空格以及其他的 * 字符各有多少 * 作者:challengehope * 日期:2008/06/09 ***************************************************************/ #include <stdio.h> /*number of blanks*/ int nBanlks = 0;
/*number of upper alpha*/ int nUpperAlpha = 0;
/*number of lower alpha*/ int nLowerAlpha = 0;
/*number of other character*/ int nOtherCharacter = 0;
/*the flag of end file*/ const EOF = -1;
//************************************************************ // * Method Name : analyString // * Description : count upper alpha , lower alpha, // * banlks and other character // * args : none // *********************************************************** void analyseString() { int c ; while(( c = getchar()) != EOF)) { if(isBanlk(c)) { nBanlks ++; } else if (isUpperAlpha(c)) { nUpperAlpha++; } else if (isLowerAlpha(c)) { nLowerAlpha++; } else { nOtherCharacter ++; } } } //************************************************************ // * Method Name : isBanlk // * Description : judge is banlk // * args : input // * return : is banlk return true ,else false // *********************************************************** bool isBanlk(int c) { return (c == ' '); }
//************************************************************ // * Method Name : isUpperAlpha // * Description : judge is upper alpha // * args : input // * return : is upper alpha return true ,else false // *********************************************************** bool isUpperAlpha(int c) { return (('A'< c) && (c < 'Z')); }
//************************************************************ // * Method Name : isLowerAlpha // * Description : judge is lower alpha // * banlks and other character // * args : input // * return : is lower alpha return true ,else false // *********************************************************** bool isLowerAlpha(int c) { return (('a'< c) && (c < 'z')); }
//************************************************************ // * Method Name : printAnalyResult // * Description : print Analy Result // * args : none // * return : none // *********************************************************** void printAnalyResult() { printf("number of blanks : %d \n", nBanlks); printf("number of upper alpha : %d \n", nUpperAlpha); printf("number of lower alpha : %d \n", nLowerAlpha); printf("number of other character : %d \n", nOtherCharacter); }
//*********************************** // * Method Name : main function // * Description : just for test // * args : none // ********************************** int main(){ //test analyseString(); }
第一次用C语言写比较正式的程序。
|
|