Chinaunix首页 | 论坛 | 博客
  • 博客访问: 59986
  • 博文数量: 15
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 180
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-04 13:29
文章分类
文章存档

2011年(1)

2008年(14)

我的朋友

分类: C/C++

2008-05-05 21:30:44

 
今天上班的时候突然想起小时候背的九九乘法表,还记得我六年级的时候缠着妈妈花了100元给我买了一个学习机,什么牌子的现在忘了,当时的说辞是为了学习电脑作准备,其实主要用途是玩游戏啊,当时有一本说明书上说可以用PASCAL语言来编程,还列举除了诸如九九乘法表之类的PASCAL代码,当时哪里懂这些啊,就依葫芦画瓢将代码敲进去了,一运行果然在DOS的环境下面打印出了九九乘法表,当时觉得还挺有成就感的呢,想想真是好笑啊
 
闲着也是闲着,于是想到用C去写一下应该是很容易的事情,真正动手的时候才发现要输出诸如"3*2="这样的常量表达式还真没有想象的那么简单,而且这些常量表达式与行、列值还不容易建立起来一个一一对应的映射关系,幸亏想到了结构体指针(array of structure),一切就变得简单啦,下面是源码:
 
/*This is a multification table for primary school students,You can only run this program on a 32 bits machine at least !*/
/*Written by Rolandee 5/5 2008 for pleasure*/
#include
#include
static struct table{
       char * word;
       }; // declare a structure type
// Here I have to storage the constant expressions by array of structure
struct table Line1[]={"1*1="},
             Line2[]={"1*2=","2*2="},
             Line3[]={"1*3=","2*3=","3*3="},
             Line4[]={"1*4=","2*4=","3*4=","4*4="},
             Line5[]={"1*5=","2*5=","3*5=","4*5=","5*5="},
             Line6[]={"1*6=","2*6=","3*6=","4*6=","5*6=","6*6="},
             Line7[]={"1*7=","2*7=","3*7=","4*7=","5*7=","6*7=","7*7="},
             Line8[]={"1*8=","2*8=","3*8=","4*8=","5*8=","6*8=","7*8=","8*8="},
         Line9[]={"1*9=","2*9=","3*9=","4*9=","5*9=","6*9=","7*9=","8*9=","9*9="};
// using a pointer array to storage the each structure array's initial ram address for later visit      
int *tabset[]={&Line1[0],&Line2[0],&Line3[0],&Line4[0],&Line5[0],&Line6[0],&Line7[0],&Line8[0],&Line9[0]};
void tabpr(int,int); // declare the table printing function
int main(int argc, char *argv[])
{
  static int line , row;
  static int faciend;
  faciend=1;
  printf("This is a multification table for primary school students:\n");
  //Here i utlize two embedded loops for the proper printing ,may be you can find easier way
  for(line=1;line<=9;line++)
  {
   for(row=1;row<=line;row++)
   {
    tabpr(line,row);
    printf("%2d ", faciend*line);
    faciend++;
    }
    printf("\n");
    faciend=1;
   }
  printf("\n");
  printf("You can only run this program on a 32 bits machine at least !\n");
  system("PAUSE"); 
  return 0;
}

// function to print the multiplication table (9*9)
void tabpr(int hor,int ver)
{
 printf("%s",*(tabset[hor-1]+ver-1));
}
 
 
下面是效果图:(个人认为这个程序只能在32位及以上的机器上跑哦,看看源码就知道原因了,还是没有发挥出
结构体指针的精髓啊,惭愧,继续学习~~~)
 
阅读(2101) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-07-16 18:06:18

I think u can trying loop