上机任务三答案
1.打开程序modi3_1.c,程序存在一个错误,请修改、运行并查看结果。
#include<stdio.h>
void main()
{
int a=5,c,b=3; 求余数运算%两边应为整型数据
c=a%b;
printf("%d\n",c);
}
2.打开程序modi3_2.c,程序存在一个错误,请修改、运行并查看结果。
(注意:除了错误行,其它行不得修改)
#include<stdio.h>
void main()
{
int a=5,c;
float b=3.0;
c=a%(int)b; 强制类型转换时,类型名一定要用括号括起来
printf("%d\n",c);
}
3.打开程序modi3_3.c,程序存在一个错误,请修改、运行并查看结果。
方法一:
#include<stdio.h>
void main()
{
int a=8,b=8,c;
在变量定义中“a=b=8”为非法赋值
c=a+b;
printf("%d\n",c);
}
或者
方法二:
#include<stdio.h>
void main()
{
int a,b,c;
a=b=8; 合法的赋值语句
c=a+b;
printf("%d\n",c);
}
4.打开程序modi3_4.c,程序要求输出1000和25000;程序存在两个错误,请
修改、运行并查看结果。
#include<stdio.h>
void main()
{
float a,b;
a=1e+3; 用指数形式表示时,尾数部分不能空,指数部分必须为整数
b=0.25e5;
printf("%f\n%f\n",a,b);
}
5.打开程序modi3_5.c,程序填空题,程序要求:从键盘上输入一个小写字母(用getchar函数),要求输出大写字母(使用putchar函数);请修改、运行并查看结果。
#include<stdio.h>
void main()
{
char ch;
ch=getchar(); /* Fill Blank */
putchar(ch-32); /* Fill Blank */
putchar('\n');
}
6.打开程序modi3_6.c,程序填空题,程序要求:输入华氏温度F,利用公式C=5/9*(F-32)转换成摄氏温度C,要求输出保留两位小数。
例如输入100,输出37.78 。
#include <stdio.h>
void main()
{float f,c;
scanf("%f",&f);
c=5.0/9*(f-32);
printf("%6.2f\n",c);
}
注意:温度转换公式不能写为5/9。
7.新建程序prog3_1.c,实现以下内容:从键盘上输入一个整数x,输出=1的值,例如:输入2,输出y=9.8。
+
x
y x+
e
#include<stdio.h>
#include<math.h> 注意:若要使用数学函数,必须包含数学函数库。
void main()
{
int x;
float y;
scanf("%d",&x);
y=1+exp(x)+sqrt(x);
printf("y=%.1f\n",y);
}
8.新建程序prog3_2.c,填空实现以下内容:从键盘上输入一个字母,输出字母表中该字母的下一个字母,例如输入“A”,输出“B”;再输入“Z”,观察结果。
#include <stdio.h>
void main()
{
char a;
a=getchar();
putchar(a+1);
putchar('\n');}
或者:
#include <stdio.h>
void main()
{
char a;
scanf("%c",&a);
printf("result:%c\n",a+1);
}
注意:字符型数据的输入输出控制符。