【问题标题】:Splitting a String and Reading Every Part拆分字符串并读取每个部分
【发布时间】:2019-02-15 04:44:03
【问题描述】:

我正在尝试完成一个从文件中读取并计算 GPA 的程序。基本上有16组数据,每组有3种类型——姓名、成绩和加分。 示例文字:

兔八哥
A B+ B A- B B C+ A B A-
100

我遇到的问题是在分数的中间部分。我正在尝试阅读整个等级,然后阅读每个等级本身,例如“A”然后是“B+”。基本读“A”,值为3,加到累加器中,然后移动到下一个字母等级,直到换行符为止。

我曾想过使用 .get 但那是为了获取值。我真的不明白如何处理字符串中的成绩。但是,我知道使用了循环。

    struct infoTaker
{
   string theirName;
   string theirGrade;
   double theirDonation;
   int totalValue;
};


int main( )
{
double donation;
char letter;
ifstream file;
string fullName, actualGrade, substring;
file.open("F://Yes/thing.txt");
for ( int i = 0; i < 16; i ++){
     getline( file, fullName ); // getting the names
     infoTaker person;
     person.theirName = fullName; 
     cout << person.theirName << endl; // end of names section

     getline(file, actualGrade); // gettting the entire line
     person.theirGrade = actualGrade;  // the string of grades  
        cout << letter << endl; // Don't know what to do here

     file >> donation;
     file.ignore( 3 , '\n');
     person.theirDonation = donation;

     cout << person.theirGrade << endl;
     cout << person.theirDonation << endl;
     double convertDoodahs = person.theirDonation / 2.0;
     }  
}

【问题讨论】:

  • 您想要不同字符串中的所有成绩吗?或者一些表示等级的整数值。
  • 我在字符串中找到每个等级的值,然后将它们添加到累加器中并打印出总数

标签: c++


【解决方案1】:

这是一种方法,将您阅读的内容添加到文件中,或者您也可以只阅读特定的成绩行。我猜这会更有用,因为您以后可以检索名称和其他信息。

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>

int main(){

    std::vector<std::string> vec;
    std::string temp;
    std::string grades;

    std::ifstream input("test.txt");

    //add them to vector, and access them later
    while(getline(input, temp)) vec.push_back(temp);

    //read the grades and seperate them 
    std::stringstream ss(vec[1]);
    while(ss >> grades){
        std::cout << grades << "\n";
    }
}

示例 txt 文件

Bugs Bunny
A B C D+
100

输出

A
B
C
D+

【讨论】:

    【解决方案2】:
    #include<iostream>
    #include<string>
    using namespace std;
    
    int convert(char a,char b='\0')
    {
        int result = 0;
        if(b == '\0')
        {
            switch(a)
            {
            case 'A':
                result = 9;
                break;
    
            case 'B':
                result = 9;
                break;
    
            case 'C':
                result = 9;
                break;
            }
        }else
        {
            switch(a)
            {
            case 'A':
                if(b=='+')
                result = 10;
                else
                {
                    result = 8;
                }
                break;
    
            case 'B':
                if(b=='+')
                result = 10;
                else
                {
                    result = 8;
                }
                break;
    
            case 'C':
                if(b=='+')
                result = 10;
                else
                {
                    result = 8;
                }
                break;
            }
        }
        return result;
    }
    
    int getSum(string g)
    {
        int ans = 0;
        int l = g.length();
        for(int i=0;i<l;)
        {
            char a = g[i++],b='\0';
            if(g[i]=='+'||g[i]=='-')
            {
                b = g[i++];
            }
            ans+=convert(a,b);
            i++;
        }
        return ans;
    }
    
    int main()
    {
        string g = "A B+ B A- B B C+ A B A-";
        int sum = getSum(g);
    }
    

    试试这个...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      • 1970-01-01
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多