我想問幾個問題,希望有大佬能幫我解答一下:
1.在學生輸入完后再次登錄,如何判斷學生已經預約過,我試過用strcmp函式,讓輸入的學號和存入檔案的學號做比較,相同就說明預約過,但我試了一下,輸入和上次相同的學號,strcmp函式卻顯示不相同。
2.教師查看學生預約情況時,如何做到如圖所示,特別是那個學號和時段,人數在同一條是怎么做到的。(圖片上從上到下分別是日期,預約時段,可預約人數,已預約人數,預約同學學號)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int num = 0;
int w = 0;
int teacher1();
int teacher2();
struct teacher teacher3();
struct teacher teacher4( int i);
struct teacher teacher5(int i);
int student1();
int student2();
int student3(int j);
int student4(int j);
int yu();
struct teacher
{
char time[11];
int a[13];
int b[13];
int d[13];
int sum;
}t[100];
int main()
{
t[0].sum = 0;
while (1)
{
int n;
printf("**這是課程設計答辯預約系統**\n");
printf("教師請輸入1,學生請輸入2:\n");
scanf("%d", &n);
if (n == 1)
{
teacher1();
break;
}
else if (n == 2)
{
student1();
break;
}
else
{
printf("你的輸入有誤,請重新輸入。\n\n");
continue;
}
}
return 0;
}
int teacher1()
{
FILE *fp;
if ((fp = fopen("file1.dat", "wb")) == NULL)
{
printf("建立檔案出錯\n");
return 0;
}
fclose(fp);
int j;
char a[8], b[8] = { 't', 'e', 'a', 'c', 'h', 'e', 'r' };
while (1)
{
printf("請輸入正確的用戶標識:\n");
printf("(老師請輸入teacher)\n");
scanf("%s", a);
j = strcmp(a, b);
if (j == 0)
break;
else
printf("你的輸入有誤,請重新輸入。\n\n");
}
if (num!=0)
yu();
teacher2();
return 0;
}
int yu()
{
FILE *fp;
if ((fp = fopen("file1.dat", "rb")) == NULL)
{
printf("建立檔案出錯!\n");
return 0;
}
int i;
for (i = 0; i <= 1;i++)
fread(&t[i],sizeof(struct teacher),1,fp);
fclose(fp);
int j;
for (j = 0; j <= 1; j++)
{
int m, k;
printf("可預約日期: ");
printf("%s\n", t[j].time);
printf("可預約時段:");
for (m = 1; m < 13; m++)
printf("%5d", m);
printf("\n");
printf("可預約人數:");
for (k = 0; k < 12; k++)
printf("%5d", t[j].b[k]);
printf("\n");
int e;
printf("已預約人數:");
for (e = 0; e < 12; e++)
printf("%5d",t[j].d[e]);
printf("\n\n");
}
return 0;
}
int teacher2()
{
int a, b, c,i;
printf("請輸入可預約的答辯日期:\n");
printf("格式為:xxxx-xx-xx(年-月-日)\n");
printf("(中間的橫桿用減號代替)\n");
scanf("%s", t[num].time);
if (num >= 1)
{
FILE *fp;
if ((fp = fopen("file1.dat", "rb")) == NULL)
{
printf("建立檔案出錯\n");
return 0;
}
int x;
for (x = 0; x < num; x++)
fread(&t[x], sizeof(struct teacher), 1, fp);
for (i = 0; i < num;i++)
if (strcmp(t[num].time, t[i].time) == 0)
{
printf("你輸入的日期有重復,請重新輸入。\n");
teacher2();
}
fclose(fp);
}
if (num == 1)
t[num].d[4] = 1;
t[num].d[12] = '\0';
a = (t[num].time[0] - 48) * 1000 + (t[num].time[1] - 48) * 100 + (t[num].time[2] - 48) * 10 + (t[num].time[3] - 48);
b = (t[num].time[5] - 48) * 10 + (t[num].time[6] - 48);
c = (t[num].time[8] - 48) * 10 + (t[num].time[9] - 48);
if (b <= 0 || b>12)
{
printf("你的輸入有誤,請重新輸入。\n\n");
teacher2();
}
else if (c <= 0 || c > 31)
{
printf("你的輸入有誤,請重新輸入。\n\n");
teacher2();
}
else if ((a % 4 == 0) && (b == 2) && (c >= 30))
{
printf("你的輸入有誤,請重新輸入。\n\n");
teacher2();
}
else if ((a % 4 != 0) && (b == 2) && (c >= 29))
{
printf("你的輸入有誤,請重新輸入。\n\n");
teacher2();
}
else if (((b = 4) || (b = 6) || (b = 9) || (b = 11)) && (c == 31))
{
printf("你的輸入有誤,請重新輸入。\n\n");
teacher2();
}
else
{
if (num == 0)
{
int j,y;
for (y = 0; y <= 100;y++)
for (j = 0; j < 12;j++)
t[y].d[j] = { 0 };
}
teacher3();
}
return 0;
}
struct teacher teacher3()
{
int i = 0;
printf("請輸入可預約時段:\n");
printf("(時段按1-12節課劃分)\n");
printf("(每個時段以空格區分,輸入完畢請以0結尾)\n");
while (1)
{
scanf("%d", &t[num].a[i]);
if (t[num].a[i] == 0)
{
t[num].a[i + 1] = '\0';
teacher4(i);
break;
}
i++;
}
return t[num];
}
struct teacher teacher4( int i)
{
int k;
for (k = 0; k < 12;k++)
t[num].b[k] = 0;
int j;
for (j = 0; j < i;j++)
{ printf("請輸入第%d時段可預約人數:\n", t[num].a[j]);
printf("(每節課答辯人數少于5比較合適)\n");
scanf("%d", &t[num].b[(t[num].a[j]-1)]);
}
teacher5(i);
return t[num];
}
struct teacher teacher5( int i)
{
int n;
{
printf("繼續設定日期請輸入1,\n");
printf("預覽預約設定請輸入2,\n");
printf("退出本角色請輸入0。\n");
scanf("%d", &n);
if (n == 1)
{
t[0].sum++;
num++;
teacher2();
}
else if (n == 2)
{
t[num].b[12] = '\0';
FILE *fp;
if ((fp = fopen("file1.dat", "rb")) == NULL)
{
printf("建立檔案出錯\n");
return t[num];
}
int x;
for (x = 0; x <= num;x++)
fread(&t[x], sizeof(struct teacher), 1, fp);
int j;
for (j = 0; j<= num; j++)
{
int m, k;
printf("可預約日期: ");
printf("%s\n", t[j].time);
printf("可預約時段:");
for (m = 1; m < 13; m++)
printf("%5d",m);
printf("\n");
printf("可預約人數:");
for (k = 0; k < 12;k++)
printf("%5d", t[j].b[k]);
printf("\n\n");
}
fclose(fp);
teacher5(i);
}
else if (n == 0)
{
FILE *fp;
if ((fp = fopen("file1.dat", "ab")) == NULL)
{
printf("建立檔案出錯\n");
return t[num];
}
int x;
for (x = 0; x <= num;x++)
if (fwrite(&t[x], sizeof(struct teacher), 1, fp) != 1)
printf("file write error\n");
fclose(fp);
main();
}
else
{ printf("你的輸入有誤,請重新輸入。\n");
teacher5(i);
}
}
return t[num];
}
struct student
{
char hao[13];
char a1[11];
int b1;
int c1;
}s[100];
int student1()
{
char sn[13];
printf("請輸入正確的用戶標識:\n");
printf("(學生請輸入學號)\n");
scanf("%s",s[w].hao);
if ((fp = fopen("file1.dat", "rb")) == NULL)
{
printf("建立檔案出錯\n");
return 0;
}
int x;
for (x = 0; x <= 1; x++)
fread(&t[x], sizeof(struct teacher), 1, fp);
fclose(fp);
int j;
for (j = 0; j <= 1; j++)
{
int m, k;
printf("可預約日期: ");
printf("%s\n", t[j].time);
printf("可預約時段:");
for (m = 1; m < 13; m++)
printf("%5d", m);
printf("\n");
printf("可預約人數:");
for (k = 0; k < 12; k++)
printf("%5d", t[j].b[k]);
printf("\n");
int n;
printf("已預約人數:");
t[j].d[12] = '\0';
for (n = 0; n < 12; n++)
printf("%5d",t[j].d[n]);
printf("\n\n");
}
student2();
return 0;
}
int student2()
{
FILE *fp;
int i,j,n=0;
printf("請選擇預約日期:\n");
printf("(日期輸入格式為:xxxx-xx-xx,分隔符用減號代替)\n");
scanf("%s",s[w].a1);
if ((fp = fopen("file1.dat", "rb")) == NULL)
{
printf("建立檔案出錯\n");
return 0;
}
int x;
for (x = 0; x <=1; x++)
fread(&t[x], sizeof(struct teacher), 1, fp);
for (i = 0; i <=1; i++)
if (strcmp(s[w].a1, t[i].time) == 0)
{
n++;
j = i;
}
if (n!=0)
{
fclose(fp);
student3(j);
}
else
{
fclose(fp);
printf("你選的日期不存在,請重新選擇!\n");
student2();
}
return 0;
}
int student3(int j)
{
FILE *fp;
if ((fp = fopen("file1.dat", "rb")) == NULL)
{
printf("建立檔案出錯\n");
return 0;
}
int x;
for (x = 0; x <=1; x++)
fread(&t[x], sizeof(struct teacher), 1, fp);
printf("請選擇預約時段:\n");
scanf("%d",&s[w].c1);
if (s[w].c1<1 || s[w].c1>12)
{
printf("你輸入的時段不存在,請重新輸入!\n");
student3(j);
}
else if (t[j].b[s[w].c1 - 1] == 0)
{
printf("你所選的預約時段不在老師設定的時段之中,請重新選擇!\n");
student3(j);
}
else if (t[j].d[s[w].c1 - 1] == t[j].b[s[w].c1 - 1])
{
printf("你所選的預約時段人數已滿,請重新選擇!\n");
student3(j);
}
student4(j);
return 0;
}
int student4(int j)
{
int i,y;
printf("確定請輸入1,\n");
printf("重選請輸入2,\n");
printf("退出本角色請輸入0.\n");
scanf("%d",&i);
if (i == 1)
{
FILE *fp;
if ((fp = fopen("file1.dat", "wb+")) == NULL)
{
printf("建立檔案出錯\n");
return 0;
}
int x;
for (x = 0; x <=1; x++)
fread(&t[x], sizeof(struct teacher), 1, fp);
fclose(fp);
if ((fp = fopen("file1.dat", "wb+")) == NULL)
{
printf("建立檔案出錯\n");
return 0;
}
t[j].d[s[w].c1 - 1] = (t[j].d[s[w].c1 - 1])+1;
int n;
for (n = 0; n <= 1;n++)
if (fwrite(&t[n], sizeof(struct teacher), 1, fp) != 1)
printf("file write error\n");
fclose(fp);
if ((fp = fopen("file1.dat", "wb+")) == NULL)
{
printf("建立檔案出錯\n");
return 0;
}
s[w].b1 = t[j].d[s[w].c1 - 1];//第幾個預約
int z;
for (z = 0; z <= 10; z++)
if (fwrite(&s[z], sizeof(struct student), 1, fp) != 1)
printf("file write error\n");
fclose(fp);
if ((fp = fopen("file1.dat", "ab")) == NULL)
{
printf("建立檔案出錯\n");
return 0;
}
int w1;
w1 = w;
if (fwrite(&w1, sizeof(int), 1, fp) != 1)
printf("file write error\n");
fclose(fp);
printf("你的預約資訊為:%s:%d:%d,請謹記!\n", s[w].a1, s[w].c1, s[w].b1);
printf("預約資訊格式為:日期:時段:序號.\n");
while (1)
{
printf("你已預約,退出本角色請輸入0:\n");
scanf("%d",&y);
if (y == 0)
{
main();
break;
}
else
printf("你的輸入有誤,請重新輸入!\n");
}
}
else if (i == 2)
student2();
else if (i == 0)
main();
else
{
printf("你的輸入有誤,請重新輸入!\n");
student4(j);
}
return 0;
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/122299.html
標籤:C語言
上一篇:這個圖形怎么撰寫
