C language implements weather information management system
- 2020-10-07 18:48:56
- OfStack
This article shares the specific code of C language weather information management system for your reference. The specific content is as follows
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include<time.h>
#include<unistd.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
typedef int Status;
typedef int ElemType;
typedef struct
{
char Sweather[10]; // The weather conditions
double Htem; // The highest temperature
double Ltem; // The lowest temperature
double Stem; // The temperature
double Shum; // humidity
}area;
typedef struct node
{
int num; // City Numbers
area weather; // The weather in
char name[5]; // Place names
struct node *next;
}*LinkList,LNode; // Define structure
int NUM=0; // The global variable Count number of nodes
Status CreatList_L(LinkList &L,int n); // Information input
void Print_LinkList( LinkList H); // Check the information
int login(); // The login
Status Delete_LinkList(LinkList &H); // Remove nodes
Status GetElem_L(LinkList &L); // To find the
Status changeElem_L(LinkList &L); // Modify the
int Clearlist(LinkList &L); // Whole table deleted,
void welcome(LinkList &L); //welcome
void menu2(LinkList &L); // Administrator menu
void menu1(LinkList &L); // The user menu
void printf_node(node*p); // Output node
void my_return(LinkList &L); //return
void clothes(node *p); // Dressing index
void save_node(LinkList &L,int i); // Save the node to the file
void read_node(LinkList &L) ; // Read from file
int Register(); // registered
int signin(); // The login
void read_node(LinkList &L) // Read from file
{
LNode *p,*pr;
int i,n;
pr=L;
FILE *fp;
if((fp=fopen("weatherinformation.txt","r"))==NULL)
{
printf(" Failed to open file ");
exit(0);
}
fscanf(fp,"%d",&n); // Read the number of nodes from the file
for(i=1;i<=n;i++)
{
p=(LinkList)malloc(sizeof(LNode));
fscanf(fp,"%d",&p->num);
fscanf(fp,"%s",p->name);
fscanf(fp,"%s",p->weather.Sweather);
fscanf(fp,"%lf%lf",&p->weather.Ltem,&p->weather.Htem);
p->weather.Stem=(p->weather.Htem+p->weather.Ltem)/2;
fscanf(fp,"%lf",&p->weather.Shum);
p->next=NULL;
while(pr->next!=NULL){
pr=pr->next;
}
p->next=pr->next;
pr->next=p;
pr=pr->next;
NUM++;
}
fclose(fp);
}
void save_node(LinkList &L) // Save to file
{
LNode *p,*pr;
pr=L;
FILE *fp;
if((fp=fopen("weatherinformation.txt","w"))==NULL)
{
printf(" Failed to open file ");
exit(0);
}
fprintf(fp,"%d\n",NUM);
while(pr->next!=NULL)
{
p=pr->next;
fprintf(fp,"%d ",p->num);
fprintf(fp,"%s ",p->name);
fprintf(fp,"%s ",p->weather.Sweather);
fprintf(fp,"%lf %lf ",p->weather.Ltem,p->weather.Htem);
fprintf(fp,"%lf\n",p->weather.Shum);
pr=pr->next;
}
printf(" Save successfully!! ");
fclose(fp);
}
int Register() // registered
{
FILE* fp;
char name[10],passworld[10];
if((fp=fopen("demo.txt","a"))==NULL)
{
printf(" Failed to open file ");
exit(0);
}
printf(" Please enter the user name :");
scanf("%s",name);
fprintf(fp,"%s ",name);
printf(" Please enter your password: ");
scanf("%s",passworld);
fprintf(fp,"%s\n",passworld);
fclose(fp);
printf(" Registered successfully !");
return OK;
}
int signin() // The login
{ FILE* fp;
char name[10],passworld[10]; // The account password entered by the user
char name1[10],passworld1[10]; // The account and password read from the file
int flag=0;
if((fp=fopen("demo.txt","r"))==NULL)
{
printf(" Failed to open file ");
exit(0);
}
printf(" Enter user name :");
scanf("%s",name);
printf(" Enter the password :");
scanf("%s",passworld);
while(!flag&&!feof(fp))
{
fscanf(fp,"%s",name1);
fscanf(fp,"%s",passworld1);
if(strcmp(name,name1)==0&&strcmp(passworld,passworld1)==0)
flag=1;
}
fclose(fp);
return flag;
}
int login() //login
{
char f;
int H;
char a[10],ah[10],am[10];
char b[10],bh[10],bm[10];
FILE *fp;
printf("1. Registered! \n");
printf("2. Login! \n");
scanf("%d",&H);
if(H==1)
{
Register();
while(1){
printf(" Are you logged in? y or n\n");
getchar();
scanf("%c",&f);
system("cls");
if(f=='n')
exit(0);
else if(f=='y')
{
if(signin())
{
printf(" Login successful! \n");
return OK;
}
else
printf(" Password or user error !\n");
}
}
}
else if(H==2)
while(1){
if(signin())
{
printf(" Login successful! \n");
return OK;
}
else
printf(" Password or user error !\n");
}
return 0;
}
void clothes(node *p) // Dressing index
{
if(p->weather.Stem>=28)
printf(" Summer clothing: light cotton fabric made of short clothing, short skirts, thin short skirts, shorts \n\n");
else if(p->weather.Stem>=24&&p->weather.Stem<28)
printf(" Summer wear: cotton linen shirt, thin skirt, thin T The T-shirt \n\n");
else if(p->weather.Stem>=21&&p->weather.Stem<24)
printf(" Spring and Autumn transition clothing: short suit made of single layer cotton and linen fabric, T T-shirt, thin jeans and trousers, casual wear, professional suit \n\n");
else if(p->weather.Stem>=18&&p->weather.Stem<21)
printf(" Spring and Autumn transitional outfit: suit, jacket, windbreaker, casual outfit, jacket, suit, thin sweater \n\n");
else if(p->weather.Stem>=15&&p->weather.Stem<18)
printf(" Spring and Autumn clothing: windbreaker, coat, coat, sweater, wool suit, suit, cold clothing \n\n");
else if(p->weather.Stem>=11&&p->weather.Stem<15)
printf(" Autumn and winter outfit: sweater, windbreaker, wool suit, suit \n\n");
else if(p->weather.Stem>=6&&p->weather.Stem<11)
printf(" Winter clothing: winter coat, winter coat, leather jacket, tweed coat, felt hat, gloves, down jacket, fur coat \n\n");
else
printf(" Winter clothing: winter coat, winter coat, leather jacket, tweed coat, felt hat, gloves, down jacket, fur coat \n\n");
}
Status CreatList_L(LinkList &L) /* Information input */
{
LNode *p,*pr;
int i,n;
printf(" Please enter the locale ");
scanf("%d",&n);
pr=L;
for(i=n;i>0;i--)
{
p=(LinkList)malloc(sizeof(LNode));
printf(" Please enter the city number \n");
scanf("%d",&p->num);
printf(" Please enter place name \n");
scanf("%s",p->name);
printf(" Please enter the weather conditions for this area \n");
scanf("%s",p->weather.Sweather);
printf(" Minimum and maximum temperatures \n");
scanf("%lf%lf",&p->weather.Ltem,&p->weather.Htem);
p->weather.Stem=(p->weather.Htem+p->weather.Ltem)/2;
printf(" Relative humidity \n");
scanf("%lf",&p->weather.Shum);
p->next=NULL;
while(pr->next!=NULL){
pr=pr->next;
}
p->next=pr->next;
pr->next=p;
pr=pr->next;
NUM++;
}
return OK;
}
void printf_node(node*p) // The output 1 A node
{
printf("%s The weather is: %s\n",p->name,p->weather.Sweather);
printf(" The minimum temperature is: %.2lf The highest temperature is: %.2lf , the average temperature is: %.2lf The degree of \n",p->weather.Ltem,p->weather.Htem,p->weather.Stem);
printf(" Relative humidity is: %.1lf%%\n",p->weather.Shum);
clothes(p);
}
void my_return(LinkList &L,int n) //my_return
{
char f;
while(1){
printf(" Return to menu or not? y or n\n");
getchar();
scanf("%c",&f);
if((f=='y'||f=='Y'))
{
if(n==1)
menu1(L);
else if(n==2)
menu2(L);
}
else if(f=='n'||f=='N')
{
printf("bye-bye");
exit(0);
}
}
}
void Print_LinkList( LinkList H) /* Output chain table */
{
LNode *p;
p=H->next;
if(p==NULL){
printf(" No data stored \n");
return ;
}
while(p!=NULL)
{
printf_node(p);
p=p->next;
}
printf("\n");
}
Status Delete_LinkList(LinkList &H) /* Delete table serial number is i The record of */
{
int i;
int j=1;
LNode *p,*pr;
p=H;
if(NUM==0){
printf(" No data stored \n");
return 0;
}
printf(" Please enter the city number \n");
scanf("%d",&i);
while(p&&i!=p->next->num)
{
p=p->next;
}
if(!p) {
printf(" There is no city \n");
return ERROR;}
pr=p->next;
p->next=pr->next;
free(pr);
printf(" Delete successful! \n");
NUM--;
return OK;
}/* Delete_LinkList */
int Clearlist(LinkList &L) // Whole table deleted,
{
LNode *p,*q;
system("cls");
printf(" Please confirm whether you have obtained the highest permission. y or n\n");
getchar();
getchar();
printf("ERROR ! You don't have the highest authority! ");
exit(0);
p=L->next;
while(p)
{
q=p->next ;
free(p);
p=q;
NUM--;
}
L->next=NULL;
save_node(L);
return 1;
}
Status GetElem_L(LinkList &L) // To find the
{
int num;
char a[5];
int j=1;
printf(" Please enter the city name \n");
scanf("%s",&a);
LinkList p;
p=L->next;
while(p&&strcmp(p->name,a)!=0)
{
p=p->next;
}
if(!p){
printf(" There is no city! ");
return ERROR;
}
printf_node(p);
return OK;
}
Status changeElem_L(LinkList &L) // Modify the
{
int num;
int j=1;
printf(" Please enter the city number \n");
scanf("%d",&num);
LinkList p;
p=L->next;
while(p&&num!=p->num)
{
p=p->next;
}
if(!p){printf(" There is no city! \n");
return ERROR;
}
printf_node(p);
printf(" Please amend: \n");
printf(" Please enter place name \n");
scanf("%s",p->name);
printf(" Please enter the city number \n");
scanf("%d",&p->num);
printf(" Please enter the weather conditions for this area \n");
scanf("%s",p->weather.Sweather);
printf(" Maximum and minimum temperatures \n");
scanf("%lf%lf",&p->weather.Htem,&p->weather.Ltem);
p->weather.Stem=(p->weather.Htem+p->weather.Ltem)/2;
printf(" Relative humidity \n");
scanf("%lf",&p->weather.Shum);
printf(" The modified information is: \n");
printf_node(p);
return OK;
}
void menu1(LinkList &L) // The user menu
{
int choose;
system("cls");
printf("\n\n");
printf("\t\t\t**************** The user menu ****************\n");
printf("\t\t\t* *\n");
printf("\t\t\t* Have a good time *\n");
printf("\t\t\t* *\n");
printf("\t\t\t* Press to find information 1 -- Overview please click 2 *\n");
printf("\t\t\t* Exit please click 3 *\n");
printf("\t\t\t* *\n");
printf("\t\t\t* *\n");
printf("\t\t\t****************************************\n");
printf("\n\t\t\t\t Press to enter the administrator menu 0\n");
scanf("%d",&choose);
switch(choose)
{
case 0:menu2(L);break;
case 1:GetElem_L(L); my_return(L,1);break;
case 2:Print_LinkList(L); my_return(L,1);break;
case 3:exit(0) ;break;
default:menu1(L);
}
}
void menu2(LinkList &L) // Administrator menu
{
int choose;
system("cls");
printf("\n\n");
printf("\t\t\t**************** Administrator menu ****************\n");
printf("\t\t\t* *\n");
printf("\t\t\t* Please press for information entry 1 -- To delete information, click 2 *\n");
printf("\t\t\t* Press for information 3 -- Please press to modify the information 4 *\n");
printf("\t\t\t* Please click to view the information 5 -- Save and exit the press 6 *\n");
printf("\t\t\t* *\n");
printf("\t\t\t* Clear all data press 8 *\n");
printf("\t\t\t* *\n");
printf("\t\t\t******************************************\n");
printf("\n\t\t\t\t Click to enter the user menu 0\n");
scanf("%d",&choose);
switch(choose)
{
case 0:menu1(L);break;
case 1:CreatList_L(L) ;my_return(L,2);break;
case 2:Delete_LinkList(L) ;my_return(L,2);break;
case 3:GetElem_L(L) ;my_return(L,2);break;
case 4:changeElem_L(L); my_return(L,2);break;
case 5:Print_LinkList(L); my_return(L,2);break;
case 6:save_node(L);exit(0);
case 8:Clearlist(L);break;
default :menu2(L);
}
}
void welcome(LinkList &L) //welcome
{
int a;
login();
printf(" User login please press 1\n");
printf(" Please click for administrator login 2\n");
while(3>2)
{
scanf("%d",&a);
if(a==1)
{ menu1(L);
break;
}
else if(a==2)
{
menu2(L);
break;
}
else
{
printf(" Typo! Please re-enter \n");
continue;
}
}
}
int main() //main
{
int i=0;
LinkList L;
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
printf("\n\n\n\n\t\t\t Please wait while you read data from the file ");
read_node(L);
while(i<3)
{ printf(".");
sleep(1);
i++;
}
printf("\n\n\t\t\t Read successfully!! \n");
system("pause");
system("cls");
welcome(L);
return 0;
}
For more information, please pay attention to the topic management System Development.