Ⅰ 關於c語言 圖書管理系統的一些問題
修改了不少地方,自己測試一下吧。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <memory.h>
int count=0;
struct BOOK
{
int id;//id為書號
int total;//total為總數
int store;//store為現庫存數
int user[10];//user數組表示借閱者名字
int days[10];//days表示借閱天數
char name[31],author[21]; //分別用於存放書名,及其作者
}books[100];
/*上面是結構體的定義,用於存放書籍及借書的信息。*/
void page_title(char *menu_item)
{
printf("◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆圖★書★管★理★系★統◆◆◆◆◆◆◆◆◆◆◆◆◆◆\n",menu_item);
}
/*上面是列印頁眉的函數,同時通過參數menu_item,可以顯示當前的狀態。*/
void return_confirm(void)
{
printf("\n按任意鍵返回……\n");
getch();
}
/*上面是返回前請求確認的函數,以便在返回前觀察結果*/
int search_book(void)
{
int n,i;
printf("請輸入圖書ID:");
scanf("%d",&i);
/*因為count已記錄實際的圖書數,循環的終止條件應該是n<count吧*/
for(n=0;n<count;n++)
{
if(books[n].id==i)
{
printf("書名:%s\n",books[n].name);
printf("作者:%s\n",books[n].author);
printf("存數:%d of ",books[n].store);
printf("%d\n",books[n].total);
return n;
}
}
printf("\n輸入錯誤或無效圖書ID.\n");
return -1;
}
/*上面的函數是在數組中找到圖書號匹配的記錄,顯示其信息並返
回數組下標,如果找不到相應記錄則提示錯誤並返回-1。*/
void book_out(void)
{
int n,s,l,d;
if((n=search_book())!=-1&&books[n].store>0)
{
printf("請輸入借書證序號:");
scanf("%d",&s);
printf("請輸入可借天數:");
scanf("%d",&d);
for(l=0;l<10;l++)
{
if(books[n].user[l]==0)
{
books[n].user[l]=s;
books[n].days[l]=d;
break;
}
}
books[n].store--;
}
if(n!=-1&&books[n].store==0) printf("此書已經全部借出.\n");
return_confirm();
}
/*上面是借書的函數,首先調用找書函數*/
void book_return(void)
{
int n,s,l;
if((n=search_book())!=-1&&books[n].store<books[n].total)
{
printf("借閱者圖書證列表:\n");
for(l=0;l<10;l++)
if (books[n].user[l]!=0)
printf("[%d] - %d天\n",books[n].user[l],books[n].days[l]);
printf("請輸入借書證序號:");
scanf("%d",&s);
for(l=0;l<10;l++)
{
if(books[n].user[l]==s)
{
books[n].user[l]=0;
books[n].days[l]=0;
break;
}
}
books[n].store++;
}
if(n!=-1&&books[n].store==books[n].total)
printf("全部入藏.\n");
return_confirm();
}
/*這個函數需要注意*/
void book_add(void)
{
int n,i,id;
n = 0;
while (n < 100)
{
printf("ID:");
scanf("%d",&id);
if(id==0) break;
while (books[n].id != 0) n++;
if (n >= 100) break;
books[n].id = id;
for(i=0; i<n; i++)
{
if(books[i].id==books[n].id)
{
printf("提示:此ID已經存在,請重新輸入!\n");
printf("ID:");
scanf("%d", &books[n].id);
i=-1;
continue;
}
}
printf("書名:");
scanf("%s",&books[n].name);
printf("作者:");
scanf("%s",&books[n].author);
printf("數量:");
scanf("%d",&books[n].total);
books[n].store=books[n].total;
count++;
n++;
}
return_confirm();
}
void book_view()
{
int n;
printf("|書號 \t| 書名 \t|作者\t|庫存 \t|\n");
/* 這里循環的終止條件改為n<count*/
for(n=0;n<count;n++)
{
if (books[n].id!=0)
printf("|%d \t| %s \t|%s\t|%d of %d\t\t|\n",
books[n].id,books[n].name,books[n].author,books[n].store,books[n].total);
}
return_confirm();
}
/*以上是瀏覽全部藏書的基本信息*/
void book_del(void)
{
int n;
if((n=search_book())!=-1) memset(&books[n], 0, sizeof(BOOK));
/*books[n]=books[count];*/
printf("該書已注銷.\n");
return_confirm();
count--;
}
int main(void)
{
memset(books, 0, sizeof(struct BOOK) * 100);
menu: page_title("操作選單");
printf("\t\t\t 1 借閱圖書\t2 歸還圖書\n\n");
printf("\t\t\t 3 注冊新書\t4 注銷舊書\n\n");
printf("\t\t\t 5 瀏覽藏書\t0 退出\n");
switch(getch())
{
case '1' : book_out();break;
case '2' : book_return();break;
case '3' : book_add();break;
case '4' : book_del();break;
case '5' : book_view();break;
case '0' : exit(0);
}
goto menu;
return 0;
}
Ⅱ c語言中學生成績管理的問題
#include
#include
#include
struct STUDENT{
float score[3];
long id;
char names[20];
};
typedef struct STUDENT student;//simplify the struct STUDENT
typedef struct STUDENT *Pstudent;
void print();
void append();
void course_total();
void student_total();
void score_sort(int (*compare)(float a,float b));
void number_sort();
void name_sort(Pstudent names_[30]);
void number_search();
void name_search();
void statistic(Pstudent scores_[30]);
void show(int i);
int ascend(float a, float b){
if(a>b) return 1;
else return 0;
}
int descend(float a, float b){
if(a<b) return 1;
else return 0;
}
int n;//the number of students
int flg=1;//true print the result
student *stuArray[30];//the global variable can simplify the compute
int again=1;//whether to continue
int main(){
int i;
printf("Input student number(n<30):");
scanf("%d",&n);
int choice;
while(again){
print();
scanf("%d",&choice);
switch(choice){
case 1:
append();
break;
case 2:
course_total();//use flag to define whether to print
break;
case 3:
student_total();
break;
case 4:
score_sort(descend);
if(flg){
printf("Sort in descending order by total score of every student:\n");
printf("NO\tName\tMT\tEN\tPH\tSUM\tAVER\n");
for(i=0;i<n;i++)
show(i);
}
break;
case 5:
score_sort(descend);
if(flg){
printf("Sort in ascending order by total score of every student:\n");
printf("NO\tName\tMT\tEN\tPH\tSUM\tAVER\n");
for(i=0;i<n;i++)
show(n-1-i);
}
break;
case 6:
number_sort();
break;
case 7:
name_sort(stuArray);
break;
case 8:
number_search();
break;
case 9:
name_search();
break;
case 10:
statistic(stuArray);
break;
case 0:
again=0;
printf("End of program!\n");
break;
default:
printf("Input error!\n");
break;
}
}
return 0;
}
void print(){
printf("1.Append record\n");
printf("2.Calculate total and average score of every course\n");
printf("3.Calculate total and average score of every student\n");
printf("4.Sort in descending order by total score of every student\n");
printf("5.Sort in ascending order by total score of every student\n");
printf("6.Sort in ascending order by number\n");
printf("7.Sort in dictionary order by name\n");
printf("8.Search by number\n");
printf("9.Search by name\n");
printf("10.Statistic analysis\n");
printf("Please Input your choice:");
}
void append(){
int i;
printf("Input student's ID,name and score:\n");
for(i=0;i<n;i++){////the most significant part malloc the memory when appending record
stuArray[i] = (student *)malloc(sizeof(student));
scanf("%ld%s",&stuArray[i]->id,stuArray[i]->names);
scanf("%f",&stuArray[i]->score[0]);
scanf("%f",&stuArray[i]->score[1]);
scanf("%f",&stuArray[i]->score[2]);
}
}
void course_total(){
int i;
float sum0=0.0,sum1=0.0,sum2=0.0;
for(i=0;i<n;i++){
sum0+=stuArray[i]->score[0];
sum1+=stuArray[i]->score[1];
sum2+=stuArray[i]->score[2];
}
if(flg){
printf("course %d:sum=%.0f,aver=%.0f\n",1,sum0,sum0/n);
printf("course %d:sum=%.0f,aver=%.0f\n",2,sum1,sum1/n);
printf("course %d:sum=%.0f,aver=%.0f\n",3,sum2,sum2/n);
}
}
void student_total(){
float total[30]={0.0};
int i;
for(i=0;i<n;i++){
total[i]=stuArray[i]->score[0]+stuArray[i]->score[1]+stuArray[i]->score[2];
}
if(flg){
for(i=0;i<n;i++)
printf("student %d:sum=%.0f,aver=%.0f\n",i+1,total[i],total[i]/3);
}
}
void score_sort(int (*compare)(float a,float b)){
int i,j;
float total[30]={0.0};
for(i=0;i<n;i++){
total[i]=stuArray[i]->score[0]+stuArray[i]->score[1]+stuArray[i]->score[2];
}
for(i=0;i<n;i++){
for(j=0;j<=i;j++)
//if((*compare)(stuArray[i]->score[0]+stuArray[i]->score[1]+stuArray[i]->score[2],stuArray[j]->score[0]+stuArray[j]->score[1]+stuArray[j]->score[2])==0){
if((*compare)(total[i],total[j])==0){//just swap the pointer it simplify the program
student *tmp=(student *)malloc(sizeof(student));
memcpy(tmp,stuArray[i],sizeof(student));
memcpy(stuArray[i],stuArray[j],sizeof(student));
memcpy(stuArray[j],tmp,sizeof(student));
}//memcpy-> the hole the memory
}
}
void number_sort(){//沒必要傳參
int i,j;
for(i=0;i<n;i++){
for(j=0;j<i;j++)
if(stuArray[i]->idid){
student *tmp=(student *)malloc(sizeof(student));
memcpy(tmp,stuArray[i],sizeof(student));
memcpy(stuArray[i],stuArray[j],sizeof(student));
memcpy(stuArray[j],tmp,sizeof(student));
}
}
if(flg){
printf("Sort in ascending order by number:\n");
printf("NO\tName\tMT\tEN\tPH\tSUM\tAVER\n");
for(i=0;i<n;i++)
show(i);
}
}
void name_sort(Pstudent names_[30]){
int i,j;
for(i=0;i<n;i++){
for(j=0;j<=i;j++)
if(strcmp(names_[i]->names,names_[j]->names)<0){
student *tmp=(student *)malloc(sizeof(student));
memcpy(tmp,stuArray[i],sizeof(student));
memcpy(stuArray[i],stuArray[j],sizeof(student));
memcpy(stuArray[j],tmp,sizeof(student));
}
}
if(flg){
printf("Sort in dictionary order by name:\n");
printf("NO\tName\tMT\tEN\tPH\tSUM\tAVER\n");
for(i=0;i<n;i++)
show(i);
}
}
void number_search(){
long query;
printf("Input the number you want to search:");
scanf(" %ld",&query);
int i;
score_sort(descend);//100 98 87
for(i=0;i<n;i++){
if(stuArray[i]->id==query)
break;
}
if(i!=n){
printf("%d\t",i+1);
show(i);
}
else
printf("Not found!\n");
}
void name_search(){
char query[20];
score_sort(descend);
printf("Input the name you want to search:");
scanf("%s",query);
int i;
for(i=0;i<n;i++){
if(!strcmp(query,stuArray[i]->names)){
break;
}
}
if(i!=n){
printf("%d\t",i+1);
show(i);
}
else
printf("Not found!\n");
}
void statistic(Pstudent scores_[30]){//a pointer array stands for scores
float MT[30],EN[30],PH[30];
int i;
for(i=0;i<n;i++){
MT[i]=scores_[i]->score[0];
EN[i]=scores_[i]->score[1];
PH[i]=scores_[i]->score[2];
}
int sta[6]={0};//means the statistic of every student (<60 or 60-70 ....)
for(i=0;i<n;i++){
if(MT[i]<60)
sta[0]++;
if(MT[i]==100)
sta[5]++;
if(MT[i]>=60&&MT[i]<=69)
sta[1]++;
if(MT[i]>=70&&MT[i]<=79)
sta[2]++;
if(MT[i]>=80&&MT[i]<=89)
sta[3]++;
if(MT[i]>=90&&MT[i]<=100)
sta[4]++;
}
if(flg){
printf("For course %d:\n",1);
printf("<60\t%d\t%.2f%%\n",sta[0],sta[0]/(float)n*100);//change n to float
printf("60-69\t%d\t%.2f%%\n",sta[1],sta[1]/(float)n*100);
printf("70-79\t%d\t%.2f%%\n",sta[2],sta[2]/(float)n*100);
printf("80-89\t%d\t%.2f%%\n",sta[3],sta[3]/(float)n*100);
printf("90-100\t%d\t%.2f%%\n",sta[4],sta[4]/(float)n*100);
printf("100\t%d\t%.2f%%\n",sta[5],sta[5]/(float)n*100);
}
memset(sta,0,6*sizeof(int));//initialize the sta array
for(i=0;i<n;i++){
if(EN[i]<60)
sta[0]++;
if(EN[i]==100)
sta[5]++;
if(EN[i]>=60&&EN[i]<=69)
sta[1]++;
if(EN[i]>=70&&EN[i]<=79)
sta[2]++;
if(EN[i]>=80&&EN[i]<=89)
sta[3]++;
if(EN[i]>=90&&EN[i]<=100)
sta[4]++;
}
if(flg){
printf("For course %d:\n",2);
printf("<60\t%d\t%.2f%%\n",sta[0],sta[0]/(float)n*100);//change n to float
printf("60-69\t%d\t%.2f%%\n",sta[1],sta[1]/(float)n*100);
printf("70-79\t%d\t%.2f%%\n",sta[2],sta[2]/(float)n*100);
printf("80-89\t%d\t%.2f%%\n",sta[3],sta[3]/(float)n*100);
printf("90-100\t%d\t%.2f%%\n",sta[4],sta[4]/(float)n*100);
printf("100\t%d\t%.2f%%\n",sta[5],sta[5]/(float)n*100);
}
memset(sta,0,6*sizeof(int));
for(i=0;i<n;i++){
if(PH[i]<60)
sta[0]++;
if(PH[i]==100)
sta[5]++;
if(PH[i]>=60&&PH[i]<=69)
sta[1]++;
if(PH[i]>=70&&PH[i]<=79)
sta[2]++;
if(PH[i]>=80&&PH[i]<=89)
sta[3]++;
if(PH[i]>=90&&PH[i]<=100)
sta[4]++;
}
if(flg){
printf("For course %d:\n",3);
printf("<60\t%d\t%.2f%%\n",sta[0],sta[0]/(float)n*100);//change n to float
printf("60-69\t%d\t%.2f%%\n",sta[1],sta[1]/(float)n*100);
printf("70-79\t%d\t%.2f%%\n",sta[2],sta[2]/(float)n*100);
printf("80-89\t%d\t%.2f%%\n",sta[3],sta[3]/(float)n*100);
printf("90-100\t%d\t%.2f%%\n",sta[4],sta[4]/(float)n*100);
printf("100\t%d\t%.2f%%\n",sta[5],sta[5]/(float)n*100);
}
}
void show(int i){
printf("%ld\t%s\t",stuArray[i]->id,stuArray[i]->names);//order is the id after sort
printf("%.0f\t%.0f\t%.0f\t",stuArray[i]->score[0],stuArray[i]->score[1],stuArray[i]->score[2]);
float sum=stuArray[i]->score[0]+stuArray[i]->score[1]+stuArray[i]->score[2];
printf("%.0f\t%.0f\n",sum,sum/3);
}
Ⅲ C語言停車場管理問題
細節上的優化就看Lz怎麼想了,我覺得提示做得還不夠好,免強能用了。
#include
#include
#define N 3 /*停車場大小*/
#define MAX 50 /*過道大小*/
#define sign 10/*車牌大小*/
#define price 10/*每分鍾的價錢*/
char part[N][sign];
char Rpart[MAX][sign];
char time[N][20];
int P,R;
partadd(char *t)
{
strcpy(&part[P][0],t);
printf("請輸入時間:例如十點十分格式為「10.10」\n");
scanf("%s",&time[P][0]);
getchar();
P++;
}
Rpartadd(char *t)
{
if(R<MAX)
{
strcpy(&Rpart[R][0],t);
R++;
}
else
{
printf("過道己滿。無法停車。\n");
}
}
newcar()
{
char temp[sign];
printf("請輸入車牌號:");
scanf("%s",temp);
getchar();
if(P<N)
{
partadd(temp);
}
else if(R<MAX)
{
Rpartadd(temp);
}
}
int timed(char *t1,char *t2)
{
int i=0,y=0,x=0,j,n=1;
while(1)
{
if(t1[i]=='.')
{
for(j=i-1;j>=0;j--)
{
y=y+(t1[j]-'0')*(60*n);
n=n*10;
}
while(1)
{
if(t1[j]==NULL)
{
for(n=1;j>i;j--)
{
y=y+(t1[j]-'0')*n;
n=n*10;
}
break;
}
j++;
}
i=0;
while(1)
{
if(t2[i]=='.')
{
for(j=i-1;j>=0;j--)
{
x=x+(t2[j]-'0')*(60*n);
n=n*10;
}
while(1)
{
if(t2[j]==NULL)
{
for(n=1;j>i;j--)
{
x=x+(t2[j]-'0')*n;
n=n*10;
}
break;
}
j++;
}
y=(x-y)*price;
return y;
}
i++;
}
}
i++;
}
}
partcarout(int i)
{
int j,money;
char t[20];
printf("請輸入現在的時間:例如十點十分格式為「10.10」\n");
scanf("%s",t);
getchar();
money=timed(t,&time[i][0]);
printf("收費%d\n",money);
for(j=i;j<P;j++)
{
strcpy(&part[j][0],&part[j+1][0]);
P--;
}
if(R!=0)
{
strcpy(&part[N-1][0],&Rpart[0][0]);
P++;
strcpy(&time[P][0],t);
Rpartcarout(0);
}
}
Rpartcarout(int i)
{
int j;
for(j=i;j<R;j++)
{
strcpy(&Rpart[j][0],&Rpart[j+1][0]);
R--;
}
}
carout()
{
char t[sign];
int i,get=0;
printf("請入要離開的車牌號:");
scanf("%s",t);
getchar();
for(i=0;i<P;i++)
{
if(strcmp(t,&part[i][0])==0)
{
get=1;
partcarout(i);
break;
}
}
for(i=0;i<R&&get==0;i++)
{
if(strcmp(t,&Rpart[i][0])==0)
{
get=1;
Rpartcarout(i);
break;
}
}
if(get==0)
{
printf("查無此車。\n");
}
}
jopart()
{
int i;
for(i=0;i<P;i++)
{
printf("%d.%s\n",i,&part[i][0]);
}
}
joRpart()
{
int i;
for(i=0;i<R;i++)
{
printf("%d.%s\n",i,&Rpart[i][0]);
}
}
main()
{
int c;
while(1)
{
printf("請選擇要做的事:\n");
printf("1.加入新車。\n");
printf("2.有車離開。\n");
printf("3.顯示在停車場內的車。\n");
printf("4.顯示在過道上的車。\n");
printf("5.退出。\n");
c=getchar();
getchar();
switch (c)
{
case '1':newcar();
break;
case '2':carout();
break;
case '3':jopart();
break;
case '4':joRpart();
break;
case '5':exit(1);
break;
}
}
}
Ⅳ C語言學生管理系統問題
1、排序的細節肯定有問題,檢查排序是否寫對了
2、可能是對鏈表理解不透,鏈表寫錯(應該是判斷條件寫錯了)
3、用文件保存信息,重新登錄時從文件中重新讀入數據,否則程序一關閉,內存就釋放,數據就沒有了
Ⅳ C語言,內存管理問題!
一樣大。
name是char指針類型,它只記錄地址,不記錄地址裡面存的東西
所以不管存多少東西,地址的長度是不會變的。
這個sizeof struct mm的長度目測是8(在32位程序里)
Ⅵ C語言學生信息管理系統中出現的問題
這是你自己寫的程序嗎?我很少使用這個類型,不太了解它的方法,要是源文件還是可以看看。下面是簡單的分析。
FILE的用法學習了嗎?
fwrite (&n,sizeof (int),1,fp2);//想文件中寫入1個int的0啊?不是要刪除嗎?
size_t fwrite(const void* buffer, size_t size, size_t count, FILE* stream);
Ⅶ C語言中,關於文件管理和記錄的一些問題
char fileDir[30]; // 定義一個字元數組,用於後面保存目錄路徑
strcpy(fileDir,".."); // 將字元串".."拷貝到數組fileDir中
_chdir(fileDir); // 自定義函數調用,參數為fileDir,而fileDir的內容就是上一句拷貝進來的字元串
".."在路徑中表示當前工作目錄的上一級目錄,比如我們當前在C:\Windows\system32\目錄下,那麼".."表示的就是C:\Windows\這個目錄。
_chdir這個應該是一個自定義函數,猜測,其大致功能應該實現的是「Change Directory」,即改變當前工作目錄
總的來看,如果猜測正確,那這三行代碼的意思就是返回上一層目錄的意思
Ⅷ 關於C語言中函數調用時內存管理方面的問題。
一般情況下C里能造成你這種效果的唯一可能就是 malloc之類申請的內存沒用free之類釋放。非一般情況指你用更底層的方法(比如Windows得HeapAlloc之類)申請內存來著
----
生化的基本原理我倒是還懂的,並且我堅信就算小學沒上過自然課的程序員也能看出來你的問題出在哪兒|||
Ⅸ C語言程序學生系統管理,主要提問什麼問題
1、輸入學生信息(姓名、學號、成績,可能還會有備注等額外信息,但是演算法是一樣的)
2、學生成績進行排列,規則自定
3、查找任意條件的學生或是成績
以上是我根據我們所做的題目回答的,供樓主參考。不過轉來轉去都是考核那麼幾個演算法。
Ⅹ C語言 商品管理問題,要用純 C語言中的文件
#include <stdio.h>
#include <string.h>
struct commodity
{
char name[20]; /*商品名*/
int id; /*商品ID*/
int count; /*數量*/
double price; /*價格*/
};
int main()
{
int n;
int i;
FILE *fp;
struct commodity data;
char *filename= "c:\\commodity.dat";
printf("請輸入記錄數量(整數:0-100)\n");
scanf("%d",&n);
if(n<0 || n>=100)
{
printf("Err:輸入記錄數量有誤!");
return -1;
}
if(n==0)/*建立一個新文件*/
{
fp=fopen(filename,"w+b");
if(NULL==fp)
{
printf("文件初始化有誤!");
return -2;
}
memset(&data,0,sizeof(data));
for(i=0;i<100;i++)
fwrite(&data,sizeof(data),1,fp);
printf("文件初始化成功");
}
else /*更新文件*/
{
fp=fopen(filename,"r+b");
if(NULL==fp)
{
printf("文件不存在");
return -3;
}
printf("開始更新文件記錄%d!\n",n);
fseek(fp,(n-1)*sizeof(data),SEEK_SET); /*定位到記錄位置*/
fwrite(&data,sizeof(data),1,fp); /*寫一條記錄到文件*/
printf("更新文件成功!\n");
/*輸出*/
fseek(fp,0,SEEK_SET); /*定位到記錄開頭*/
for(i=0;i<100;i++)
{
fread(&data,sizeof(data),1,fp);
/*列印記錄*/
printf("記錄:%d\n",i+1);
printf("商品名:%s\n",data.name);
printf("商品ID:%d\n",data.id);
printf("數量:%d\n",data.count);
printf("價格:%f\n\n",data.price);
}
}
fclose(fp); /*關閉文件*/
return 0;
}
以上代碼是操作文件,進行初始化與更新的方法。
對於記錄的輸入,你可以自己寫個函數,具體的內容你可以根據實際情況,添加。