Code:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <process.h>
#define STU_NAME_LEN 8
typedef struct
{
char stuName[STU_NAME_LEN];
float chinese;
float math;
float english;
} StudentInfo;
StudentInfo* stuArray = NULL;
int stuNum = 0;
int controlThread = 1;
int function = 0;
HANDLE Mutex;
void bubleSort(int i)
{
int j;
StudentInfo temp;
float** select = malloc(sizeof(float*) * stuNum);
if (select == NULL) exit(1);
switch (i)
{
case 1:
{
for (j=0; j<stuNum; j++) select[j] = &stuArray[j].chinese;
break;
}
case 2:
{
for (j=0; j<stuNum; j++) select[j] = &stuArray[j].english;
break;
}
case 3:
{
for (j=0; j<stuNum; j++) select[j] = &stuArray[j].math;
break;
}
default: ;
}
for (i=stuNum; i>1; i--)
{
for (j=0; j<i - 1; j++)
{
if (*select[j] > *select[j + 1])
{
temp = stuArray[j];
stuArray[j] = stuArray[j + 1];
stuArray[j + 1] = temp;
}
}
}
free(select);
return;
}
void sort()
{
int i;
printf("please input the subject that you want to set to the keyword\n\
1. chinese 2. english 3. math\n\
select: ");
scanf("%d", &i);
bubleSort(i);
return;
}
void print()
{
int i;
printf("the detail information of the students : \n");
for (i=0; i<stuNum; i++)
{
printf("%d. %s\n", i, stuArray[i].stuName);
printf("english score : %g\n", stuArray[i].english);
printf("chinese score : %g\n", stuArray[i].chinese);
printf("math score : %g\n", stuArray[i].math);
printf("\n");
}
return;
}
void search()
{
int i;
char name[STU_NAME_LEN];
int j = 0;
printf("input the name you search : ");
scanf("%s", name);
for (i=0; i<stuNum; i++)
{
if (strcmp(name, stuArray[i].stuName) == 0)
{
printf(" %s\n", stuArray[i].stuName);
printf("english score : %g\n", stuArray[i].english);
printf("chinese score : %g\n", stuArray[i].chinese);
printf("math score : %g\n", stuArray[i].math);
printf("\n");
}
j = 1;
}
if (j == 0) printf("no student\n");
return;
}
void inputData()
{
int i;
for (i=0; i<stuNum; i++)
{
printf("input information of the %dth student:\n", i + 1);
printf("student name: ");
scanf("%s", stuArray[i].stuName);
printf("chinese score: ");
scanf("%f", &stuArray[i].chinese); /*error*/
printf("math score: ");
scanf("%f", &stuArray[i].math); /*error*/
printf("english score: ");
scanf("%f", &stuArray[i].english); /*error*/
}
return;
}
void statistics()
{
int i;
double totalEnglishScore = 0.0,
totalChineseScore = 0.0,
totalMathScore = 0.0;
/* double max[3]; /*max[0] english , max[1] chinese , max[2] math
double min[3];
for (i=0; i<3; i++)
{
max[i] = 0.0;
min[i] = 9999.9;
}
*/
for (i=0; i<stuNum; i++)
{
totalEnglishScore += stuArray[i].english;
totalChineseScore += stuArray[i].chinese;
totalMathScore += stuArray[i].math;
}
printf("the average score of english is : %g\n" , totalEnglishScore / stuNum);
printf("the average score of chinese is : %g\n" , totalChineseScore / stuNum);
printf("the average score of math is : %g\n" , totalMathScore / stuNum);
}
void welcome()
{
printf("\
*****************************************************\n\
************** homework of OS **********************\n\
************** author: 柯舒勤 **********************\n\
************** date: 2006.11.20 *********************\n\n");
return;
}
unsigned __stdcall InputData(void* pArguments)
{
while (controlThread)
{
if (function == 1)
{
WaitForSingleObject(Mutex, INFINITE);
system("cls");
welcome();
inputData();
function = 0;
system("pause");
ReleaseMutex(Mutex);
}
Sleep(200);
}
_endthreadex(0);
return 0;
}
unsigned __stdcall Statistics(void* pArguments)
{
while (controlThread)
{
if (function == 4)
{
WaitForSingleObject(Mutex, INFINITE);
system("cls");
welcome();
statistics();
function = 0;
system("pause");
ReleaseMutex(Mutex);
}
Sleep(200);
}
_endthreadex(0);
return 0;
}
unsigned __stdcall Search(void* pArguments)
{
while (controlThread)
{
if (function == 3)
{
WaitForSingleObject(Mutex, INFINITE);
system("cls");
welcome();
search();
function = 0;
system("pause");
ReleaseMutex(Mutex);
}
Sleep(200);
}
_endthreadex(0);
return 0;
}
unsigned __stdcall Sort(void* pArguments)
{
while (controlThread)
{
if (function == 2)
{
WaitForSingleObject(Mutex, INFINITE);
system("cls");
welcome();
sort();
print();
function = 0;
system("pause");
ReleaseMutex(Mutex);
}
Sleep(200);
}
_endthreadex(0);
return 0;
}
int main()
{
int i;
HANDLE hThread[4];
unsigned threadID[4];
Mutex = CreateMutex(NULL,FALSE,NULL);
printf("Please input the number of the students : ");
scanf("%d", &stuNum);
stuArray = (StudentInfo*)malloc(sizeof(StudentInfo) * stuNum);
if (stuArray == NULL)
{
printf("allocate failed!");
exit(1);
}
hThread[0] = (HANDLE)_beginthreadex(NULL, 0, &InputData, NULL, 0, &threadID[0]);
hThread[1] = (HANDLE)_beginthreadex(NULL, 0, &Search, NULL, 0, &threadID[1]);
hThread[2] = (HANDLE)_beginthreadex(NULL, 0, &Sort, NULL, 0, &threadID[2]);
hThread[3] = (HANDLE)_beginthreadex(NULL, 0, &Statistics, NULL, 0, &threadID[3]);
while (function != 5)
{
WaitForSingleObject(Mutex, INFINITE);
system("cls");
welcome();
printf("1. input data 2. sort 3. search 4. statistic 5. exit ");
scanf("%d", &function);
system("pause");
if (function < 1 && function > 5) printf("error\n");
ReleaseMutex(Mutex);
}
controlThread = 0;
for (i=0; i<4; i++) WaitForSingleObject(hThread[i], INFINITE);
for (i=0; i<4; i++) CloseHandle(hThread[i]);
CloseHandle(Mutex);
free(stuArray);
return 0;
}
the error was caused by scanf("%f",&stuArray[i].chinese)