1. 使用printf() 函数显示下列菜单:
Menu
===================================
1. Input the students’ names and scores
2. Search scores of some students
3. Modify scores of some students
4. List all students’ scores
5. Quit the system
===================================
Please input your choise (1-5):
/*
本程序有问题,输入姓名和分数后提示有问题需关闭,估计问题在字符串的输入处
*/
#include <stdio.h>
#include <stdlib.h>
int select(int n);
char name[10];
float score;
void main(){
int n;
printf("/t/t/tmenu/n");
printf("************************************************/n");
printf("1. Input the students’ names and scores/n");
printf("2. Search scores of some students/n");
printf("3. Modify scores of some students/n");
printf("4. List all students’ scores/n");
printf("5. Quit the system/n");
printf("************************************************/n");
printf("Please input your choise (1-5): ");
do{
scanf("%d",&n);
}while(n>5||n<1);
select(n);
}
int select(int n){
switch(n){
case 1:{
printf("please input the student's name and scores:");
scanf("%s%f",name,score);
printf("**************************************************/n");
printf("name/t/tscore/t/n");
printf("%s/t/t%f",name,score);
printf("**************************************************/n");
break;
}
case 2:{
printf("please input the student's name:");
scanf("%s",name);
break;
}
case 3:{
printf("please input the student's new name and new scores:");
scanf("%s%f",name,score);
break;
}
case 4:{
printf("please input the student's new name and new scores:");
scanf("%s%f",name,score);
break;
}
case 5:{
system("exit");
}
}
return n;
}
2. 参照例1.7和例1.8,选择一种方法编写一个程序,实现输入
六个数输出最小数。
#include <stdio.h>
void main(){
int num1,num2,num3,num4,num5,num6;
int min;
printf("please input 6 number:");
scanf("%d%d%d%d%d%d",&num1,&num2,&num3,&num4,&num5,&num6);
min=num1;
if(min>num2){
min=num2;
}
if(min>num3){
min=num3;
}
if(min>num4){
min=num4;
}
if(min>num5){
min=num5;
}
if(min>num6){
min=num6;
}
printf("%d/n",min);
}
3. 编写一个程序,从键盘上输入华氏温度,屏幕显示对应的
摄氏温度。华氏温度和摄氏温度的转换公式为:
c=(f-32)/1.8
#include <stdio.h>
int main(){
float tem1,tem2;
printf("Please input the temperature:");
scanf("%f",&tem1);
tem2=(tem1-32)/1.8;
printf("the temperature is %f",tem2);
}
4. 编程输出字符0、9、A、Z、a、z的ACSII码的十进制、八进制和十六进制的表示形式。
#include <stdio.h>
int main(){
int a=0;
int b=9;
char c='a';
char d='A';
char e='z';
char f='Z';
printf("0/t%o/t%d/t%x/n",a,a,a);
printf("9/t%o/t%d/t%x/n",b,b,b);
printf("a/t%o/t%d/t%x/n",c,c,c);
printf("A/t%o/t%d/t%x/n",d,d,d);
printf("z/t%o/t%d/t%x/n",e,e,e);
printf("Z/t%o/t%d/t%x/n",f,f,f);
}
5. 参考例L2_6.C编写一个程序,从键盘输入字符(例如’1’),转换成十进制数(即1),并输出。
#include <stdio.h>
void main(){
char a;
int b;
scanf("%c",&a);
b= ((int)a) - 48;
printf("the number is %d",b);
}
提示:“1”的ASCII码为十进制数49,将其减去一个数等于十进制1即可。
6. 已知a=3,b=2,c=2.5,计算(float)(a+b)/3+(int)c的值。
#include <stdio.h>
int main(){
int a=3;
int b=2;
float c=2.5;
float d;
d=(float)(a+b)/3+(int)c;
printf("the d is %f",d);
}
7. 编写一个程序输出5!、10!的结果。
#include <stdio.h>
void main(){
int i;
int n;
int s=1;
printf("please input a number:");
scanf("%d",&n);
for(i=1;i<=n;i++){
s=i*s;
}
printf("%d/n",s);
}
8. 参考例L2_8.C编写一个程序,输入2个学生的姓名、学号、英语、数学、计算机成绩,输出这两个学生的姓名、学号和平均分。
#include<stdio.h>
void main()
{
long num;
char name[12];
float math,english,computer;
float average;
int n;
for(n=1;n<=2;n++)
{
printf("please input a student's number:");
scanf("%ld",&num);
printf("please input the student's name:");
scanf("%s",name);
do
{
printf("please input the student's score(math english computer):");
scanf("%f%f%f",&math,&english,&computer);
}while((math<0||math>100)||(english<0||english>100)||(computer<0||computer>100));
average=(math+english+computer)/3;
printf("the %s average is %f/n",name,average);
}
}