首页 > 编程语言 >实验3 数组、指针与现代C++标准库

实验3 数组、指针与现代C++标准库

时间:2022-10-19 20:12:27浏览次数:41  
标签:Info city cout int C++ 数组 include string 指针

实验任务5

task5.cpp

 1 #include<iostream>
 2 #include<vector>
 3 #include<string>
 4 #include"Info.hpp"
 5 using namespace std;
 6 int main()
 7 {
 8     const int capacity=100;
 9     vector<Info> audience_info_list;
10     cout<<"录入信息:"<<endl;
11     cout<<endl; 
12     cout<<"昵称\t";
13     cout<<"联系方式(邮箱/手机号)\t"; 
14     cout<<"所在城市\t"; 
15     cout<<"预定参加人数\t";
16     cout<<endl;
17     string nickname,contact,city;
18     int n,num=0;
19     while(cin>>nickname)
20     {
21         cin>>contact>>city>>n;
22         num+=n;
23     if(capacity-num>=0)
24     {
25         Info p=Info(nickname,contact,city,n);
26         audience_info_list.push_back(p);
27     }
28     else
29     {
30         num-=n;
31         char c;
32         cout<<"对不起,只剩"<<capacity-num<<"个位置."<<endl;
33         cout<<"1. 输入u,更新(update)预定信息"<<endl;
34         cout<<"2. 输入q,退出预定"<<endl;
35         cout<<"你的选择:";
36         cin>>c;
37         cout<<endl;
38         if(c =='q') 
39         break;
40         else if(c =='u')
41         {
42             cout<<"请更新您的预定信息"<<endl;
43             continue; 
44         }
45     }
46 }
47     cout<<"截至目前,一共有"<<num<<"名听众预定参加。预定听众信息如下:"<<endl;
48     for(int i=0;i<audience_info_list.size();++i)
49     {
50         audience_info_list.at(i).print();
51         cout<<endl;
52      } 
53 }

Info.hpp

 1 #pragma once
 2 #include<iostream>
 3 #include<iomanip>
 4 #include<string>
 5 using namespace std;
 6 class Info{
 7     public:
 8         Info(string name0,string contact0,string city0,int n):nickname{name0},contact{contact0},city{city0},n{n}{}
 9         void print();
10     private:
11         string nickname,contact,city;
12         int n;
13 };
14 void Info::print()
15 {
16     cout<<"昵称:\t"<<nickname<<endl;
17     cout<<"联系方式:\t "<<contact<<endl;
18     cout<<"所在城市:\t "<<city<<endl;
19     cout<<"预定人数:\t "<<n<<endl;
20 }

 

 

 

实验任务6

标签:Info,city,cout,int,C++,数组,include,string,指针
From: https://www.cnblogs.com/cwj202183290470/p/16807511.html

相关文章

  • ERROR: <bits/stdc++.h>, 'cstdalign' file not found, running C++17
    Modified ​​1year,1monthago​​Viewed 9ktimes4I'mtryingtorunapieceofcodein VisualStudioCode,onmacOSCatalina.Thecode:#include<bits/stdc+......
  • VSCode搭建C和C++环境
    @目录前言下载安装设置.vscodec_cpp_properties.jsonlaunch.jsonsettings.json下载CodeRunner插件运行代码VSCode我个人的配置项设置前言说明下如何在VSCode下面搭建C/......
  • React遍历数组的时候报错:key需要保持不一样
    需要加上key值,使每个dom不一样报错:{jigui.map((item,index)=>{return<p>{item?.name}</p>;})}不会报错:{jigui.map((item,index)=>{......
  • 实验3 数组、指针与现代C++标准库
    实验目的知道什么是类模板,会正确定义和使用简单的类模板能够描述数组的特性,会使用C++语法正确定义和访问内置数组,知道其局限性能够解释指针变量的特性,会使用C++语法正......
  • 实验3 数组,指针与现代c++标准库
    实验五:Info.hpp1#include<iostream>2#include<string>3#include<iomanip>4usingnamespacestd;5classInfo6{7private:8......
  • 找到数组中第k小的元素
    intpartition(inta[],intstart,intend){//快排的partition操作if(start>=end){returnstart;}inti=start,j=end;inttmp=......
  • go 数组去重
    //rmDuplicate数组去重funcrmDuplicate(list[]string)[]string{varx[]stringfor_,i:=rangelist{iflen(x)==0{x=ap......
  • RestTemplate请求参数有MultipartFile[] 附件数组
    一、RestTemplate请求参数有MultiplateFile[]附件数组的情况,该如何处理二、代码示例@PostMapping("/testSendEmail")@ResponseBodypublicbooleantestSendEma......
  • C++11 实现一个自动注册的工厂
    之前在项目代码里面看到同事写了个自动注册的工厂类,虽然当时我看不懂,但我大受震撼。今天又重新温习了一下这种写法,分享给大家,可见学好C++是多么的重要。实现动机工厂方法......
  • React:数组、列表渲染
    数组JSX允许在模板中插入数组,数组会自动展开所有成员vararr=[<h1>HTML</h1>,<h2>CSS</h2>];ReactDOM.render(<div>{arr}</div>,document.getElementB......