void addition(int a[],int b[],int q) /*高精度加法运算*/ { int i,c[251]={0},k; if(fa*fb>0||q) { if(an>bn) k=an; else k=bn; /*用k纪录结果的最小位数*/ for(i=0; i<k; i++) { c[i]=a[i]+b[i]+c[i]; c[i+1]=(int)c[i]/10; c[i]=(int)c[i]%10; } /*高精度加法运算过程*/ if(c[k]) k++; /*判断最后结果的位数*/ if(fa<0&&q||fa<0) printf("-"); for(i=k-1; i>=0; i--) printf("%d",c[i]); /*输出结果*/ return; } else subtraction(a,b,1); return; }
int main(){ int com; srand(time(NULL)); do{ com = displaymenu(); switch(com){ case 1: addition(); break; case 2: subtraction(); break; case 3: multiplication(); break; case 4: division(); break; case 5: printf("Have a nice day\n"); } }while(com != 5); //needed for all basic programs to run for the professor system("pause"); return(0); }
// Start of Main Program int main() { int X=1; char Calc_oprn; // Function call calculator_operations(); while(X) { printf("\n"); printf("%s : ", "Enter the calculator Operation you want to do:"); Calc_oprn=getche(); switch(Calc_oprn) { case '+': addition(); break; case '-': subtraction(); break; case '*': multiplication(); break; case '/': division(); break; case '?': modulus(); break; case '!': factorial(); break; case '^': power(); break; case 'H': case 'h': calculator_operations(); break; case 'Q': case 'q': exit(0); break; case 'c': case 'C': system("cls"); calculator_operations(); break; default : system("cls"); printf("\n**********You have entered unavailable option"); printf("***********\n"); printf("\n*****Please Enter any one of below available "); printf("options****\n"); calculator_operations(); } } }
char *sub(char *op1, char *op2, char **av) { if (op1[0] == av[2][3] && op2[0] == av[2][3]) { return (subtraction(op2 + 1, op1 + 1, av)); } if (op1[0] != av[2][3] && op2[0] == av[2][3]) { return (addition(op1, op2 + 1, 0, av)); } if (op1[0] == av[2][3] && op2[0] != av[2][3]) { return (addition(op1 + 1, op2, 1, av)); } return (subtraction(op1, op2, av)); }
/** * main */ int main(void) { int input; srand(time(NULL)); do { input = menu(); switch (input) { case 1: addition(); break; case 2: subtraction(); break; case 3: multiplication(); break; case 4: division(); break; case 5: break; } } while(input != 5); printf("\nGoodbye!\n\n"); system("pause"); return 0; }
int main() { float a, b, r; char op; do { printf("number op number? "); scanf(" %f %c %f", &a, &op, &b); switch (op) { case '+' : r = add(a,b); break; case '*' : r = multiply(a,b); break; case '-' : r = subtraction(a,b); break; case '/' : r = division(a,b); break; case 'm' : r = min(a,b); break; case 'M' : r = max(a,b); break; case 'q' : break; default : op='?'; } if (op=='?') printf("Unknown operator\n"); else if (op=='q') printf("Bye\n"); else printf("%f %c %f = %f\n", a, op, b, r); } while (op != 'q'); return 0; }
int main() { int x=2, y=3; printf("%i\n\n", addition(x,y)); printf("%i\n\n", subtraction(x,y)); printf("%i\n\n", multiplication(x,y)); printf("%f\n", division(x,y)); return 0; }
int main(){ bigint* a = int_to_bigint(283743); bigint* b = int_to_bigint(27); printf("substraction : %llx\n",bigint_to_int(subtraction(a, b))); printf("division : %llx\n",bigint_to_int(divide(a, b))); }
int division(int a, int b) { int res=0; while(a>=b) { a=subtraction(a,b); res++; } return res; }
int main() { int x=10000, y=1231; int z=2922, u=28; z = subtraction (3032,2012); std::cout << "Result of first subtraction >" << z << "\n"; std::cout << "\n"; std::cout << "Result of second subtraction >" << subtraction (3031,2012) << "\n"; std::cout << "\n"; std::cout << "Result of third subtraction >" << subtraction (x,y) << "\n"; std::cout << "\n"; std::cout << "Result of fourth subtraction >" << subtraction (z,u) << "\n"; std::cout << "\n"; z =4 + subtraction ( y,u); std::cout << "Result of fifth subtraction >" << z << "\n"; std::cin.get(); std::cin.get(); return 0; }
void do_op(char C) { switch(C) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': push(C-'0'); break; case '+': addition(); break; case '-': subtraction(); break; case '*': multiplication(); break; case '/': division(); break; case '%': modulo(); break; case '^': north(); break; case '>': east(); break; case 'V': case 'v': south(); break; case '<': west(); break; case '?': spin(); break; case '!': lnot(); break; case '`': gt(); break; case '_': hif(); break; case '|': vif(); break; case '"': tsm(); break; case ':': dup(); break; case '\\': swap(); break; case '$': chomp(); break; case '#': jump(); break; case 'p': put(); break; case 'g': get(); break; case 'H': gate(); break; case '.': print_i(); break; case ',': print_c(); break; case '&': input_i(); break; case '~': input_c(); break; case '@': hacf(); break; case '{': left_b(); break; case '}': right_b(); break; case '[': carry_l(); break; case ']': carry_r(); break; case ';': empty(); break; case 'O': portal_o(); break; case 'B': portal_b(); break; default: /* DO NOTHING! */ break; } }
int main(void) { int a[N]; make_int(a,100); int b[N]; make_int(b,10); int c[N]; int k=2; subtraction(a,b,c); int i=0; for(i=0;i<5;i++) printf("%d",c[i]); return 0; }
main() { int a[250]={0},b[250]={0}; input(a,b); printf("\n%s+%s=",b1,b2); addition(a,b,0); printf("\n%s-%s=",b1,b2); subtraction(a,b,0); printf("\n%s*%s=",b1,b2); multiplication(a,b); while(1); }
int main(int argc, char* argv[]) { if (argc < 3) { std::cout << "Please provide two numbers. Run this program as follows: ./compute a b\n"; return 0; } double sum = addition(atof(argv[1]), atof(argv[2])); std::cout << argv[1] << " + " << argv[2] << " = " << sum << "\n"; double difference = subtraction(atof(argv[1]), atof(argv[2])); std::cout << argv[1] << " - " << argv[2] << " = " << difference << "\n"; return 0; }
int main () { int x=5, y=3, z; z = subtraction (7,2); printf("Hello World\n"); printf("Number = 5: right? %i\n", z); /* cout << "The first result is " << z << '\n'; cout << "The second result is " << subtraction (7,2) << '\n'; cout << "The third result is " << subtraction (x,y) << '\n'; z= 4 + subtraction (x,y); cout << "The fourth result is " << z << '\n'; */ }
/***********************************************************************//** * @brief Unary matrix subtraction operator * * @param[in] matrix Matrix. * * @exception GException::matrix_mismatch * Incompatible matrix size. * * This method performs a matrix addition. The operation can only succeed * when the dimensions of both matrices are identical. ***************************************************************************/ GMatrix& GMatrix::operator-=(const GMatrix& matrix) { // Raise an exception if the matrix dimensions are not compatible if (m_rows != matrix.m_rows || m_cols != matrix.m_cols) { throw GException::matrix_mismatch(G_OP_SUB, m_rows, m_cols, matrix.m_rows, matrix.m_cols); } // Subtract matrices using base class method subtraction(matrix); // Return result return *this; }
jfloatArray Java_fr_bowserf_nbodyproblem_CalculationNDK_computeNewPosition(JNIEnv *env, jobject obj){ int numberCoordinates = N * 3; float *newPositions = (float*)malloc((numberCoordinates) * sizeof(float)); for(int i = 0 ; i < N ; i++){ float *acc = (float*)calloc(3, sizeof(float)); for(int j = 0 ; j < N ; j++){ float square = squaredNorm(p + i * 3, p + j * 3); float denominateur = (float) pow(square + squaredEpsilon, THREE_AND_HALF); float* resultSub = subtraction(p + i * 3, p + j * 3); float* numerateur = mult(m[j], resultSub); float* resultMult = mult(1 / denominateur, numerateur); add(acc, resultMult); free(resultSub); free(numerateur); free(resultMult); } float *tmp_v = addition(v + i * 3, mult(G, acc)); v[i * 3] = tmp_v[0]; v[i * 3 + 1] = tmp_v[1]; v[i * 3 + 2] = tmp_v[2]; float *tmp_rep = addition(v + i * 3, p + i * 3); newPositions[i * 3] = tmp_rep[0]; newPositions[i * 3 + 1] = tmp_rep[1]; newPositions[i * 3 + 2] = tmp_rep[2]; free(tmp_rep); free(acc); free(tmp_v); } for(int i = 0 ; i < numberCoordinates ; i++){ p[i] = newPositions[i]; } free(newPositions); (*env)->SetFloatArrayRegion(env, result, 0, numberCoordinates, p); return result; }
int main(int argc, const char *argv[])//funcao principal { int seconds, hours, minutes, aux1, aux2, sec; printf("Insira os segundos: \n"); scanf("%d", &seconds); //insere a quantidade de segundos hours = sec_hours(seconds); //chamada da funcao de conversao segundos ---> horas aux1 = seconds - (hours*3600); //tira a diferenca entre os segundos iniciais e as horas convertidas em segundos minutes = sec_minutes(aux1); //o que sobra da diferenca é chamada na funcao de conversao seg ---> minutos aux2 = ((hours*3600)+(minutes*60)); //soma das horas e minutos transformados em segundos sec = subtraction(seconds, aux2); //subtracao dos segundos iniciais com as horas e minutos transformados em segundos printf("%d segundos equivalem a %d horas, %d minutos e %d segundos \n", seconds, hours, minutes, sec); return 0; }
void calculate(LinkedList<int> *destination, LinkedList<int> *source1, LinkedList<int> *source2, string op){ destination -> clear(); switch(op[0]){ case '+': addition(destination, source1, source2); break; case '-' : subtraction(destination, source1, source2); break; case '*' : multiply(destination, source1, source2); break; case '^': exponential(destination, source1, source2); break; } }
/* função principal*/ int main(int argc, const char *argv[]) { int seconds, hours, minutes, aux1, aux2, sec; printf("Insira os segundos: \n"); scanf("%d", &seconds); hours = sec_hours(seconds); aux1 = seconds - (hours*3600); minutes = sec_minutes(aux1); aux2 = ((hours*3600)+(minutes*60)); sec = subtraction(seconds, aux2); printf("%d segundos equivalem a %d horas, %d minutos e %d segundos \n", seconds, hours, minutes, sec); return 0; }
int main() { push(5); push(5); addition(); cout << "pushed 5, 5, and added, top now " << pop() << endl; push(5); push(4); subtraction(); cout << "pushed 5, 5, and subtracted, top now " << pop() << endl; push(2); push(5); multiplication(); cout << "pushed 2, 5 and multiplied, top now " << pop() << endl; push(9); push(3); division(); cout << "pushed 9, 3 and divided, top now " << pop() << endl; push(9); push(0); division(); cout << "pushed 9, 0 and divided, top now " << pop() << endl; push(9); push(3); modulo(); cout << "pushed 9, 3 and modulosed, top now " << pop() << endl; push(9); push(0); modulo(); cout << "pushed 9, 0 and modulosed, top now " << pop() << endl; }
int main() { int choice, temp1, temp2, temp3; complex a; complex b; complex c; while(1) { printf("Press 1 to add two complex numbers.\n"); printf("Press 2 to subtract two complex numbers.\n"); printf("Press 3 to multiply two complex numbers.\n"); printf("Press 4 to divide two complex numbers.\n"); printf("Press 5 to exit.\n"); printf("Enter your choice\n"); scanf("%d",&choice); if( choice == 5) { exit(0); } if(choice >= 1 && choice <= 4) { printf("Enter a and b where a + ib is the first complex number."); printf("\na = "); scanf("%d", &a.real); printf("b = "); scanf("%d", &a.img); printf("Enter c and d where c + id is the second complex number."); printf("\nc = "); scanf("%d", &b.real); printf("d = "); scanf("%d", &b.img); } if ( choice == 1 ) { c = addition(a, b); if ( c.img >= 0 ) { printf("Sum of two complex numbers = %d + %di",c.real,c.img); } else { printf("Sum of two complex numbers = %d %di",c.real,c.img); } } else if ( choice == 2 ) { c = subtraction(a, b); if ( c.img >= 0 ) { printf("Difference of two complex numbers = %d + %di",c.real,c.img); } else { printf("Difference of two complex numbers = %d %di",c.real,c.img); } } else if ( choice == 3 ) { c = multiply(a, b); if ( c.img >= 0 ) { printf("Multiplication of two complex numbers = %d + %di",c.real,c.img); } else { printf("Multiplication of two complex numbers = %d %di",c.real,c.img); } } else if ( choice == 4 ) { if ( b.real == 0 && b.img == 0 ) { printf("Division by 0 + 0i is not allowed."); } else { c = divide(a, b); if ( c.img >= 0 ) { printf("Division of two complex numbers = %d + %di",c.real,c.img); } else { printf("Division of two complex numbers = %d %di",c.real,c.img); } } } else { printf("Invalid choice."); } printf("\nPress any key to enter choice again...\n"); } }
struct map_type *dfp( function func, struct map_type *map_x, __float128 grad_toler, __float128 fx_toler, const unsigned int max_iter ) { unsigned int iter; __float128 lambda, result_func_value, temp_func_value; struct map_type *result, *b, *grad1, *tmp, *s, *grad2, *g, *tmp2, *tmp3, *d, *x1, *x2, *x3, *x4, *x5; result = NULL; result_func_value = func( map_x ); b = get_identity_matrix( map_x->j_size ); grad1 = first_derivatives( func, map_x ); tmp = transposition( grad1 ); deallocate( grad1 ); grad1 = tmp; for( iter = 0 ; iter < max_iter; iter++ ) { tmp = multiplicate_on_value( -1, b ); s = multiplicate( tmp, grad1 ); deallocate( tmp ); tmp = transposition( s ); tmp2 = multiplicate_on_value( powf( get_euclidean_distance( s ), -1 ), tmp ); deallocate( s ); deallocate( tmp ); s = tmp2; lambda = 1; lambda = line_search( func, map_x, lambda, s ); d = multiplicate_on_value( lambda, s ); tmp = addition( map_x, d ); deallocate( d ); deallocate( map_x ); map_x = tmp; temp_func_value = func( map_x ); if( result_func_value > temp_func_value ) { iter = 0; result_func_value = temp_func_value; if( result != NULL ) { deallocate( result ); } result = clone( map_x ); } grad2 = first_derivatives( func, map_x ); tmp = transposition( grad2 ); deallocate( grad2 ); grad2 = tmp; g = subtraction( grad2, grad1 ); deallocate( grad1 ); grad1 = grad2; if( get_euclidean_distance( grad1 ) < grad_toler ) { /// TODO: Нужно очистить память break; } tmp = transposition( s ); x1 = multiplicate( s, tmp ); x2 = multiplicate( s, g ); deallocate( s ); deallocate( tmp ); tmp = multiplicate_on_value( lambda, x1 ); tmp2 = get_inverse( x2 ); tmp3 = multiplicate( tmp, tmp2 ); deallocate( tmp ); tmp = addition( b, tmp3 ); deallocate( x1 ); deallocate( x2 ); deallocate( tmp2 ); deallocate( tmp3 ); deallocate( b ); b = tmp; x3 = multiplicate( b, g ); tmp = transposition( b ); x4 = multiplicate( tmp, g ); deallocate( tmp ); tmp = transposition( g ); tmp2 = multiplicate( tmp, b ); x5 = multiplicate( tmp2, g ); deallocate( g ); deallocate( tmp ); deallocate( tmp2 ); tmp = transposition( x4 ); tmp2 = multiplicate( x3, tmp ); deallocate( tmp ); tmp = get_inverse( x5 ); tmp3 = multiplicate( tmp, tmp2 ); deallocate( tmp ); tmp = subtraction( b, tmp3 ); deallocate( b ); b = tmp; deallocate( tmp2 ); deallocate( tmp3 ); deallocate( x3 ); deallocate( x4 ); deallocate( x5 ); } return result; }
void SpeedFiltration(float *V,float *vF) { TargSpeed.x = V[0]; TargSpeed.y = V[1]; TVector temp; CurSpeed.x = vF[0]; CurSpeed.y = vF[1]; temp = subtraction(CurSpeed,TargSpeed); ACCEL_INC = pow(mod(temp) * 10.0, 2) / 10.0 + 0.001 + mod(CurAccel) / 2.0; if (ACCEL_INC > MAX_ACCEL_INC) { ACCEL_INC = MAX_ACCEL_INC;} if (ACCEL_INC < -MAX_ACCEL_INC) { ACCEL_INC = -MAX_ACCEL_INC;} TVector accelInc; temp = subtraction(TargSpeed, CurSpeed); temp = subtraction(temp, CurAccel); temp = normalization(temp, ACCEL_INC); accelInc = temp; temp = subtraction(TargSpeed, CurSpeed); temp = subtraction(CurAccel, temp ); temp = normalization(temp, ACCEL_INC); TVector accelDec = temp; TVector newAccelInc, newAccelDec; newAccelInc = addition(CurAccel, accelInc); newAccelDec = addition(CurAccel, accelDec); TVector newSpeedInc, newSpeedDec, newSpeedZer; newSpeedInc = addition(CurSpeed, newAccelInc); newSpeedDec = addition(CurSpeed, newAccelDec); newSpeedZer = addition(CurSpeed, CurAccel); float timeToStop; if (mod(accelInc) > 0) timeToStop = abs(mod(CurAccel)/mod(accelInc))/2.0 + 0.50; else timeToStop = 1; float incErr = mod(subtraction(TargSpeed,addition(newSpeedInc, addition(scale(newAccelInc,timeToStop), scale(accelInc,timeToStop*timeToStop/2.0))))); float decErr = mod(subtraction(TargSpeed,addition(newSpeedDec, addition(scale(newAccelDec,timeToStop), scale(accelDec,timeToStop*timeToStop/2.0))))); float zerErr = mod(subtraction(TargSpeed,addition(newSpeedDec, scale(newAccelDec,timeToStop)))); if (incErr > decErr) { if (decErr < zerErr) { AccelInc1 = accelDec; CurSpeed = newSpeedDec; CurAccel = newAccelDec; } else { AccelInc1.x = 0; AccelInc1.y = 0; CurSpeed = newSpeedZer; } } else { if (incErr < zerErr) { AccelInc1 = accelInc; CurSpeed = newSpeedInc; CurAccel = newAccelInc; } else { AccelInc1.x = 0; AccelInc1.y = 0; CurSpeed =newSpeedZer; } } if (mod(CurAccel) > MAX_ACCEL) CurAccel = normalization(CurAccel, MAX_ACCEL); vF[0] = CurSpeed.x; vF[1] = CurSpeed.y; vF[2] = V[2]; }
int main(void) { int x, y; char sign; float numerator, denominator; do { printf("> "); scanf("%c",&sign); switch(sign) { case '+': scanf("%d%d",&x,&y); printf("# %d\n",additional(x,y)); break; case '-': scanf("%d%d",&x,&y); printf("# %d\n",subtraction(x,y)); break; case '/': scanf("%f%f",&numerator,&denominator); printf("# %.2f\n",division(numerator,denominator)); break; case 'd': scanf("%d%d",&x,&y); printf("# %d\n",divisionRemain(x,y)); break; case 'm': scanf("%d%d",&x,&y); printf("# %d\n",modulo(x,y)); break; case '*': scanf("%d%d",&x,&y); printf("# %d\n",multiply(x,y)); break; case '^': scanf("%d%d",&x,&y); printf("# %ld\n",power(x,y)); break; case '!': scanf("%d",&x); printf("# %lld\n",factorial(x)); break; case 's': scanf("%d",&x); printf("# %d\n",sum(x)); break; case 'a': scanf("%d",&x); printf("# %.2f\n",average(x)); break; case 'c': scanf("%d%d",&x,&y); printf("# %lld\n",combinations(x,y)); break; } getchar(); } while(sign != 'q'); return 0; }
void phi(float c[50][50],float a[50][50],float d[50][50],float x[50][1],int n,int m) // points are stored by x //this function calculates the transformation of { //karmarkar's algorithm float cdash[50][50]; //need serious reviewing float cp[50][50]; float z[50][50]; float cpcap[50][50]; int i,j; float value; float anot[50][50]; float bprime[50][50]; float e[50][50]; float ddash[50][50]; float constant; float b[50][50]; float val,atemp[50][50]; for(i=0;i<m;i++) //loop for input of constarints for(j=0;j<n;j++) atemp[i][j]=a[i][j]; for(i=0;i<n;i++) for(j=0;j<n;j++) ddash[i][j]=d[i][j]; //copying d to ddash since d will b destroyed mul(d,c,n,n,n,1); //d=d.c for(i=0;i<n;i++) cdash[i][0]=d[i][0]; //c'=d's first column mul(atemp,ddash,m,n,n,n); //a=a.d augment(atemp,m,n); //a=augment of a for(i=0;i<(m+1);i++) //z is a m+1*n matrix so is a for(j=0;j<n;j++) z[i][j]=atemp[i][j]; //z=b transpose(atemp,m+1,n); //atemp=bt mul(z,atemp,m+1,n,n,m+1); //z=zztrans=BBtrans inverse(z,m+1); //z=zinverse transpose(atemp,n,m+1); //taking the transpose of a ie Btrans to obtain back the original z or B ie a=B mul(z,atemp,m+1,m+1,m+1,n); //z=inv(zztrans)z or z=inv(BBtrans)B mul(z,cdash,m+1,n,n,1); //multiplying with cdash result in z transpose(atemp,m+1,n); //taking transpose of a ie z or Btrans mul(atemp,z,n,m+1,m+1,1); //m loses its effects from here onwards subtraction(cdash,atemp,n,1); //subtracting a from cdash result in cdash for(i=0;i<n;i++) cp[i][0]=cdash[i][0]; //copying cdash to cp cp=n*1 matrix value=mod(cp,n,1); //obtaining the mod of cp if(value==0) { for(i=0;i<n;i++) cpcap[i][0]=0; //cpcap=0;for all zeros } else { value=1/value; for(i=0;i<n;i++) cpcap[i][0]=cp[i][0]*value; } //obtaing cpcap from formula, cpcap=n*1 matrix value=(float)n; value=1/value; for(i=0;i<n;i++) anot[i][0]=value; //initializing anot to 1\n val=consradii(n); val=alpha*val; for(i=0;i<n;i++) cpcap[i][0]=(val*cpcap[i][0]); subtraction(anot,cpcap,n,1); //subtracting anot from cpcap result in anot for(i=0;i<n;i++) bprime[i][0]=anot[i][0]; //copying anot to bprime for(i=0;i<n;i++) e[0][i]=1; //initializing the elements of the array e with 1 mul(e,ddash,1,n,n,n); //multiplying e with ddash or the original d mul(e,bprime,1,n,n,1); constant=e[0][0]; //the single element array in e is terrmed constant mul(ddash,bprime,n,n,n,1); for(i=0;i<n;i++) { b[i][0]=ddash[i][0]; //assigns the values of ddash to b b[i][0]=b[i][0]/constant; //computes the value of b } for(i=0;i<n;i++) x[i][0]=b[i][0]; }