Project (CSIT114)
Assignment Date: 06.01.2006
Submit Date : 23.01.2006
THE PROJECT SHOULD BE SUBMITTED DURING THE FINAL WEEK BOTH ON FLOPPY AND PAPER.
Write a C program for a Company called stock.cpp that uses 5 functions,
creat_file(), (first create the file that the user will insert information about stock)
insert_info(), (the user will input stock information(stockName, stockNumber, buyPrice, currentPrice))
list_info() (list the original content of the file)
list_info1() (list the the file with deleted stock)
delete_info(). (delete stock from the file)
Your program should display a Menu as Follows:
Stock Menu
C – Create File
A – Add new stock
D – Delete stocks
O – List file content (without deleted stock)
L – List file content (with deleted stock)
Q – Quit
Your choice:
DATA TYPES of the variables
Stock name | 1-D String (max. 15 characters) |
Stock number | 1-D String (max. 5 characters) |
Current_price | Float number (2 digits after the decimal number) |
Buy_price | Float number (2 digits after the decimal number) |
The formulae to calculate the current price:
current_price = buy_price / 11
MY SOLUTION
#include <stdio.h>
#include <string.h>
void creat_file(void);
void insert_info(void);
void list_info(void);
void list_info1(void);
void delete_info(void);
main()
{
printf(”
Stock Menu
——————
“);
char secenek;
printf(” C – Create File
A – Add New Stock
D – Delete Stocks
O – List File Content(without deleted stock)
L – List File Content(with deleted stock)
Q – Quit
“);
printf(“Your Choice : “);
scanf(“%c”,&secenek);
if(secenek==’c’)
creat_file();
else if(secenek==’a’)
insert_info();
else if(secenek==’d’)
delete_info();
else if(secenek==’o’)
list_info1();
else if(secenek==’l’)
list_info();
else if(secenek==’q’)
printf(“Program Stopped”);
else
printf(“Bad Command ! Please use lowercase letters”);
}
//I am creating the file here
void creat_file()
{
FILE *yol;
if ( ( yol = fopen( “database.txt”, “a+” ) ) == NULL ) {
printf( “File could not be opened
” );
}
else{
printf(“File created SUCCESSFULLY”);
}
rewind(yol);
fclose(yol);
}
// here is inserting info function definition
void insert_info()
{
FILE *yol;
yol = fopen( “database.txt”, “a+” );
char stockName[15];
char stockNumber[5];
float current;
float buyprice;
printf(”
Enter the Stock Number : “);
scanf(“%s”,&stockNumber);
printf(“Enter the Stock Name : “);
scanf(“%s”,&stockName);
printf(”
Enter the Buy Price : “);
scanf(“%f”,&buyprice);
current=buyprice/11;
while ( !feof( stdin ) ) {
fprintf( yol, “%s %s %.2f %.2f
“, stockNumber, stockName, current, buyprice );
printf(”
Enter the Stock Number : “);
scanf(“%s”,&stockNumber);
printf(“Enter the Stock Name : “);
scanf(“%s”,&stockName);
printf(”
Enter the Buy Price : “);
scanf(“%f”,&buyprice);
current=buyprice/11;
}
rewind(yol);
fclose(yol);
}
void delete_info()
{
FILE *yol;
yol = fopen( “database.txt”, “a+” );
FILE *yol2;
yol2 = fopen( “del.txt”, “w+”);
char number[5];
char str[60]={0};
printf(“Which record do you want to delete? Enter the Stock Number : “);
scanf(“%s”,&number);
int x;
x=strlen(number);
while(!feof(yol))
{
if(strncmp(str,number,x)!=0)
{
fputs(str,yol2);
}
fgets(str,60,yol);
}
printf(“%s . stock successfully DELETED”,number);
rewind(yol2);
rewind(yol);
fclose(yol);
fclose(yol2);
}
void list_info()
{
FILE *yol;
yol = fopen( “database.txt”, “a+” );
char deneme[100]={0};
printf(“No Name CP BP
—————
“);
while(!feof(yol))
{
printf(“%s”,deneme);
fgets(deneme,100,yol);
}
rewind(yol);
fclose(yol);
}
void list_info1()
{
FILE *yol2;
yol2 = fopen( “del.txt”, “a+” );
char deneme[100]={0};
printf(“No Name CP BP
—————
“);
while(!feof(yol2))
{
printf(“%s”,deneme);
fgets(deneme,100,yol2);
}
rewind(yol2);
fclose(yol2);
}
Kaynak: Csit114 course
belgesi-614