struct DATA
{
int *pData;
int nSize;
int nSum;
int nMax;
int nMin;
float fMean;
};
struct DATA ReadDataFile(const char *pFileName);
int main(void)
{
DATA stData = ReadDataFile ("data.txt");
printf("We read %d numbers\n", stData.nSum);
printf("SUM: %d\nMEAN: %f\nMAX: %d\nMIN: %d\n",
(float)stData.nSum / stData.nSize, stData.nMax, stData.nMin); return 0;
}
如何将data.txt
的值分配到*pData
并计算nSum
?
我通过将值存储到数组中来完成它。但无法分配和完成使用指针?
我确实使用了如下数组。
if (infile.is_open())
{
while (infile.good())
{
infile.getline(cNum, 256, ' ');
arrays[count]= atoi(cNum) ;
++count ;
}
infile.close();
min = arrays[0];
max = arrays[0];
for(int j=0; j<count; j++)
{
if(min > arrays[j])
min = arrays[j];
if(max < arrays[j])
max = arrays[j];
sum = sum + arrays[j];
}
mean = sum/count;
cout<<"Sum= "<<sum<<"; Min= "<<min<<"; Max= "<<max<<"; Mean= "<<mean<<"\n";
}
但是如何使用结构来做同样的事情?
答案 0 :(得分:1)
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/test.py", line 30, in run_from_argv
super(Command, self).run_from_argv(argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/test.py", line 74, in execute
super(Command, self).execute(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/test.py", line 90, in handle
failures = test_runner.run_tests(test_labels)
File "/usr/local/lib/python2.7/dist-packages/django/test/runner.py", line 210, in run_tests
old_config = self.setup_databases()
File "/usr/local/lib/python2.7/dist-packages/django/test/runner.py", line 166, in setup_databases
**kwargs
File "/usr/local/lib/python2.7/dist-packages/django/test/runner.py", line 370, in setup_databases
serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/creation.py", line 368, in create_test_db
test_flush=not keepdb,
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 120, in call_command
return command.execute(*args, **defaults)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 179, in handle
created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 317, in sync_apps
cursor.execute(statement)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "auth_user" does not exist
应该是您动态分配的数组。
获取文件的元素数量并分配大量内存:
pData
然后,您可以在文件中填入your_struct.pData = new int[your_struct.nSize];
值,并使用数组计算总和,最大值,最小值和平均值。
当你完成它时,不要忘记int
。