linux下面用C语言设计实现一个计算器



我们对于计算器的功能已经非常熟悉了,这个计算器包括 二进制,八进制,十进制,十六进制,四种格式。格式之间可以相互切换。


能够实现的的运算有,加减乘除  最基本的功能,还有比如  tan   sin   cos   Exp    x^y   x^3   x^2   n!   log    ln    求倒数   And   Not   Or    Mod   Int取整  等


首先定义头文件,将程序需要的全局变量定义在头文件myhead.h当中,包括两个参与运算的变量,小数点标识,运算符标识,进制标识,以及GTK+图形界面编程中常用的元件指针。



源码如下所示:myhead.h


#include 
double a,b;          /*两个参与运算的数*/
double p=0;
int hasdot;          /*是否有小点数*/
int method;          /*用于区别不同的运算*/
int principle;       /*标识不同的进制*/
char out[20]="0"; 
GtkWidget *window;   /*这部分时定义元件*/
GtkWidget *vbox;     /*垂直框*/
GtkWidget *table1;   /*表格一*/
GtkWidget *table2;   /*表格二*/
GSList *group;
GtkWidget *radio;    /*单选按钮*/
GtkWidget *entry;    /*文本框*/
GtkWidget *button1;  /*42个按钮*/
GtkWidget *button2;
GtkWidget *button3;
GtkWidget *button4;
GtkWidget *button5;
GtkWidget *button6;
GtkWidget *button7;
GtkWidget *button8;
GtkWidget *button9;
GtkWidget *button10;
GtkWidget *button11;
GtkWidget *button12;
GtkWidget *button13;
GtkWidget *button14;
GtkWidget *button15;
GtkWidget *button16;
GtkWidget *button17;
GtkWidget *button18;
GtkWidget *button19;
GtkWidget *button20;
GtkWidget *button21;
GtkWidget *button22;
GtkWidget *button23;
GtkWidget *button24;
GtkWidget *button25;
GtkWidget *button26;
GtkWidget *button27;
GtkWidget *button28;
GtkWidget *button29;
GtkWidget *button30;
GtkWidget *button31;
GtkWidget *button32;
GtkWidget *button33;
GtkWidget *button34;
GtkWidget *button35;
GtkWidget *button36;
GtkWidget *button37;
GtkWidget *button38;
GtkWidget *button39;
GtkWidget *button40;
GtkWidget *button41;
GtkWidget *button42;

下面是十六进制显示界面:   show_Hex_window.c


#include 
void show_Hex_window()
{
   gtk_button_set_label(GTK_BUTTON(button1)," ");
   gtk_widget_show(button1);
   gtk_button_set_label(GTK_BUTTON(button2)," ");
   gtk_widget_show(button2);
   gtk_button_set_label(GTK_BUTTON(button3)," ");
   gtk_widget_show(button3);
   gtk_button_set_label(GTK_BUTTON(button4)," ");
   gtk_widget_show(button4);
   gtk_button_set_label(GTK_BUTTON(button6)," ");
   gtk_widget_show(button6);
   gtk_button_set_label(GTK_BUTTON(button14),"7");
   gtk_widget_show(button14);
   gtk_button_set_label(GTK_BUTTON(button15),"4");
   gtk_widget_show(button15);
   gtk_button_set_label(GTK_BUTTON(button18),"A");
   gtk_widget_show(button18);
   gtk_button_set_label(GTK_BUTTON(button19),"8");
   gtk_widget_show(button19);
   gtk_button_set_label(GTK_BUTTON(button20),"5");
   gtk_widget_show(button20);
   gtk_button_set_label(GTK_BUTTON(button21),"2");
   gtk_widget_show(button21);
   gtk_button_set_label(GTK_BUTTON(button23),"B");
   gtk_widget_show(button23);
   gtk_button_set_label(GTK_BUTTON(button24),"9");
   gtk_widget_show(button24);
   gtk_button_set_label(GTK_BUTTON(button25),"6");
   gtk_widget_show(button25);
   gtk_button_set_label(GTK_BUTTON(button26),"3");
   gtk_widget_show(button26);
   gtk_button_set_label(GTK_BUTTON(button28),"C");
   gtk_widget_show(button28);
   gtk_button_set_label(GTK_BUTTON(button33),"D");
   gtk_widget_show(button33);
   gtk_button_set_label(GTK_BUTTON(button38),"E");
   gtk_widget_show(button38);
   gtk_button_set_label(GTK_BUTTON(button42),"F");
   gtk_widget_show(button42);
}


十进制显示界面  show_Dec_window.c


#include 
void show_Dec_window()
{
gtk_button_set_label(GTK_BUTTON(button1),"pi");
gtk_widget_show(button1);
gtk_button_set_label(GTK_BUTTON(button2),"sin");
gtk_widget_show(button2);
gtk_button_set_label(GTK_BUTTON(button3),"cos");
gtk_widget_show(button3);
gtk_button_set_label(GTK_BUTTON(button4),"tan");
gtk_widget_show(button4);
gtk_button_set_label(GTK_BUTTON(button6),"Exp");
gtk_widget_show(button6);
gtk_button_set_label(GTK_BUTTON(button14),"7");
gtk_widget_show(button14);
gtk_button_set_label(GTK_BUTTON(button15),"4");
gtk_widget_show(button15);
gtk_button_set_label(GTK_BUTTON(button18)," ");
gtk_widget_show(button18);
gtk_button_set_label(GTK_BUTTON(button19),"8");
gtk_widget_show(button19);
gtk_button_set_label(GTK_BUTTON(button20),"5");
gtk_widget_show(button20);
gtk_button_set_label(GTK_BUTTON(button21),"2");
gtk_widget_show(button21);
gtk_button_set_label(GTK_BUTTON(button23)," ");
gtk_widget_show(button23);
gtk_button_set_label(GTK_BUTTON(button24),"9");
gtk_widget_show(button24);
gtk_button_set_label(GTK_BUTTON(button25),"6");
gtk_widget_show(button25);
gtk_button_set_label(GTK_BUTTON(button26),"3");
gtk_widget_show(button26);
gtk_button_set_label(GTK_BUTTON(button28)," ");
gtk_widget_show(button28);
gtk_button_set_label(GTK_BUTTON(button33)," ");
gtk_widget_show(button33);
gtk_button_set_label(GTK_BUTTON(button38)," ");
gtk_widget_show(button38);
gtk_button_set_label(GTK_BUTTON(button42)," ");
gtk_widget_show(button42);
}

八进制显示界面  show_Oct_window.c


#include 
void show_Oct_window()
{
gtk_button_set_label(GTK_BUTTON(button1)," ");
gtk_widget_show(button1);
gtk_button_set_label(GTK_BUTTON(button2)," ");
gtk_widget_show(button2);
gtk_button_set_label(GTK_BUTTON(button3)," ");
gtk_widget_show(button3);
gtk_button_set_label(GTK_BUTTON(button4)," ");
gtk_widget_show(button4);
gtk_button_set_label(GTK_BUTTON(button6)," ");
gtk_widget_show(button6);
gtk_button_set_label(GTK_BUTTON(button14),"7");
gtk_widget_show(button14);
gtk_button_set_label(GTK_BUTTON(button15),"4");
gtk_widget_show(button15);
gtk_button_set_label(GTK_BUTTON(button18)," ");
gtk_widget_show(button18);
gtk_button_set_label(GTK_BUTTON(button19)," ");
gtk_widget_show(button19);
gtk_button_set_label(GTK_BUTTON(button20),"5");
gtk_widget_show(button20);
gtk_button_set_label(GTK_BUTTON(button21),"2");
gtk_widget_show(button21);
gtk_button_set_label(GTK_BUTTON(button23)," ");
gtk_widget_show(button23);
gtk_button_set_label(GTK_BUTTON(button24)," ");
gtk_widget_show(button24);
gtk_button_set_label(GTK_BUTTON(button25),"6");
gtk_widget_show(button25);
gtk_button_set_label(GTK_BUTTON(button26),"3");
gtk_widget_show(button26);
gtk_button_set_label(GTK_BUTTON(button28)," ");
gtk_widget_show(button28);
gtk_button_set_label(GTK_BUTTON(button33)," ");
gtk_widget_show(button33);
gtk_button_set_label(GTK_BUTTON(button38)," ");
gtk_widget_show(button38);
gtk_button_set_label(GTK_BUTTON(button42)," ");
gtk_widget_show(button42);	
}


二进制显示界面  show_Bin_window.


#include 
void show_Bin_window()
{
gtk_button_set_label(GTK_BUTTON(button1)," ");
gtk_widget_show(button1);
gtk_button_set_label(GTK_BUTTON(button2)," ");
gtk_widget_show(button2);
gtk_button_set_label(GTK_BUTTON(button3)," ");
gtk_widget_show(button3);
gtk_button_set_label(GTK_BUTTON(button4)," ");
gtk_widget_show(button4);
gtk_button_set_label(GTK_BUTTON(button6)," ");
gtk_widget_show(button6);
gtk_button_set_label(GTK_BUTTON(button14)," ");
gtk_widget_show(button14);
gtk_button_set_label(GTK_BUTTON(button15)," ");
gtk_widget_show(button15);
gtk_button_set_label(GTK_BUTTON(button18)," ");
gtk_widget_show(button18);
gtk_button_set_label(GTK_BUTTON(button19)," ");
gtk_widget_show(button19);
gtk_button_set_label(GTK_BUTTON(button20)," ");
gtk_widget_show(button20);
gtk_button_set_label(GTK_BUTTON(button21)," ");
gtk_widget_show(button21);
gtk_button_set_label(GTK_BUTTON(button23)," ");
gtk_widget_show(button23);
gtk_button_set_label(GTK_BUTTON(button24)," ");
gtk_widget_show(button24);
gtk_button_set_label(GTK_BUTTON(button25)," ");
gtk_widget_show(button25);
gtk_button_set_label(GTK_BUTTON(button26)," ");
gtk_widget_show(button26);
gtk_button_set_label(GTK_BUTTON(button28)," ");
gtk_widget_show(button28);
gtk_button_set_label(GTK_BUTTON(button33)," ");
gtk_widget_show(button33);
gtk_button_set_label(GTK_BUTTON(button38)," ");
gtk_widget_show(button38);
gtk_button_set_label(GTK_BUTTON(button42)," ");
gtk_widget_show(button42);	
}


各种进制之间需要一个转换函数:


Conversion.c


#include  
#include  
#include  
#include 

int Conversion(char num[20], int t, int n)
{
int i,ii,j,k,m,x,b[30],h[30],c;
double d,r;
ii=0; 
p=0;
memset(out,0,20);
m=strlen(num);//求字符串的长度
x=m; 
for(k=0,j=0;k=0;j--) 
{ 
   if(num[j]=='-') break; //符号的处理 
   if(num[j]=='A') r=10; 
   else if(num[j]=='B') 
      r=11; 
   else if(num[j]=='C') 
      r=12; 
   else if(num[j]=='D') 
      r=13; 
   else if(num[j]=='E') 
      r=14; 
   else if(num[j]=='F')
      r=15; 
   else 
      r=num[j]-'0';//将字符转换成数字 
   p=p+r*(pow ((double)t,(double)(x-j-1)));//计算结果 
}

if(num[x]=='.') 
{ 
   for(j=x+1;j=10) 
            break;//主要是控制小数后面万一出现无限小数的时候好跳出循环以免出现死循环 
      } 
   }
   if(num[0]=='-')
   {   
       out[0]='-';
       ii++;
   }
   for(j=i-1;j>=0;j--,ii++)//反序输出,大于10的数字进行相应的变化 
   { 
      if(h[j]==10) out[ii]='A';
      else if(h[j]==11) out[ii]='B';
      else if(h[j]==12) out[ii]='C';
      else if(h[j]==13) out[ii]='D';
      else if(h[j]==14) out[ii]='E';
      else if(h[j]==15) out[ii]='F';
      else if(h[j]==9)  out[ii]='9';
      else if(h[j]==8)  out[ii]='8';
      else if(h[j]==7)  out[ii]='7';
      else if(h[j]==6)  out[ii]='6';
      else if(h[j]==5)  out[ii]='5';
      else if(h[j]==4)  out[ii]='4';
      else if(h[j]==3)  out[ii]='3';
      else if(h[j]==2)  out[ii]='2';
      else if(h[j]==1)  out[ii]='1';
      else out[ii]='0';
   } 
   if(p!=(int)p)//选择性输出,这样可以节约输出时间和程序的运行时间 
   {
      out[ii++]='.';
      for(j=0;j

信号模块处理  

#include 
#include 
#include 
#include  
#include  

void Binary_Operator()          /*双目运算*/
{
   char num[20];
   strcpy(num, gtk_entry_get_text(GTK_ENTRY(entry)));/*取得文本框的内容*/
   if(a==0)                      /*如果没有第一个数,则存储为第一个数*/
   {
	   if(principle==16)
	   {
	      Conversion (num,16,10); /*将输入的十六进制数转换为十进制*/
		  a=p;
	   }
       if(principle==10)
       {
	       a=atof(num);           /*直接转换成浮点型*/
	   }
	   if(principle==8)
	   {
	      Conversion (num,8,10);  /*将输入的八进制数转换为十进制*/
		  a=p;
	   }
	   if(principle==2)
	   {
	      Conversion (num,2,10);  /*将输入的二进制数转换为十进制*/
		  a=p;
	   }
      gtk_entry_set_text(GTK_ENTRY(entry),"");  /*清空文本框*/
   }
   else        /*如果已有第一个数,则应存储为第二个数*/
   {
       if(principle==16)
	   {
	       Conversion(num,16,10);  /*将输入的十六进制数转换为十进制*/
		   b=p;
	   }
       if(principle==10)
       {
	       b=atof(num);            /*直接转换成浮点型*/
	   }
	   if(principle==8)
	   {
	       Conversion(num,8,10);   /*将输入的八进制数转换为十进制*/
		   b=p;
	   }
	   if(principle==2)
	   {
	       Conversion(num,2,10);   /*将输入的二进制数转换为十进制*/
		   b=p;
	   }
   }
   hasdot=0;   /*表示已经没有小数点*/
}

void Right_output()
{
   char num[20];
   gcvt(a,32,num);             /*运算结果转换成字符串*/
   if(principle==16)
   {
	  Conversion(num,10,16);   /*将运算结果(十进制字符串)转换成十六进制数*/
	  gtk_entry_set_text(GTK_ENTRY(entry),out);     /*显示结果*/
   }
   if(principle==10)
   {  
	  //gcvt(a,32,num);             /*运算结果转换成字符串*/
	  gtk_entry_set_text(GTK_ENTRY(entry),num);     /*直接显示结果*/
   }
   if(principle==8)
   {
	  Conversion(num,10,8);    /*将运算结果(十进制字符串)转换成八进制数*/
	  gtk_entry_set_text(GTK_ENTRY(entry),out);     /*显示结果*/
   }
   if(principle==2)
   {
	  Conversion(num,10,2);    /*将运算结果(十进制字符串)转换成二进制数*/
	  gtk_entry_set_text(GTK_ENTRY(entry),out);     /*显示结果*/
   }
   a=0;
   b=0;
   method=0;
}

float fun(int c)   /*递归函数求阶乘*/
{
	float d;
	if(c==0 || c==1) d=1;
	else d=fun(c-1)*c;
	return d;
} 

void output()
{
  char num[20]="0";
  strcpy(num, gtk_entry_get_text(GTK_ENTRY(entry)));   /* 取得文本框输入的内容*/
  if(principle==16)
  {
      Conversion(num,16,10);  /*将输入的十六进制数转换为十进制*/
      b=p;
  }
  if(principle==10)
  {
      b=atof(num);            /*直接转换成浮点型*/
  }
  if(principle==8)
  {
      Conversion(num,8,10);   /*将输入的八进制数转换为十进制*/
      b=p;
  }
  if(principle==2)
  {
      Conversion(num,2,10);   /*将输入的二进制数转换为十进制*/
      b=p;
  }
  switch(method)
  {
     case 0:
       a=a+b; Right_output(); break;
     case 1:
       a=a-b; Right_output(); break;
     case 2:
	   a=a*b; Right_output(); break;
     case 3:
	   if(b==0)
	   {
           a=0; b=0; method=0;
		   gtk_entry_set_text (GTK_ENTRY(entry),
			       g_locale_to_utf8("除数不能为零",-1,NULL,NULL,NULL)); /*显示出错信息*/
	   }
       else
	   {
          a=a/b;
		  Right_output();
	   }break;
	 case 4:
       a=pow(a,b); Right_output(); break;
	 case 5:
       a=((int)a) & ((int)b); Right_output(); break;
     case 6:
       a=((int)a) | ((int)b); Right_output(); break;
     case 7:
       a=((int)a) ^ ((int)b); Right_output(); break;
     case 8:
       if(b==0)
	   {
           a=0; b=0; method=0;
		   gtk_entry_set_text (GTK_ENTRY(entry),
			       g_locale_to_utf8("除数不能为零",-1,NULL,NULL,NULL)); /*显示出错信息*/
	   }
       else
	   {
          a=((int)a) % ((int)b);
		  Right_output();
	   }break;
	 case 9:
       a=sin(b); Right_output(); break;
     case 10:
       a=cos(b); Right_output(); break;
     case 11:
	   a=tan(b); Right_output(); break;
     case 12:
       a=exp(b); Right_output(); break;
	 case 13:
       a=b*b*b; Right_output(); break;
	 case 14:
       a=b*b; Right_output(); break;
     case 15:
	   if(b<=0)
	   {
           a=0; b=0; method=0;
		   gtk_entry_set_text (GTK_ENTRY(entry),
			       g_locale_to_utf8("对数必须为正数",-1,NULL,NULL,NULL)); /*显示出错信息*/
	   }
	   else
	   {
          a=log(b);
		  Right_output(); 
	   }break;
     case 16:
	   if(b<=0)
	   {
          a=0; b=0; method=0;
		  gtk_entry_set_text (GTK_ENTRY(entry),
			       g_locale_to_utf8("对数必须为正数",-1,NULL,NULL,NULL)); /*显示出错信息*/
	   }
	   else
	   {
          a=log10(b);
		  Right_output(); 
	   }break;
     case 17:
	   if(b<0)
	   {
          a=0; b=0; method=0;
		  gtk_entry_set_text (GTK_ENTRY(entry),
			       g_locale_to_utf8("函数输入无效",-1,NULL,NULL,NULL)); /*显示出错信息*/

	   }
       else
	   { 
         a=fun((int)(b));
	     Right_output();
	   }break;
	 case 18:
	   if(b==0)
	   {
           a=0; b=0; method=0;
		   gtk_entry_set_text (GTK_ENTRY(entry),
			       g_locale_to_utf8("除数不能为零",-1,NULL,NULL,NULL)); /*显示出错信息*/
	   }
	   else
	   {
          a=1/b;
		  Right_output();
	   }break;
	 case 19:
       a=~((int)b); Right_output(); break;
	 case 20:
       a=floor(b); Right_output(); break;
     default: break;
  }
}

void Add(GtkWidget *widget, gpointer data)    /*加法运算*/
{
   method=0;
   Binary_Operator();
}

void Sub(GtkWidget *widget,gpointer data)     /*减法运算*/
{
   method=1;
   Binary_Operator();
}

void Mul(GtkWidget *widget,gpointer data)     /*乘法运算*/
{
   method=2;
   Binary_Operator();
}

void Division(GtkWidget *widget,gpointer data)   /*除法运算*/
{
   method=3;
   Binary_Operator();
}

void Mathpowxy(GtkWidget *widget,gpointer data)  /*幂运算*/
{
   method=4;
   Binary_Operator();
}

void And(GtkWidget *widget,gpointer data)   /*逻辑与*/
{
   method=5;
   Binary_Operator();
}

void Or(GtkWidget *widget,gpointer data)    /*逻辑或*/
{
   method=6;
   Binary_Operator();
}

void Xor(GtkWidget *widget,gpointer data)    /*逻辑异或*/
{
   method=7;
   Binary_Operator();
}

void Mod(GtkWidget *widget,gpointer data)    /*模运算(取余)*/
{
   method=8;
   Binary_Operator();
}

void Sin(GtkWidget *widget,gpointer data)    /*求正弦(按弧度值)*/
{
   method=9;
   output();
}

void Cos(GtkWidget *widget,gpointer data)    /*求余弦(按弧度值)*/
{
   method=10;
   output();
}

void Tan(GtkWidget *widget,gpointer data)    /*求正切(按弧度值)*/
{
   method=11;
   output();
}

void Exp(GtkWidget *widget,gpointer data)    /*指数运算*/
{
   method=12;
   output();
}

void Cube(GtkWidget *widget,gpointer data)    /*立方*/
{
   method=13;
   output();
}

void Square(GtkWidget *widget,gpointer data)    /*平方*/
{
   method=14;
   output();
}

void Log_e(GtkWidget *widget,gpointer data)    /*底数为e求对数*/
{
   method=15;
   output();
}

void Log_10(GtkWidget *widget,gpointer data)    /*底数为10求对数*/
{
   method=16;
   output();
}

void Factorial(GtkWidget *widget,gpointer data)    /*阶乘*/
{
   method=17;
   output();
}

void Inverse(GtkWidget *widget,gpointer data)    /*求倒数*/
{
   method=18;
   output();
}

void Not(GtkWidget *widget,gpointer data)    /*逻辑非*/
{
   method=19;
   output();
}

void Floor(GtkWidget *widget,gpointer data)    /*取整*/
{
   method=20;
   output();
}

void dot(GtkWidget *widget,gpointer data)
{                     
   if(hasdot==0) /* 没有小数点则添加一个小数点。*/
     {
        gtk_entry_append_text (GTK_ENTRY(entry), gtk_button_get_label(widget));
        hasdot=1;/*  表示有一个小数点。*/
     }
}

void Sign()
{
   char num[20];
   float c;
   strcpy(num, gtk_entry_get_text(GTK_ENTRY(entry)));/*取得文本框的内容。*/
   c=atof(num);             /*转换成浮点型*/
   c=-c;
   gcvt(c,32,num);          /*结果转换成字符串*/
   gtk_entry_set_text(GTK_ENTRY(entry),num);    /*显示结果*/
}

void clear(GtkWidget *widget,gpointer data)
{
   gtk_entry_set_text(GTK_ENTRY(entry),"");
   hasdot=0;
   a=0;
   b=0;
   method=0;
}

void input (GtkWidget *widget, gpointer data)
{
   gtk_entry_append_text (GTK_ENTRY (entry), gtk_button_get_label(widget));
}

void input_pi (GtkWidget *widget, gpointer data)
{
   gtk_entry_set_text (GTK_ENTRY (entry), "3.1415926535897932384626433832795");
}

void addsignal()
{
/* 下面的17个按钮实现数字的输入*/
g_signal_connect (G_OBJECT(button1), "clicked", G_CALLBACK(input_pi), NULL);
g_signal_connect (G_OBJECT(button14), "clicked", G_CALLBACK(input), NULL);
g_signal_connect (G_OBJECT(button15), "clicked", G_CALLBACK(input), NULL);
g_signal_connect (G_OBJECT(button16), "clicked", G_CALLBACK(input), NULL);
g_signal_connect (G_OBJECT(button17), "clicked", G_CALLBACK(input), NULL);
g_signal_connect (G_OBJECT(button18), "clicked", G_CALLBACK(input), NULL); /*A*/
g_signal_connect (G_OBJECT(button19), "clicked", G_CALLBACK(input), NULL);
g_signal_connect (G_OBJECT(button20), "clicked", G_CALLBACK(input), NULL);
g_signal_connect (G_OBJECT(button21), "clicked", G_CALLBACK(input), NULL);
g_signal_connect (G_OBJECT(button23), "clicked", G_CALLBACK(input), NULL); /*B*/
g_signal_connect (G_OBJECT(button24), "clicked", G_CALLBACK(input), NULL);
g_signal_connect (G_OBJECT(button25), "clicked", G_CALLBACK(input), NULL);
g_signal_connect (G_OBJECT(button26), "clicked", G_CALLBACK(input), NULL);
g_signal_connect (G_OBJECT(button28), "clicked", G_CALLBACK(input), NULL); /*C*/
g_signal_connect (G_OBJECT(button33), "clicked", G_CALLBACK(input), NULL); /*D*/
g_signal_connect (G_OBJECT(button38), "clicked", G_CALLBACK(input), NULL); /*E*/
g_signal_connect (G_OBJECT(button42), "clicked", G_CALLBACK(input), NULL); /*F*/
/*下面的按钮实现小数点的输入*/
g_signal_connect (G_OBJECT(button27), "clicked", G_CALLBACK(dot), NULL);
/*下面的按钮实现正负号的输入*/
g_signal_connect (G_OBJECT(button22), "clicked", G_CALLBACK(Sign), NULL);
/*下面的按钮实现各种运算的输入*/
g_signal_connect (G_OBJECT(button2), "clicked", G_CALLBACK(Sin), NULL);
g_signal_connect (G_OBJECT(button3), "clicked", G_CALLBACK(Cos), NULL);
g_signal_connect (G_OBJECT(button4), "clicked", G_CALLBACK(Tan), NULL);
g_signal_connect (G_OBJECT(button6), "clicked", G_CALLBACK(Exp), NULL);
g_signal_connect (G_OBJECT(button7), "clicked", G_CALLBACK(Mathpowxy), NULL);
g_signal_connect (G_OBJECT(button8), "clicked", G_CALLBACK(Cube), NULL);
g_signal_connect (G_OBJECT(button9), "clicked", G_CALLBACK(Square), NULL);
g_signal_connect (G_OBJECT(button10), "clicked", G_CALLBACK(Log_e), NULL);
g_signal_connect (G_OBJECT(button11), "clicked", G_CALLBACK(Log_10), NULL);
g_signal_connect (G_OBJECT(button12), "clicked", G_CALLBACK(Factorial), NULL);
g_signal_connect (G_OBJECT(button13), "clicked", G_CALLBACK(Inverse), NULL);
g_signal_connect (G_OBJECT(button32), "clicked", G_CALLBACK(Add), NULL);
g_signal_connect (G_OBJECT(button31), "clicked", G_CALLBACK(Sub), NULL);
g_signal_connect (G_OBJECT(button30), "clicked", G_CALLBACK(Mul), NULL);
g_signal_connect (G_OBJECT(button29), "clicked", G_CALLBACK(Division), NULL);
g_signal_connect (G_OBJECT(button35), "clicked", G_CALLBACK(And), NULL);
g_signal_connect (G_OBJECT(button36), "clicked", G_CALLBACK(Or), NULL);
g_signal_connect (G_OBJECT(button37), "clicked", G_CALLBACK(Mod), NULL);
g_signal_connect (G_OBJECT(button39), "clicked", G_CALLBACK(Not), NULL);
g_signal_connect (G_OBJECT(button40), "clicked", G_CALLBACK(Xor), NULL);
g_signal_connect (G_OBJECT(button41), "clicked", G_CALLBACK(Floor), NULL);
/* 下面的按钮实现复位功能*/
g_signal_connect (G_OBJECT(button34), "clicked", G_CALLBACK(clear),NULL);
/* 下面的按钮实现结果输出*/
g_signal_connect (G_OBJECT(button5), "clicked", G_CALLBACK(output),NULL);
g_signal_connect (G_OBJECT(window),"delete_event" ,gtk_main_quit, NULL);
}



主函数  main.c

#include 
#include 
#include 
#include "myhead.h"
#include "show_Hex_window.c"
#include "show_Dec_window.c"
#include "show_Oct_window.c"
#include "show_Bin_window.c"
#include "Signal_Process.c"
#include "Conversion.c"

void on_clicked(GtkWidget *widget, gpointer data)
{

      if(GTK_TOGGLE_BUTTON(widget)->active)
	  {
		  if((char *)data=="Hex")
		  {
             show_Hex_window();
             principle=16;
		  }	 
		  if((char *)data=="Dec")
		  {
             show_Dec_window();
			 principle=10;
		  }
		  if((char *)data=="Oct")
		  {
             show_Oct_window();
			 principle=8;
		  }
		  if((char *)data=="Bin")
		  {
             show_Bin_window();
			 principle=2;
		  }
	  }   
}

int main (int argc, char *argv[])
{
a=0;
b=0;
hasdot=0;
gtk_set_locale(); 
gtk_rc_add_default_file("./gtkrc.zh_CN");   
gtk_init (&argc,&argv);      /* 建立窗口。*/
method=0;                    /*运算方法。*/
window=gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), 
		g_locale_to_utf8("我的计算器",-1,NULL,NULL,NULL));

vbox = gtk_vbox_new (FALSE, 0); /*创建一个垂直框*/
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_widget_show (vbox);
table1 = gtk_table_new (2,4,FALSE);       /*建立一个2行4列的表格1*/
gtk_box_pack_start (GTK_BOX (vbox), table1, TRUE, FALSE, 0);
gtk_widget_show (table1);
table2= gtk_table_new (5,9,FALSE);       /*建立一个5行9列的表格*/
gtk_box_pack_start (GTK_BOX (vbox), table2, TRUE, FALSE, 0);
gtk_widget_show (table2);
entry = gtk_entry_new ( );                 /*用于输入和输出的文本框*/
gtk_table_attach (GTK_TABLE (table1), entry, 0, 4, 0, 1,
            (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
            (GtkAttachOptions) (0),0,0);
gtk_widget_show (entry);

button1 = gtk_button_new_with_mnemonic("pi");       /*pi*/
gtk_table_attach (GTK_TABLE (table2), button1, 0, 1, 0, 1, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);
gtk_widget_set_size_request (button1,40,30);
 
button2 = gtk_button_new_with_mnemonic("sin");        /*sin*/
gtk_table_attach (GTK_TABLE (table2), button2, 0, 1, 1, 2, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button3 = gtk_button_new_with_mnemonic("cos");        /*cos*/
gtk_table_attach (GTK_TABLE (table2), button3, 0, 1, 2, 3, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button4 = gtk_button_new_with_mnemonic("tan");        /*tan*/
gtk_table_attach (GTK_TABLE (table2), button4, 0, 1, 3, 4,
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);
 
button5 = gtk_button_new_with_mnemonic("=");        /*===*/
gtk_widget_show (button5);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button5, 0, 3, 4, 5, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);
 
button6 = gtk_button_new_with_mnemonic("Exp");      /*Exp*/
gtk_table_attach (GTK_TABLE (table2), button6, 1, 2, 0, 1, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);
gtk_widget_set_size_request (button6,40,30);

button7 = gtk_button_new_with_mnemonic("x^y");        /*x^y*/
gtk_widget_show (button7);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button7, 1, 2, 1, 2, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);
  
button8 = gtk_button_new_with_mnemonic("x^3");        /*x^3*/
gtk_widget_show (button8);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button8, 1, 2, 2, 3, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button9 = gtk_button_new_with_mnemonic("x^2");      /*x^2*/
gtk_widget_show (button9);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button9, 1, 2, 3, 4, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);
  
button10 = gtk_button_new_with_mnemonic("ln");       /*ln*/
gtk_widget_show (button10);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button10, 2, 3, 0, 1, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);
gtk_widget_set_size_request (button10,40,30);
    
button11 = gtk_button_new_with_mnemonic("log");       /*log*/
gtk_widget_show (button11);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button11, 2, 3, 1, 2, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);
 
button12 = gtk_button_new_with_mnemonic("n!");        /*n!*/
gtk_widget_show (button12);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button12, 2, 3, 2, 3, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button13 = gtk_button_new_with_mnemonic("1/x ");        /*1/x*/
gtk_widget_show (button13);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button13, 2, 3, 3, 4, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button14 = gtk_button_new_with_label("7");        /*数字按钮7*/
gtk_table_attach (GTK_TABLE (table2), button14, 3, 4, 0, 1, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);
gtk_widget_set_size_request (button14,40,30);

button15 = gtk_button_new_with_mnemonic("4");        /*数字按钮4*/
gtk_table_attach (GTK_TABLE (table2), button15, 3, 4, 1, 2, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);
 
button16 = gtk_button_new_with_mnemonic("1");        /*数字按钮1*/
gtk_widget_show (button16);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button16, 3, 4, 2, 3, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button17 = gtk_button_new_with_mnemonic("0");        /*数字按钮0*/
gtk_widget_show (button17);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button17, 3, 4, 3, 4, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);
  
button18 = gtk_button_new_with_mnemonic("A");        /*数字按钮A*/
gtk_table_attach (GTK_TABLE (table2), button18, 3, 4, 4, 5, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button19 = gtk_button_new_with_mnemonic("8");      /*数字按钮8*/
gtk_table_attach (GTK_TABLE (table2), button19, 4, 5, 0, 1, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);
gtk_widget_set_size_request (button19,40,30);

button20 = gtk_button_new_with_mnemonic("5");       /*数字按钮5*/
gtk_table_attach (GTK_TABLE (table2), button20, 4, 5, 1, 2, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button21 = gtk_button_new_with_mnemonic("2");       /*数字按钮2*/
gtk_table_attach (GTK_TABLE (table2), button21, 4, 5, 2, 3, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button22 = gtk_button_new_with_mnemonic("+/-");     /*+/-*/
gtk_widget_show (button22);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button22, 4, 5, 3, 4, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button23 = gtk_button_new_with_mnemonic("B");       /*数字按钮B*/
gtk_table_attach (GTK_TABLE (table2), button23, 4, 5, 4, 5, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button24 = gtk_button_new_with_mnemonic("9");       /*数字按钮9*/
gtk_table_attach (GTK_TABLE (table2), button24, 5, 6, 0, 1, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);
gtk_widget_set_size_request (button24,40,30);

button25 = gtk_button_new_with_mnemonic("6");       /*数字按钮6*/
gtk_table_attach (GTK_TABLE (table2), button25, 5, 6, 1, 2, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button26 = gtk_button_new_with_mnemonic("3");       /*数字按钮3*/
gtk_table_attach (GTK_TABLE (table2), button26, 5, 6, 2, 3, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button27 = gtk_button_new_with_mnemonic(".");     /*小数点*/
gtk_widget_show (button27);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button27, 5, 6, 3, 4, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button28 = gtk_button_new_with_mnemonic("C");       /*数字按钮C*/
gtk_table_attach(GTK_TABLE (table2), button28, 5, 6, 4, 5, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button29 = gtk_button_new_with_mnemonic("/");       /*除法*/
gtk_widget_show (button29);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button29, 6, 7, 0, 1, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);
gtk_widget_set_size_request (button29,40,30);

button30 = gtk_button_new_with_mnemonic("*");      /*乘法*/
gtk_widget_show (button30);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button30, 6, 7, 1, 2, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button31 = gtk_button_new_with_mnemonic("-");      /*减法*/
gtk_widget_show (button31);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button31, 6, 7, 2, 3, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button32 = gtk_button_new_with_mnemonic("+");      /*加法*/
gtk_widget_show (button32);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button32, 6, 7, 3, 4, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button33 = gtk_button_new_with_mnemonic("D");   /*D*/
gtk_table_attach (GTK_TABLE(table2), button33, 6, 7, 4, 5, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button34 = gtk_button_new_with_mnemonic("CR");   /*CR*/
gtk_widget_show (button34);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button34, 7, 9, 0, 1, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);
gtk_widget_set_size_request (button34,80,30);

button35 = gtk_button_new_with_mnemonic("And");   /*And*/
gtk_widget_show (button35);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button35, 7, 8, 1, 2, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button36 = gtk_button_new_with_mnemonic("Or");   /*Or*/
gtk_widget_show (button36);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button36, 7, 8, 2, 3, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button37 = gtk_button_new_with_mnemonic("Mod");   /*Mod*/
gtk_widget_show (button37);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button37, 7, 8, 3, 4, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button38 = gtk_button_new_with_mnemonic("E");   /*E*/
gtk_table_attach (GTK_TABLE(table2), button38, 7, 8, 4, 5, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button39 = gtk_button_new_with_mnemonic("Not");    /*Not*/
gtk_widget_show (button39);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button39, 8, 9, 1, 2, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button40 = gtk_button_new_with_mnemonic("Xor");   /*Xor*/
gtk_widget_show (button40);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button40, 8, 9, 2, 3, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button41 = gtk_button_new_with_mnemonic("Int");   /*Int*/
gtk_widget_show (button41);/*常显示,不变*/
gtk_table_attach (GTK_TABLE (table2), button41, 8, 9, 3, 4, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

button42 = gtk_button_new_with_mnemonic("F");   /*F*/
gtk_table_attach (GTK_TABLE(table2), button42, 8, 9, 4, 5, 
            (GtkAttachOptions) (GTK_FILL),
            (GtkAttachOptions) (0), 0, 0);

/*下面是创建四个单选按钮,并将"十进制"按钮设置为默认选中*/
radio = gtk_radio_button_new_with_label (NULL,
      g_locale_to_utf8("十六进制",-1,NULL,NULL,NULL));
g_signal_connect(GTK_OBJECT(radio),"clicked",
                G_CALLBACK(on_clicked),"Hex");
gtk_table_attach (GTK_TABLE (table1), radio, 0, 1, 1, 2,
            (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
            (GtkAttachOptions) (0),0,0);
gtk_widget_show (radio);
group = gtk_radio_button_group (GTK_RADIO_BUTTON(radio));
radio = gtk_radio_button_new_with_label (group,
      g_locale_to_utf8("十进制",-1,NULL,NULL,NULL));
g_signal_connect(GTK_OBJECT(radio),"clicked",
                G_CALLBACK(on_clicked),"Dec");
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio),TRUE);/*十进制radio设置为默认选中状态*/
gtk_table_attach (GTK_TABLE (table1), radio, 1, 2, 1, 2,
            (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
            (GtkAttachOptions) (0),0,0);
gtk_widget_show (radio);
group = gtk_radio_button_group (GTK_RADIO_BUTTON(radio));
radio = gtk_radio_button_new_with_label (group,
       g_locale_to_utf8("八进制",-1,NULL,NULL,NULL));
g_signal_connect(GTK_OBJECT(radio),"clicked",
                G_CALLBACK(on_clicked),"Oct");
gtk_table_attach (GTK_TABLE (table1), radio, 2, 3, 1, 2,
            (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
            (GtkAttachOptions) (0),0,0);
gtk_widget_show (radio);
group = gtk_radio_button_group (GTK_RADIO_BUTTON(radio));
radio = gtk_radio_button_new_with_label (group,
      g_locale_to_utf8("二进制",-1,NULL,NULL,NULL));
g_signal_connect(GTK_OBJECT(radio),"clicked",
                G_CALLBACK(on_clicked),"Bin");
gtk_table_attach (GTK_TABLE (table1), radio, 3, 4, 1, 2,
            (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
            (GtkAttachOptions) (0),0,0);
gtk_widget_show (radio);
addsignal ();                 /*添加事件。*/
gtk_widget_show (window);
gtk_main ();
return 0;
}


然后编译连接程序   gcc -o main main.c `pkg-config --libs --cflags gtk+-2.0

  然后就可以   ./main   了

你可能感兴趣的:(Linux下面c编程)