C++学习笔记

//forstr2.cpp -- reversing an array
#include<iostream>
#include<string>
int main(){
  using namespace std;
  cout<<"Enter a word:";
  string word;
  cin>>word;
  //physically modify string object
  char temp;
  int i,j;
  for(j=0,i=word.size()-1;j<i;--i,++j){
    temp=word[i];
    word[i]=word[j];
    word[j]=temp;
  }
  cout<<word<<"\nDone\n";
  system("pause");
  return 0;
}

在这里插入图片描述

//equal.cpp --equality vs assignment
#include<iostream>
int main(){
  using namespace std;
  int quizscores[10]={20,20,20,20,20,19,20,18,20,20};
  cout<<"Doing it right:\n";
  int i;
  for(i=0;quizscores[i]==20;i++)
  cout<<"quiz "<<i<<" is a 20\n";
  cout<<"Doing it dangerously wrong:\n";
  for(i=0;quizscores[i]=20;i++)
  cout<<"quiz "<<i<<" is a 20\n";
  return 0;
}
//compstr1.cpp -- comparing strings using arrays
#include<iostream>
#include<cstring>
int main(){
  using namespace std;
  char word[5]="?ate";
  for(char ch = 'a';strcmp(word,"mate");ch++){
    cout<<word<<endl;
    word[0]=ch;
  }
  cout<<"After loop ends,word is"<<word<<endl;
  system("pause");
  return 0;
}

在这里插入图片描述

//compstr2.cpp --comparing strings using arrays
#include<iostream>
#include<string>
int main(){
  using namespace std;
  string word = "?ate";
  for(char ch='a';word!="mate";ch++){
    cout<<word<<endl;
    word[0]=ch;
  }
  cout<<"After loop ends,word is"<<word<<endl;
  system("pause");
  return 0;
}

在这里插入图片描述

//5.2while循环
#include<iostream>
const int ArSize = 20;
int main(){
  using namespace std;
  char name[ArSize];
  cout<<"Your first name,please: ";
  cin>>name;
  cout<< "Here is your name,verticalized and ASCIIized:\n";
  int i = 0;
  while (name[i]!='\0')
  {
    /* code */
    cout<<name[i]<<":"<<int(name[i])<<endl;
    i++;
  }

  system("pause");
  return 0;
}

运行结果:
在这里插入图片描述

//5.14演示了如何利用clock()和头文件ctime来创建延迟循环
#include<iostream>
#include<ctime>
int main(){
  using namespace std;
  cout<<"Enter the delay time, in seconds: ";
  float secs;
  cin>>secs;
  clock_t delay = secs * CLOCKS_PER_SEC;//convert to clock ticks
  cout<<"starting\a\n";
  clock_t start = clock();
  while (clock()-start<delay)
  {
    /* code */

  }
  cout<<"done \a\n";
  system("pause");
  return 0;
}

输出结果:
在这里插入图片描述

//5.15使用do while
#include<iostream>
int main(){
  using namespace std;
  int n;

  cout<<"Enter numbers in the range 1-10 to find ";
  cout<<"my favorite number\n";
  do{
    cin>>n; 
  }while ( n!=7);
  cout<<"Yes, 7 is my favorite.\n";
  system("pause");
  return 0;
}

输出截图:
在这里插入图片描述

//报告处理的总字符数
#include<iostream>
int main(){
  using namespace std;
  char ch;
  int count = 0;
  cout<<"Enter characters;enter # to quit:\n";
  cin>>ch;
  while ((ch != '#'))
  {
    cout<<ch;
    ++count;
    cin>>ch;
    /* code */
  }
  cout<<endl<<count<<" characters read\n";
  system("pause");
  return 0;
}

输出截图:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值