Q1.条件运算符与错误防范
将variable == value 反转为value == variable,当少输入一个等号时,编译器会报错,有助于几时发现错误。
Q2.逻辑OR运算符||
||的优先级比关系运算符低,所以不需要使用括号。
编译器想修改左侧的值,再对右侧的值进行判定;如果左侧表达式为true,则不判定右侧表达式。
Q3.逻辑AND运算符&&
同上,如果左侧表达式为false,则不判定右侧表达式。
需要注意的是,!运算符的优先级高于所有的关系运算符与算术运算符,并且&&的优先级高于||。
Q4.可以使用枚举量enum来充当switch中的case
可以在switch语句或任何循环中使用break语句,是程序跳到switch或循环后面的语句执行。
continue语句用于循环中,让程序跳过余下的代码,并开始新一轮的循环。但不会跳过循环的更新表达式。
Q5.如何处理用户的输入错误
while(!(cin >> golf[i]))
{
cin.clear();
while(cin.get() != '\n')
continue;
cout << " please enter a number: ";
}
Q6.将string 转换为 char数组
char* zhuanhuan(string src)
{
char *dst = new char[255];
int i;
for(i=0;i <=src.length();i++)
dst[i]=src[i];
dst[i] = '\0';
return dst;
}
课后练习
// exam6.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "iostream"
#include "cctype"
#include <string>
using namespace std;
char* zhuanhuan(string src)
{
char *dst = new char[255];
int i;
for (i = 0; i <= src.length(); i++)
dst[i] = src[i];
dst[i] = '\0';
return dst;
}
int main()
{
e1 编写一个程序,读取键盘输入,知道遇到@为止,并回显输入(数字除外),同时将大写字符
转换为小写字符,小写字符转换为大写字符
cout << endl << "enter :";
char ch;
cin >> ch;
while (ch != '@' )
{
if (isdigit(ch))
{
//continue;
}
else if (islower(ch))
{
cout << (char)toupper(ch);
}
else if (isupper(ch))
{
cout << (char)tolower(ch);
}
else
{
cout << ch;
}
cin >> ch;
//cout << strupr(&ch);
}
e2 编写一个程序,最多将10个donation值读入一个double数组中,程序遇到非数字输入时
将结束输入,,并报告这些数字的平均值以及数组中有多少个数字大于平均值。
double donation[10];
cout << "enter ten numbers:";
int t = 0;
while (t < 10 )
{
if (!(cin >> donation[t]))
{
cout << " *****";
break;
}
t++;
}
int temp = 0;
for (int i = 0;i < t;i++)
{
temp = temp +donation[i];
}
int num = 0;
for (int i = 0; i < t; i++)
{
if (donation[i] > (temp / t))
num++;
}
cout << "count is :" << temp / 10 << "up num is :" << num;
e3 编写一个菜单驱动程序的雏形,该程序显示一个提供四个选项的菜单,每个选项用一个
字母标记,如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母
直到用户这样做为止
cout << endl << "please enter one of the following choices:";
cout << endl << "c)carnivore p)pianist";
cout << endl << "t)tree g)game";
char choice;
while (true)
{
cin >> choice;
switch (choice)
{
case 'c':
cout << "a maple is a carnivore ";
break;
case 'p':
cout << "a maple is a pianist ";
break;
case 't':
cout << "a maple is a tree ";
break;
case 'g':
cout << "a maple is a game ";
break;
default:cout << "please enter a c,p,t,or g:";
}
}
e4 在王国,货币单位是tvarp,收入所得税的计算方式如下:
5000 不收税;5001~15000 10%;150001~35000 15%
35000以上 20%;请编写一个程序要求用户输入收入,并报告所得税。但用户输入负数或者
非数字时,循环结束
int money = 0;
while (cin >> money && tex(money))
{
cout << "your tex is:" << tex(money);
}
//e5 编写一个程序,它每次都读取一个单词,直到用户只输入q,然后,该程序指出有多少个单词
//以元音打头,有多少个单词以辅音打头,还有多少个单词不属于这两类。
string sword;
cout << "enter words (q to quit)";
cin >> sword;
//getline(cin,sword);
int yuan = 0; int fu = 0;
while (sword != "q")
{
char *cword = zhuanhuan(sword);
//cout << *cword;
//cout << isalpha(*cword);
//if(*cword >= '')//不做了太麻烦
cin >> sword;
//getline(cin, sword);
}
cin.get();
cin.get();
system("pause");
return 0;
}
int tex(int money)
{
if (money <=0)
{
return -1;
}
if (money <= 5000 && money > 0)
return 0;
else if (money >= 5001 && money <= 15000)
{
return money*0.1;
}
else if (money >= 15001 && money <= 35000)
{
return money*0.15;
}
else if (money >= 35001)
{
return money*0.2;
}
return 0;
}