更新 1
好消息是,现在我通过更正如下调用来查看输出mat()
。
但是,有一个问题“问题”。
“问题”如下
Implicit declaration of function 'mat' is invalid in C99
//
// main.m
// MultRosetta
//
// Created by Brian Schott on 12/30/12.
// Copyright (c) 2012 Brian Schott. All rights reserved.
//
#import <Cocoa/Cocoa.h>
// #include "code.m"
/*
int main(int argc, char *argv[])
{
int mat();
return NSApplicationMain(argc, (const char **)argv);
}
*/
int main(int argc, const char * argv[])
{
return( (int) mat() );
}
更新 1
更新 0
我的目录列表如下。如何从终端(控制台)运行应用程序?我什至不认识要运行的可执行文件。
server:MultRosetta brian$ ls -al
total 32
-rw-r--r-- 1 brian staff 449 Dec 30 19:31 inline
-rw-r--r-- 1 brian staff 1441 Dec 30 08:52 code.m
drwxr-xr-x 5 brian staff 170 Dec 30 15:40 MultRosetta.xcodeproj
drwxr-xr-x 9 brian staff 306 Dec 30 16:11 MultRosetta
-rw-r--r--@ 1 brian staff 6148 Dec 31 08:26 .DS_Store
drwxr-xr-x 13 brian staff 442 Dec 30 08:43 ..
drwxr-xr-x 7 brian staff 238 Dec 31 08:26 .
server:MultRosetta brian$ ls -al MultRosetta.xcodeproj/
total 24
drwxr-xr-x 5 brian staff 170 Dec 30 15:40 .
drwxr-xr-x 7 brian staff 238 Dec 31 08:26 ..
-rw-r--r-- 1 brian staff 12197 Dec 30 15:40 project.pbxproj
drwxr-xr-x 4 brian staff 136 Dec 30 08:43 project.xcworkspace
drwxr-xr-x 3 brian staff 102 Dec 30 08:43 xcuserdata
server:MultRosetta brian$ ls -al MultRosetta
total 48
drwxr-xr-x 9 brian staff 306 Dec 30 16:11 .
drwxr-xr-x 7 brian staff 238 Dec 31 08:26 ..
-rw-r--r--@ 1 brian staff 283 Dec 30 08:43 BSAppDelegate.h
-rw-r--r--@ 1 brian staff 380 Dec 30 08:43 BSAppDelegate.m
-rw-r--r-- 1 brian staff 1098 Dec 30 08:43 MultRosetta-Info.plist
-rw-r--r--@ 1 brian staff 153 Dec 30 08:43 MultRosetta-Prefix.pch
-rw-r--r--@ 1 brian staff 1531 Dec 30 16:11 code.m
drwxr-xr-x 5 brian staff 170 Dec 30 08:43 en.lproj
-rw-r--r--@ 1 brian staff 289 Dec 30 10:39 main.m
server:MultRosetta brian$
更新 0
这是一个后续问题。在最初的问题中,我终于能够毫无错误地编译,但问题仍然存在。
我在下面的代码中看不到矩阵结果的任何输出。
(代码仅与原始代码略有不同。按照建议,我在“inline”语句中添加了“static”,我#imported了两个Xcode内容,并将主函数名称从“main()”更改为“mat( )”并从main.m
.)中调用“mat()”
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#include <stdio.h>
#include <stdlib.h>
/* Make the data structure self-contained. Element at row i and col j
is x[i * w + j]. More often than not, though, you might want
to represent a matrix some other way */
typedef struct { int h, w; double *x;} matrix_t, *matrix;
static inline double dot(double *a, double *b, int len, int step)
{
double r = 0;
while (len--) {
r += *a++ * *b;
b += step;
}
return r;
}
matrix mat_new(int h, int w)
{
matrix r = malloc(sizeof(matrix) + sizeof(double) * w * h);
r->h = h, r->w = w;
r->x = (double*)(r + 1);
return r;
}
matrix mat_mul(matrix a, matrix b)
{
matrix r;
double *p, *pa;
int i, j;
if (a->w != b->h) return 0;
r = mat_new(a->h, b->w);
p = r->x;
for (pa = a->x, i = 0; i < a->h; i++, pa += a->w)
for (j = 0; j < b->w; j++)
*p++ = dot(pa, b->x + j, a->w, b->w);
return r;
}
void mat_show(matrix a)
{
int i, j;
double *p = a->x;
for (i = 0; i < a->h; i++, putchar('\n'))
for (j = 0; j < a->w; j++)
printf("\t%7.3f", *p++);
putchar('\n');
}
int mat()
{
double da[] = { 1, 1, 1, 1,
2, 4, 8, 16,
3, 9, 27, 81,
4,16, 64, 256 };
double db[] = { 4.0, -3.0, 4.0/3,
-13.0/3, 19.0/4, -7.0/3,
3.0/2, -2.0, 7.0/6,
-1.0/6, 1.0/4, -1.0/6};
matrix_t a = { 4, 4, da }, b = { 4, 3, db };
matrix c = mat_mul(&a, &b);
/* mat_show(&a), mat_show(&b); */
mat_show(c);
/* free(c) */
// NSLog (@"Here is some amazing text! %@",c);
return 0;
}