fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. #define MAX_TRIES 3
  7. #define MAX_NAME 50
  8. #define ACCOUNT_FILE "account.txt"
  9. #define TRANSACTION_FILE "transactions.txt"
  10.  
  11. // Structure to hold account information
  12. typedef struct {
  13. int accountNumber;
  14. char name[MAX_NAME];
  15. int pin;
  16. float balance;
  17. } Account;
  18.  
  19. // Structure to hold a transaction record
  20. typedef struct {
  21. char type[10]; // "Deposit" or "Withdrawal"
  22. float amount;
  23. char date[11]; // Format: DD/MM/YYYY
  24. } Transaction;
  25.  
  26. // Function prototypes
  27. int login(Account *acc);
  28. void showMenu();
  29. void checkBalance(Account acc);
  30. void deposit(Account *acc);
  31. void withdraw(Account *acc);
  32. void miniStatement();
  33. void changePIN(Account *acc);
  34. void logTransaction(const char *type, float amount);
  35. void loadAccount(Account *acc);
  36. void saveAccount(Account acc);
  37.  
  38. // Entry point of the program
  39. int main() {
  40. Account user;
  41. loadAccount(&user);
  42.  
  43. int attempts = 0;
  44. int loggedIn = 0;
  45. while (attempts < MAX_TRIES && !loggedIn) {
  46. loggedIn = login(&user);
  47. if (!loggedIn) {
  48. printf("Incorrect PIN. Try again.\n");
  49. attempts++;
  50. }
  51. }
  52. if (!loggedIn) {
  53. printf("Too many failed attempts. Session terminating.\n");
  54. return 0;
  55. }
  56.  
  57. int choice;
  58. do {
  59. showMenu();
  60. printf("Enter your choice: ");
  61. scanf("%d", &choice);
  62. switch (choice) {
  63. case 1: checkBalance(user); break;
  64. case 2: deposit(&user); break;
  65. case 3: withdraw(&user); break;
  66. case 4: miniStatement(); break;
  67. case 5: changePIN(&user); break;
  68. case 6:
  69. printf("Thank you for using the ATM. Goodbye!\n");
  70. break;
  71. default:
  72. printf("Invalid choice. Please try again.\n");
  73. }
  74. } while (choice != 6);
  75.  
  76. return 0;
  77. }
  78.  
  79. // Reads account information from file into 'acc'
  80. void loadAccount(Account *acc) {
  81. FILE *file = fopen(ACCOUNT_FILE, "r");
  82. if (file == NULL) {
  83. // If file does not exist, create a default user
  84. acc->accountNumber = 12345;
  85. strcpy(acc->name, "Huzaifah");
  86. acc->pin = 1234;
  87. acc->balance = 5000.0f;
  88. saveAccount(*acc);
  89. } else {
  90. fscanf(file, "%d,%49[^,],%d,%f",
  91. &acc->accountNumber, acc->name, &acc->pin, &acc->balance);
  92. fclose(file);
  93. }
  94. }
  95.  
  96. // Writes updated account information back to file
  97. void saveAccount(Account acc) {
  98. FILE *file = fopen(ACCOUNT_FILE, "w");
  99. if (file != NULL) {
  100. fprintf(file, "%d,%s,%d,%.2f\n",
  101. acc.accountNumber, acc.name, acc.pin, acc.balance);
  102. fclose(file);
  103. }
  104. }
  105.  
  106. // Handles user login by comparing entered PIN to stored PIN
  107. int login(Account *acc) {
  108. int inputPIN;
  109. printf("Enter your 4-digit PIN: ");
  110. scanf("%d", &inputPIN);
  111. return inputPIN == acc->pin;
  112. }
  113.  
  114. // Displays the main menu options
  115. void showMenu() {
  116. printf("\n========= ATM MENU =========\n");
  117. printf("1. Check Balance\n");
  118. printf("2. Deposit Money\n");
  119. printf("3. Withdraw Money\n");
  120. printf("4. Mini Statement\n");
  121. printf("5. Change PIN\n");
  122. printf("6. Exit\n");
  123. printf("============================\n");
  124. }
  125.  
  126. // Displays the current account balance
  127. void checkBalance(Account acc) {
  128. printf("Your current balance is ₹%.2f\n", acc.balance);
  129. }
  130.  
  131. // Processes a deposit transaction
  132. void deposit(Account *acc) {
  133. float amount;
  134. printf("Enter amount to deposit: ₹");
  135. scanf("%f", &amount);
  136. if (amount > 0) {
  137. acc->balance += amount;
  138. saveAccount(*acc);
  139. logTransaction("Deposit", amount);
  140. printf("Deposit successful. New balance: ₹%.2f\n", acc->balance);
  141. } else {
  142. printf("Invalid amount. Please enter a positive value.\n");
  143. }
  144. }
  145.  
  146. // Processes a withdrawal transaction
  147. void withdraw(Account *acc) {
  148. float amount;
  149. printf("Enter amount to withdraw: ₹");
  150. scanf("%f", &amount);
  151. if (amount > 0 && amount <= acc->balance) {
  152. acc->balance -= amount;
  153. saveAccount(*acc);
  154. logTransaction("Withdrawal", amount);
  155. printf("Please collect your cash. New balance: ₹%.2f\n", acc->balance);
  156. } else {
  157. printf("Error: Insufficient balance or invalid amount.\n");
  158. }
  159. }
  160.  
  161. // Displays the last 5 transactions from the transaction file
  162. void miniStatement() {
  163. FILE *file = fopen(TRANSACTION_FILE, "r");
  164. if (file == NULL) {
  165. printf("No transactions found.\n");
  166. return;
  167. }
  168. // Read all transactions into memory
  169. Transaction records[100];
  170. int count = 0;
  171. while (fscanf(file, "%9[^,],%f,%10s\n",
  172. records[count].type,
  173. &records[count].amount,
  174. records[count].date) == 3 && count < 100) {
  175. count++;
  176. }
  177. fclose(file);
  178.  
  179. printf("----- Mini Statement -----\n");
  180. int start = (count < 5) ? 0 : count - 5;
  181. for (int i = count - 1; i >= start; i--) {
  182. printf("%s ₹%.2f on %s\n",
  183. records[i].type, records[i].amount, records[i].date);
  184. }
  185. printf("--------------------------\n");
  186. }
  187.  
  188. // Allows the user to change their PIN securely
  189. void changePIN(Account *acc) {
  190. int oldPIN, newPIN, confirmPIN;
  191. printf("Enter your current PIN: ");
  192. scanf("%d", &oldPIN);
  193. if (oldPIN != acc->pin) {
  194. printf("Incorrect current PIN.\n");
  195. return;
  196. }
  197. printf("Enter your new PIN: ");
  198. scanf("%d", &newPIN);
  199. printf("Confirm your new PIN: ");
  200. scanf("%d", &confirmPIN);
  201. if (newPIN == confirmPIN && newPIN != oldPIN) {
  202. acc->pin = newPIN;
  203. saveAccount(*acc);
  204. printf("PIN changed successfully.\n");
  205. } else {
  206. printf("PIN change failed. PINs did not match or new PIN equals old PIN.\n");
  207. }
  208. }
  209.  
  210. // Appends a transaction record to the transaction log file
  211. void logTransaction(const char *type, float amount) {
  212. FILE *file = fopen(TRANSACTION_FILE, "a");
  213. if (file != NULL) {
  214. time_t t = time(NULL);
  215. struct tm tm = *localtime(&t);
  216. char date[11];
  217. snprintf(date, sizeof(date), "%02d/%02d/%04d",
  218. tm.tm_mday, tm.tm_mon + 1, tm.tm_year + 1900);
  219. fprintf(file, "%s,%.2f,%s\n", type, amount, date);
  220. fclose(file);
  221. }
  222. }
  223.  
  224.  
Success #stdin #stdout 0.01s 5292KB
stdin
/*  Berechnung des Hamming-Abstandes zwischen zwei 128-Bit Werten in 	*/
/*	einer Textdatei. 													*/
/*  Die Werte müssen auf einer separaten Zeile gespeichert sein			*/
/* 																		*/
/*	Erstellt: 17.5.2010													*/
/*  Autor: Thomas Scheffler												*/

#include <stdio.h>
#include <stdlib.h>

#define ARRAY_SIZE 32

unsigned Hamdist(unsigned x, unsigned y)
{
  unsigned dist = 0, val = x ^ y;
 
  // Count the number of set bits
  while(val)
  {
    ++dist; 
    val &= val - 1;
  }
 
  return dist;
}



int main (void)
{
	char hex;
	int i;
	int a[ARRAY_SIZE];
	int b[ARRAY_SIZE];
	int hamDist = 0;
	FILE* fp;
	
	//Arrays mit 0 initialisieren
	for (i = 0; i < ARRAY_SIZE; ++i)
	{
  		a[i] = 0;
  		b[i] = 0;
	}

	
	fp = fopen("hex.txt","r");
	if (fp == NULL) 
	{
		printf("Die Datei hex.txt wurde nicht gefunden!");
		exit(EXIT_FAILURE);
	}

	i=0;
	printf("1.Zeile einlesen.\n");

 	while((hex=fgetc(fp))!='\n' && hex != EOF)
    {
        a[i]=strtol(&hex,0,16);
		i++;
    }
	i=0;
	printf("2.Zeile einlesen.\n");

 	while((hex=fgetc(fp))!='\n' && hex != EOF)
    {
    	b[i]=strtol(&hex,0,16);
        i++;
    }
	fclose(fp);

	printf("Hamming-Abweichung pro Nibble:\n");
	for (i = 0; i < ARRAY_SIZE; ++i)
	{
		printf ("%i\t%i\t%i\n",a[i],b[i],Hamdist(a[i],b[i]));
		hamDist += Hamdist(a[i],b[i]);
	}
	printf ("\nHamming-Abweichung der Hash-Werte:%d\n",hamDist);
}

stdout
Enter your 4-digit PIN: Incorrect PIN. Try again.
Enter your 4-digit PIN: Incorrect PIN. Try again.
Enter your 4-digit PIN: Incorrect PIN. Try again.
Too many failed attempts. Session terminating.