JBTALKS.CC

标题: 很难的C!!!对菜鸟难如登天?对老鸟易如反掌?我是菜鸟! [打印本页]

作者: aiklone1314    时间: 2009-9-6 11:43 PM
标题: 很难的C!!!对菜鸟难如登天?对老鸟易如反掌?我是菜鸟!
Question1: Matrix operations using array and function.

Write a complete C program to perform Matrix operations such as Addition, Subtraction, Multiplication and Transpose according to the user’s choice. Define functions to perform these various operations and call them from the main function whenever needed. The program should display the following menu to the user for them to select one of the five options listed.

                ------------------
                    MAIN MENU
                        ------------------
1.MATRIX ADDITION
2.MATRIX SUBTRACTION
3.MATRIX MLTIPLICATION
4.MATRIX TRANSPOSE
5.TO EXIT
---------------------------
                Please enter your option <1/2/3/4/5>:

The program should ask the elements of the input matrix or matrices from the user once the required operation is selected and also should check whether the required operation can be carried out or not based on the input data. The program should produce a neat output showing both the input and the resultant matrix in matrix form. The program should not be terminated until the user selects the option number 5 (TO EXIT).  


我到这里就不会了:

#include "stdafx.h"
#include <stdio.h>

int main()
{
        int add,sub,mul,tran,opt;
        printf("---------\n");
        printf("MAIN MENU\n");
        printf("---------\n");
        printf("1-MATRIX ADDITION\n");
        printf("2-MATRIX SUBTRACTION\n");
        printf("3-MATRIX MULTIPLICATION\n");
        printf("4-MATRIX TRANPOSE\n");
        printf("5-EXIT\n");
        printf("-------------------\n");
        printf("Please enter your option <1/2/3/4/5>:\n");

        scanf("%d",&opt);
switch(opt);
{
case 1:这里开始完全不懂怎样写!



while(opt == 5);
{
        printf("---------\n");
        printf("MAIN MENU\n");
        printf("---------\n");
        printf("1-MATRIX ADDITION\n");
        printf("2-MATRIX SUBTRACTION\n");
        printf("3-MATRIX MULTIPLICATION\n");
        printf("4-MATRIX TRANPOSE\n");
        printf("5-EXIT\n");
        printf("-------------------\n");
        printf("Please enter your option <1/2/3/4/5>:\n");

        scanf("%d",&opt);
}
}


p/s:应该是C不是C++,谢谢三楼大大的提醒!

[ 本帖最后由 aiklone1314 于 2009-9-7 10:09 AM 编辑 ]
作者: 宅男-兜着走    时间: 2009-9-7 12:21 AM
标题: 回复 #1 aiklone1314 的帖子
怎么你的 C++ 怪怪的 跟我看过的使用法有点不太相同。

你的switch 开错了。

int main()
{
        int add,sub,mul,tran,opt;
        printf("---------\n");
        printf("MAIN MENU\n");
        printf("---------\n");
        printf("1-MATRIX ADDITION\n");
        printf("2-MATRIX SUBTRACTION\n");
        printf("3-MATRIX MULTIPLICATION\n");
        printf("4-MATRIX TRANPOSE\n");
        printf("5-EXIT\n");
        printf("-------------------\n");
        printf("lease enter your option <1/2/3/4/5>:\n");

scanf("%d",&opt);

switch(opt)
{
case 1:
//在这里执行动作吧;
break;

case 2:
break;

case 3:
break;

case 4:
break;

case 5:
break;
}
作者: Super-Tomato    时间: 2009-9-7 01:07 AM
原帖由 aiklone1314 于 2009-9-6 11:43 PM 发表


我到这里就不会了:
[quote]#include "stdafx.h"
#include <stdio.h>

int main()
{
        int add,sub,mul,tran,opt;
        printf("---------\n");
        printf("MAIN MENU\n");
        printf("---------\n");
        printf("1-MATRIX ADDITION\n");
        printf("2-MATRIX SUBTRACTION\n");
        printf("3-MATRIX MULTIPLICATION\n");
        printf("4-MATRIX TRANPOSE\n");
        printf("5-EXIT\n");
        printf("-------------------\n");
        printf("Please enter your option <1/2/3/4/5>:\n");

        scanf("%d",&opt);
switch(opt);
{
case 1:这里开始完全不懂怎样写!


while(opt == 5);
{
        printf("---------\n");
        printf("MAIN MENU\n");
        printf("---------\n");
        printf("1-MATRIX ADDITION\n");
        printf("2-MATRIX SUBTRACTION\n");
        printf("3-MATRIX MULTIPLICATION\n");
        printf("4-MATRIX TRANPOSE\n");
        printf("5-EXIT\n");
        printf("-------------------\n");
        printf("Please enter your option <1/2/3/4/5>:\n");

        scanf("%d",&opt);
}
}

[/quote]


switch 的用法沒仔細研究或多練習
去翻翻你的筆記看看怎麼使用 switch ... case

妳的 option 沒退回 main menu 就不需要用到 while 循環




p/s: 你是在寫 C 而不是 C++
作者: aiklone1314    时间: 2009-9-7 09:59 AM
回复 #2 宅男-兜着走 的帖子
回复 #3 Super-Tomato 的帖子

我懂格式是要怎样,不过switch里的action要怎样打,我完全不知道。你们明白题目的意思吗?我看到不是很明白。
作者: aiklone1314    时间: 2009-9-7 10:04 AM
标题: 回复 #3 Super-Tomato 的帖子
因为题目是要如果按任何号码,除了5,都会从新跳回menu。
就是说计算操作完了后,会在从新逃回menu。
然后,只有按5,程式才会跳出来。
作者: Super-Tomato    时间: 2009-9-7 12:16 PM
原帖由 aiklone1314 于 2009-9-7 10:04 AM 发表
因为题目是要如果按任何号码,除了5,都会从新跳回menu。
就是说计算操作完了后,会在从新逃回menu。
然后,只有按5,程式才会跳出来。



那麼你也就明白其中的條件是 "非5循環"
這樣的話你就看看 while 的條件值是甚麼

從你第一樓的 coding 來看, 妳好像完全沒去練習哦
既然這樣你就先撇開你的題目, 先在這裡說明看看 while 和 switch...case 怎麼應用吧
作者: aiklone1314    时间: 2009-9-7 09:07 PM
我知道是 !=5,不过当我execute时,不知道为何他是倒反的,就是说当我放!=5时他反而会从新跳回menu,放==5他就不会这样。
作者: Super-Tomato    时间: 2009-9-7 09:52 PM
原帖由 aiklone1314 于 2009-9-7 09:07 PM 发表
我知道是 !=5,不过当我execute时,不知道为何他是倒反的,就是说当我放!=5时他反而会从新跳回menu,放==5他就不会这样。



那是你其他錯誤問題導致的
如果你真知道怎麼做的話, 那麼就用心繼續做吧
作者: 宅男-兜着走    时间: 2009-9-7 09:53 PM
标题: 回复 #7 aiklone1314 的帖子
while(opt != 5){} 翻译成人话的话就是  当OPT 不是5 的话请执行。
while loop 不是 if else, 当然不同啊。 我没C的篇写工具也 也没 compiler , 抱歉。 所以我照我懂的跟你说。


int main()
{

        int add,sub,mul,tran,opt; // 这是什么?
        printf("---------\n");
        printf("MAIN MENU\n");
        printf("---------\n");
        printf("1-MATRIX ADDITION\n");
        printf("2-MATRIX SUBTRACTION\n");
        printf("3-MATRIX MULTIPLICATION\n");
        printf("4-MATRIX TRANPOSE\n");
        printf("5-EXIT\n");
        printf("-------------------\n");
        printf("lease enter your option <1/2/3/4/5>:\n");

        scanf("%d",&opt);

switch(opt != 5){
// 如果是使用者输入 1 的话就会一直在 case 1 循环, 记得在 switch 之前问他们是不是要退出程序, 不然就要 ALT + CRTL + DELETE 了。
//当opt 是5的时候就会退出 while loop 了。你要用do while 也是可以。
case 1:
break;

case 2:
break;

case 3:
break;

case 4:
break;
}

printf(program exit.); // 通知使用者退出program 中。


应该就是这样罢了。
作者: Super-Tomato    时间: 2009-9-7 10:12 PM
原帖由 宅男-兜着走 于 2009-9-7 09:53 PM 发表
while(opt != 5){} 翻译成人话的话就是  当OPT 不是5 的话请执行。
while loop 不是 if else, 当然不同啊。 我没C的篇写工具也 也没 compiler , 抱歉。 所以我照我懂的跟你说。


int main()
{

      ...


其實 while 和 if 的參數值性質是一樣的, 只要 while(條件) 能夠滿足就會繼續執行
作者: 宅男-兜着走    时间: 2009-9-7 10:18 PM
标题: 回复 #10 Super-Tomato 的帖子
嗯~ 我老师是说 while 能 looping if 只能 recursive 不能使用本身来loop , 性质可能相似,但是某些地方还是有少许不同   。 有时累的时候会搞乱。
作者: 宅男-兜着走    时间: 2009-9-7 10:20 PM

int opt = 0;
        System.out.println("---------\n");
        System.out.println("MAIN MENU\n");
        System.out.println("---------\n");
        System.out.println("1-MATRIX ADDITION\n");
        System.out.println("2-MATRIX SUBTRACTION\n");
        System.out.println("3-MATRIX MULTIPLICATION\n");
        System.out.println("4-MATRIX TRANPOSE\n");
        System.out.println("5-EXIT\n");

        while(opt!=5){
        opt = Integer.parseInt(in.readLine());
        switch(opt){
            case 1:
                System.out.print("doing Addition\n");
                break;
            case 2:
                System.out.print("doing Subtraction\n");
                break;
            case 3:
                System.out.print("doing Multiplication\n");
                break;
            case 4:
                System.out.print("doing Tranpose\n");
                break;
        }
        if(opt!=5){System.out.print("Any command?\n");}
    }
        System.out.println("\nExit program");
    }
}


我这里成功了, 但是还是觉得 用 method / function 比较好。
作者: sohaipeople    时间: 2009-9-7 10:22 PM
标题: 回复 #11 宅男-兜着走 的帖子
if else 只执行一次,可是while loop只要达到它的condition它就会执行,如果我没搞错的话.
作者: 宅男-兜着走    时间: 2009-9-7 11:11 PM
if else 对, while 你terbalik 了~ 只要没达到条件前都会一直 做同样的东西。 我都是这样做无限的insert 的 -.- |||。 while 很危险,如果控制不好的话会当机的。 我写网页while 没给他一个停止的条件那个网页alt + f4 都关不掉。
作者: Super-Tomato    时间: 2009-9-7 11:28 PM
原帖由 宅男-兜着走 于 2009-9-7 10:20 PM 发表


我这里成功了, 但是还是觉得 用 method / function 比较好。


還能再優化寫法
作者: 宅男-兜着走    时间: 2009-9-7 11:36 PM
原帖由 Super-Tomato 于 2009-9-7 11:28 PM 发表


還能再優化寫法



呵呵呵~请赐教  我想知道你会使用什么方法, 方法就好。
作者: Super-Tomato    时间: 2009-9-8 12:11 AM
原帖由 宅男-兜着走 于 2009-9-7 11:36 PM 发表



呵呵呵~请赐教  我想知道你会使用什么方法, 方法就好。



選擇的時候輸入英文字母應該會看到問題點吧

p/s: 順便吧選項加到 while 中會比較好
作者: 宅男-兜着走    时间: 2009-9-8 12:53 AM
标题: 回复 #17 Super-Tomato 的帖子
除此之外呢? 还有没有任何的缩写?  上次你教的类似 java script 的这样 ? true: false; 的方法是好用的。

每个语言都可以如此?
作者: 宅男-兜着走    时间: 2009-9-8 12:59 AM
标题: 回复 #17 Super-Tomato 的帖子
java 可以, 已经证实了。
作者: Super-Tomato    时间: 2009-9-8 01:06 AM
原帖由 宅男-兜着走 于 2009-9-8 12:53 AM 发表
除此之外呢? 还有没有任何的缩写?  上次你教的类似 java script 的这样 ? true: false; 的方法是好用的。

每个语言都可以如此?


哦, 大部分由 C 或 Java 所衍生出來的語言都可以支援 ?:
++, += 這類的 operator 你應該都清楚了, 大致上也沒甚麼縮寫方式



原帖由 宅男-兜着走 于 2009-9-8 12:59 AM 发表
java 可以, 已经证实了。


  你沒用 exception 可以執行??
可否把你完整的 Java coding 貼出來??
作者: 宅男-兜着走    时间: 2009-9-8 02:00 AM
标题: 回复 #20 Super-Tomato 的帖子
你是对的,我没写,我知道try catch 没有不行。
作者: Super-Tomato    时间: 2009-9-8 12:17 PM
原帖由 宅男-兜着走 于 2009-9-8 02:00 AM 发表
你是对的,我没写,我知道try catch 没有不行。


哦... 原來是你省略掉了哦
作者: duo8668    时间: 2009-9-9 08:39 PM
#include "stdafx.h"
#include <stdio.h>

int main()
{
        int add,sub,mul,tran,opt;
        printf("---------\n");
        printf("MAIN MENU\n");
        printf("---------\n");
        printf("1-MATRIX ADDITION\n");
        printf("2-MATRIX SUBTRACTION\n");
        printf("3-MATRIX MULTIPLICATION\n");
        printf("4-MATRIX TRANPOSE\n");
        printf("5-EXIT\n");
        printf("-------------------\n");
        printf("Please enter your option <1/2/3/4/5>:\n");

        scanf("%d",&opt);
switch(opt);
{
case 1:这里开始完全不懂怎样写!



while(opt == 5);
{
        printf("---------\n");
        printf("MAIN MENU\n");
        printf("---------\n");
        printf("1-MATRIX ADDITION\n");
        printf("2-MATRIX SUBTRACTION\n");
        printf("3-MATRIX MULTIPLICATION\n");
        printf("4-MATRIX TRANPOSE\n");
        printf("5-EXIT\n");
        printf("-------------------\n");
        printf("Please enter your option <1/2/3/4/5>:\n");

        scanf("%d",&opt);


为何不直接 用
main();

}
}
作者: Super-Tomato    时间: 2009-9-9 08:44 PM
原帖由 duo8668 于 2009-9-9 08:39 PM 发表
#include "stdafx.h"
#include

int main()
{
        int add,sub,mul,tran,opt;
        printf("---------\n");
        printf("MAIN MENU\n");
        printf("---------\n");
        printf( ...

为何不直接 用
main();




int main 是 C 的標準入口函數, 而 main 即 void/int main 的縮寫雖然也可以, 但有些編譯器無法對不明確的函數類型進行編譯

p/s: http://www.safercode.com/blog/20 ... n-vs-void-main.html

[ 本帖最后由 Super-Tomato 于 2009-9-9 08:52 PM 编辑 ]
作者: duo8668    时间: 2009-9-11 01:41 AM
对不起对不起。。。

我的意思是为何他那边不 recall main function 而要重新code menu。
就是红色那行换去  main();

[ 本帖最后由 duo8668 于 2009-9-11 01:43 AM 编辑 ]
作者: Super-Tomato    时间: 2009-9-11 01:44 AM
原帖由 duo8668 于 2009-9-11 01:41 AM 发表
对不起对不起。。。

我的意思是为何他那边不 recall main function 而要重新code menu。
就是红色那行换去  main();


這是新手還不熟悉編寫的過程
作者: 宅男-兜着走    时间: 2009-9-11 02:44 AM
原帖由 duo8668 于 2009-9-9 08:39 PM 发表
#include "stdafx.h"
#include

int main()
{
        int add,sub,mul,tran,opt;
        printf("---------\n");
        printf("MAIN MENU\n");
        printf("---------\n");
        printf( ...



问你哦, 你刚说的 main(); 这样的方法 在其他语言也适用? paiseh 我也是菜鸟。
作者: goodhermit95    时间: 2009-9-11 11:57 AM
if 和 while 最大的不同就是
比如说
if this->Text == this->Text {Messagebox.Show();}
这个只会执行一次
while this->Text == this->text{messagebox.show():}
这个会跳很多个出来,而且当你尝试关掉该程序,就会not responding
可以说是用掉整个程序的效能
p/s virus 最佳

switch case??google 不是有很多吗?
比如说我要探测textbox1的字

switch(textbox1->Text)
{
case "goodhermit95"{
//里面的字是goodhermit95
}
case "super-tomato"
//里面的字是super-tomato,你要做什么你自己看
}
}

continue 和break的意思是
比如说在循环函数中,continue就是继续循环,break就是跳过循环,继续指令
while(xxxx)
{
//如果在这里continue,他就会继续
//a
}
如果在 a break,就会跳来这里




欢迎光临 JBTALKS.CC (https://jbtalks.my/) Powered by Discuz! X2.5