帮我处理我的代码 [英] Help me with my code

查看:89
本文介绍了帮我处理我的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在大学有一个实验室问题

我尝试编写代码但是没有用



这个是我的实验室问题:



多种名词形式



在英语中,名词是由语法数字表示 - 单数或复数。在这个问题中,我们使用从单数形式构造复数的简单模型。



这个模型并不总能正确地制作英文复数形式,但它在大多数情况下都适用。在解决问题时忘记你所知道的真实规则,并将声明用作正式文件。



您将获得一些单数形式的名词,您的程序应该翻译使用以下规则将它们转换为复数形式:



·如果单数名词以ch,x,s,o结尾,则通过添加es形成复数形式。例如,巫婆>巫婆,番茄 - >西红柿。



·如果单数名词以f或fe结尾,则复数形式以ves结尾。例如,leaf->叶子,刀子>刀。注意字母f成为v。



·以y结尾的名词将结尾改为ies复数。例如,family->家庭。



·在所有其他情况下,复数是通过添加s形成的。例如,book-> books。





输入(从文件中读取)



从文件(file1.txt)中读取很多单词。一个单词由2到25个小写拉丁字母组成。不能保证给定的单词是词汇表中真正的英语单词。



输出(写入文件)



在文件的不同行上以复数形式打印给定单词。保持文字的顺序与输入中的顺序相同。



我尝试过:



i开始像这样写它







i have a post-lab question at the university
and i tried to write the code but it did not work

this is my post-lab question:

Plural Form of Nouns

In the English language, nouns are inflected by grammatical number — that is singular or plural. In this problem we use a simple model of constructing plural from a singular form.

This model doesn't always make English plural forms correctly, but it works in most cases. Forget about the real rules you know while solving the problem and use the statement as a formal document.

You are given several nouns in a singular form and your program should translate them into plural form using the following rules:

· If a singular noun ends with ch, x, s, o the plural is formed by adding es. For example, witch-> witches, tomato-> tomatoes.

· If a singular noun ends with f or fe, the plural form ends with ves. For example, leaf-> leaves, knife-> knives. Pay attention to the letter f becoming v.

· Nouns ending with y change the ending to ies in plural. For example, family-> families.

· In all other cases plural is formed by adding s. For example, book->books.


Input (READ from a file)

Read from the file (file1.txt) many words. A word consists from 2 to 25 lowercase Latin letters. It is not guaranteed that the given words are real English words from vocabulary.

Output (WRITE into a file)

Print n given words in their plural forms on separate lines on a file. Keep the words in the same order as they are given in the input.

What I have tried:

i start to write it like so



#include <iostream>
#include <string>
#include <fstream>
using namespace std;


int main(){
	string singular;
	ifstream writing;
	ifstream reading;
	writing.open("singular.txt");

	while (!writing.eof())
	{
	cin>>singular;
		writing>>singular;
		for(int i=0;i<singular.length();i++)
			if (singular[i]>='a' && singular[i]<='z')
				continue;
			else 
			break;

			if (singular.length()-1=='o' || singular.length()-1=='x' || singular.length()-1=='s' )
				writing>>singular+"es\n";
			else if ( singular.length()-1=='f')
				writing>>singular+"ves\n";
			else if (singular.length()-2=='ch')
				writing>>singular+"es\n";
			else if (singular.length()-2=='fe')
			{	writing>>singular+"ves\n";
			writing>>singular.erase(singular.length()-1)+"ies\n";}

     		else if ( singular.length()-1=='y')
				
				writing>>singular.erase(singular.length()-1)+"ies\n";
			else
				writing>>singular+"s\n";

	}
	
	
writing.close();
return 0;}

推荐答案

您需要做的第一件事是:学会使用调试器!

这是一个内置于Visual Studio中的工具 - 可以让您运行代码和完全控制发生的事情,查看数据,逐个执行行,并且通常跟踪正在发生的事情。

所以,这将取决于你。从这里开始:演练:调试项目(C ++) [ ^ ]



在函数的第一行放置断点,然后通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但是我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:debugging!
The first thing you need to do is: learn to use the debugger!
This is a tool - built into Visual studio - that lets you run your code and take complete control over what happens, looking at data, executing lines one by one, and generally following what is going on.
So, it's going to be up to you. Start here: Walkthrough: Debugging a Project (C++)[^]

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


为什么要从名为的流中读取文本写作?并且对该流的每个引用都在读取数据,而应该写出已更改的数据。考虑一下你需要按逻辑顺序执行的步骤。

Why are you reading text from the stream named writing? and every reference to that stream is reading data in, whereas the changed data should be written out. Think about the steps you need to perform in logical order.
InStream reader = // open the input file
OutStream writer = // open as output
While NOT end of file on reader
    read next token into the string variable
    if the string ends with "ch", or "x", or "s", or "o"
        Add "es" to the end
    else if ... other tests here

    write the amended string to the output file
// continue the while loop
close both files and end


这篇关于帮我处理我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆