Exemplo n.º 1
0
uint64_t Power(uint64_t b, uint64_t e, uint64_t m)
{
   uint64_t r = 1;
   for(; e; e >>= 1) {
      if(e & 1)
         r = Multiplication(r, b, m);
      b = Multiplication(b, b, m);
   }
   return r;
}
Exemplo n.º 2
0
void CPolynomial::Multiplication()
{
    if(m_apoly.Size()==0 || m_bpoly.Size()==0)
        return;

    //multiply the two polynomials
    Multiplication(m_apoly,m_bpoly,m_cpoly);
}
int main()
{
	int a, value;

	printf("数値:");	scanf("%d", &a);

	value = Multiplication(a);

	printf("%d", value);

	getchar();
	return 0;
}
bool PremierExemple::qt_invoke( int _id, QUObject* _o )
{
    switch ( _id - staticMetaObject()->slotOffset() ) {
    case 0: Addition(); break;
    case 1: Soustraction(); break;
    case 2: Multiplication(); break;
    case 3: DivisionEntiere(); break;
    case 4: Effacer(); break;
    case 5: Terminer(); break;
    case 6: languageChange(); break;
    default:
	return QDialog::qt_invoke( _id, _o );
    }
    return TRUE;
}
int Multiply(void *arg) {

    /* Get the thread identifier */
    int threadNum = *(int *) arg;

    /* Call the library which multiplies */
    Multiplication(m1, m2, m1r, m1c, m2c, &m3, threadNum,
                   &cond.key, &cond.done);
#ifdef DEBUG
    printf("Thread %d finished multiplication\n",threadNum);
#endif
    /* If the thread is the elected, start the sum */
    if (threadNum == theChosenOne) {
        pthread_mutex_lock(&cond.key);
#ifdef DEBUG
        printf("Thread %d waiting for diagonal values!\n",threadNum);
#endif
        int select = m3c;
        if (m3r < m3c) select = m3r;

        /* Check each element of the diagonal, wait until it is filed */
        for (int i = 0; i < select; i++) {
            while (*(m3 + (i * m3c) + i) == -1) {
#ifdef DEBUG
                printf("Blocked\n");
#endif
                pthread_cond_wait(&cond.done, &cond.key);
            }
#ifdef DEBUG
            printf("Adding: %d\n",*(m3+(i*m3c)+i));
#endif
            /* When it is filled, sum the resultant */
            diagonalSum += *(m3 + (i * m3c) + i);
        }


#ifdef DEBUG
        printf("Diagonal sum = %f\n",diagonalSum);
#endif
        /* When done, unlock the mutex */
        pthread_mutex_unlock(&cond.key);
    }

#ifdef DEBUG
    printf("Thread: %d returning...\n",threadNum);
#endif
    return EXIT_SUCCESS;
}
Exemplo n.º 6
0
int MillerRabin(uint64_t n, uint64_t k)
{
   // Factor n-1 as d*2^s
   uint64_t s = 0;
   uint64_t d = n - 1;
   for(; !(d & 1); s++)
      d >>= 1;
   // Verify x = k^(d 2^i) mod n != 1
   uint64_t x = Power(k % n, d, n);
   if(x == 1 || x == n-1)
      return 1;
   while(s-- > 1) {
      // x = x^2 mod n
      x = Multiplication(x, x, n);
      if(x == 1)
         return 0;
      if(x == n-1)
         return 1;
   }
   return 0;
}
Exemplo n.º 7
0
void CallFunction(FunctionCall fc)
{
	int function = GetFunction(fc->function);
	
	switch (function)
	{
		case VAR : NewVariable(fc); break;
		case NMX : NewMatrix(fc); break;
		case ADD : Addition(fc); break;
		case SUB : Substraction(fc); break;
		case MUL : Multiplication(fc); break;
		case MSC : Scalar_Mult(fc); break;
		case EXP : Exponentiation(fc); break;
		case TRA : Transpose(fc); break;
		case DET : Determinant(fc); break;
		case DLU : Decomposition(fc); break;
		case SOL : Solve(fc); break;
		case INV : Inversion(fc); break;
		case RNK : Rank(fc); break;
		case DSP : Display(fc); break;
		case NOF : // default
		default :
		{
			if (GetFunction(fc->name)==SPT) SpeedTest(fc);
			else if (IndexVariable(fc->function)!=-1) NewVariable(fc);
			else if (IndexMatrix(fc->function)!=-1) NewMatrix(fc);
			else
			{
				printf("\t%s : Function Not Implemented\n", fc->function);
				fni++;
			}
			break;
		}
	}
	
	if (function!=NOF && function !=VAR) fni = 0;
}
Exemplo n.º 8
0
ClassLong ClassLong::operator*(unsigned long long int value) 
{
	return Multiplication(this->number, InputNumber(value));
}
Exemplo n.º 9
0
ClassLong ClassLong::operator*(const ClassLong &right) 
{
	return Multiplication(this->number, right.number);
}
Exemplo n.º 10
0
Constante* Eval::getValue() const {
    Expression* exp = dynamic_cast<Expression*>(c);
    QStack<Constante*> p;
    if (exp != NULL){
        int i = 0;
        QString s = exp->getExp();
        Constante * result = 0;
        while(i<s.length()){
            //on passe les espaces;
            if(s[i] == ' '){
                if(result != 0){
                    p.push(result);
                    result = 0;
                }
            }
            else if(s[i] >= '0' && s[i] <= '9'){
                if(result == 0)result = Addition(Rationnel(0),Rationnel(0),mModeConstante, mModeComplexes).getValue();
                result->addChiffre(s[i].toAscii() - '0');
            }
            else if(s[i] == '$'){
                if(result == 0) throw EvalException("$ mal placé");
                result->setDollarEntre();
            }
            else if(s[i] == '/'){
                if(result != 0){
                    result->setSlashEntre();
                }
                else{
                    if(p.size() < 2) throw EvalException("Pas assez d'opérandes pour /");
                    Constante *op2 = p.pop(),  *op1 = p.pop();
                    try{
                        result = Division(*op1,*op2,mModeConstante,mModeComplexes).getValue();
                    }
                    catch(DivException e){
                        throw EvalException("division par zéro");
                    }

                    delete op1;
                    delete op2;
                    p.push(result);
                    result = 0;
                }
            }
            else if(s[i] == ',' || s[i] == '.'){
                if(result == 0) throw EvalException(", ou . mal placé");
                result->setVirguleEntree();
            }
            else if(s[i] == '+'){
                if(result!=0){
                    p.push(result);
                    result = 0;
                }
                if(p.size() < 2) throw EvalException("Pas assez d'opérandes pour +");
                Constante *op1 = p.pop(),  *op2 = p.pop();
                result = Addition(*op1,*op2,mModeConstante,mModeComplexes).getValue();
                delete op1;
                delete op2;
                p.push(result);
                result = 0;
            }
            else if(s[i] == '-'){
                if(result!=0){
                    p.push(result);
                    result = 0;
                }
                if(p.size() < 2) throw EvalException("Pas assez d'opérandes pour -");
                Constante *op2 = p.pop(),  *op1 = p.pop();
                result = Soustraction(*op1,*op2,mModeConstante,mModeComplexes).getValue();
                delete op1;
                delete op2;
                p.push(result);
                result = 0;
            }
            else if(s[i] == '*'){
                if(result!=0){
                    p.push(result);
                    result = 0;
                }
                if(p.size() < 2) throw EvalException("Pas assez d'opérandes pour *");
                Constante *op2 = p.pop(),  *op1 = p.pop();
                result = Multiplication(*op1,*op2,mModeConstante,mModeComplexes).getValue();
                delete op1;
                delete op2;
                p.push(result);
                result = 0;
            }
            else{
                throw EvalException("Caractère inconnu");
            }
            i++;
        }
        if(result!=0)p.push(result);
        if(p.size() > 1) throw EvalException("Il manque un opérateur.");
        if(p.size() < 1) throw EvalException("Pile d'évaluation vide.");
        return p.at(0);
    }
    else throw TypeConstanteException("Ceci n'est pas une expression");
}
Exemplo n.º 11
0
void main()	// main function
{
	int menu, result[100], total=0, count=0;
	char con;
	struct tm time;
		_getsystime(&time);
	int hr, min, sec;

	hr = time.tm_hour;
	min = time.tm_min;
	sec = time.tm_sec;

	int i=0;
	do
	{
		// Menu
		system("CLS");
		printf("_-_-_-_Menu_-_-_-_\n"
			   "[1] Multiplication\n"
			   "[2] Division\n"
			   "[3] Addition\n"
			   "[4] Subtraction\n"
			   "[5] Quit\n\n");

		printf("Choose a selection > ");
		scanf("%d",&menu);

		switch(menu)	// switch statement
		{
		case 1:
			{
				result[i] = Multiplication();	// function call Multiplication();
				output(result[i]);	// function call output();
				count++;	// the times of test
				break;
			}
		case 2:
			{
				result[i] = Division();	// function call Division();
				output(result[i]);	// function call output();
				count++;	// the times of test
				break;
			}
		case 3:
			{
				result[i] = Addition();	// function call Addition();
				output(result[i]);	// function call output();
				count++;	// the times of test
				break;
			}
		case 4:
			{
				result[i] = Subtraction();	// function call Subtraction();
				output(result[i]);	// function call output();
				count++;	// the times of test
				break;
			}
		case 5:
			{
				Quit(total,count,hr,min,sec);	// function call Quit();
				break;
			}
		default:
			{
				printf("Invalid Input! Please re-enter\n");
				main();	// re-run the main function
			}
			i++;
		}
		total = total + result[i];	// sum all the tests' marks
		// ask for user want to continue the test or not
		printf("Do you want to continue? (Y/N) ");
		fflush(stdin);
		scanf("%c",&con);
		toupper(con);
		while(con!='Y' && con!='N' && con!='y' && con!='n')
			{
				printf("Invalid Input! Please re-enter (Y/N)> ");
				fflush(stdin);
				scanf("%c",&con);
				toupper(con);
			}
	}while(con == 'Y' || con=='y');
	Quit(total,count,hr,min,sec);	// Print out the total result & Quit the program
}
Exemplo n.º 12
0
int main()
{
    // Part One:


    // FIXME: Declare variables
    // Declare two variables: an integer named "age", and a string named "name" with corresponding values (your name and age)
    int age = 19;
    char name[] = "Stan";

    // FIXME: Print
    // Print the following sentence in the console "You are NAME and you are AGE years old !". Don't forget to add a newline at the end
    printf("You are %s and you are %d years old !\n", name, age);

    // FiXME: Concatenation
    // Create a new string variable called "hello" which value is "Hello ". Add "name" at the end of "hello" (Concatenation) then print it
    char hello[] = "Hello ";
    strcat(hello, name);
    printf("%s\n", hello);

    // FIXME: Array
    // create a new string array called "shoppingList", with three elements of your choice. Create an int variable containing the number of
    // elements in "shoppingList" (using a function of the array/using the array)
    char *shoppingList[3] = {"milk", "a Chevy Camaro", "a life"};
    const int nbOfElements = numberOfElements(shoppingList);

    // FIXME: For-loop - Integer
    // Create a simple for-loop for an integer "i" going from 1 to 10 that print the value of "i"
    for (int i = 0; i < 10; i++)
        printf("%d ", i);
    printf("\n");

    // FIXME: For-loop - shoppingList
    // Create a for loop that iterate through "shoppingList" and prints each element with "You have to buy (elemt)".
    for (int j = 0; j <nbOfElements; j++)
        printf("You have to buy %s\n", shoppingList[j]);


    // FIXME: Foreach-loop
    // Do the same with a foreach-loop.
    // The C programming language doesn't have a foreach, but you can look for macros if you are
    // curious.



    // FIXME: If-statement
    // Modify the first for-loop (with i from 1 to 10) such that it prints "(value of i) is even" when "i" is divisible
    // by 2 (You may want to learn more about "modulo" (%)). Else, print "(value of i) is odd".
    for (int i = 0; i < 10; i++)
    {
        if (i % 2)
            printf("%d is odd\n", i);
        else
            printf("%d is even\n", i);
    }



    // FIXME: Sum Up
    // Create a string variable called "element" with the value of your choice. Then create a for-loop/foreach-loop that checks if "shoppingList"
    // contains "element". If yes, print "You have to buy (value of element) !", and stop the loop (search how to stop a loop).
    // If not, print "Nope, you don't need (value of "element")".
    char element[] = "a life";
    for (int k = 0; k < nbOfElements; k++)
    {
        if (strcmp(element, shoppingList[k]) == 0)
        {
            printf("You have to buy %s !\n", shoppingList[k]);
            break;
        }
        else
        {
            printf("Nope, you don't need %s !\n", shoppingList[k]);
        }
    }



    // Part Two:


    // FIXME: Functions - Ascii
    // Create a function that returns nothing and which doesn't takes any parameter. It should just be named "TriForce"
    // and print the TriForce symbol (one triangle over two other ones, can be found on internet) with "TRIFORCE"
    // Don't forget to call the function !
    TriForce(); //Calling the functions in main()


    // FIXME: Functions - One parameter
    // Create a function that takes a string as parameter and returns "Hello (value of string) !"
    // http://stackoverflow.com/questions/1745726/how-to-store-printf-into-a-variable
    printf("%s", Hello("Stan"));

    // FIXME: Functions - Multiple parameters
    // Create a function that takes two integers as parameters and returns the addition of these two.
    // You can do the same with multiplication, subtraction and division.
    printf("%d\n", Addition(5, 12));
    printf("%d\n", Subtraction(5, 12));
    printf("%d\n", Multiplication(5, 12));
    printf("%d\n", Division(5, 12)); // returns 0 because "a" and "b" are Integers
    printf("%d\n", Division(10, 2));


    // FIXME: User entry
    // Create a string variable that takes what the user enter in the console as value. Then print "You entered (value of string)"
    char userInput[100]; // 100 will be the max length of the input
    printf("Enter a word:");
    scanf("%s", userInput);
    printf("You entered %s\n", userInput);


    // FIXME: While loop
    // Create a while loop that takes a number and divides it by 2 until it is less than 3
    int number = 52;
    while (number > 3)
    {
        number /= 2; //or number = number / 2;
        printf("%d ", number);
    }
    printf("\n");



    // FIXME: do-While loop
    // Do the same with a do-while loop
    int number2 = 84;
    do
    {
        number2 = number2 / 2;
        printf("%d ", number2);
    } while (number2 > 3);
    printf("\n");



    // FIXME: Random generator
    // Create a function that returns a random number
    srand((unsigned)time(NULL)); // time used for seeding
    printf("%d\n", rdm());


    // FIXME: Random generator with bounds
    // Create another function that returns a random number between two bounds given as parameters.
    printf("%d\n", rdmBounds(10, 20));



    // FIXME: Multi-dimensionnal array
    // Create a two dimensionnal int array of 3 columns and 3 rows. Use 2 for-loops to add a random number
    // between 1 and 9 in each of the 9 rooms.
    // You may use one of the two previously created function.
    // Then print them such that they appear like this (with [x1, x9] being the 9 random integers):
    // {x1, x2, x3, }
    // {x4, x5, x6, }
    // {x7, x8, x9, }
    int multiArray[3][3];
    for (int l = 0; l < numberOfElements(multiArray); l++)
        for (int m = 0; m < numberOfElements(multiArray[l]); m++)
            multiArray[l][m] = rdmBounds(1, 9);

    for (int n = 0; n < numberOfElements(multiArray); n++)
    {
        printf("{");

        for (int o = 0; o < numberOfElements(multiArray[n]); o++)
            printf("%d, ", multiArray[n][o]);

        printf("}\n");
    }

    // FIXME: Switch
    // Create a Switch that takes an integer "a" and return a sentence regarding the value of a
    // (Create 3 statements for 3 specific values and a default one)

    int a = 2;

    switch(a)
    {
    case 0:
        printf("a is 0\n");
        break;

    case 2:
        printf("a is 2\n");
        break;

    case 37:
        printf("Who cares about a anyway ?");
        break;

    default:
        printf("I don't know what \"a\" is... :'(\n");
        break;
    }

    // FIXME: logic Gates
    // Create 7 functions for each logic gates (And (0), Or (1), No (2), Nand (3), Nor (4), Xnor (5), Xor (6)).
    // Each function takes two booleans as parameters and returns the result of the logic gate.
    // (or You can do it with a switch and only one function)

    logic_gates(1, 0, 1);
    logic_gates(1, 1, 1);
    logic_gates(1, 2, 1);

    // FIXME - Reverse
    // Create a function that reverse a string

    reverse("Hello world");
    return 0;
}
Exemplo n.º 13
0
int main(int argc, const char * argv[])
{
	if (argc < 5) 
	{
        printf("Error: Too few arguments. <first_long_number_filename> [ + | - | * | / | % | ^ ] <second_long_number_filename> <result_long_number_filename>\n");
        return 0;
    }
    
    if (argc > 7) 
	{
        printf("Error: Too many arguments. <first_long_number_filename> [ + | - | * | / | % | ^ ] <second_long_number_filename> <result_long_number_filename> <module_long_number_filename> <-b>\n");
        return 0;
    }
    
    FILE* firstLongNumFile = fopen(argv[1], "r");
    if (!firstLongNumFile) 
	{
        printf("Error: Unable to open file: %s \n", argv[1]);
        return 0;
    }
	fclose(firstLongNumFile);

    const char* operation = argv[2];
    if ((strlen(operation) > 1 || operation[0] == '\0') || operation[0] != '+' && operation[0] != '-' && operation[0] != 'u' && operation[0] != '/' && operation[0] != '%' && operation[0] != 'd') 
	{
        printf("Error: Wrong operation: %s \n", operation);
        return 0;
    }
    
    FILE* secondLongNumFile = fopen(argv[3], "r");
    if (!secondLongNumFile) 
	{
        printf("Error: Unable to open file: %s \n", argv[3]);
        return 0;
    }
    fclose(secondLongNumFile);

	FILE* resultLongNumFile = fopen(argv[4], "r");
    if (!resultLongNumFile) 
	{
        printf("Error: Unable to open file: %s \n", argv[4]);
        return 0;
    }
    fclose(resultLongNumFile);

	int bin = 0;

	if (argc == 5)
		if (argv[2][0] == 'd')
		{
			printf("Error: Input module file\n");
			return 0;
		}

    if (argc == 6) 
	{
		if (argv[2][0] == 'd')
		{
			FILE* moduleLongNumFile = fopen(argv[5], "r");
			if (!moduleLongNumFile) 
			{
				printf("Error: Unable to open file: %s \n", argv[5]);
				return 0;
			}
			fclose(moduleLongNumFile);
		}
		else
		{
			if (strcmp(argv[5], "-b")) 
			{
				printf("Error: Invalid flag: %s \n", argv[5]);
				return 0;
			}
			bin = 1;
		}
		
    }

	if (argc == 7) 
	{
		FILE* moduleLongNumFile = fopen(argv[5], "r");
		if (!moduleLongNumFile) 
		{
			printf("Error: Unable to open file: %s \n", argv[5]);
			return 0;
		}
		fclose(moduleLongNumFile);

        if (strcmp(argv[6], "-b")) 
		{
            printf("Error: Invalid flag: %s \n", argv[6]);
            return 0;
        }
		bin = 1;
    }
    
	struct LongNumber a,b;

  if(bin == 1)
		a = InputBinFile(argv[1]);
	else
		a = InputTextFile(argv[1]);
    
  if(bin == 1)
		b = InputBinFile(argv[3]);
	else
		b = InputTextFile(argv[3]);
    
  
    struct LongNumber result;
    
    switch (operation[0]) {
        case '+':
        {
            result = Sum(a, b);
            break;
        }
        case '-':
        {
            result = Subtracting(a, b);
            break;
        }
        case 'u':
        {
            result = Multiplication(a, b);
            break;
        }
        case '/':
        {
            result = Division(a, b, 1);
            break;
        }
        case '%':
        {
            result = Division(a, b, 2);
            break;
        }
        case 'd':
        {
			struct LongNumber c;
            if(bin == 1)
				c = InputBinFile(argv[5]);
			else
				c = InputTextFile(argv[5]);

			result = Degree(a, b, c);
			free(c.digit);
            break;
        }
        default:
            break;
    }
     
    if (bin == 1)
		OutputBinFile(argv[4], result);
	else
		OutputTextFile(argv[4], result);

    free(a.digit);
    free(b.digit);
    free(result.digit);

	return 0;
}