Files
ikcu-yzm/YZM502/final-soru3.c
2023-04-04 02:24:59 +03:00

189 lines
4.0 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* Bu basit uygulama bir kullanıcıının forex işlemlerinin
* kaydının tutulmasını ve sorgulanmasını simüle etmektedir
*/
#include <stdio.h>
#include <time.h>
#include <stdint.h>
#include <string.h>
#define DOSYA_ADI "data"
struct fv {
struct tm tarih;
char dvz[4];
char islemTur;
float miktar;
float kur;
} typedef fiyatveri;
void flushStdInBuffer() {
scanf("%*[^\n]");
scanf("%*c");
}
int tarihAl(struct tm *date) {
char buffer[20];
memset(date, 0, sizeof(*date));
// memset(buffer, 0, sizeof(buffer));
printf("\nİşlem Tarihini Giriniz (gg/aa/yy): ");
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
perror("Hata: ");
return -1;
}
if (sscanf(buffer, "%d/%d/%d", &date->tm_mday, &date->tm_mon, &date->tm_year) == 3) {
if (date->tm_mday > 31 || date->tm_mday < 1 || date->tm_mon > 12 || date->tm_mon < 1) {
fprintf(stderr, "Hatalı tarih değeri\n");
return -1;
}
}
return 0;
}
int dvzAl(char *dvz) {
memset(dvz, 0, 4);
printf("Döviz Türü (usd/eur/gbp): ");
if (fgets(dvz, 4, stdin) == NULL) {
perror("Hata: ");
return -1;
}
return 0;
}
int dvzKontrolEt(char *dvz) {
char *baz[] = {"usd", "gbp", "eur", "btc"};
int len = sizeof(baz) / sizeof(baz[0]);
int i;
for (i = 0; i < len; i++) {
if (strcmp(baz[i], dvz) == 0) {
return 1;
}
}
return 0;
}
int yaz(fiyatveri v) {
FILE *pDosya;
pDosya = fopen(DOSYA_ADI, "a+");
if (pDosya == NULL) {
pDosya = fopen(DOSYA_ADI, "w");
if (pDosya == NULL) {
perror("Hata: ");
return -1;
}
}
fwrite(&v, sizeof(v), 1, pDosya);
fclose(pDosya);
return 0;
}
int islemGir() {
int rc;
fiyatveri v;
// tarihi alalım
rc = tarihAl(&v.tarih);
if (rc != 0) {
return rc;
}
// döviz cinsi
do {
rc = dvzAl(v.dvz);
if (rc != 0) {
return rc;
}
flushStdInBuffer();
} while (dvzKontrolEt(v.dvz) == 0);
do {
printf("İşlem Türü ( [a]lış / [s]atış ): ");
scanf("%c%*c", &v.islemTur);
} while (v.islemTur != 97 && v.islemTur != 115);
do {
printf("İşlem Kuru: ");
scanf("%f%*c", &v.kur);
} while (v.kur <= 0);
do {
printf("İşlem Miktaı: ");
scanf("%f%*c", &v.miktar);
} while (v.miktar <= 0);
rc = yaz(v);
if (rc != 0) {
return rc;
}
return 0;
}
void islemListele() {
FILE *pDosya;
fiyatveri v;
pDosya = fopen(DOSYA_ADI, "a+");
if (pDosya == NULL) {
printf("Veri dosyası okunamadı. Hiç veri girilmemiş olabilir");
return;
}
printf("Tarih\t\tDvz\t\tİşlem\tKur\t\t\tİşlem Miktarı\n");
while (fread(&v, sizeof(v), 1, pDosya) == 1) {
printf("%d/%d/%d", v.tarih.tm_mday, v.tarih.tm_mon, v.tarih.tm_year);
printf("\t%s", v.dvz);
printf("\t\t %c", v.islemTur);
printf("\t\t%.4f", v.kur);
printf("\t\t%.2f", v.miktar);
printf("\n");
}
}
int menu() {
int secim = -1;
printf("\n\n-------------------------------------------------\n");
printf("(1) İşlem girişi \n");
printf("(2) İşlemleri Listele \n");
printf("ÇIKIŞ İÇİN : 0 \n");
printf("-------------------------------------------------\n\n");
printf("Seçiminizi Giriniz: ");
do {
scanf("%d%*c", &secim);
if (secim < 0 || secim > 3) {
printf("\nYanlış değer girdiniz tekrar giriş yapınız: ");
}
} while (secim < 0 || secim > 3);
return secim;
}
int main() {
int secim = 0;
int rc;
do {
rc = 0;
secim = menu();
switch (secim) {
case 1:
islemGir();
break;
case 2:
islemListele();
break;
case 3:
//kokluFormul();
break;
case 0:
return 0;
}
} while (1 < 2);
}