c++第六章课后编程总结

6.7

1.赋值 不能在声明多个变量的同时赋值
int a_grade=b_grade=c_grade=d_grade=0;(错误)
2.switch{
case “A”:{do sth ;break;}
default : {do sth ;}}

6…1

void a_6_1(void)
{
char ch;
cout << “input … @ quit\n”;
cin.get(ch);
while(ch != ‘@’) //while(cin.get(ch)) { if (ch == ‘@’) break;}
{
if (97<=ch and ch <=122)
cout << char(ch-32) << endl;
else if (isdigit(ch)) //ctype字符函数库
{
cout << “digit”;
cin.get(ch); //要在这里重新获得字符,不然ch内容不会改变
continue;
}
else
cout << ch << endl;
cin.get(ch);
}
}

6…2

void a_6_2(void)
{
double a[10];
double average,sum=0.0;
int i=0,b,c=0;
cout << “input numbers…”;
while(cin>>a[i]) //cin>>a[i] 输入非数字时 cin 会返回false 退出循环
{
cout << a[i] << endl;
sum+=a[i];
i++;
}
average = sum/i;
cout << "sum: "<< sum << endl;
cout << "average: " << average << endl;
for (b=0;b<i;b++)
{
if (a[b] > average)
++c;
}
cout << c << “bigger than agerage\n”;
}

6…3

void a_6_3(void)
{
cout << “please enter one of the following choices: \n c) carnivore p)pianist \n t) tress g) game \n”;
char ch;
while(cin.get(ch))
{
cin.get(); //吸收 回车符
// cin.clear();
if (ch == ‘@’) break;
switch(ch)
{
case ‘c’ : {cout << “hello\n”;break;}
case ‘p’ : {cout << “helloo\n”;break;}
case ‘t’ : {cout << “trees\n”;break;}
case ‘g’ : {cout << “game\n”;break;}
default : {cout << "please enter a c,p,t,or g : ";}
}
}
}

6…4

void a_6_4(void)
{
struct bop
{
char fullname[20];
char title[20];
char bopname[20];
int preference;
};
bop a[3]={{“SUN”,“programmer”,“sun”,0},{“MM”,“trainee”,“mm”,1},{“DD”,“free”,“dd”,2}}; //结构体数组初值这样赋值
char ch;
string choice;
int i;
// a[0]={“SUN”,“programmer”,“sun”,0}; //不能这样赋值
cout<<“enter a a,b,c,d, or q\n a.name b.title\nc.bopname d.preference\nq.quit\n”;
while(cin.get(ch))
{
cin.get();
if (ch==‘q’){cout << “BYE!\n” ;break;}
switch(ch)
{
case ‘a’ :
{
choice = “fullname”;
for(i=0;i<3;i++)
{
cout<<a[i].fullname<<endl;
}
break;}
case ‘b’ : {choice = “title”;break;}
case ‘c’ : {choice = “bopname”;break;}
case ‘d’ :
{
for (i=0;i<3;i++)
{
switch(a[i].preference)
{
case 0 : {cout << a[i].fullname << endl;break;}
case 1 : {cout << a[i].title << endl;break;}
case 2 : {cout << a[i].bopname << endl;break;}
}
}
break;
}
default : {choice = “00”;cout<< “next …”;}
}

6…5

void a_6_5(void)
{
double number,tax;
cout << “enter your number …\n”;
while(cin>>number && number >=0) //
{
cin.get();
//if (number<0) {cout << “wrong number ,try again…\n”;continue;}
if (number<5000) { tax =0;}
else if (number < 15000) { tax = (number-5000)*0.1;}
else if (number < 35000) {tax = (number-15000)*0.15+1000;}
else {tax = (number - 35000)*0.2+1000+3000;}
cout << "tax is " << tax;
}
}

6…6

void a_6_6(void)
{
int num,i,j=0;
struct patron
{
string name;
double money;
};
cout << “enter the number of patron\n”;
cin >> num;
cin.get();
patron a[num];
int b[num]; //记录大于1000的人的下标
for(i=0;i<num;++i)
{
cout << “enter name and money \n”;
cin >> a[i].name;
cin >> a[i].money;
cin.get(); //吸收回车符
if (a[i].money > 1000)
{
b[j]=i;
++j;
}
}
for(i=0;i<j;i++)
{
cout << “name :” << a[b[i]].name << " money : " << a[b[i]].money << endl;
}
} // 误把 a[b[i]] 写成 a[b[j]] 报错误:Segmentation fault (core dumped) 数组越界

6…7

void a_6_7(void)
{
int vowel = 0,consonant = 0,other =0;
string word;
//char word[15];用字符数组时,无法判断q退出,word==“q” 数据结构不一致,无法判断
cout << “enter words (q to quit):”;
while (cin>>word) //cin 会以空格为断点接收
{
cout << word << endl ;
if (word == “q”){cout << ‘q’ ;break;}
else if(isalpha(word[0]))
{
if (word[0]‘a’||word[0]‘e’ || word[0]‘i’ || word[0]‘o’ || word[0]==‘u’) {++vowel;}
else ++consonant;
}
else ++other;
}
cout << vowel << endl << consonant << endl << other <<endl;
}

cin 相关问题 参考https://blog.csdn.net/bravedence/article/details/77282039

6…8

void a_6_8(void)
{
ifstream inFile;
int sum =0 ;
char ch;
string filename;
cin >> filename;
inFile.open(filename);
if (inFile.is_open())//
{
inFile >>ch ;
do
{
cout << ch;
++sum;
**inFile >>ch ;//inFile 赋给ch **
}while(inFile.good())//
cout << sum;
}
}

6…9

void a_6_6(void)
{
int num,i,j=0;
string filename;//
struct patron
{
string name;
double money;
};
ifstream inFile;
cout << “enter the filename\n”;
cin >> filename;
cin.get();
inFile.open(filename);
if (!inFile.is_open()){cout << “error”;exit(EXIT_FAILURE);}//判断文件打开有无问题
inFile >> num;
patron a[num];
int b[num];
for(i=0;i<num;++i)
{
inFile >> a[i].name;// 赋名字
inFile >> a[i].money;
if (a[i].money > 1000)
{
b[j]=i;
++j;
}
}
for(i=0;i<j;i++)
{
cout << “name :” << a[b[i]].name << " money : " << a[b[i]].money << endl;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值