c ++ n程序 [英] c++n program

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

问题描述

编写一个程序,该程序从名称由用户给出的文件中读取数据,然后输出到另一个名为myoutput.txt的文件。输入文件包含10个数字 - 输出文件将包含分为以下类别的10个数字中的每一个的数据:

有多少数字是正数的一个

A列出所有数字

计算负数的数量

所有正数的总和

所有负数的总和数字


i有点想知道怎么做但我不知道如何计算总和,一旦我确定数字是负数还是正数以及如何保存到输出文件。

请帮助!!!

解决方案


编写一个从a读取数据的程序文件名由用户提供,然后输出到另一个名为myoutput.txt的文件。输入文件包含10个数字 - 输出文件将包含分为以下类别的10个数字中的每一个的数据:

有多少数字是正数的一个

A列出所有数字

计算负数的数量

所有正数的总和

所有负数的总和数字


i有点想知道怎么做但我不知道如何计算总和,一旦我确定数字是负数还是正数以及如何保存到输出文件。

请帮忙!



基本上,您希望将10个数字读入数组。然后遍历数组中的所有数字,检查是正还是负,并执行正确的操作。下面是一些描述基本算法的伪代码。

展开 | 选择 | Wrap | 行号


感谢您的回复。我现在正在编写程序但是当我尝试将数字读入我的输入时,编译器会给我一个错误消息。这就是我到目前为止。

#include< string>

#include< iostream>

#include< fstream>

使用命名空间std;

int main()

{

int one,two,three,4,五,六,七,八,九,十;

int positivecount,negativecount,sumneg,sumpos;


ofstream outdata;

ifstream filename;

string filename;


cout<< 输入文件名:" ;;

cin>> filename;

infile.open(filename.c_str());


infile>>一个>>两个>>三......

这看起来到目前为止看起来是否正确?


我也不明白你对数组的意思我是怎么做的。

write a program that reads data from a file whose name is given by the user and then outputs to another file called myoutput.txt. the input file contains 10 numbers-the output file will contain data on each of the 10 numbers broken up into the following categories:
A of how many numbers are positive
A listing of all the numbers
A count of how many numbers are negative
A sum of all the positive numbers
A sum of all the negative numbers

i kinda have an idea of how to do this but i can''t figure out how to compute the sum once i determine if the number is negative or positive and how to save that to the output file.
please help !!!

解决方案

write a program that reads data from a file whose name is given by the user and then outputs to another file called myoutput.txt. the input file contains 10 numbers-the output file will contain data on each of the 10 numbers broken up into the following categories:
A of how many numbers are positive
A listing of all the numbers
A count of how many numbers are negative
A sum of all the positive numbers
A sum of all the negative numbers

i kinda have an idea of how to do this but i can''t figure out how to compute the sum once i determine if the number is negative or positive and how to save that to the output file.
please help !!!

Basically, you want to read the 10 numbers into an array. Then loop through all the numbers in the array checking whether positive or negative, and perform the proper actions. Here is some psuedo code describing the basic algorithm.

Expand|Select|Wrap|Line Numbers


thanks for the reply. am writing the program now but when i try to read the numbers into my input the compiler gives me an error message. this is what i have so far.
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int one, two, three, four, five, six, seven, eight, nine, ten;
int positivecount, negativecount, sumneg, sumpos;

ofstream outdata;
ifstream filename;
string filename;

cout << "enter the file name: ";
cin >> filename;
infile.open(filename.c_str());

infile >> one >> two >> three...
does this look correct so far?


also i don''t understand what you mean by array how do i do that.


这篇关于c ++ n程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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