第3周 运算的流程控制
一、程序流程控制
二、for语句
#include<iostream> #include<stdio.h> #include<cmath> using namespace std; int main() { int sum=0,i,n; cin>>n; for(i=1;i<=n;i++) sum=sum+i; cout<<sum<<endl; return 0; }
#include<iostream> #include<stdio.h> #include<cmath> using namespace std; int main() { int n=9,i,j; for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { cout<<j<<"*"<<i<<"="<<j*i<<"\t"; } cout<<endl; } return 0; }
三、一维数组
#include<iostream> #include<stdio.h> #include<cmath> using namespace std; int main() { char str[10]; int i=0; cin>>str; while(str[i]!='\0') { if (str[i]>=97&&str[i]<=122) { str[i]=str[i]-32;//ascii码转换 } i=i+1; } cout<<str<<endl; return 0; }
四、二维数组
#include<iostream> #include<stdio.h> #include<cmath> using namespace std; int main() { const int m=2,n=3; int a[m*n],b[m][n]; int i,j,x=1; for(i=0;i<6;i++,x++) { a[i]=x; } x=1; for(i=0;i<2;i++) { for(j=0;j<3;j++,x++) { b[i][j]=x; cout<<a[i*n+j]<<"\t"<<b[i][j]<<endl; } } return 0; }
#include <iostream> using namespace std; int main() { char month[12][10]={"January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"};//月份和英文对应起来 int m; cin>>m; if(m>0&&m<13) cout<<month[m-1]; else cout<<"The month is wrong"; return 0; }
五、结构体
五、单步调试
设置断点----调试-------添加查看-------下一步
#include <iostream> using namespace std; int main() { struct telelist/、每个结构体的具体内容 { char name[8];//名字 char sex;//性别 char num1[5];//第一个电话 char num2[5];//第二个电话 }list1[3];//结构体名 int i; for(i=0;i<=2;i++)//赋值 { cin>>list1[i].name>>list1[i].sex>>list1[i].num1>>list1[i].num2; } for(i=2;i>=0;i--)//逆序输出 { cout<<list1[i].name<<"/"<<list1[i].sex<<"/"<<list1[i].num1<<"/"<<list1[i].num2<<endl; } return 0; }
int main() { struct data { int year,month,day; }; struct data today={2018,3,15}; cout<<today.year<<"-"<<today.month<<"-"<<today.day<<endl; return 0; }
#include <iostream> using namespace std; int main() { struct data { int year,month,day; }; struct data days[]={{2018,3,14},{2018,3,15}}; cout<<days[0].day<<endl; return 0; }