Untitled Document

三：编程及简答题
1: 答：
#include <stdio.h>
int main()
{
int i,j;

for (i=2; i<=100; i++)
{
for (j=2; j<i; j++)
{
if (i%j == 0)
break;
}
if (j==i)
{
printf("%d 是素数\n", i);
}
}
printf("按任意键结束");
return 0;
}
2: 答：
#include <stdio.h>
#include <iostream.h>

int main()
{
unsigned char iChar, sString[512];
int i = 0;

while (true && i<511)
{
printf("请输入一个能显示的字符(q或Q退出)：");
cin>>iChar;
if (iChar == 'q' || iChar == 'Q')
break;
else if (iChar <='9' && iChar >= '0')
printf("%c为数字\n", iChar);
else
{
switch(iChar)
{
case 'a':
case 'A':
printf("The first char of word 'an'\n");
break;
case 'b':
case 'B':
printf("The first char of word 'big'\n");
break;
case 'c':
case 'C':
printf("The first char of word 'china'\n");
break;
case 'd':
case 'D':
printf("The first char of word 'decad'\n");
break;
case 'e':
case 'E':
printf("The first char of word 'english'\n");
break;
case 'f':
case 'F':
printf("The first char of word 'flash'\n");
break;
case 'g':
case 'G':
printf("The first char of word 'great'\n");
break;
case 'h':
case 'H':
printf("The first char of word 'happy'\n");
break;
case 'i':
case 'I':
printf("The first char of word 'integer'\n");
break;
case 'j':
case 'J':
printf("The first char of word 'judge'\n");
break;
case 'k':
case 'K':
printf("The first char of word 'kick'\n");
break;
case 'l':
case 'L':
printf("The first char of word 'large'\n");
break;
case 'm':
case 'M':
printf("The first char of word 'manage'\n");
break;
case 'n':
case 'N':
printf("The first char of word 'nurse'\n");
break;
case 'o':
case 'O':
printf("The first char of word 'ok'\n");
break;
case 'p':
case 'P':
printf("The first char of word 'public'\n");
break;
case 'r':
case 'R':
printf("The first char of word 'repair'\n");
break;
case 's':
case 'S':
printf("The first char of word 'save'\n");
break;
case 't':
case 'T':
printf("The first char of word 'test'\n");
break;
case 'u':
case 'U':
printf("The first char of word 'unable'\n");
break;
case 'v':
case 'V':
printf("The first char of word 'valid'\n");
break;
case 'w':
case 'W':
printf("The first char of word 'west'\n");
break;
case 'x':
case 'X':
printf("The first char of word 'xanthin'\n");
break;
case 'y':
case 'Y':
printf("The first char of word 'yes'\n");
break;
case 'z':
case 'Z':
printf("The first char of word 'zoo'\n");
break;
default:
printf("忽略输入的为控制字符！\n");
continue;
}
}
sString[i] = iChar;
i++;
}
sString[i] = '\0';
printf("\n输入的字符串为:%s\n", sString);
printf("按任意键结束");
return 0;
}
3: 答：
#include <iostream.h>

void main()
{
char c;
while(true)
{
cout << "MAIN MENU:" << endl;
cout << "l: left, r: right, q: quit -> ";
cin >> c;
if (c == 'q') // while结束
break;
switch(c)
{
case 'l':
cout << "LEFT MENU:" << endl;
cout << "select c or d: ";
cin >> c;
switch(c)
{
case 'c':
cout << "you chose 'c'" << endl;
break;
case 'd':
cout << "you chose 'd'" << endl;
break;
default:
cout << "you didn't choose c or d!" << endl;
break;
}
break;
case 'r':
cout << "RIGHT MENU:" << endl;
cout << "select a or b: ";
cin >> c;
switch(c)
{
case 'a':
cout << "you chose 'a'" << endl;
break;
case 'b':
cout << "you chose 'b'" << endl;
break;
default:
cout << "you didn't choose a or b!" << endl;
break;
}
break;
default:
cout << "you must type l or r or q!" << endl;
break;
}
}
cout << "quitting menu..." << endl;
}
4: 答：是等待输入一个字符，如果是字符b，则响铃，否则，如果是回车，则输出what a boring select on...，不是，则输出bye!。
5: 答：
#include <iostream.h>
void main( )
{
long a,b,c;
cout<<"输入三个数"<<endl;
cin>>a>>b>>c;
if (a<b)
if (b<c)
cout <<c;
else
cout<<b;
else
if (a<c)
cout<<c;
else
cout<<a;
}
6: 答：程序段1中else 与第二个if 配对，程序段2中else 与第一个if 配对。
7: 答：
程序段1接受键盘输入，直到输入的是Y,y,N,n为止。
程序段2是一个死循环。
程序段3当i为0时,结束循环。
程序段4求1+2+3+……+100的和。
8: 答：
#include <iostream.h>
#include <stdio.h>
void main( )
{
char c;
int letters=0,digits=0,others=0;
cout<<"Please input a line charaters\n";
while ((c=getchar( ))!='\n')
{
if (c>='a' && c<='z' || c>='A' && c<='Z' )
letters++;
else
if (c>='0' && c<='9')
digits++;
else
others++;
}
cout<<"letters:"<<letters<<endl
<<"digits"<<digits<<endl
<<"others"<<others<<endl;
}
9: 答：
#include <iostream.h>
void main()
{
int i=0;
double sum=0.0,limit;
cout <<"Please input limit:\n";
cin>>limit;
do {
i++;
sum+=1.0/i;
cout<<i<<" "<<sum<<endl;
} while (sum<limit);
cout<<i;
}
10：答：
#include <iostream.h>
void main()
{
int i1=1,i2=1,i3,i;
cout<<i1<<' '<<i2<<' ';
for (i=3;i<=20;i++)
{
i3=i1+i2;
cout<<i3<<' ';
i1=i2;
i2=i3;
}
}
11：答：
#include <iostream.h>
void main ( )
{
int sco, grade;
cout<<"please input a num\n";
cin>>sco;
grade=sco/10;
switch (grade)
{
case 10:
case 9: cout<<"优"; break;
case 8: cout<<"良"; break;
case 7: cout<<"中"; break;
case 6: cout<<"及格"; break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0: cout<<"不及格"; break;
default :cout<<"输入错误\n"; break; //break 可省略
}
}
12：答：
#include <iostream.h>
#include <stdio.h>
void main( )
{
char c;
int count=0;
while ((c=getchar())!='\n')
{
switch (c) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': count++;break;
}
}
cout<<count<<endl;
}