C语言作业..急~~
一.将分数定义为一个结构类型Fraction,编写addFraction()和printFraction()函数,计算并打印两个分数的和。(要求用函数实现Euclid算法...
一. 将分数定义为一个结构类型Fraction,编写addFraction()和printFraction()函数,计算并打印两个分数的和。(要求用函数实现Euclid算法求最大公约数)
主函数如:
void main()
{
struct Fraction fs1={1,2}, fs2={1,6}, fssum;
fssum = addFraction(fs1, fs2);
printFraction(fssum);
}
程序运行结果如:
两分数之和为: 2/3
二. 定义一个结构类型student,其中包含学号(字符),数学成绩,英语成绩,专业成绩和总分5个成员。编写一程序从student.txt文件(见作业附件)中读入学生数据,并将按总分排序后的学生数据写入rank.txt文件。
两个题 希望得到完整的源代码。今晚之前。 C语言高手们帮帮忙~~
附件内容:
2007131001 88 77 66
2007131123 67 78 89
2007131112 66 54 76
2007131143 76 67 98
2007131176 54 56 58
2007131223 37 65 45
2007131333 87 88 89
2007131324 64 72 69
2007131163 56 66 65
2007131126 97 88 79 展开
主函数如:
void main()
{
struct Fraction fs1={1,2}, fs2={1,6}, fssum;
fssum = addFraction(fs1, fs2);
printFraction(fssum);
}
程序运行结果如:
两分数之和为: 2/3
二. 定义一个结构类型student,其中包含学号(字符),数学成绩,英语成绩,专业成绩和总分5个成员。编写一程序从student.txt文件(见作业附件)中读入学生数据,并将按总分排序后的学生数据写入rank.txt文件。
两个题 希望得到完整的源代码。今晚之前。 C语言高手们帮帮忙~~
附件内容:
2007131001 88 77 66
2007131123 67 78 89
2007131112 66 54 76
2007131143 76 67 98
2007131176 54 56 58
2007131223 37 65 45
2007131333 87 88 89
2007131324 64 72 69
2007131163 56 66 65
2007131126 97 88 79 展开
2个回答
展开全部
/*第一个*/
#include <stdio.h>
typedef struct Fraction
{
int up, down;
}Fraction;
Fraction addFraction ( Fraction a, Fraction b )
{
int up, down, tmp;
Fraction ret;
up = a.up * b.down + a.down * b.up;
down = a.down * b.down;
ret.up = up;
ret.down = down;
tmp = up % down;
while ( tmp != 0 )
{
up = down;
down = tmp;
tmp = up % down;
}
ret.up /= down;
ret.down /芦逗肆键= down;
return ret;
}
void printFraction ( Fraction x )
{
printf ( "%d\/%d\n", x.up, x.down );
}
void main()
{
struct Fraction fs1={1,2}, fs2={1,6}, fssum;
fssum = addFraction(fs1, fs2);
printFraction(fssum);
}
/*第二个*/
#include <stdio.h>
#include <stdlib.h>
#define M 3
typedef struct student
{
char *id;
float mark[M];
float sum;
}stu;
stu *list;
int line;
char* GetStringUntilSpace ( char **dst, char *src )
{
char *res;
char count;
char *p;
count = 0;
for ( res = src; *res != ' ' && *res != '\n'; res++ ) count++;
p = *dst = (char*)malloc ( count + 1 );
while ( count-- > 0 ) *p++ = *src++;
*p = 0;
return res+1;
}
float StringToFloat ( char *s )
{
float res = 0;
float ten = 0.1f;
while ( *s != '.' )
{
res *= 10;
res += (float)( *s - '0' );
s++;
if ( *s == 0 )
return res;
}
s++;
while ( *s != 0 )
{
float tmp = (float)( *s++ - '0' );
res += tmp * ten;
ten *= 0.1f;
}
return res;
}
void Init ( void )
{
int i,j;
FILE *fp;
char info[256];
char *mark;
char *pInfo;
stu *pStu;
fp = fopen ( "student.txt"陪雹卖, "r" );
if ( fp == NULL )
{
fprintf ( stderr, "Cannot File the file!\n" );
exit(1);
}
for ( line = 0; NULL != fgets ( info, sizeof(info), fp ); line++);
fseek ( fp, 0, SEEK_SET );
pStu = list = (stu*) malloc ( sizeof(stu) * line );
for ( i = 0; i < line; i++ )
{
if ( NULL == fgets ( info, sizeof(info), fp ) )
{
fprintf ( stderr, "We need more information of student(s)!\n" );
exit(2);
}
pInfo = GetStringUntilSpace ( &pStu->id, info );
for ( j = 0; j < M; j++ )
{
pInfo = GetStringUntilSpace ( &mark, pInfo );
pStu->mark[j] = StringToFloat ( mark );
free(mark);
}
pStu++;
}
fclose ( fp );
}
void End ( void )
{
int i,j;
for ( i = 0; i < line; i++ )
{
free ( list[i].id );
}
free ( list );
}
void Calc ( void )
{
int i, j;
stu *pStu = list;
for ( i = 0; i < line; i++ )
{
float sum = 0;
for ( j = 0; j < M; j++ )
{
sum += pStu->mark[j];
}
pStu->sum = sum;
pStu++;
}
}
void Sort ( void )
{
int i,j;
stu tmp;
for ( i = line - 1; i >= 0; i-- )
{
for ( j = 0; j < i; j++ )
{
int k = j+1;
if ( list[k].sum > list[j].sum )
{
memcpy ( &tmp, list+j, sizeof(stu) );
memcpy ( list+j, list+k, sizeof(stu) );
memcpy ( list+k, &tmp, sizeof(stu) );
}
}
}
}
void ShowSingle ( FILE *fp, stu *pStu )
{
int i;
printf ( "%s %.2f", pStu->id, pStu->sum );
fprintf ( fp, "%s %.2f", pStu->id, pStu->sum );
for ( i = 0; i < M; i++ )
{
printf ( " %.2f", pStu->mark[i] );
fprintf ( fp, " %.2f", pStu->mark[i] );
}
}
void Print ( void )
{
int i;
stu *pStu = list;
FILE *fp;
fp = fopen ( "rank.txt", "w" );
if ( fp == NULL )
{
fprintf ( stderr, "Error in open rank.txt\n" );
exit(2);
}
printf ( "Id Sum course1 course2 course3\n" );
for ( i = 0; i < line; i++ )
{
ShowSingle ( fp, pStu++ );
fputc ( '\n', fp );
putchar ( '\n' );
}
fclose ( fp );
}
int main(void)
{
Init();
Calc();
Sort();
Print();
End();
}
#include <stdio.h>
typedef struct Fraction
{
int up, down;
}Fraction;
Fraction addFraction ( Fraction a, Fraction b )
{
int up, down, tmp;
Fraction ret;
up = a.up * b.down + a.down * b.up;
down = a.down * b.down;
ret.up = up;
ret.down = down;
tmp = up % down;
while ( tmp != 0 )
{
up = down;
down = tmp;
tmp = up % down;
}
ret.up /= down;
ret.down /芦逗肆键= down;
return ret;
}
void printFraction ( Fraction x )
{
printf ( "%d\/%d\n", x.up, x.down );
}
void main()
{
struct Fraction fs1={1,2}, fs2={1,6}, fssum;
fssum = addFraction(fs1, fs2);
printFraction(fssum);
}
/*第二个*/
#include <stdio.h>
#include <stdlib.h>
#define M 3
typedef struct student
{
char *id;
float mark[M];
float sum;
}stu;
stu *list;
int line;
char* GetStringUntilSpace ( char **dst, char *src )
{
char *res;
char count;
char *p;
count = 0;
for ( res = src; *res != ' ' && *res != '\n'; res++ ) count++;
p = *dst = (char*)malloc ( count + 1 );
while ( count-- > 0 ) *p++ = *src++;
*p = 0;
return res+1;
}
float StringToFloat ( char *s )
{
float res = 0;
float ten = 0.1f;
while ( *s != '.' )
{
res *= 10;
res += (float)( *s - '0' );
s++;
if ( *s == 0 )
return res;
}
s++;
while ( *s != 0 )
{
float tmp = (float)( *s++ - '0' );
res += tmp * ten;
ten *= 0.1f;
}
return res;
}
void Init ( void )
{
int i,j;
FILE *fp;
char info[256];
char *mark;
char *pInfo;
stu *pStu;
fp = fopen ( "student.txt"陪雹卖, "r" );
if ( fp == NULL )
{
fprintf ( stderr, "Cannot File the file!\n" );
exit(1);
}
for ( line = 0; NULL != fgets ( info, sizeof(info), fp ); line++);
fseek ( fp, 0, SEEK_SET );
pStu = list = (stu*) malloc ( sizeof(stu) * line );
for ( i = 0; i < line; i++ )
{
if ( NULL == fgets ( info, sizeof(info), fp ) )
{
fprintf ( stderr, "We need more information of student(s)!\n" );
exit(2);
}
pInfo = GetStringUntilSpace ( &pStu->id, info );
for ( j = 0; j < M; j++ )
{
pInfo = GetStringUntilSpace ( &mark, pInfo );
pStu->mark[j] = StringToFloat ( mark );
free(mark);
}
pStu++;
}
fclose ( fp );
}
void End ( void )
{
int i,j;
for ( i = 0; i < line; i++ )
{
free ( list[i].id );
}
free ( list );
}
void Calc ( void )
{
int i, j;
stu *pStu = list;
for ( i = 0; i < line; i++ )
{
float sum = 0;
for ( j = 0; j < M; j++ )
{
sum += pStu->mark[j];
}
pStu->sum = sum;
pStu++;
}
}
void Sort ( void )
{
int i,j;
stu tmp;
for ( i = line - 1; i >= 0; i-- )
{
for ( j = 0; j < i; j++ )
{
int k = j+1;
if ( list[k].sum > list[j].sum )
{
memcpy ( &tmp, list+j, sizeof(stu) );
memcpy ( list+j, list+k, sizeof(stu) );
memcpy ( list+k, &tmp, sizeof(stu) );
}
}
}
}
void ShowSingle ( FILE *fp, stu *pStu )
{
int i;
printf ( "%s %.2f", pStu->id, pStu->sum );
fprintf ( fp, "%s %.2f", pStu->id, pStu->sum );
for ( i = 0; i < M; i++ )
{
printf ( " %.2f", pStu->mark[i] );
fprintf ( fp, " %.2f", pStu->mark[i] );
}
}
void Print ( void )
{
int i;
stu *pStu = list;
FILE *fp;
fp = fopen ( "rank.txt", "w" );
if ( fp == NULL )
{
fprintf ( stderr, "Error in open rank.txt\n" );
exit(2);
}
printf ( "Id Sum course1 course2 course3\n" );
for ( i = 0; i < line; i++ )
{
ShowSingle ( fp, pStu++ );
fputc ( '\n', fp );
putchar ( '\n' );
}
fclose ( fp );
}
int main(void)
{
Init();
Calc();
Sort();
Print();
End();
}
2010-06-17
展开全部
给点程序说明啊.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询