zoukankan      html  css  js  c++  java
  • array and structure

    #include <iostream>
    using namespace std;
    
    const int SLEN = 20;
    struct student {
        char fullname[SLEN];
        char hobby[SLEN];
        int ooplevel;
    };
    
    int getinfo(student pa[], int n)
    {
        int total = 0;
        for(int i = 0; i < n; i++)
        {
            cout << "Enter your " << i + 1 << "#: ";
            if(!(cin >> pa[i].fullname >> pa[i].hobby >> pa[i].ooplevel))
            {
                cin.clear();
                while(cin.get() != '\n')
                    continue;
                cout <<"Bad input!" << endl;
                break;
            }
            else total++;
        }
        return total;
    }
    
    void display1(student st)
    {
        cout << "tradional way:"<<endl;
        cout << "student name: "<<st.fullname << endl;
        cout <<"student hobby: "<< st.hobby << endl;
        cout << "student level: "<< st.ooplevel << endl;
        cout << "------------------------------"<< endl;
    }
    
    void display2(const student *st)
    {
        cout << "Use pointer:"<<endl;
        cout << "student name: "<<st-> fullname << endl;
        cout <<"student hobby: "<< st-> hobby << endl;
        cout << "student level: "<< st-> ooplevel << endl;
        cout << "------------------------------"<< endl;
    }
    
    void display3(const student pa[], int n)
    {
        for(int i =0; i < n; i++)
        {
            cout << i+1 << "# student infomation: " << pa[i].fullname << endl;
            cout << "Hobby: " << pa[i].hobby << endl;
            cout << "Level: " << pa[i].ooplevel << endl;
            cout << "---------------------------------"<< endl;
        }
    }
    
    int main()
    {
        cout << "Enter your class size: ";
        int class_size;
        cin>> class_size;
        while(cin.get()!= '\n')
            continue;
    
        student * ptr_stu = new student [class_size];
        int entered = getinfo(ptr_stu, class_size);
        for(int i = 0; i<entered; i++)
        {
            display1(ptr_stu[i]);
            display2(&ptr_stu[i]);
        }
        display3(ptr_stu, entered);
        delete [] ptr_stu;
        cout << "Done!\n";
        return 0;
    }
    

     

    Here is the official key below:

    int getinfo(student pa[], int n)
    {
    	int		num_array_elem = n;
    	char	tmp[SLEN];
    	for (unsigned i = 0; i < n; ++i) {
    		cout << "Your name: ";
    		cin.getline(tmp, SLEN);
    		bool	blank_line = true;
    		for (unsigned j = 0; j < strlen(tmp); ++j) {
    			if (!isspace(tmp[j])) {
    				blank_line = false;
    				break;
    			}
    		}
    		if (blank_line) {
    			num_array_elem = i;
    			break;
    		}
    		strcpy(pa[i].fullname, tmp);
    
    		cout << "Hobby:";
    		cin.getline(pa[i].hobby, SLEN);
    
    		cout << "OOP score: ";
    		cin >> pa[i].ooplevel;
    		cin.get();
    	}
    
    	return (num_array_elem);
    }
    

      

  • 相关阅读:
    c学习第3天
    [BZOJ2124] 等差子序列
    CF710F String Set Queries
    Cow Hopscotch (dp+树状数组优化)
    CF528D Fuzzy Search (bitset)
    Gym 101667L Vacation Plans (dp)
    Codeforces 432D Prefixes and Suffixes (kmp+dp)
    [题解]BZOJ2115 XOR
    洛谷 P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
    从中国矢量图筛选出江苏省行政区划图
  • 原文地址:https://www.cnblogs.com/TadGuo/p/7943071.html
Copyright © 2011-2022 走看看