giúp mình về phần con trỏ với
có 1 bài với nd là nhập các giá trị sau đó sửa lại các giá trị cần sửa rồi in ra mình làm đc bước 1 với bước 3 còn bước 2 về con trỏ ko làm đc bạn nào giúp mình với
code mình dang làm
Code:
#include <conio.h>
#include <stdio.h>
#define MAX 20
//look for item[index] = code and return index if found, otherwise -1
int find(int item[], int no, int code)
{
int i, rc = -1;
for (i=0; i < no && rc == -1; i++)
if(item[i] == code) rc = i;
return rc;
}
// enter item list: no more than MAX items
int input_item(int item[], double unit_cost[], int no)
{
int i, keep = 1;
if(no == MAX) {printf("No more than %d items", MAX); return no;}
printf("Enter Item\n");
printf("=================================\n");
do
{
printf("Enter Item code (0 to exit):");
scanf("%d", &item[no]);
if (item[no] == 0) keep = 0;
else
{
if(find(item, no, item[no]) != -1)
printf("**It exits\n**Enter again\n");
else {printf("Enter Unit cost:");
scanf("%lf", &unit_cost[no]);
no++;}
}
}while(keep == 1);
return no;
}
// view item list
void view_item(int item[], double unit_cost[], int no)
{
int i;
printf("Item list\n");
printf("=================================\n");
printf(" Item code Unit cost\n");
printf("---------- ----------\n");
for(i = 0; i < no; i++)
printf(" %03d %10.2lf\n", item[i], unit_cost[i]);
}
//view invoice
void view_invoice(int item_code[], double item_cost[], int quantity[], int no)
{
double sum = 0;
printf("Invoice list\n");
printf("=================================\n");
printf(" Item code Unit cost quantity Amount\n");
printf("---------- ---------- ---------- ----------\n");
for(int i = 0; i < no; i++){
printf(" %03d %10.2lf %10d %10.2lf\n", item_code[i], item_cost[i], quantity[i], item_cost[i]*quantity[i]);
sum += item_cost[i]*quantity[i];}
printf("---------- ---------- ---------- ----------\n");
printf(" TOTAL %10.2lf\n", sum);
}
//input invoice: no more than MAX items
void input_invoice(int item[], double unit_cost[], int no)
{
int i, n=0, j, item_code[MAX], quantity[MAX], keep = 1;
double item_cost[MAX];
printf("Enter Invoice\n");
printf("=================================\n");
do
{
printf("Enter Item code (0 to exit):");
scanf("%d", &item_code[n]);
if (item_code[n] == 0) keep = 0;
else
{
i = find(item, no, item_code[n]);
if (i == -1) {
printf("**It doesn't exist\n");
printf("**Enter again :\n");}
else
{
printf("Enter Quantity :");
scanf("%d", &quantity[n]);
item_cost[n] = unit_cost[i];
n++;
}
}
} while(keep == 1 && n < MAX);
view_invoice(item_code, item_cost, quantity, n);
}
main()
{
int choice, no=0;
int item[MAX];
double unit_cost[MAX];
do
{
clrscr();
printf("1) Enter Item\n");
printf("2) Enter Invoice\n");
printf("3) Exit\n");
printf("Please select 1, 2, or 3:");
scanf("%d", &choice);
switch(choice)
{
case 1: no = input_item(item,unit_cost,no);
view_item(item, unit_cost, no);
getch();
break;
case 2: input_invoice(item, unit_cost, no);
getch();
break;
}
}while(choice != 3);
}
các bạn giúp mình với còn đây là đề bài
Code:
MAIN MENU
--------------------------
1) Enter a Transaction
2) Edit a Transaction by No
3) View Transaction journal
4) Exit
Please select 1, 2, 3, or 4: 1
Nhập giá trị
Code:
Transaction No 1
Enter a transaction type (1 for Purchase, 2 for Sale) : 1
Enter a item code : 001
Enter unit cost : -3
**Value must be greater than 0
**Enter again : 3.00
Enter quantity : 10
sửa giá trị cần sửa (phần cần giúp đỡ)
Code:
Enter No of transaction: 1
Type Item code Unit cost Quantity
Purchase 001 3.00 10
Edit Menu
---------
1) Type
2) Item code
3) Unit cost
4) Quantity
5) Exit editing
Please select 1, 2, 3, 4, or 5: 4
Enter Quantity : -30
**Value must be greater than 0
**Enter again : 30
Edit Menu
---------
1) Type
2) Item code
3) Unit cost
4) Quantity
5) Exit editing
Please select 1, 2, 3, 4, or 5: 5
MAIN MENU
--------------------------
1) Enter a Transaction
2) Edit a Transaction by No
3) View Transaction journal
4) Exit
Please select 1, 2, 3, or 4: 3
cuối cùng in ra
Code:
Transaction journal
No Transactions Item Unit cost Units purchased Units sold -- ------------- ----- ---------- --------------- -------------
1 Purchase 001 3.0 30
2 Purchase 001 5.0 20
3 Sale 001 30
4 Purchase 002 1.0 20
5 Sale 002 10