当前位置:网站首页>It's the first time to write your own program in C language. If you have a boss, you can help a little
It's the first time to write your own program in C language. If you have a boss, you can help a little
2022-07-27 01:45:00 【Count of Jiangnan】
For the first time c Language to write their own programs
For the first time c Language to write their own programs
Is there anyone who can give some advice 0.0( Changed a mobile number , By the way, I copied the excerpts I wrote before )
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct goods
{
int num;
char name[20];
int price;
}gds;
jiemian()
{
printf("\t** Welcome to use ssh Commodity system **\n");
printf("\t************************\n");
printf("\t1. Enter clothing information \n");
printf("\t2. Display clothing information \n");
printf("\t3. Find clothing information \n");
printf("\t4. Insert clothing information \n");
printf("\t5. Delete clothing information \n");
printf("\t6. Sort clothes information \n");
printf("\t7. Exit the system \n");
printf("\t************************\n");
}
jiemian2()
{
printf("\t******* To find the way *********\n");
printf("\t************************\n");
printf("\t1. Find by number \n");
printf("\t2. Search by name \n");
printf("\t3. Find by price \n");
printf("\t************************\n");
}
void write_word(gds arr[],int n)
{
int i=0;
FILE *fp=NULL;
fp=fopen("E:\\123.txt","w");
if(fp==NULL)
{
printf("open error\n!");exit(0);}
while(i<n)
{
fprintf(fp,"%d\t",arr[i].num);
fprintf(fp,"%s\t",arr[i].name);
fprintf(fp,"%d\t",arr[i].price);
i++;
}
fclose(fp);
}
void read_word(gds arr[],int *n)
{
int i=0;
FILE *fp=NULL;
fp=fopen("E:\\123.txt","r");
if(fp==NULL){
printf("open error!\n");exit(0);}
while(!feof(fp))
{
fscanf(fp,"%d",&arr[i].num);
fscanf(fp,"%s",&arr[i].name);
fscanf(fp,"%d",&arr[i].price);
i++;
}
*n=i-1;
fclose(fp);
}
int takein(gds arr[],int n)
{
int i,m;
printf("\t************************\n");
printf("\t Please enter the quantity you added :");
scanf("%d",&m);
if(m>0)
{
for(i=n;i<n+m;i++)
{
printf("\t Enter clothing number :");
scanf("%d",&arr[i].num);
printf("\t Enter the clothing name :");
scanf("%s",&arr[i].name);
printf("\t Enter the price of clothes :");
scanf("%d",&arr[i].price);
printf("\n");
}
}
else printf(" This product information does not exist !");
printf("\t************************\n");
return n+m;
}
void expose(gds arr[],int n)
{
int i;
printf("\t Clothing number \t Clothing name \t Clothing price \n");
for(i=0;i<n;i++,arr++)
{
printf("\t%4d\t%13s\t%14d\n",arr->num,arr->name,arr->price);
}
}
int search(gds arr[],int n)
{
int i,a=0;
printf("\t************************\n");
printf("\t Enter the looking for clothes number :");
scanf("%d",&i);
if(n>0)
{
printf("\t Clothing number \t Clothing name \t Clothing price \n");
while(arr->num!=i&&a<n)
{
arr++;a++;
}
if(a<n){
printf("\t%4d\t%13s\t%14d\n",arr->num,arr->name,arr->price);}
else printf("\t This product does not exist in the database !\n");
}
else printf("\t This product does not exist !\n");
printf("\t************************\n");
return 0;
}
int insert(gds arr[],int n)
{
int i,a;
expose(arr,n);
printf("\t************************\n");
printf("\t Enter the serial number of the desired insertion position :");
scanf("%d",&a);
if(a>n+1||a<=0){
printf("\t Out of range !\n");
}
else {
if(0<a<n+1){
for(i=n-1;i>=a-2;i--)
{
arr[i+1]=arr[i];
}
printf("\t Enter clothing number :");
scanf("%d",&arr[a-1].num);
printf("\t Enter the clothing name :");
scanf("%s",&arr[a-1].name);
printf("\t Enter the price of clothes :");
scanf("%d",&arr[a-1].price);
}
else{
printf("\t Enter clothing number :");
scanf("%d",&arr[a-1].num);
printf("\t Enter the clothing name :");
scanf("%s",&arr[a-1].name);
printf("\t Enter the price of clothes :");
scanf("%d",&arr[a-1].price);
}
printf("\t************************\n");
n++;}//( For the time being, only one number can be added )
return n;
}
int reduce(gds arr[],int n)
{
int i,a,b=0;
expose(arr,n);
printf("\t************************\n");
printf("\t Enter the desired deletion number :");
scanf("%d",&a);
for(i=0;i<n;i++)
{
if(arr[i].num==a)
{
for(;i<n-1;i++) arr[i]=arr[i+1];
}
else b++;
}
if(b==n) printf("\t Out of range !\n");
else {
printf("\t Delete successful !\n");--n;}
printf("\t************************\n");
return n;
}
void sort(gds arr[],int n)// From small to large
{
int i,j;
gds k[10];
printf("\t************************\n");
for(i=n-1;i>0;i--)
{
for(j=0;j<i;j++)
{
if(arr[j].num>arr[j+1].num)
{
k[1]=arr[j];
arr[j]=arr[j+1];
arr[j+1]=k[1];
}
}
}
expose(arr,n);
printf("\t************************\n");
}
int search2(gds arr[],int n)
{
char i[20];
int j=0,b=0;
printf("\t************************\n");
printf("\t Enter the name of the clothes you are looking for :");
scanf("%s",&i);
if(n>0)
{
printf("\t Clothing number \t Clothing name \t Clothing price \n");
for(j=0;j<n;j++)
{
if(strcmp(arr[j].name,i)==0)
{
printf("\t%4d\t%13s\t%14d\n",arr[j].num,arr[j].name,arr[j].price);
b=1;
}
}
if(j==n&&b==0)printf("\t This product does not exist in the database !\n");
}
else printf("\t This product does not exist !\n");
printf("\t************************\n");
return 0;
}
void search3(gds arr[],int n)
{
int a,b,i,j=0;
printf("\t************************\n");
printf("\t Enter the price range of looking for clothes ( Such as 50-100):");
scanf("%d-%d",&a,&b);
if(n>0)
{
printf("\t Clothing number \t Clothing name \t Clothing price \n");
for(i=0;i<n;i++)
{
if(arr[i].price<=b&&arr[i].price>=a)
{
printf("\t%4d\t%13s\t%14d\n",arr[i].num,arr[i].name,arr[i].price);j=1;}
}
if(i==n&&j==0)printf("\t Out of range !\n");
}
printf("\t************************\n");
}
void method(gds arr[],int n)
{
int a;
printf("\t************************\n");
jiemian2();
printf("\t************************\n");
printf("\t Enter the action you need :");
scanf("%d",&a);
switch (a)
{
case 1 :search(arr,n);break;
case 2 :search2(arr,n);break;
case 3 :search3(arr,n);break;
}
}
int change(gds arr[],int n)
{
int a,i;
char b[10];
printf(" Modify the category name :");
scanf("%s",&b);
printf(" Revise the price :");
scanf("%d",&a);
for(i=0;i<n;i++)
{
while(strcmp(arr[i].name,b)==0)
arr[i].price=a;
}
}
void main()
{
int n,p=0;
gds arr[100];
read_word(arr,&p);
while(1)
{
printf("\t************************\n");
jiemian();
printf("\t************************\n");
printf("\t Enter the action you need :");
scanf("%d",&n);
switch (n)
{
case 1 :p=takein(arr,p);break;
case 2 :expose(arr,p);break;
case 3 :method(arr,p);break;
case 4 :p=insert(arr,p);break;
case 5 :p=reduce(arr,p);break;
case 6 :sort(arr,p);break;
case 7 :change
case 8 :write_word(arr,p);exit(0);
}
getchar();printf("\t Press any key to continue ...");
getchar();system("cls");
}
}
This is a simple small system written in the school training course , There are only a few main functions .
I don't know if there is anything else that can be improved
边栏推荐
猜你喜欢

Web服务器(01)——介绍web服务器

DNS
![[cloud native | learn kubernetes from scratch] VI. kubernetes core technology pod](/img/b4/87e5b5dd0ec1327784d0350c142879.png)
[cloud native | learn kubernetes from scratch] VI. kubernetes core technology pod

引导过程与服务控制

MTCNN

【无标题】

Naive Bayes multiclass training model

标准C库的IO函数

Big model training is difficult to go to the sky? Here comes the efficient and easy-to-use "Li Bai" model library

Suggestions for beginners of MySQL (strongly recommended ~ don't regret ~ ~)
随机推荐
Shell (8) cycle
24SSH服务
数组的定义
6、 If statement
shell循环语句
小项目——开机自连校园网
39 installing LNMP
DHCP实验思路
FTP service
构造函数,拷贝函数和析构函数的区别
Makefile
Iptables firewall (I)
[Oracle] get the latest working day and the previous N working days
MySQL system variables and state variables
标准C库的IO函数
Buuctf casual note, exec, easysql, secret file
Excel changes the format of scientific counting method into text
九、冒泡排序
31 regular expression
9、 Bubble sort