中国大学MOOC-哈工大-C语言程序设计精髓第六-十二周编程题答案_

中国大学MOOC-哈工大-C语言程序设计精髓第六-十二周编程题答案_


2024年5月20日发(作者:)

6.1

下面代码的功能是将百分制成绩转换为5分制成绩,具体功能是:如果用户输入的是非法

字符或者不在合理区间内的数据(例如输入的是a,或者102,或-45等),则程序输出 Input error!,

并允许用户重新输入,直到输入合法数据为止,并将其转换为5分制输出。目前程序存在错误,

请将其修改正确。并按照下面给出的运行示例检查程序。

#include

#include

int main()

{

char score[100];

int flag = 0, i, s;

char grade;

printf("Please input score:n");

while (1)

{

flag=0;

scanf("%s", score);

for (i = 0; i < strlen(score); i++)

{

if (score[i] >= '0' && score[i] <= '9')

{

continue;

}

else

{

flag = 1;

break;

}

}

s = atoi(score);

if (s < 0 || s > 100 || flag == 1)

{

printf("Input error!n");

printf("Please input score:n");

continue;

}

else{

break;

}

}

s = atoi(score);

if (s >= 90)

}

{

grade = 'A';

}

else if (s >= 80)

{

grade = 'B';

}

else if (s >= 70)

{

grade = 'C';

}

else if (s >= 60)

{

grade = 'D';

}

else

{

grade = 'E';

}

printf("grade: %cn", grade);

return 0;

6.2 编程计算a+aa+aaa+…+aa…a(n个a)的值(4分)

题目内容:

编程计算 a+aa+aaa+…+aa…a(n个a)的值,n和a的值由键盘输入。例如,当n=4,a=2,表示计算

2+22+222+2222的值。

#include

#include

int main()

{

int n,a,i,j;

double p=0,q=0;

printf("Input a,n:n");

scanf( "%d,%d",&a,&n);

for(i=1;i<=n;i++)

{

for(j=0,p=0;j

{

p=p+a*pow(10,j);

}

q=p+q;

}


发布者:admin,转转请注明出处:http://www.yc00.com/news/1716191568a2726607.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信