标签:style blog io ar color os 使用 sp for
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 char ch; 6 int spaces=0,total=0; 7 cin.get(ch); 8 while(ch !=‘.‘)//注意使用单引号。 9 { 10 if(ch ==‘ ‘)spaces++; 11 total++; 12 cin.get(ch); 13 } 14 cout<<spaces<<" sapces, "<<total<<" characters total in sentence"<<endl; 15 return 0; 16 }
||和&&
1 #include<iostream> 2 using namespace std; 3 const int size=6; 4 int main() 5 { 6 float a[size]; 7 cout<<"enter value,when you make "<<size<<" enteries or enter a negetive"<<endl; 8 int i=0; 9 float temp; 10 cout<<"frist value"; 11 cin>>temp; 12 while(i<size&&temp>=0) 13 { 14 a[i]=temp; 15 i++; 16 if(i<size) 17 { 18 cout<<"next value:"; 19 cin>>temp; 20 } 21 } 22 if(i==0)cout<<"no data"<<endl; 23 else 24 { 25 cout<<"enter your naaq"; 26 float you; 27 cin>>you; 28 int count=0; 29 for(int j=0;j<i;j++)if(a[j]>you)count++; 30 cout<<count; 31 cout<<" of your neighbors have greater awarense of the new age then you do"; 32 } 33 return 0; 34 }
||是顺序点,也就是说,先修改左侧的值 ,在对右侧的值进行判定。
如 i++<||i==j;
&&也是顺序点。
?运算符:
x=(x>y)?x:y;
switch语句
1 switch(interger-expression) 2 { 3 case label1:statement(a); 4 case label2:statement(b); 5 … 6 case labeln:statement(n); 7 default:statement(s); 8 }
interger-expression为一个结果为整数值的表达式。
每个标签都必须是整数。
依次执行之后的所有语句,可以使用break停止。
1 #include<iostream> 2 using namespace std; 3 void showmenu(); 4 void report(); 5 void comfort(); 6 int main() 7 { 8 char choice; 9 cin>>choice; 10 while(choice!=‘q‘&&choice!=‘Q‘) 11 { 12 switch(choice) 13 { 14 case ‘a‘: 15 case ‘A‘:cout<<"a and A"; 16 break; 17 case‘b‘: 18 case‘B‘:cout<<"b and B"; 19 break; 20 case‘c‘: 21 case‘C‘:cout<<"c and C"; 22 break; 23 case‘d‘: 24 case‘D‘:cout<<"d and D"; 25 break; 26 default:cout<<"that is not a choice"<<endl; 27 } 28 cin>>choice; 29 } 30 return 0; 31 }
标签:style blog io ar color os 使用 sp for
原文地址:http://www.cnblogs.com/taoxiuxia/p/4148830.html