JR 精品文章 - 面向对象的c程序
AD: jr (at) javaresearch.org


首页 | 动态 | 文章 | FAQ  | 新闻 | 下载 | 代码 | 工作 | 调查 | 术语 | 站点 | 图书 | 论坛 | 帮助 | 全部  

TOP | 交流 | 软件 | 专栏 | 开源 | 译/著 | 源码 | API  | 推荐 | FTP  | 积分 | 统计 | 搜索 | Blog | 我们  
首页 » 研究文集 » 其它 搜索标题相关文章 搜索标题相关文章    评论此文章 发表评论     开始监控此文章 开始监控   加入收藏夹  加入收藏夹
面向对象的c程序
challengehope 原创   更新:2008-06-09 20:50:12  版本: 1.0   

今天受朋友所托用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语言写比较正式的程序。

版权声明   给作者写信
本篇文章对您是否有帮助?  投票:         投票结果:     7       0
作者其它文章: 作者全部文章
评论人:twangjie 发表时间: Thu Jun 12 16:56:56 CST 2008
C语言写面向对象的程序?面向过程的语言也可以面向对象?[han]看来我孤陋寡闻了,回家补课去。

这个文章共有 1 条评论
主题: 女秘书PK老板,“邮件门”传遍全国外企圈 上一篇文章
返回文章列表 返回〔其它〕
下一篇文章 主题: 背下这18条来没人敢和你忽悠CPU(转)


文字广告链接
        自主、快速定制基于JAVA的B/S业务系统          重量级企业在线自定义WEB报表平台
        Excel制表、零代码发布、打印、图表结合——快逸报表,免费、稳定、功能强大的java工具
        技术圈: 关于Java、dotNet、PHP、Ruby、奇客、Web2.0等更多资讯博客精选文章

关于 JR  |  版权声明  |  联系我们 

©2002-2006 JR 版权所有 沪ICP备05019622号