第七次作业

1.

11-3:

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int main()
{
    double values[]={1.23,35.36,653.7,4358.24};//定义double数组values【】 
    string names[]={"zoot","timmy","al","stan"};//定义string字符串数组name【】 
    for(int i=0;i<4;i++)
    {
        cout<<setiosflags(ios_base::left)//开启左对齐 
        <<setw(6)<<names[i]//输出 
        <<setw(10)<<values[i]//输出 
        <<resetiosflags(ios_base::left)//恢复默认值 
        <<endl;
    }
    return 0;
}

11-4:

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int main()
{
    double values[]={1.23,35.36,653.7,4358.24};
    string names[]={"Zoot","Jimmy","Al","Sten"};//定义数组 
    for(int i=0;i<4;i++)
    {
        cout<<setiosflags(ios_base::left)//左对齐 
        <<setw(6)<<names[i]//输出宽度为6的string类数组names 
        <<resetiosflags(ios_base::left)//重新恢复默认值 
        <<setw(10)<<setprecision(1)//改变精度为1位有效数字 
        <<values[i]<<endl;//输出double型数组values 
    }
    return 0;
 } 

11-7:

#include<iostream>
using namespace std;
int main()
{
    char ch;//定义一个字符型变量ch 
    while((ch=cin.get())!=EOF)//输入变量ch的值,并且cin.get()函数是指输入数据并包含空白字符 
    {
        cout.put(ch) ;//输出ch 
     } 
     return 0;//结束 
 } 

2.

1.

 #include<iostream>
 #include<fstream>
 #include<string>
  #include<cstdlib>
  #include<ctime>
  const int MAX = 83;
  using namespace std;
  
  int main() {
     ifstream is("listr.txt");//打开文件
   if (!is) {
           cout << "ERROR" << endl;
      return 1;
         }
     string STU[MAX];
    string str;
     int i=0;
     while (getline(is, str)) //逐行读取,直到结束
     {
        STU[i]=str;
        i++;
     }
    ofstream roll("roll.txt");
    srand(time(NULL));//设置种子值
     int a;
     for (int i = 0; i<5; i++)
    {
         a = rand() % MAX + 1;//生成随机数
         cout << STU[a] << endl;
        roll<< STU[a] << endl;
     }
     is.close();
     roll.close();
     return 0;
 }

2.

 #include<iostream>
 #include<string>
 #include<fstream>
 using namespace std;
  int main()
  {
     string papa;
     cout << "输入地址文件名:" << endl;
      cin >> papa;
     ifstream in("papa");
    if (!in) {
         cout << "打开错误" << endl;
         return 1;
     }
     long line = 1, word = 0, ch = 0;//行数,单词数,字符数 
     char c;
     while (in.get(c))
     {
             ch++;
             if ((c < 'A' || c > 'Z'&&c < 'a' || c > 'z')&&c!='\n')
                 ++word;
             if (c == '\n')
                 ++line;
     }
     cout << "字符数:" << ch << endl << "单词数:" << word << endl << "行数:" << line<<endl;
     in.close();
     return 0;
 }

 

posted @ 2018-06-20 22:30  菜鸡少年  阅读(112)  评论(0编辑  收藏  举报