7th time C + + experiment

Source: Internet
Author: User

One. Experimental conclusion:

1. Basic part:

(1) 11-7

#include <iostream>using namespacestd;intMain () {ios_base::fmtflags original_flags=cout.flags ();//Toggle the declaration of the output mode classcout<<812<<"|"; COUT.SETF (Ios_base::left,ios_base::adjustfield);//determines the alignment direction of the output content is left justifiedCout.width (Ten);//Determine the output content width is ten cout<<813<<815<<"\ n"; COUT.UNSETF (Ios_base::adjustfield);//Clear AlignmentCout.precision (2);//Determine the output numeric accuracy is two digits after the decimal pointCOUT.SETF (ios_base::uppercase|ios_base::scientific);//Convert to scientific notation modecout<<831.0; Cout.flags (original_flags);//Switch back to standard output mode;    return 0;}

(2) 11-3

Code:

#include <fstream>usingnamespace  std; int Main () {    ofstream file1 ("test1.txt");    File1<<"Thefile has been written successfully";    File1.close ();     return 0 ;}

Conclusion:

(3) 11-4

Code:

#include <fstream>#include<iostream>usingnamespace  std; int Main () {    char  ch;    Ifstream file2 ("test1.txt");      while (File2. Get (CH))    {        cout<<ch;    }    File2.close ();     return 0 ;}

Conclusion:

2. Application section

(1) Code

#include <iostream>#include<cstdlib>#include<string>#include<fstream>using namespacestd;structstudent{stringnum; stringSnum; stringname; stringCLA;} a[ $];using namespacestd;intMain () {inti,j,n=0; Ifstream Fin ("List.txt");  while(fin>>a[n].num>>a[n].snum>>a[n].name>>A[n].cla) {N++;    } fin.close (); Ofstream Fout ("Roll.txt");  for(i=0;i<5; i++) {J=rand ()%n+1; cout<<a[j].num<<" "<<a[j].snum<<" "<<a[j].name<<" "<<a[j].cla<<Endl; Fout<<a[j].num<<" "<<a[j].snum<<" "<<a[j].name<<" "<<a[j].cla<<Endl;    } fout.close (); return 0;}

Error:

Note: This question I studied for nearly 2 days, but also borrowed from the other students of the program, or did not find a good solution, begging the help of the big boys ....

(2) code (borrowed from the idea of the big guy)

#include <iostream>#include<fstream>#include<string>using namespacestd;intMain () {Ifstreaminch("Article.txt"); intLine=0, word=0, num=0; strings; inti,m;  while(Getline (inch, s)) { Line++; M=s.size (); Num=num+m;  for(i=0; i<m;i++)        {            if((s[i]>='A'&&s[i]<='Z')|| (s[i]>='a'&&s[i]<='Z'))            {                if((s[i+1]<='A'|| s[i+1]>='Z') && (s[i+1]<='a'|| s[i+1]>='Z')) {Word=word+1; } }}} cout<<"The article has"<<line<<"lines,"<<word<<"words,"<<num<<"Letters."; return 0;}

Idea: Read line by line, count the number of rows according to the number of reads, and then count the number of characters and the number of words per row (the number of words in the last letter of each word +1), calculate the sum of the number of characters per line and the sum of words.

Results:

Two. Experimental Analysis and discussion:

From my personal point of view, this experiment is the most difficult experiment of the semester, the basic part of the relatively smooth, to the application part of the card is not .... Application part of the two questions I have been more or less to learn from the other students of the ideas and methods, but I have to do a similar problem to ensure that I can draw on the students ' ideas and methods of thoroughly understand, I think this method of learning efficiency is good 、、、、、

7th time C + + experiment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.