int main () { bank b1 = {3876, "checkings account", 1000}; bank b2 = {9845, "savings account", 9000}; b1 = deposit(b1, 100); printf ("After your deposit, this is the new account balance for account %d: %f\n", b1.accountNumber, b1.accountBalance); b1 = withdraw(b1, 400); printf ("After your withdrawl, this is the new account balance for account %d: %f\n", b1.accountNumber, b1.accountBalance); b2 = deposit(b2, 5000); printf ("After your deposit, this is the new account balance for account %d: %f\n", b2.accountNumber, b2.accountBalance); b2 = withdraw(b2, 690); printf ("After your withdrawl, this is the new account balance for account %d: %f\n", b2.accountNumber, b2.accountBalance); }
TEST(test_bank_account, test_deposit) { srand(time(0)); auto amount1 = rand() / (RAND_MAX / 100.0); auto amount2 = rand() / (RAND_MAX / 100.0); auto a = BankAccount("Roope Rikas", "12345", amount1); auto b = BankAccount("Kaisa Köyhä", "53457", amount2); auto deposit1 = rand() / (RAND_MAX / 100.0); auto deposit2 = rand() / (RAND_MAX / 100.0); a.deposit(deposit1); b.deposit(deposit2); EXPECT_EQ(amount1+deposit1, a.getBalance()); EXPECT_EQ(amount2+deposit2, b.getBalance()); }
static void *moneyguy(void *amountptr) { int amount = *(int*)amountptr; char name[15]; sprintf(name, "moneyguy %d", amount); notify("born\n", name); while (1){ pthread_mutex_lock(&acc1.mutex); while ((acc1.max - acc1.balance) < amount){ /* not enough space to deposit */ sleepedguy++; notify("go sleep\n", name); /* sleep and drop mutex in a atomic functon */ pthread_cond_wait(&acc1.cond_full, &acc1.mutex); /* here mutex is automatically aquired again */ notify("waked!\n", name); } deposit(name, amount, &acc1); pthread_mutex_unlock(&acc1.mutex); adv_sleep(deposit_time * (double) random() / RAND_MAX); sleep(0.1 * ((double) random()) / RAND_MAX); } }
int main(int argc, char *argv[]) { char p[30]; unsigned int r=0 ; int i=0,j; int check=0; strcpy(p,argv[1]); //while(check < 2) //{ //strcat(p,"A"); //check++; for(j=2;j<argc;j++) { i=atoi(argv[j]); message m1; r |= (1<<i); m1.m1_i1 = (int)r; m1.m1_p1 = p; deposit(m1); } //} return(0); }
void BrassPlus::withDraw(double amt) { // set up ###.## format format initialState = setFormat(); precies prec = cout.precision(2); double temp; if (amt < 0) { cout << "can not withdraw litter than 0!"; } else if (amt <= getBalance()) { Brass::withDraw(amt); } else { if (amt < getBalance() + maxLoan - owesBank) { temp = amt - getBalance(); owesBank += temp * (1 + rate); deposit(temp); Brass::withDraw(amt); } else { cout << "your balance and loan is litter than withdraw!" << endl; } } restore(initialState, prec); }
void run_wdune() // run { int t_poll = 0; // poll counter variable while (t_poll < (ncols * nrows)) { poll_flag=true; update_surfPrev(poll_flag); picksite_ero(); // pick a site to erode from if (ero_flag) // flag is true if the site is good for erosion { surf[i_ero][j_ero]--; // remove a slab off the erosion site ////////////////////////////////////////////////////////// avalanche_up(i_ero, j_ero); // avalanche up after removing the sand ////////////////////////////////////////////////////////// picksite_depo(i_ero, j_ero); // pick a site to deposit the sand deposit(i_depo, j_depo); // put a slab of sand onto the deposition site } update_sinTime(); // update the time slabs had to sinter t_poll++; // advance the poll counter } newSandEngine(); // add some new sand if required }
/** * Forks processes based on the type of process * that the user wants to occur */ void process_fork(int deposit_or_withdraw, int request) { pid_t child_pid; child_pid = fork(); // Fork failed if (child_pid == -1) { perror("Failed to fork process!\n"); exit(EXIT_FAILURE); } // We have a child process else if (child_pid == 0) { // Withdraw if (deposit_or_withdraw == WITHDRAW) { withdraw(request); } // Deposit else if (deposit_or_withdraw == DEPOSIT) { deposit(request); } else { printf("Invalid process type. Choose Withdraw or Deposit.\n"); exit(EXIT_FAILURE); } } // Parent else { return; } }
void search_and_insert(char* chunk, unsigned int size, ringbuffer_t* buf) { char result[RESULTSIZE]; unsigned int resultlen = 0; char* found; unsigned int start; /*fuer jeden moeglichen String*/ for(start = 0; start < size; ++start) { unsigned int offs; /* fuer jeden moeglichen vergleichbaren String */ for(offs = MINLEN; (offs < RESULTSIZE) && (start + offs < size); ++offs) { /* Gemeinsamkeiten Suchen */ resultlen = 0; while(chunk[start + resultlen] == chunk[start + offs + resultlen]) { result[resultlen] = chunk[start + resultlen]; resultlen++; } /* mehr als 10 gemeinsame Zeichen? */ if(resultlen >= 10) { /* Ergebnis in Puffer einfuegen */ found = (char*) malloc((RESULTSIZE) * sizeof(char)); memcpy(found, result, resultlen); found[resultlen] = '\0'; deposit(buf, found); /* hinter Wiederholung weitersuchen*/ start += resultlen; break; } } } }
/** * Transfer from Bank function * Transfers desired amount from bank * @param Checking amount - Checking object to transfer to * @param dollars - dollars to transfer to Checking * @param cents - cents to transfer to Checking */ void Checking::transfer_from_Bank(Checking &amount, Bank &amount2, int &dollars, int ¢s) { //calls withdrawal to deduct amount deposit(amount, dollars, cents); //calls banks deposit function to add the funds to the bank object Bank::withdrawal(amount2, dollars, cents); }
int main(){ char name; printf("What is your name"); scanf("%s", &name); printf("Would you like to deposit(type 1) or withdraw (type 2)\n"); bank bank1 = {1, name, 600.0}; bank bank2 = {2, name, 500.0}; int response; scanf("%d", &response); if(response == 1){ float depo; printf("How much are you depositing"); scanf("%f", &depo); deposit(depo, bank1); } if(response ==2){ float with; printf("How much are you withdraw"); scanf("%f",&with); withdraw(bank1,with); } printf("%f",bank1.balance); }
void transfer(int fromB, int toB, int account, int value) { if (fromB < toB) { pthread_mutex_lock(&(Bank[fromB].b_lock)); pthread_mutex_lock(&(Bank[toB].b_lock)); } else if (fromB > toB) { pthread_mutex_lock(&(Bank[toB].b_lock)); pthread_mutex_lock(&(Bank[fromB].b_lock)); } else return; int money = withdraw(fromB, account, value); if (money > 0) deposit(toB, account, money); if (fromB < toB) { pthread_mutex_unlock(&(Bank[toB].b_lock)); pthread_mutex_unlock(&(Bank[fromB].b_lock)); } else if (fromB > toB) { pthread_mutex_unlock(&(Bank[fromB].b_lock)); pthread_mutex_unlock(&(Bank[toB].b_lock)); } }
void run_receiver(Node *store, BankConfig config) { int buf_size = config.bank_max_msg_length * 2; char buf[buf_size]; struct sockaddr_in server, client; int sfd = socket(AF_INET, SOCK_DGRAM, 0); int sfd_ds = socket(AF_INET, SOCK_DGRAM, 0); int len = 0; bzero(&server, sizeof(server)); server.sin_family = AF_INET; server.sin_port = htons(config.bank_port); inet_aton("0.0.0.0", &server.sin_addr); log_debug("bind=%d", bind(sfd, (struct sockaddr *) &server, sizeof(server))); socklen_t l_client = sizeof(client); int count = 0; while (1) { count++; len = recvfrom(sfd, buf, buf_size, 0, (struct sockaddr *) &client, &l_client); buf[len] = '\0'; log_info("MESSAGE FROM CLIENT: %s", buf); deposit(buf, store); if (count >= config.bank_queue_limit) { log_debug("SEND FROM RECEIVER."); send_to_downstream(store, sfd_ds, config); count = 0; } } }
void Lattice::doKMC() { ///create and save random number float ranX; ranX=ranlist[iran]; iran++; float Trate,Drate; Drate=.25*mcount*difrate; Trate=Drate+(deprate* (float) latsize); float prob=(Drate/Trate); //cout<<"ranX="<<ranlist[iran]<<endl; if(ranX<prob) diffuse(); else { deposit(); ndep++; } nevent++; }
int main () { Account account1; Account account2; account1.balance = 1000; account2.balance = 5000; strcpy (account1.name, "John"); strcpy (account2.name, "Bob"); account1.number = 3000; account2.number = 2000; printf ("%s your balance is %d\n", account1.name, account1.balance); printf ("%s your balance is %d\n", account2.name, account2.balance); account1.balance = deposit (account1, 3); account2.balance = deposit (account2, 5); account1.balance = withdraw (account1, 10); account2.balance = withdraw (account2, 15); return 0; }
main() { struct Account person; person = fill("mike", 100, 1000.00); print(person); person = deposit(person,50.00); print(person); }
/************************************************************************* * Base worker routine * * Child process looks at req_buf to determine requested operation. * Executes assoicated account routine, sends response back to client. *************************************************************************/ void process_request(char *req_buf, int conn) { char resp_buf[COMM_BUF_SIZE]; int trans_id; /* The transaction type of the request */ sscanf(req_buf, "%d", &trans_id); DPRINTF(("SERVER: child process %d, processing request \n", child_pid)); /* "p()" num active semaphore */ num_active_sem_buf.sem_num = 0; num_active_sem_buf.sem_op = -1; /* "p()" */ num_active_sem_buf.sem_flg = SEM_UNDO; if (semop(num_active_sem_id, &num_active_sem_buf, 1) < 0) { perror("SERVER: child process, num_active p semop() failed %d\n"); exit (1); } switch(trans_id) { case OPEN_ACCT_TRANS: open_account(resp_buf); break; case DEPOSIT_TRANS: deposit(req_buf, resp_buf); break; case WITHDRAW_TRANS: withdraw(req_buf, resp_buf); break; case BALANCE_TRANS: balance(req_buf, resp_buf); break; default: handle_bad_trans_id(req_buf, resp_buf); break; } server_comm_send_response(conn, resp_buf); DPRINTF(("SERVER: child process %d, response sent\n", child_pid)); /* "v()" num active semaphore */ num_active_sem_buf.sem_num = 0; num_active_sem_buf.sem_op = 1; /* "v()" */ num_active_sem_buf.sem_flg = SEM_UNDO; if (semop(num_active_sem_id, &num_active_sem_buf, 1) < 0) { perror("SERVER child process, num_active v semop() failed %d\n"); exit (1); } exit(0); }
// simulate id performing 1000 transactions void do1000Transactions(unsigned long id) { for (unsigned i = 0; i < 1000; i++) { if ( odd( id ) ) { deposit(100.00); // odd threads deposit } else { withdraw(100.00); // even threads withdraw } } }
void displayAccounts(int *aPtr, float *irPtr, float *iaPtr, int accountNum) { int choice = 0; printf("Account#: %d\n",accountNum+1); printf("\tBalance:\t%d\n",*(aPtr+accountNum)); printf("\tInterest Rate:\t%.2f\n",*(irPtr+accountNum)); printf("\tInterest:\t%.2f\n",*(iaPtr+accountNum)); printf("Select one of the following options:\n"); printf("1 - Deposit\n"); printf("2 - Withdraw\n"); printf("3 - Done\n"); scanf("%d",&choice); while(choice<1 || choice >3) { printf("You have entered an invalid option. Please choose again.\n"); printf("Please select one of the following options:\n"); printf("1 - Deposit\n"); printf("2 - Withdraw\n"); printf("3 - Done\n"); scanf("%d",&choice); } if(choice==1) { deposit(aPtr, accountNum); calculateInterest(aPtr, irPtr, iaPtr, accountNum); printf("Account#: %d\n",accountNum+1); printf("\tBalance:\t%d\n",*(aPtr+accountNum)); printf("\tInterest Rate:\t%.2f\n",*(irPtr+accountNum)); printf("\tInterest:\t%.2f\n",*(iaPtr+accountNum)); } else if(choice==2) { withdraw(aPtr, accountNum); calculateInterest(aPtr, irPtr, iaPtr, accountNum); printf("Account#: %d\n",accountNum+1); printf("\tBalance:\t%d\n",*(aPtr+accountNum)); printf("\tInterest Rate:\t%.2f\n",*(irPtr+accountNum)); printf("\tInterest:\t%.2f\n",*(iaPtr+accountNum)); } else { printf("Account#: %d\n",accountNum+1); printf("\tBalance:\t%d\n",*(aPtr+accountNum)); printf("\tInterest Rate:\t%.2f\n",*(irPtr+accountNum)); printf("\tInterest:\t%.2f\n",*(iaPtr+accountNum)); } return; }
void *petar(void *arg) { int i; for(i = 0; i < 1000000; i++) { if(rand() % 1000 <= 500) { deposit(rand() % 10000, &petar_account); } else { withdraw(rand() % 10000, &petar_account); } } }
void Chemistry::deposit (const IM& im, Treelog& msg) { const Unit& unit = units.get_unit (Chemical::surface_flux_unit ()); for (IM::const_iterator i = im.begin (); i != im.end (); i++) { const symbol chem = *i; const double flux = im.get_value (chem, unit); deposit (chem, flux, msg); } }
void *test(void *data) { int rand_max; thread_data_t *d = (thread_data_t *)data; unsigned short seed[3]; seeds = seed_rand(); //#ifdef __sparc__ phys_id = the_cores[d->id]; cluster_id = get_cluster(phys_id); //#else // phys_id = d->id; //#endif /* Initialize seed (use rand48 as rand is poor) */ seed[0] = (unsigned short)rand_r(&d->seed); seed[1] = (unsigned short)rand_r(&d->seed); seed[2] = (unsigned short)rand_r(&d->seed); rand_max = d->bank->size - 1; /* local initialization of locks */ local_th_data[d->id] = init_lock_array_local(phys_id, d->bank->size, the_locks); /* Wait on barrier */ barrier_cross(d->barrier); while (stop == 0) { uint32_t nb = (uint32_t) (my_random(&(seeds[0]),&(seeds[1]),&(seeds[2])) % 100); uint32_t acc = (uint32_t) my_random(&(seeds[0]),&(seeds[1]),&(seeds[2])) & rand_max; account_t* accp = &d->bank->accounts[acc]; if (nb < d->deposit_perc) { deposit(accp, 1, d->id); d->nb_deposit++; } else if (nb < d->withdraw_perc) { withdraw(accp, 1, d->id); d->nb_withdraw++; } else /* nb < balance_perc */ { check(accp, d->id); d->nb_balance++; } } /* Free locks */ //free_local(local_th_data[d->id], d->bank->size); return NULL; }
void Account::transfer( const int& to, const double& amount ) { //Transfer to checking account //Withdraw from savings, deposit to checkings if ( to == 0 ) { withdraw( 1, amount ); deposit( 0, amount ); } //Transfer to savings account //Withdraw from checking, deposit to saving else if ( to == 1 ) { withdraw( 0, amount ); deposit( 1, amount ); } return; }
void *ivan(void *arg) { int i; for(i = 0; i < 1000000; i++) { if(rand() % 2 <= 1) { deposit(rand() % 10000, &ivan_account); } else { withdraw(rand() % 10000, &ivan_account); } } }
void *consoleThread(void *args) { struct thread_info *tinfo; tinfo = (thread_info *) args; userDB *users = tinfo->users; // string that is printed to the console std::string sendStr; std::cout << ("bank ~ : "); for(std::string line; getline(std::cin, line);) { int index = 0; std::string command = advanceCommand(line, index); std::string username = advanceCommand(line, index); if (command.compare("balance") != 0 && command.compare("deposit") != 0) { sendStr = "Bank Commands: [deposit|balance] [username] <amount>"; } else if (!users->userExists(username)) { sendStr = "user \"" + username + "\" does not exist"; } else if(command.compare("deposit") == 0) { //deposit [username] [amount] Increase <username>’s balance by <amount> std::string amount = advanceCommand(line, index); int testNeg = atoi(amount.c_str()); bool isInt = true; for (int i = 0; i < amount.length(); i++) { if (!(amount[i] >= '0' && amount[i] <= '9')) { isInt = false; // std::cout << test[i] << " not a int!" << std::endl; } } if (!isInt) { sendStr = amount + " is not a number!!!"; } else if (testNeg < 0) { sendStr = "no negative deposits!!!"; } else { sendStr = "deposited " + amount; deposit(username, amount, users); } } else if(command.compare("balance") == 0) { //balance [username] Print the balance of <username> sendStr = balance(username, users); } std::cout << sendStr << std::endl; std::cout << ("bank ~ : "); } pthread_exit(NULL); // Ends the thread }
void Work(int hours, int hourlyPay) { while(1) { work(); wait(s); deposit(); if(bankAccount >= 649 && !isBought) { signal(t); isBought = true; } signal(s); } }
/** * Other threads can continue depositing if this thread goes to sleep. This * is good. */ void goodSession(int id, FILE *fp) { double amount; while(fscanf(fp, "%lf", &amount) != EOF) { if(amount < 1) { sleep(30); // Simulates user walking away from machine. } /* lock around smallest chunk of code possible. */ pthread_mutex_lock(&(myacct->mutex)); deposit(myacct, amount); // Uses shared myacct variable. pthread_mutex_unlock(&(myacct->mutex)); printf("\tThread [%d]: Deposited: %f\n", id, amount); fflush(stdout); } }
/** * Starves the other threads from depositing if this thread goes to sleep. */ void badSession(int id, FILE *fp) { double amount; /* This is not a good place to lock! */ pthread_mutex_lock(&(myacct->mutex)); while(fscanf(fp, "%lf", &amount) != EOF) { if(amount < 1) { sleep(30); // Simulates user walking away from machine. } deposit(myacct, amount); // Uses shared myacct variable. printf("\tThread [%d]: Deposited: %f\n", id, amount); fflush(stdout); } pthread_mutex_unlock(&(myacct->mutex)); }
void environment::start() { end(); m_time = 0; m_running = true; m_world.resize(settings::world_width, settings::world_height); m_world.generate(m_settings.seed()); spawn_player(m_world.spawn() * point2(settings::cell_width, settings::cell_height)); // ui tie_map(); m_ui->deactivate("title"); m_ui->activate("ingame"); // make chest auto chest = m_factory->produce(); chest->add_appearance('$', { 1, 1, 1 }); chest->add_location({ 2, 2 }); auto body = chest->add_body(250, 150); body->set_name("Iron chest"); chest->add_container(); m_terrain.add(*chest); auto ore = std::make_shared<body_component::item_type>(); ore->add({ rl::effect::ore_power, 0x00, 10 }); ore->add({ rl::effect::value, 0x00, 5 }); ore->add({ rl::effect::weight, 0x00, 1 }); ore->set_name("Copper ore"); ore->set_tag("ore_copper"); auto vein = m_factory->produce(); vein->add_appearance('0'); vein->add_location({ 3, 3 }); vein->add_body(); auto v = vein->add_resource(); v->deposit(ore); m_terrain.add(*vein); auto anvil = m_factory->produce(); anvil->add_appearance('t'); anvil->add_location({ 3, 0 }); anvil->add_body(); anvil->add_container(); m_terrain.add(*anvil); }
//Provided main function from the Stegman Textbook for this course from page 232 int main (void) { char code; double amount, service, balance; double amtCheck, amtDeposit, openBalance, closeBalance; int numCheck, numDeposit; if (!(fpIn = fopen("account.txt", "r"))) { printf("account.txt could not be opened for input."); exit(1); } if (!(fpOut = fopen("csis.txt", "w"))) { printf("csis.text could not be opened for output"); exit(1); } amount = 0.0; service = 0.0; balance = 0.0; amtCheck = 0.0; amtDeposit = 0.0; openBalance = 0.0; closeBalance = 0.0; numCheck = 0; numDeposit = 0; outputHeaders(); while (!feof(fpIn)) { fscanf(fpIn, "%c %lf\n", &code, &amount); if (code == 'I') { initialBalance(amount, &balance, &service, &openBalance); } else if (code == 'D') { deposit(amount, &balance, &service, &numDeposit, &amtDeposit); } else { check(amount, &balance, &service, &numCheck, &amtCheck); } } closeBalance = balance - service; outputSummary(numDeposit, amtDeposit, numCheck, amtCheck, openBalance, service, closeBalance); fclose(fpIn); fclose(fpOut); return 0; }
int terrainIterateParticleDeposition(int numIt) { int x,z,i,dir; if (terrainHeights == NULL) return TERRAIN_ERROR_NOT_INITIALISED; x = rand() % terrainGridWidth; z = rand() % terrainGridLength; for (i=0; i < numIt; i++) { iterationsDone++; dir = rand() % 4; if (dir == 2) { x++; if (x >= terrainGridWidth) x = 0; } else if (dir == 3){ x--; if (x == -1) x = terrainGridWidth-1; } else if (dir == 1) { z++; if (z >= terrainGridLength) z = 0; } else if (dir == 0){ z--; if (z == -1) z = terrainGridLength - 1; } if (terrainParticleMode == ROLL) deposit(x,z); else terrainHeights[x * terrainGridLength + z] += maxDisp; } return(TERRAIN_OK); }