C语言课程设计 麻烦高手进来帮帮忙 谢谢啦!~~~
每个产品销售记录由产品代码dm(字符型4位),产品名称mc(字符型10位),单价dj(整型),数量sl(整型),金额je(长整型)五部分组成。其中金额=单价*数量计算得出...
每个产品销售记录由产品代码dm(字符型4位),产品名称mc(字符型10位),单价dj(整型),数量sl(整型),金额je(长整型)五部分组成。其中金额=单价*数量计算得出。请编制函数SortDat(),其功能要求:按产品代码从大到小进行排列,若产品代码相同,则按金额从大到小进行排列。
[设计思想] 定义一个产品信息的结构体类型PRO,其包含题目所要求的5个成员。首先输入产品实际销售记录数n,然乎调用函数Data_In()来输入产品销售记录,输入之前,通过动态内存分配函数malloc建立n个产品记录信息的内存块,其首地址赋给指针变量p,产品信息输入完后,将指针p返回。再通过调用函数Data_Sort来对产品信息按代码从大到小排序,代码相同按金额从大到小排序,最后调用函数Data_Out输出排序后的结果。
要求编译的环境为Windows XP TC 2.0 的
主体内容主要包括
1.设计分析;
2.程序结构(画流程图)
3.各模块的功能及程序说明;
4. 源程序;
5.操作方法(流程);
6.试验结果(包括输入数据和输出结果)
7.设计体会。
这是我们的课程设计题,太难了,不会写!
麻烦各位高手帮帮忙!
谢谢啦!~~~ 展开
[设计思想] 定义一个产品信息的结构体类型PRO,其包含题目所要求的5个成员。首先输入产品实际销售记录数n,然乎调用函数Data_In()来输入产品销售记录,输入之前,通过动态内存分配函数malloc建立n个产品记录信息的内存块,其首地址赋给指针变量p,产品信息输入完后,将指针p返回。再通过调用函数Data_Sort来对产品信息按代码从大到小排序,代码相同按金额从大到小排序,最后调用函数Data_Out输出排序后的结果。
要求编译的环境为Windows XP TC 2.0 的
主体内容主要包括
1.设计分析;
2.程序结构(画流程图)
3.各模块的功能及程序说明;
4. 源程序;
5.操作方法(流程);
6.试验结果(包括输入数据和输出结果)
7.设计体会。
这是我们的课程设计题,太难了,不会写!
麻烦各位高手帮帮忙!
谢谢啦!~~~ 展开
1个回答
展开全部
这个很easy的了. 如果用C++ STL 几句代码就搞定了,只需要重载个<. 再用std::sort.
如果用纯C, 我写了代码和注释. 公司都要求用英文写注识,习惯了. 很容易懂的. 我全用的标准C的接口, 在VS2005下编译通过了, 我没TC 2.0的编译器. 应该能够通过.
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
/*define the struct header*/
struct sProductRecord
{
char cpProductCode[4]; /*product code */
char cpProductNAME[10]; /*product name */
int iPrice; /*single price */
int iNumOfProduct; /*the number of product */
int iTotalPrice; /*the total price of product,and it equal to iPrice * iNumOfProduct */
};
/*define the compare function
@Parameter first:one of the two Product which will be compared
@Parameter second:one of the two Product which will be compared
*/
bool ProductCompare( sProductRecord *first,sProductRecord *second )
{
if ( atoi(first->cpProductCode) == atoi(second->cpProductCode) )
{
return first->iTotalPrice > second->iTotalPrice;
}
else
{
return atoi(first->cpProductCode) > atoi(second->cpProductCode);
}
}
/*define the swap function
This function will swap the position of two record which is in the array
@Parameter lpProductRecord:Point to the array of records
@Parameter iAPosition:index of position A
@Parameter iBPosition:index of Position B*/
void swap(sProductRecord *lpProductRecord, int iAPosition, int iBPosition)
{
sProductRecord sTempRecord;
memcpy(&sTempRecord, &lpProductRecord[iBPosition], sizeof(sProductRecord) );
memcpy(&lpProductRecord[iBPosition], &lpProductRecord[iAPosition], sizeof(sProductRecord));
memcpy(&lpProductRecord[iAPosition], &sTempRecord, sizeof(sProductRecord));
}
/*This function is the submarine of the ProductSort function
i use the quick sort algorithm*/
int partition(sProductRecord *lpProducRecord, int l, int r, sProductRecord &element)
{
do
{
while(ProductCompare( &lpProducRecord[++l], &element ));
while(!ProductCompare( &lpProducRecord[--r], &element ));
swap(lpProducRecord, l, r );
} while(l < r);
swap(lpProducRecord, l, r);
return l;
}
/*define the sort function
@Parameter lpProductRecord:Point to the array which will be sorted
@Parameter iStart:The start position of the array
@Parameter iEnd:The end postion of the array
*/
void ProductSort(sProductRecord *lpProductRecord, int iStart, int iEnd)
{
if (iEnd <= iStart )
{
return;
}
int iMid = (iStart + iEnd)/2;
swap(lpProductRecord, iMid, iEnd);
int k = partition(lpProductRecord, iStart - 1,iEnd, lpProductRecord[iEnd] );
swap(lpProductRecord, k,iEnd);
ProductSort(lpProductRecord, iStart, k - 1);
ProductSort(lpProductRecord, k + 1, iEnd);
}
/*define the function which will get the input information
@Parameter lpProductRecord:Point to the array which will save the records
@Parameter iRecordNum:The Number of records will be inputed to the program
*/
void Data_In(sProductRecord *lpProductRecord,int iRecordNum)
{
int i = 0;
while (i < iRecordNum)
{
printf("Product Code:");
scanf("%s",&lpProductRecord[i].cpProductCode);
printf("Product Name:");
scanf("%s",&lpProductRecord[i].cpProductNAME);
printf("Product Price:");
scanf("%d",&lpProductRecord[i].iPrice);
printf("Product Number:");
scanf("%d",&lpProductRecord[i].iNumOfProduct);
lpProductRecord[i].iTotalPrice = lpProductRecord[i].iNumOfProduct * lpProductRecord[i].iPrice;
++i;
}
}
/*define the function which will output the result
@Parameter lpProductRecord:Point to the array which saved the result.
@Parameter iRecordNum:The Number of records in the array.
*/
void Data_Out(sProductRecord *lpProductRecord,int iRecordNum)
{
int i = 0;
while ( i < iRecordNum)
{
printf("Product Code:%s, Product Name:%s, Product Price: %d, Product Number: %d, Product total Price: %d\n",
lpProductRecord[i].cpProductCode,
lpProductRecord[i].cpProductNAME,
lpProductRecord[i].iPrice,
lpProductRecord[i].iNumOfProduct,
lpProductRecord[i].iTotalPrice);
++i;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int iRecordNum = 0;
printf("Please input the number of records which will be sorted:");
scanf( "%d", &iRecordNum);
if ( iRecordNum <= 0 )
{
printf("invalid number");
return 0;
}
sProductRecord *lpProductRecord = (sProductRecord *)malloc(sizeof(sProductRecord) * iRecordNum);
if ( NULL == lpProductRecord)
{
printf("memory allocate error");
return 0;
}
memset(lpProductRecord, 0x0, sizeof(sProductRecord) * iRecordNum);
/*get the input information*/
Data_In(lpProductRecord,iRecordNum);
/*sort the array*/
ProductSort(lpProductRecord, 0, iRecordNum - 1);
/*output the information*/
Data_Out(lpProductRecord,iRecordNum);
free(lpProductRecord);
return 0;
}
好了我下了个TC2.0, 调式了一下, 稍微修改了下代码,用下面的可以build.
#include <stdio.h>
#include <stdlib.h>
/*#include <memory.h>*/
typedef char bool;
/*define the struct header*/
typedef struct
{
char cpProductCode[4]; /*product code */
char cpProductNAME[10]; /*product name */
int iPrice; /*single price */
int iNumOfProduct; /*the number of product */
int iTotalPrice; /*the total price of product,and it equal to iPrice * iNumOfProduct */
}sProductRecord;
/*define the compare function*/
/*@Parameter first:one of the two Product which will be compared*/
/*@Parameter second:one of the two Product which will be compared*/
bool ProductCompare( sProductRecord* first, sProductRecord* second )
{
if ( atoi(first->cpProductCode) == atoi(second->cpProductCode) )
{
return first->iTotalPrice > second->iTotalPrice;
}
else
{
return atoi(first->cpProductCode) > atoi(second->cpProductCode);
}
}
/*define the swap function
This function will swap the position of two record which is in the array
@Parameter lpProductRecord:Point to the array of records
@Parameter iAPosition:index of position A
@Parameter iBPosition:index of Position B*/
void swap(sProductRecord *lpProductRecord, int iAPosition, int iBPosition)
{
sProductRecord sTempRecord;
memcpy(&sTempRecord, &lpProductRecord[iBPosition], sizeof(sProductRecord) );
memcpy(&lpProductRecord[iBPosition], &lpProductRecord[iAPosition], sizeof(sProductRecord));
memcpy(&lpProductRecord[iAPosition], &sTempRecord, sizeof(sProductRecord));
}
/*This function is the submarine of the ProductSort function
i use the quick sort algorithm*/
int partition(sProductRecord *lpProducRecord, int l, int r, sProductRecord *element)
{
do
{
while( ProductCompare( &lpProducRecord[++l], element ));
while(!ProductCompare( &lpProducRecord[--r], element ));
swap(lpProducRecord, l, r );
} while(l < r);
swap(lpProducRecord, l, r);
return l;
}
/*define the sort function
@Parameter lpProductRecord:Point to the array which will be sorted
@Parameter iStart:The start position of the array
@Parameter iEnd:The end postion of the array
*/
void ProductSort(sProductRecord *lpProductRecord, int iStart, int iEnd)
{
int iMid,k;
if (iEnd <= iStart )
{
return;
}
iMid = (iStart + iEnd)/2;
swap(lpProductRecord, iMid, iEnd);
k = partition(lpProductRecord, iStart - 1,iEnd, &lpProductRecord[iEnd] );
swap(lpProductRecord, k,iEnd);
ProductSort(lpProductRecord, iStart, k - 1);
ProductSort(lpProductRecord, k + 1, iEnd);
}
/*define the function which will get the input information
@Parameter lpProductRecord:Point to the array which will save the records
@Parameter iRecordNum:The Number of records will be inputed to the program
*/
void Data_In(sProductRecord *lpProductRecord,int iRecordNum)
{
int i = 0;
while (i < iRecordNum)
{
printf("Product Code:");
scanf("%s",&lpProductRecord[i].cpProductCode);
printf("Product Name:");
scanf("%s",&lpProductRecord[i].cpProductNAME);
printf("Product Price:");
scanf("%d",&lpProductRecord[i].iPrice);
printf("Product Number:");
scanf("%d",&lpProductRecord[i].iNumOfProduct);
lpProductRecord[i].iTotalPrice = lpProductRecord[i].iNumOfProduct * lpProductRecord[i].iPrice;
++i;
}
}
/*define the function which will output the result
@Parameter lpProductRecord:Point to the array which saved the result.
@Parameter iRecordNum:The Number of records in the array.
*/
void Data_Out(sProductRecord *lpProductRecord,int iRecordNum)
{
int i = 0;
while ( i < iRecordNum)
{
printf("Product Code:%s, Product Name:%s, Product Price: %d, Product Number: %d, Product total Price: %d\n",
lpProductRecord[i].cpProductCode,
lpProductRecord[i].cpProductNAME,
lpProductRecord[i].iPrice,
lpProductRecord[i].iNumOfProduct,
lpProductRecord[i].iTotalPrice);
++i;
}
}
int main(int argc, char* argv[])
{
int iRecordNum = 0;
sProductRecord *lpProductRecord;
printf("Please input the number of records which will be sorted:");
scanf( "%d", &iRecordNum);
if ( iRecordNum <= 0 )
{
printf("invalid number");
return 0;
}
lpProductRecord = (sProductRecord *)malloc(sizeof(sProductRecord) * iRecordNum);
if ( NULL == lpProductRecord)
{
printf("memory allocate error");
return 0;
}
memset(lpProductRecord, 0x0, sizeof(sProductRecord) * iRecordNum);
/*get the input information*/
Data_In(lpProductRecord,iRecordNum);
/*sort the array*/
ProductSort(lpProductRecord, 0, iRecordNum - 1);
/*output the information*/
Data_Out(lpProductRecord,iRecordNum);
free(lpProductRecord);
return 0;
}
如果用纯C, 我写了代码和注释. 公司都要求用英文写注识,习惯了. 很容易懂的. 我全用的标准C的接口, 在VS2005下编译通过了, 我没TC 2.0的编译器. 应该能够通过.
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
/*define the struct header*/
struct sProductRecord
{
char cpProductCode[4]; /*product code */
char cpProductNAME[10]; /*product name */
int iPrice; /*single price */
int iNumOfProduct; /*the number of product */
int iTotalPrice; /*the total price of product,and it equal to iPrice * iNumOfProduct */
};
/*define the compare function
@Parameter first:one of the two Product which will be compared
@Parameter second:one of the two Product which will be compared
*/
bool ProductCompare( sProductRecord *first,sProductRecord *second )
{
if ( atoi(first->cpProductCode) == atoi(second->cpProductCode) )
{
return first->iTotalPrice > second->iTotalPrice;
}
else
{
return atoi(first->cpProductCode) > atoi(second->cpProductCode);
}
}
/*define the swap function
This function will swap the position of two record which is in the array
@Parameter lpProductRecord:Point to the array of records
@Parameter iAPosition:index of position A
@Parameter iBPosition:index of Position B*/
void swap(sProductRecord *lpProductRecord, int iAPosition, int iBPosition)
{
sProductRecord sTempRecord;
memcpy(&sTempRecord, &lpProductRecord[iBPosition], sizeof(sProductRecord) );
memcpy(&lpProductRecord[iBPosition], &lpProductRecord[iAPosition], sizeof(sProductRecord));
memcpy(&lpProductRecord[iAPosition], &sTempRecord, sizeof(sProductRecord));
}
/*This function is the submarine of the ProductSort function
i use the quick sort algorithm*/
int partition(sProductRecord *lpProducRecord, int l, int r, sProductRecord &element)
{
do
{
while(ProductCompare( &lpProducRecord[++l], &element ));
while(!ProductCompare( &lpProducRecord[--r], &element ));
swap(lpProducRecord, l, r );
} while(l < r);
swap(lpProducRecord, l, r);
return l;
}
/*define the sort function
@Parameter lpProductRecord:Point to the array which will be sorted
@Parameter iStart:The start position of the array
@Parameter iEnd:The end postion of the array
*/
void ProductSort(sProductRecord *lpProductRecord, int iStart, int iEnd)
{
if (iEnd <= iStart )
{
return;
}
int iMid = (iStart + iEnd)/2;
swap(lpProductRecord, iMid, iEnd);
int k = partition(lpProductRecord, iStart - 1,iEnd, lpProductRecord[iEnd] );
swap(lpProductRecord, k,iEnd);
ProductSort(lpProductRecord, iStart, k - 1);
ProductSort(lpProductRecord, k + 1, iEnd);
}
/*define the function which will get the input information
@Parameter lpProductRecord:Point to the array which will save the records
@Parameter iRecordNum:The Number of records will be inputed to the program
*/
void Data_In(sProductRecord *lpProductRecord,int iRecordNum)
{
int i = 0;
while (i < iRecordNum)
{
printf("Product Code:");
scanf("%s",&lpProductRecord[i].cpProductCode);
printf("Product Name:");
scanf("%s",&lpProductRecord[i].cpProductNAME);
printf("Product Price:");
scanf("%d",&lpProductRecord[i].iPrice);
printf("Product Number:");
scanf("%d",&lpProductRecord[i].iNumOfProduct);
lpProductRecord[i].iTotalPrice = lpProductRecord[i].iNumOfProduct * lpProductRecord[i].iPrice;
++i;
}
}
/*define the function which will output the result
@Parameter lpProductRecord:Point to the array which saved the result.
@Parameter iRecordNum:The Number of records in the array.
*/
void Data_Out(sProductRecord *lpProductRecord,int iRecordNum)
{
int i = 0;
while ( i < iRecordNum)
{
printf("Product Code:%s, Product Name:%s, Product Price: %d, Product Number: %d, Product total Price: %d\n",
lpProductRecord[i].cpProductCode,
lpProductRecord[i].cpProductNAME,
lpProductRecord[i].iPrice,
lpProductRecord[i].iNumOfProduct,
lpProductRecord[i].iTotalPrice);
++i;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int iRecordNum = 0;
printf("Please input the number of records which will be sorted:");
scanf( "%d", &iRecordNum);
if ( iRecordNum <= 0 )
{
printf("invalid number");
return 0;
}
sProductRecord *lpProductRecord = (sProductRecord *)malloc(sizeof(sProductRecord) * iRecordNum);
if ( NULL == lpProductRecord)
{
printf("memory allocate error");
return 0;
}
memset(lpProductRecord, 0x0, sizeof(sProductRecord) * iRecordNum);
/*get the input information*/
Data_In(lpProductRecord,iRecordNum);
/*sort the array*/
ProductSort(lpProductRecord, 0, iRecordNum - 1);
/*output the information*/
Data_Out(lpProductRecord,iRecordNum);
free(lpProductRecord);
return 0;
}
好了我下了个TC2.0, 调式了一下, 稍微修改了下代码,用下面的可以build.
#include <stdio.h>
#include <stdlib.h>
/*#include <memory.h>*/
typedef char bool;
/*define the struct header*/
typedef struct
{
char cpProductCode[4]; /*product code */
char cpProductNAME[10]; /*product name */
int iPrice; /*single price */
int iNumOfProduct; /*the number of product */
int iTotalPrice; /*the total price of product,and it equal to iPrice * iNumOfProduct */
}sProductRecord;
/*define the compare function*/
/*@Parameter first:one of the two Product which will be compared*/
/*@Parameter second:one of the two Product which will be compared*/
bool ProductCompare( sProductRecord* first, sProductRecord* second )
{
if ( atoi(first->cpProductCode) == atoi(second->cpProductCode) )
{
return first->iTotalPrice > second->iTotalPrice;
}
else
{
return atoi(first->cpProductCode) > atoi(second->cpProductCode);
}
}
/*define the swap function
This function will swap the position of two record which is in the array
@Parameter lpProductRecord:Point to the array of records
@Parameter iAPosition:index of position A
@Parameter iBPosition:index of Position B*/
void swap(sProductRecord *lpProductRecord, int iAPosition, int iBPosition)
{
sProductRecord sTempRecord;
memcpy(&sTempRecord, &lpProductRecord[iBPosition], sizeof(sProductRecord) );
memcpy(&lpProductRecord[iBPosition], &lpProductRecord[iAPosition], sizeof(sProductRecord));
memcpy(&lpProductRecord[iAPosition], &sTempRecord, sizeof(sProductRecord));
}
/*This function is the submarine of the ProductSort function
i use the quick sort algorithm*/
int partition(sProductRecord *lpProducRecord, int l, int r, sProductRecord *element)
{
do
{
while( ProductCompare( &lpProducRecord[++l], element ));
while(!ProductCompare( &lpProducRecord[--r], element ));
swap(lpProducRecord, l, r );
} while(l < r);
swap(lpProducRecord, l, r);
return l;
}
/*define the sort function
@Parameter lpProductRecord:Point to the array which will be sorted
@Parameter iStart:The start position of the array
@Parameter iEnd:The end postion of the array
*/
void ProductSort(sProductRecord *lpProductRecord, int iStart, int iEnd)
{
int iMid,k;
if (iEnd <= iStart )
{
return;
}
iMid = (iStart + iEnd)/2;
swap(lpProductRecord, iMid, iEnd);
k = partition(lpProductRecord, iStart - 1,iEnd, &lpProductRecord[iEnd] );
swap(lpProductRecord, k,iEnd);
ProductSort(lpProductRecord, iStart, k - 1);
ProductSort(lpProductRecord, k + 1, iEnd);
}
/*define the function which will get the input information
@Parameter lpProductRecord:Point to the array which will save the records
@Parameter iRecordNum:The Number of records will be inputed to the program
*/
void Data_In(sProductRecord *lpProductRecord,int iRecordNum)
{
int i = 0;
while (i < iRecordNum)
{
printf("Product Code:");
scanf("%s",&lpProductRecord[i].cpProductCode);
printf("Product Name:");
scanf("%s",&lpProductRecord[i].cpProductNAME);
printf("Product Price:");
scanf("%d",&lpProductRecord[i].iPrice);
printf("Product Number:");
scanf("%d",&lpProductRecord[i].iNumOfProduct);
lpProductRecord[i].iTotalPrice = lpProductRecord[i].iNumOfProduct * lpProductRecord[i].iPrice;
++i;
}
}
/*define the function which will output the result
@Parameter lpProductRecord:Point to the array which saved the result.
@Parameter iRecordNum:The Number of records in the array.
*/
void Data_Out(sProductRecord *lpProductRecord,int iRecordNum)
{
int i = 0;
while ( i < iRecordNum)
{
printf("Product Code:%s, Product Name:%s, Product Price: %d, Product Number: %d, Product total Price: %d\n",
lpProductRecord[i].cpProductCode,
lpProductRecord[i].cpProductNAME,
lpProductRecord[i].iPrice,
lpProductRecord[i].iNumOfProduct,
lpProductRecord[i].iTotalPrice);
++i;
}
}
int main(int argc, char* argv[])
{
int iRecordNum = 0;
sProductRecord *lpProductRecord;
printf("Please input the number of records which will be sorted:");
scanf( "%d", &iRecordNum);
if ( iRecordNum <= 0 )
{
printf("invalid number");
return 0;
}
lpProductRecord = (sProductRecord *)malloc(sizeof(sProductRecord) * iRecordNum);
if ( NULL == lpProductRecord)
{
printf("memory allocate error");
return 0;
}
memset(lpProductRecord, 0x0, sizeof(sProductRecord) * iRecordNum);
/*get the input information*/
Data_In(lpProductRecord,iRecordNum);
/*sort the array*/
ProductSort(lpProductRecord, 0, iRecordNum - 1);
/*output the information*/
Data_Out(lpProductRecord,iRecordNum);
free(lpProductRecord);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询