Example #1
0
File: g7e4.c Project: pingicx/cx
/*
Chooses the calculation function wanted. Accepts operand 1, operand 2 and a pointer to the operator.
Returns the value calculated by the operating functions.
*/
float calculate(float n1, float n2, int *op) {
	switch (*op) {									// compares to the value of the pointer to the operator. 
	case '+':
		return plus1(n1,n2);
		break;

	case '-':
		return minus1(n1,n2);
		break;

	case '/':
		return divide1(n1,n2);
		break;

	case '*':
		return multiply1(n1,n2);
		break;

	case '^':
		return xor1(n1,n2);
		break;

	case '|':
		return or1(n1,n2);
		break;		

	case '&':
		return and1(n1,n2);
		break;

	default:
		break;
	}
}
Example #2
0
int main(int argc, const char *argv[])
{
    int a,b,ad,su,mu,de,mo;
    char i;
    fun add1;//申请一个地址空间
    add1=add;//把函数add的入口地址赋给add1 

    fun subtract1;
    subtract1=subtract;
    
    fun multiply1;
    multiply1=multiply;

    fun dev1;
    dev1=dev;

    fun mod1;
    mod1=mod;
    while(1)
    {
    printf("Input a symbol:\n");
    scanf("%c",&i);
    if(i=='#') break;
        printf("Input num1:\n");
         scanf("%d",&a);
          printf("Input num2:\n");
          scanf("%d",&b);
          ad=add1(a,b);
          getchar();//因为输入完成之后要回车才能输入字符,程序会把回车当字符读入造成程序出乱,所以加上getchar()
    switch(i)
    {
        case '+':
          ad=add1(a,b);
          printf("%d + %d= %d\n",a,b,ad);
          break;
         
        case '-':
         su=subtract1(a,b);
          printf("%d - %d= %d\n",a,b,su);
          break;
        case '*':
          mu=multiply1(a,b);
          printf("%d * %d= %d\n",a,b,mu);
          break;
        case '/':
          de=dev1(a,b);
          printf("%d / %d= %d\n",a,b,de);
          break;
        case '%':
          mo=mod1(a,b);
          printf("%d %% %d= %d\n",a,b,mo);
          //打印%的时候 中间要加一个%分开 否则会出错误
          break;
    }

    }
    return 0;
}
Example #3
0
int main(void) {
	benchmark(400,1,multiply2);
	int i,j;
	FILE* in  = fopen("input.txt","r");
	// reading matrix a
	fscanf(in,"%d %d",&m,&p);
	a = read_matrix(in,m,p);

	// reading matrix b
	fscanf(in,"%d %d",&q,&n);
	b = read_matrix(in,q,n);

	if (p!=q){
		printf("incompatible matrices --not supported\n");
		return 0;
	}

	//initializing matrix c
	c = (int**) malloc(sizeof(int *)*m);
	for (i = 0; i < m; ++i){
		c[i] = (int *) malloc(sizeof(int)*n);
		for (j = 0; j < n; ++j)
			c[i][j] = 0;
	}

	long long t1,t2;
	FILE* out = fopen("output.txt","w");

//	multiply(a,b,c);
//	multiply(a,b,c);
//	multiply(a,b,c);

	for (i = 0; i < m; ++i)
		for (j = 0; j < n; ++j)
			c[i][j] = 0;


	fprintf(out,"result of non-threaded multiplication\n");
	t1 = curTime();
	multiply(a,b,c);
	t2 = curTime();
	writeMat(c,m,n,out);
	fprintf(out,"elapsed time = %lld usec\n\n",t2-t1);

	for (i = 0; i < m; ++i)
		for (j = 0; j < n; ++j)
			c[i][j] = 0;

	fprintf(out,"result of multiplication1 (thread for each cell)\n");
	t1 = curTime();
	multiply1();
	t2 = curTime();
	writeMat(c,m,n,out);
	fprintf(out,"elapsed time = %lld usec\n\n",t2-t1);
	for (i = 0; i < m; ++i)
		for (j = 0; j < n; ++j)
			c[i][j] = 0;


	fprintf(out,"result of multiplication2 (thread for each row)\n");
	t1 = curTime();
	multiply2();
	t2 = curTime();
	writeMat(c,m,n,out);
	fprintf(out,"elapsed time = %lld usec\n\n",t2-t1);

	fclose(out);
	print(c,m,n);
	return 0;
}